Docsity

Computer Science Final Exam: Python Programming Exercises, Exams of Computer Science

Partial preview of the text

Download Computer Science Final Exam: Python Programming Exercises and more Exams Computer Science in PDF only on Docsity!

  • Computer Science Final Exam
1, Write a Python function called sum_digits that accepts an integer n and returns the sum of its digits. You must implement the solution using recur sion: def sum_digits (n): ifn < 10: return n alse: return n % 10 + sum_digits(n // 10) 2, Write a Python function called reverse_list that accepts a list Ist and returns anew list with the clements in reverse order, You must implement the solution using recursion: def reverse_list (Ist): if len (Ist) == return [] alse: return [IstI-1]] + reverse_list (Ist l:-11) 3. Coding Loops Question: Write a Python function called compute_sum that accepts a list of integers Ist and returns the sum of all the clements in the list. You must implement the solution using a loop: del compute_sum (Ist): tatal = 0 for num in Ist: total += num return total