Question: Python Functions sum_of_digits(n): Given a non-negative integer n, calculate and return the sum of all its digits. Assume: n is a non-negative integer. Return value:
Python

Functions sum_of_digits(n): Given a non-negative integer n, calculate and return the sum of all its digits. Assume: n is a non-negative integer. Return value: an integer. Examples: sum_of_digits(0) rightarrow 0 # only digit is 0 sum_of_digits (204) rightarrow 6 # 2+0+4 = 6 multiply_until_total_reached(original, total, n): Starting with a positive integer original, keep multiplying original by n and calculate the sum of all multiples generated including original until the sum is no longer smaller than total. Return the minimum number of multiplications needed to reach at value at or above the given total. Assume: All three arguments are integers: original and n are positive. Return value: an integer. Examples: multiply_until_total_reached (1, 5, 2) rightarrow 2 # 1*2=2, (1+2)5, 2 multiplications needed multiply_until_total_reached (1, 15, 2) rightarrow 3 # 1*2=2, (1+2)0, no multiplication replicate(xs, n): Given a list of Python values xs and a non-negative integer n, construct and return a copy of xs but with each value replicated n times. Assume: xs is a list of zero or more Python values; n is a non-negative integer. Return value: a list. Do NOT change the original list xs. Examples: replicate([l, 2, 2, 3], 2) rightarrow [1, 1, 2, 2, 2, 2, 3, 3] # replicate every value 2 times replicate([l, 2, 2, 3], 0) rightarrow [] # replicate every value 0 times replicate([], 2) rightarrow [] # empty list stays empty locate_second_divisor(xs, n): Check a list of positive integers xs to find the second number in xs that is a divisor of the non-negative integer n (i.e. it can divides n evenly). Return the location of that integer as a non-negative index. Return None if no such number can be found. Assume: xs is a list of zero or more positive integers; n is a non-negative integer. Return value: a non-negative integer or None. Reminder: you cannot call .index(), sec "Restrictions" section. Examples: locate_second_divisor([20, 3, 4, 2], 12) rightarrow 2 #4 (@index 2) is the 2^nd divisor locate_second_divisor([20, 3, 5, 9, 3], 12) rightarrow 4 # the 2^nd occurrence of 3 @ index 4 locate_second_divisor([20, 3, 5, 8], 12) rightarrow None # only 1 divisor for 12 all_primes(xs): Given a list of integers xs, check and return True if all the numbers in xs are prime numbers; return False otherwise. Assume: xs is a non-empty list of integers. Return value: a Boolean. Examples: all_primes([5, 2, 11, 37]) rightarrow True all_primes([5, 2, 4, 37]) rightarrow False # 4 is not a prime all_primes([5, 2, -2, 37]) rightarrow False # -2 is negative and cannot be a prime count_2d(xss, v): Given a list of values (a 2D list) xss and a value v, return a count of how many times v occurs in xss. Assume: xss is a list of values of any length; any member in xss could be empty. v can be any value (such as an integer, a string, etc.). Return value: an integer. Remainder: you cannot call .count(), see "Restrictions" section. Examples: count_2d([[2, 3], [4, 3, 5]], 3) rightarrow 2 # two 3s, 1 in the 1st list, 1 in the 2nd count_2d([[2, 2], [], [4, 1, -5]], 3) rightarrow 0 # no 3s in any of the lists count_2d([], 3) rightarrow 0 # empty list
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
