Question

In: Computer Science

Can you answer these question using python and recursive functions Write a function that consumes two...

Can you answer these question using python and recursive functions

Write a function that consumes two numbers N1 and N2 and prints out all the even numbers between those numbers, using recursion.

Using any number of list literals of maximum length 2, represent the data from your Survey List. So you cannot simply make a list of 15 values. Make a comment in your code that explicitly describes how you were able to represent this.

Write a recursive function that traverses your list of pairs and calculates their sum.

Solutions

Expert Solution

def find_evens(n1, n2):
retlist = []
if n1 % 2 == 0: # n1 is even
print(n1)
elif n1+1 <= n2: # n1 is even
print(n1+1)
  
n1 = n1+2 # increment n1 by 2
if n1 <= n2:
find_evens(n1, n2) # call the same function recursively again with new values

# Test the recursive functions.
print("evens between 2,10")
find_evens(2,10)
print("evens between 1,10")
find_evens(1,10)
print("evens between 1,11")
find_evens(1,11)
print("evens between 2,11")
find_evens(2,11)


Related Solutions

PYTHON PYTHON Recursive Functions. In this problem, you are asked to write three recursive functions. Implement...
PYTHON PYTHON Recursive Functions. In this problem, you are asked to write three recursive functions. Implement all functions in a module called problem1.py. (10 points) Write a recursive function called remove char with two parameters: a string astr and a character ch. The function returns a string in which all occurrences of ch in astr are removed. For example, remove char("object oriented", ’e’) returns the string "objct orintd". Your implementation should not contain any loops and may use only the...
This is a python question. Write a function mult_table(n) that consumes a natural number n and...
This is a python question. Write a function mult_table(n) that consumes a natural number n and returns the n+1 by n+1 multiplication table (where each entry in the inner list is equal to the product of which list it is and the inner list position number, or in other words, the product of the row and column numbers). Use accumulative recursion. def mult_table(n) ''' Returns the n+1 by n+1 multiplication table    mult_table: Nat => (listof (listof Nat))    Examples:...
Python question Write a function int(lofi, alofi) that consumes two sorted lists of distinct integers lofi...
Python question Write a function int(lofi, alofi) that consumes two sorted lists of distinct integers lofi and alofi, and returns a sorted list that contains only elements common to both lists. You must obey the following restrictions: No recursion or abstract list functions, intersect must run in O(n) where n is the combined length of the two parameters. sort function is not allowed as well as list comprehensions math is the only library that can be imported Example: int([4, 13,...
PYTHON QUESTION: - Write the body of a function most_ending_digit(L) that consumes a non-empty list of...
PYTHON QUESTION: - Write the body of a function most_ending_digit(L) that consumes a non-empty list of natural numbers L and return the single digit that occurs most frequently at the end of the numbers in the list. The function returns the smallest digit in the case of a tie. Your function should run in O(n) time. Do not mutate the passed parameter. def most_ending_digit(L) ''' Returns the single digit that occurs most frequently as the last digit of numbers in...
C++ Recursive Functions: Please call functions in a main function as well. 1. A recursive function...
C++ Recursive Functions: Please call functions in a main function as well. 1. A recursive function that print the reverse of a string. (e.g., void printReverse(string exp)). For example, if exp =”coding”, then the function should print out “gnidoc”. 2. Implement a non-recursion-based binary search function. Convert this function into a recursion-based function. 3. Implement a recursive and non-recursive Fibonacci function.
Using python pls!!!!! Write a recursive function gcd(m,n) that returns the greatest common divisor of a...
Using python pls!!!!! Write a recursive function gcd(m,n) that returns the greatest common divisor of a pair of numbers. The gcd of m and n is the largest number that divides both m and n. If one of the numbers is 0, then the gcd is the other number. If m is greater than or equal to n, then the gcd of m and n is the same as the gcd of n and m-n. If n is greater than...
I am using the program Python. Assume a function called count_primes is pre-definied. If consumes two...
I am using the program Python. Assume a function called count_primes is pre-definied. If consumes two int arguments start and stop that returns the number of prime integers between start (inclusive) and stop (exclusive). Note: The start value must be less than or equal to the stop value. Enter 3 assertEqual statements to test the count_primes function.
Write a recursive function in python called make_palindrome that takes a sequence as a parameter and...
Write a recursive function in python called make_palindrome that takes a sequence as a parameter and returns a new sequence that is twice the length of the parameter sequence but that contains the contents of the original in palindrome form. For example, if the sequence "super" is passed into the function, the function will return "superrepus".
Question I'm having trouble with: Using LISP, write a recursive function that takes a list and...
Question I'm having trouble with: Using LISP, write a recursive function that takes a list and returns the number of times the symbol 'a' occurs in it. Note: do not count a's that might occur in a sublist within the list.
Part 1: Write a recursive function that will calculate Fibonacci numbers using a recursive definition. Write...
Part 1: Write a recursive function that will calculate Fibonacci numbers using a recursive definition. Write a short program to test it. The input of this program must be a positive integer n; the output is the corresponding Fibonacci number F(n) Part 2: Write an iterative function to calculate Fibonacci numbers. Write a test driver for it. The input of this program must be a positive integer n; the output is the corresponding Fibonacci number F(n). Part 3: Write a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT