Chapter 16 Exercises

  1. Fix syntax 6 errors in the code below so that the code runs correctly. It should set combined to the concatenation of start and name. It should print the length of the combined string, print the combined string, and it should print the result of name * 3.

  2. Add code to line 1 so that the code below prints 14.

  3. Fix the 5 syntax errors in the code below so that it runs. It should print the length of my_first_list and print the result of my_first_list * 3. Then it should set my_second_list to the concatenation of my_first_list and a list containing 321.4. Then it should print the value of my_second_list.

  4. Fix the errors so that the procedure prints “My name is JohnJohn” and “19”.

  5. Fix 5 syntax errors in the code below so that it runs and prints the contents of items.

  6. Complete the code on lines 4 and 5 so that the function returns the average of a list of integers.

  7. Fix the indention in the code below so that it runs correctly. It should loop and add the current value of source to so_far each time through the loop. It should also print the value of so_far each time through the loop.

  8. Fix the code so that the code prints “[‘hihi’, 0, 0, 4]” .

  9. Fix 4 syntax errors in the code below. After the code executes the list so_far should contain the reverse of the source list.

  10. The code below currently prints the reverse of a list. Change it so that it prints a mirrored version of the list. It should print “[‘list’, ‘a’, ‘is’, ‘This’, ‘This’, ‘is’, ‘a’, ‘list’]”.

  11. Change the following code into a function. It should take the list and return a list of the values at the even indicies.

  12. The following code creates and prints a list of even numbers. Change it and add to it so that it creates a list of all multiples of 5 from 0 to 50, inclusive.

  13. Change the following into a procedure. It prints a countdown from 5 to 0. Have it take the starting number for the countdown as a parameter. Print each value till it gets to 0.

  14. Fix the errors so that the code individually adds each item from source to new_list. Make the range decrement, so it starts from the end, but keep new_list in the same order as source.

  15. Write a function that returns the values at the odd indices in a list. The function should take the number list as a parameter. If it is passed [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] for example, it should return [1, 3, 5, 7, 9].

  16. Write a function that takes a list of numbers as a parameter and adds 5 to each number and returns the list.

  17. Write a function that takes a list of numbers and returns the sum of the positive numbers in the list.

  18. Write a function that takes in a list of numbers as a parameter. The function should calculate the sum of all the positive numbers in the list, the absolute value of the sum of the negative numbers, and return the average of the two sums.

  19. Write a function to return the reverse of a list, but with only every other item from the original list starting at the end of the list. So, if it is passed the list [0,1,2,3,4,5] for example, it should return the list [5, 3, 1].

  20. Write a procedure that takes an int as a parameter. The procedure should add every other odd number from 1 to the int parameter (inclusive) into a new list. The procedure should print the new list and the sum of the new list.

Next Section - Chapter 17 - Getting pieces out of strings and lists