Posts

Learn to Program: The Fundamentals - Week 6 Exercise Answers

Learn to Program: The Fundamentals - Week 6 Exercise Score of  13.00  out of  13.00 .  Open up IDLE and try out all the code as you do the exercise! Question 1 Consider this code: def merge(L): merged = [] for i in range(0, len(L), 3): merged.append(L[i] + L[i + 1] + L[i + 2]) return merged print(merge([1, 2, 3, 4, 5, 6, 7, 8, 9])) What is printed by the code above? Your Answer Score Explanation [123, 456, 789] [1, 4, 7] [6, 15, 24] Correct 1.00 [12, 15, 18] Total 1.00 / 1.00 Question Explanation Trace the code by hand or in the visualizer, or run the code in IDLE. Question 2 Consider this code: def mystery(s): """ (str) -> bool """ matches = 0 for i in range(len(s) // 2): if s[i] == s[len(s) - 1 - i]: # <-- How many times is this line reached? matches = matches + 1 return matches == (len(s) // 2) mystery('civil') Trace the function call...

Learn to Program: The Fundamentals - Week 5 Exercise Answers

Learn to Program: The Fundamentals - Week 5 Exercise Score of  13.75  out of  14.00 Open up IDLE and try out all the code as you do the exercise! Question 1 Select the expression(s) that evaluate to  True . Your Answer Score Explanation [1, 2, 3] in len('mom') Correct 0.25 len('mom') in [1, 2, 3] Correct 0.25 int('3') in [len('a'), len('ab'), len('abc')] Correct 0.25 '3' in [1, 2, 3] Correct 0.25 Total 1.00 / 1.00 Question Explanation Evaluate each of these expressions in the Python shell and, if the results surprise you, you should also evaluate each subexpression. Question 2 Consider this code: def mystery(s): i = 0 result = '' while not s[i].isdigit(): result = result + s[i] i = i + 1 return result Select the function call(s) that result in an error. Your Answer Score Explanation mystery('abc') Correct 0.25 mystery('abc123...

learn To program: The Fundamentals Week 7 Answers

Question 1 Consider this code: >>> d = {'a': 1, 'b': 2} >>> # CODE MISSING HERE >>> d {'a': 1, 'c': 3, 'b': 2} Write the missing assignment statement that that modifies the dictionary as shown. (Just write the assignment statement; don't write the  >>>  part.) You entered: Your Answer Score Explanation d['c'] = 3 Correct 1.00 Total 1.00 / 1.00 Question Explanation You need to add  'c'  as a key and 3 as the value associated with that key. Question 2 Consider this code: >>> d = {'a': 1, 'b': 2} >>> # CODE MISSING HERE >>> d {'a': 1, 'b': 3} Write the missing assignment statement that modifies the dictionary as shown. (Just write the assignment statement; don't write the  >>>  part.) You entered: Your Answer Score Explanation d['b'] = 3 Correct 1.00 Total 1.00 / 1.00 Question Expla...