Skip to main content

Section 9.21 Write Code Questions Orig

Activity 9.21.1.

Write a function item_lister that takes in a list of at least three values, items, as a parameter. Set the first value to β€œFirst item”, set the second value to the original first value, and set the third value to its current value plus one (rounded to two decimals). (Note: the third value of items will only be numerical.) Then, return the modified list. For example, itemLister([2,4,6.222,8]) would return ['First item', 2, 7.22, 8].

Activity 9.21.2.

Create a function item_lister that takes in a list of at least three values, items, as a parameter. Set the first value to β€œFirst item”, set the second value to the original first value, and set the third value to its current value plus one (rounded to two decimals). (Note: the third value of items will only be numerical.) Then, return the modified list. For example, itemLister([2,4,6.222,8]) would return ['First item', 2, 7.22, 8].

Activity 9.21.3.

Write the function change_index3 that takes in one parameter, lst, and assigns the value at index 3 of lst to β€˜200’ and then returns lst. For example, change_index3(['hi', 'goodbye', 'python', '106', '506']) would return ['hi', 'goodbye', 'python', '200', '506'] and change_index3([1, 2, 0, -5, 4]) would return [1, 2, 0, '200', 4].

Activity 9.21.4.

Create the function change_index3 that takes in one parameter, lst, and assigns the value at index 3 of lst to β€˜200’ and then returns lst. For example, change_index3(['hi', 'goodbye', 'python', '106', '506']) would return ['hi', 'goodbye', 'python', '200', '506'] and change_index3([1, 2, 0, -5, 4]) would return [1, 2, 0, '200', 4].

Activity 9.21.5.

Write a function countWords that takes in a list, lst, as a parameter, and returns the amount of words that have a length of 5. For example, countWords(['hello', 'hi', 'good morning', 'three', 'kitty']) should return 3.

Activity 9.21.6.

Create a function countWords that takes in a list, lst, as a parameter, and returns the amount of words that have a length of 5. For example, countWords(['hello', 'hi', 'good morning', 'three', 'kitty'] should return 3.

Activity 9.21.7.

Write a function reverse that takes in one parameter, lst, and returns the reverse of a passed list. For example, reverse([1,2,3]) should return [3, 2, 1].

Activity 9.21.8.

Create a function reverse that takes in one parameter, lst, and returns the reverse of a passed list. For example, reverse([1,2,3]) should return [3, 2, 1].

Activity 9.21.9.

Write a function sort_by_length that takes in one parameter, a list of strings, lst, and returns the list sorted by the length of the strings. For example, sort_by_length(["hello", "hi", "hey", "greetings"]) would return ['hi', 'hey', 'hello', 'greetings'].

Activity 9.21.10.

Arrange the blocks to define a function sort_by_length that takes in one parameter, a list of strings, lst, and returns the list sorted by the length of the strings. For example, sort_by_length(["hello", "hi", "hey", "greetings"]) would return ['hi', 'hey', 'hello', 'greetings'].

Activity 9.21.11.

Arrange the blocks to define a function add_to_new_list that takes in a list of strings, lst, 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.21.12.

Arrange the blocks to define a function average that takes in a list of integers, aList, 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.

Activity 9.21.13.

Arrange the blocks to define 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, then returns that new list. For example, capitalize([["hi"],["hello", "hey"]]) would return ['Hi', 'Hello', 'Hey'].

Activity 9.21.14.

Arrange the blocks to define 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.21.15.

Arrange the blocks to define 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.

Activity 9.21.16.

Arrange the blocks to define 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: Jennifer, age: 19"].
You have attempted of activities on this page.