Answers: Collections & Loops#

Provided here are answers to the practice questions at the end of “Collections & Loops”.

Collections#

Collections Q1#

# actual approaches could differ
trees_a = trees[5]
trees_b = trees[-3:]
trees_c = trees[2:9:2]
trees_d = trees[::3]

Collections Q2#

Part I.

# actual values will differ
practice_list = ['a', 'b', 2.9, True, 
                 {'A':1, 'B':2}, (2, 3)]

Part II.

slice_1 = practice_list[1:4]
slice_2 = practice_list[1::2]
slice_3 = practice_list[-3]

Collections Q3#

practice_dict = {'name': 'Shannon',
                 'favorite_game' : 'Coup',
                 'height': 65}

Collections Q4#

Part I.

grading = {'A': (90, 100),
           'B': (80, 90),
           'C': (70, 80),
           'D': (60, 70),
           'F': (0, 60)}

Part II.

A_lower = grading['A'][0]
A_upper = grading['A'][1]

Collections Q5#

dict_a = cogs18_dict['Ellis']
dict_b = type(cogs18_dict)
dict_c = len(cogs18_dict)

Collections Q6#

scenario_A = 'tuple'
scenario_B = 'list'
scenario_C = 'dictionary'
scenario_D = 'dictionary'

Operators (revisited with collections)#

Operators Q1#

yes_member = current_holiday in spring_holidays
no_member = current_holiday not in spring_holidays

Operators Q2#

# actual operations will differ
memb_a = 'love' in my_string
memb_b = 'Exercises' not in my_list

Loops#

Loops Q1#

my_name = 'Shannon'
counter = 0

for char in my_name:
    counter += 1

Loops Q2#

# there are other approaches that use string methods
sentence = ''

for word in vaccination_list:
    sentence += word

Topic Synthesis#

Synthesis Q1#

output = []
val = 0 

while val <= 100: 
    if val % 10 == 0:
        output.append(val)
    
    val += 1

Synthesis Q2#

# output will differ based on my_name
my_name = 'Shannon'
counter = 0

for char in my_name:
    char = char.lower()
    if char not in ['a', 'e', 'i', 'o', 'u']:
        counter += 1

Synthesis Q3#

Part I.

# output will differ based on my_name
my_name = 'Shannon'
name_consonants = {}

vowels = ['a', 'e', 'i', 'o', 'u']
            
for char in my_name:
    char = char.lower()
    if char not in vowels:
        if char not in name_consonants:
            name_consonants[char] = 1
        else:
            name_consonants[char] += 1  

Part II.

# output will differ based on who is taking the exam
consonant_count = 0

for key in name_consonants:
    consonant_count += name_consonants[key]

Synthesis Q4#

to_contact = []

for person in staff:
    if '_IA' in person:
        to_contact.append(person)

Synthesis Q5#

# there are multiple possible solutions/approaches
cogs18_students = 0

for key in ellis_courses:
    if 'cogs18' in key:
        cogs18_students = cogs18_students + ellis_courses[key]

cogs18_students

Synthesis Q6#

Part I.

# there are multiple possible solutions/approaches
steps = []
to_do = []
grocery = []

for value in to_do_list:
    if isinstance(value, int):
        steps.append(value)
    else:
        if 'cogs' in value:
            to_do.append(value)
        else: 
            grocery.append(value)

Part II.

days_of_week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 
                'Friday', 'Saturday', 'Sunday']
steps_dict = {}

for i in range(0,7):
    key = days_of_week[i]
    val = steps[i]
    steps_dict[key] = val

Functions (revisited with collections)#

Functions Q1#

square_default = square_all([2,3,4])
power_three = square_all([2,3,4], 3)
out_4 = square_all([2,2,2,2]) # this execution could differ

Functions Q2#

joke_random = court_jester_jokes()
joke_nonrandom = court_jester_jokes(random_joke=False)

Functions Q3#

def count_int(col):
    count = 0
    
    for val in col:
        if type(val) == int:
            count += 1
    
    return count

Functions Q4#

def modify_string(input_string):
    
    new_language = {'a': 'ӓ',
                'e' : 'ɚ',
                'i' : '¡',
                'o' : 'ð', 
                'u' : 'û',
                'A' : 'Ӓ', 
                'E' : 'ɛ', 
                'O' : 'Ó', 
                'U' : 'Ü'}
    
    modified_name = ''
    for letter in input_string:
        if letter in new_language:
            modified_name = modified_name + new_language[letter]
        else:
            modified_name = modified_name + letter

    return modified_name

Functions Q5#

def calculate_points(hand):
    pointers = ['10', 'J', 'Q', 'K', 'A']
    counter = 0
    
    for card in hand:
        if card in pointers:
            counter += 1
        
    return counter

Functions Q6#

default_brackets = {0.10 : (0, 9875),
                    0.12 : (9876, 40125),
                    0.22 : (40126, 85525),
                    0.24 : (85526, 163300),
                    0.32 : (163301, 207350),
                    0.35 : (207351, 518400)
                   }

def to_taxes(salary, tax_brackets=default_brackets):
    for key in tax_brackets:
        if tax_brackets[key][0] <= salary <= tax_brackets[key][1]:
            output = key * salary
    
    return output

Functions Q7#

# wording of the returned messages may vary
def reseller_comparison(prices):

    if len(prices) == 0:
        return 'No resellers'

    difference = abs(prices[0] - prices[1])

    if prices[0] < prices[1]:
        return 'The first reseller is ' + str(difference) + ' cheaper than the second reseller'
    elif prices[0] > prices[1]:
        return 'The first reseller is ' + str(difference) + ' more expensive than the second reseller'
    else:
        return 'They are the same price'

Debugging#

Debugging Q1#

def count_consonants(input_string):
    out = {}
    vowels = ['a', 'e', 'i', 'o', 'u']
    
    for char in input_string:
        char = char.lower()
        
        if char not in vowels:
            if char not in out:
                out[char] = 1
            else:
                out[char] += 1
    return out

Debugging Q2#

def new_encrypt(input_string):
    alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    reverse_alpha = 'ZYXWVUTSRQPONMLKJIHGFEDCBA'

    output_string = ''

    for char in input_string:
        char = char.upper()
        if char in alpha:
            position = alpha.find(char)
            output_string += reverse_alpha[position]
        else:
            output_string = None
            break
        
    return output_string