Python Functions sum_of_digits(n): Given a non-negative integer n, calculate and
ID: 3804340 • Letter: P
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: 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)Explanation / Answer
code: for first 3 function
import os
import sys
def sum_of_digits(n):
summ=0
while n!=0:
summ+= int(n%10)
n=n/10
return summ
def multiply_until_total_reached(original,total,n):
cnt=0
while original<=total:
original=original*n
cnt+=1
return cnt-1
def replicate(xs,n):
newxs=[]
for i in xs:
for j in range(0,n):
newxs.append(i)
return newxs
print replicate([1,2,2,3],2)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.