****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, 3, 5, 6], count_odd([2, 3, 5, 6]))) |
[2, 3, 5, 6] : 2 |
Q3.
Write a recursive Python function, given a non-negative integer N, to calculate and return the sum of its digits.
Hint: Mod (%) by 10 gives you the rightmost digit (126 % 10 is 6), while doing integer division by 10 removes the rightmost digit (126 / 10 is 12).
This function has to be recursive; you may not use loops! This function takes in one integer and returns one integer.
For example:
Test | Result |
---|---|
print(234, ":", sum_digits(234)) |
234 : 9 |
Q4.
Write a recursive function to compute the following series:
This function has to be recursive; you may not use loops!
For example:
Test | Result |
---|---|
print('%d : %.6f' %( 2, m(2))) |
2 : 1.500000 |
Thank you!
Dear Student ,
As per the requirement submitted above , kindly find the below solution.
Question 1:
#function that return sum
def sum_up_to(num):
#checking number
if(num<=1):
#if number equal to 1 then
return num
else:
#recursively call the function
return num+sum_up_to(num-1)
#call function
print('%d : %d' % (5, sum_up_to(5)))
======================================================
Output : Compile and Run above function to get the screen as shown below
Screen 1 :main.py
*************************
Question 3:
#function to find out sum of digits
def sum_digits(num):
#checking number
if num==0:
#if num is equal to zero
return 0
else:
#call recursively sum_digits
return num%10+sum_digits(num//10)
#function call
print(234, ":", sum_digits(234))
===================================
Output :
NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.
****python**** Q1. Write a recursive function that returns the sum of all numbers up to and...
need help with python ( no loops used please!) Write a recursive function named sum_digits that takes a given a non-negative integer N and returns the sum of its digits. Hint: Mod (%) by 10 gives you the rightmost digit (126 % 10 is 6), while doing integer division by 10 removes the rightmost digit (126/10 is 12). This function has to be recursive; you are not allowed to use loops to solve this problem! This function takes in one...
PYTHON: (Sum the digits in an integer using recursion) Write a recursive function that computes the sum of the digits in an integer. Use the following function header: def sumDigits(n): For example, sumDigits(234) returns 9. Write a test program that prompts the user to enter an integer and displays the sum of its digits. Sample Run Enter an integer: 231498 The sum of digits in 231498 is 27
Write a recursive function called freq_of(letter, text) that finds the number of occurrences of a specified letter in a string. This function has to be recursive; you may not use loops! CodeRunner has been set to search for keywords in your answer that might indicate the use of loops, so please avoid them. For example: Test Result text = 'welcome' letter = 'e' result = freq_of(letter, text) print(f'{text} : {result} {letter}') welcome : 2 e and A list can be...
Write a recursive function in Python to find the sum of digits of a number. Name the function sum_of_digits. The function should use recursive algorithm (calling itself). The function should print out the sum of all the digits of a given number. For example, sum_of_digits(343) should have a output of 10. Marks will be deducted if you do not follow strictly to the instructions. [3]: N 1 def sum_of_digits(n): HNM in sum_of_digits (343) Out[3]: 10
please explain each line of code! ( in python ) 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Your function should take one parameter, root node. You may assume that the tree only contains integers. You may not call any methods from the LinkedBinaryTree class. Specifically, you should traverse the tree in your function def binary tree even sum (root): Returns the sum of al1 even integers in the binary tree 2....
Write a recursive function Programming: to_number that forms the integer sum of all digit characters in a string. For example, the result of to_number("3ac4") would be 7. C++ i know this has been answered before but I keep getting 0 when I enter a string.
in python Part I: Sum of Odd Integers Write a recursive function sum-odds that takes a non-empty list of integers as an argument and returns the sum of only the odd integers in the list. In class we explored a recursive function called rsum that recursively computes the sum of a list of integers use it as a model to get started. Your function must be recursive and must not use any loops
(for python) [27] Write a recursive function to calculate the following for a given input value for x. result = 1+ 2+ 3 ..... + x For example: if the input for x is 5, the result will be 1+2+3+4+5 = 15 if the input for x is 8, the result will be 1+2+3+4+5+6+7+8 = 36 Write recursive function.
python language. [27] Write a recursive function to calculate the following for a given input value for x. result = 1 + 2 + 3 .... + x For example: if the input for x is 5, the result will be 1+2+3+4+5 = 15 if the input for x is 8, the result will be 1+2+3+4+5+6+7+8 = 36 Write recursive function.
ANSWER USING PYTHON Write a recursive function that takes 2 sorted lists as arguments and returns a single, merged sorted list as a result. For example, if the two input lists are [0, 2, 4, 6, 8] and [1, 3, 5, 7, 9], the returned result should be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]. Remember that you can add lists in Python using the + operator ([0] + [1,2,3] = [0,1,2,3]) and can take slices of...