Question

Python 3 Write a function oneSum which takes one input, n, and finds the sum: For example: >>> print (oneSum( 1)) which is 1-1. >>> print (oneSum( 2)) 12 Which is 111 12. print (oneSum(3 ) 123 Hint: think about each element of the sum (1, 11, 111, for example) represented as a sum of powers of 10: 111-100 + 10^1 + 10^2= 1 + 10 + 100 For example: Test Result print(oneSum(3))123 print (oneSum(4)) 1234 print(oneSum(e)) print(oneSum(1) 1 1

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the code for you:

#!/usr/bin/python
def oneSum(count):
   '''
       Takes one input count, and finds the sum
       1 + 11 + 111 + 1111 + ... + up to n digits of 1.
   '''
   value = 0;
   for i in range(count):
       value = value * 10 + 1;
   if(count == 0):
       return 0
   return value + oneSum(count-1)      

print oneSum(1)  
print oneSum(2)
print oneSum(3)
print oneSum(4)
print oneSum(0)

And the output screenshot is:

Add a comment
Know the answer?
Add Answer to:
Python 3 Write a function oneSum which takes one input, n, and finds the sum: For...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Define a function called collapse() which takes a list as input. Each element of the list...

    Define a function called collapse() which takes a list as input. Each element of the list will either be an integer, or a list of integers. The function should modify the input list by replacing all of the elements which themselves are lists with the sum of their elements. For example: Test Result vals = [1, 2, 3, 4, 5] collapse(vals) print(vals) [1, 2, 3, 4, 5] vals = [1, [2, 3], 4, 5] collapse(vals) print(vals) [1, 5, 4, 5]...

  • ****python**** Q1. Write a recursive function that returns the sum of all numbers up to and...

    ****python**** Q1. Write a recursive function that returns the sum of all numbers up to and including the given value. This function has to be recursive; you may not use loops! For example: Test Result print('%d : %d' % (5, sum_up_to(5))) 5 : 15 Q2, Write a recursive function that counts the number of odd integers in a given list. This function has to be recursive; you may not use loops! For example: Test Result print('%s : %d' % ([2,...

  • Write a function that finds the sum of all numbers up to (and including) N that...

    Write a function that finds the sum of all numbers up to (and including) N that are multiples of either x or y. e.g. For N= 10 x=2 y=3 s=2+3 +4 +6+ 8 + 9 + 10 = 42 In [ ]: def multiplesum (N, x, y): returns In [ ]: # Your Provided Sample Test Cases print("#1", multipleSum (10, 2, 3) == 42) print("#2", multipleSum (25,7,8) == 90) print("#3", multipleSum (15,1, 12) == 120)

  • Write a function valid_integers(strings) that takes a list of strings as a parameter and returns a...

    Write a function valid_integers(strings) that takes a list of strings as a parameter and returns a list of the integers that can be obtained by converting the strings to integers using an expression like int(string). Strings which cannot be converted in this way are ignored. Hint: the statement pass is a statement that does nothing when executed. For example: Test Result strings = ['123', '-39', '+45', 'x', 'COSC121', '123+', '12-3'] print(valid_integers(strings)) [123, -39, 45]

  • Using Matlab Write a program which will: a. Accept two numbers from the user m and...

    Using Matlab Write a program which will: a. Accept two numbers from the user m and n b. Define an array with the dimensions a(n,m) and fill it with random numbers in the range of -100 to 100 inclusive. c. Provide the user the following menu from which he can select: 1. Sum of all elements in the array. Print the result. 2. Sum of the element in a row that he’ll specify. Print the result. 3. Sum of the...

  • Write a Python function binom_product that takes integer arguments a and b and positive integer argument...

    Write a Python function binom_product that takes integer arguments a and b and positive integer argument n and returns the product of the coefficients in the expansion of (ax + by)”. Example: Let a = 2, b = -1, and n = 3. Then (2x – y)3 = 8x3 – 12x²y + 6xy2 – 43 The product of the expansion coefficients is 8 x -12 x 6 x -1 = 576 Notes: There are two visible test cases and three...

  • Write a Python function cardinality() that takes in three Python set objects, representing sets of between...

    Write a Python function cardinality() that takes in three Python set objects, representing sets of between 0 and 50 integers, AA, BB, and UU. Your function should return a single non-negative integer value for the cardinality of the set below. AA and BB are subsets (not necessarily proper) of the universal set UU. |P(A¯¯¯¯∩B)||P(A¯∩B)| Note 1: You can copy-paste the code declaring the various visible test cases below. We strongly encourage you to do this to test your code. Note...

  • Basic Python code needed Define the get_lines_from_file) function which is passed a filename as a parameter....

    Basic Python code needed Define the get_lines_from_file) function which is passed a filename as a parameter. The function reads the information from the file (corresponding to the filename parameter) and returns a list of strings where each element of the returned list corresponds to one line of the file. The list of strings which is returned by the function should not contain any newline characters. For example, if the file contains the text: 10 440 240 4 42 4 42...

  • (Java Please) Sum of Digits Write a program that will sum the digits of a number...

    (Java Please) Sum of Digits Write a program that will sum the digits of a number input by the user. For example, if the user enters the number 1234, the sum of the digits will be 10 (1 + 2 + 3 + 4 = 10). The user will enter a number with at least 4 digits (greater than 1000). That value will be sent to a method which will return the sum. Sample Output 1: SUM OF DIGITS -------------...

  • 0% of final (S1 2019) Write a function called sum_of_products(x), where x is a list of lists of i...

    0% of final (S1 2019) Write a function called sum_of_products(x), where x is a list of lists of integers, this function returns a number which is the sum of the products of each list of integers. For example: print(sum_of_products ([[1,2], [5], [2,5]])) will result in the output: 17 That is, the result of 2 + 5+ 10 For example: Test Result print(sum_of_products( [2,3]])) print sum_of_products([[2],[3]])) print(sum_of_products([[1,21,[S],[2,5]1)) 17 0% of final (S1 2019) Write a function called sum_of_products(x), where x is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT