9.18. Write Code Questions

  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'].

    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'].

  2. 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].

  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.

    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.

  4. 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].

  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'].

    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'].

  6. 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.

  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].

    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].

  8. 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].

  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.

    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.

  10. 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'].

  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