Skip to main content

Section 9.18 Write Code Questions

Activity 9.18.1.

Write a function add_to_new_list that takes in a list of strings, lst, as a parameter and creates a new list with the length of lst and the first element of lst three times. For example, add_to_new_list(["1","2","3"]) would return [3, '111'].
Solution.
Write a function add_to_new_list that takes in a list of strings, lst, as a parameter and creates a new list with the length of lst and the first element of lst three times. For example, add_to_new_list(["1","2","3"]) would return [3, ’111’]

Activity 9.18.2.

Loading a select question (grading type: first)...

Activity 9.18.3.

Write a function average that takes in a list of integers, aList, as a parameter and returns the average of all of the integers, rounded to one decimal place. For example, average([99, 100, 74, 63, 100, 100]) would return 89.3.
Solution.
Write a function average that takes in a list of integers, aList, as a parameter and returns the average of all of the integers, rounded to one decimal place. For example, average([99, 100, 74, 63, 100, 100]) would return 89.33.

Activity 9.18.4.

Loading a select question (grading type: first)...

Activity 9.18.5.

Write a function capitalize that takes in a list of lists of strings, lst, and makes the first letter of each element capitalized and adds it to a new list and returns that new list. For example, capitalize([["hi"],["hello", "hey"]]) would return ['Hi', 'Hello', 'Hey'].
Solution.
Write a function capitalize that takes in a list of lists of strings, lst, and makes the first letter of each element capitalized and adds it to a new list and returns that new list. For example, capitalize([["hi"],["hello", "hey"]]) would return [’Hi’, ’Hello’, ’Hey’].

Activity 9.18.6.

Loading a select question (grading type: first)...

Activity 9.18.7.

Write a function chop that takes a list, lst, and modifies it, removing the first and last elements. For example, chop([1,2,3,4,5]) should return [2,3,4].
Solution.
Write a function chop that takes a list, lst, and modifies it, removing the first and last elements. For example, chop([1,2,3,4,5] should return [2,3,4].

Activity 9.18.8.

Loading a select question (grading type: first)...

Activity 9.18.9.

Write a function sumUntilEven that takes in one parameter, lst, and returns the sum of all the elements in the lst up to but not including the first even number. For example, sumUntilEven([1,2,3,4,5]) should return 1 and sumUntilEven([1,3,5,7,9]) should return 25.
Solution.
Write a function called sumUntilEven that takes in one parameter, lst, and returns the sum of all the elements in the lst up to but not including the first even number. For example, sumUntilEven([1,2,3,4,5] should return 1 and sumUntilEven([1,3,5,7,9] should return 25.

Activity 9.18.10.

Loading a select question (grading type: first)...

Activity 9.18.11.

Write a function combine(names, ages) that takes in two lists, names and ages and returns a list of strings in the format "Name: name, age: age". For example, combine(["Claire", "Jennifer"],[23, 19]) would return ["Name: Claire, age: 23", "Name: Jennfier, age: 19"].
You have attempted of activities on this page.