Write a function skip_add that takes a single argument n and computes the sum of every other integer between 0 and n. Assume n is non-negative.
defskip_add(n):""" Takes a number x and returns x + x-2 + x-4 + x-6 + ... + 0.>>> skip_add(5) # 5 + 3 + 1 + 0 9>>> skip_add(10) # 10 + 8 + 6 + 4 + 2 + 0 30>>> # Do not use while/for loops!>>> from construct_check import check>>> check('lab03.py', 'skip_add',... ['While', 'For']) True """"*** YOUR CODE HERE ***"
Q1: Solution
defskip_add(n):""" Takes a number x and returns x + x-2 + x-4 + x-6 + ... + 0.>>> skip_add(5) # 5 + 3 + 1 + 0 9>>> skip_add(10) # 10 + 8 + 6 + 4 + 2 + 0 30>>> # Do not use while/for loops!>>> from construct_check import check>>> check('lab03.py', 'skip_add',... ['While', 'For']) True """"*** YOUR CODE HERE ***"if n <=0:return0return n +skip_add(n -2)
Q2: Hailstone
Recall the hailstone function from homework 1. First, pick a positive integer n as the start. If n is even, divide it by 2. If n is odd, multiply it by 3 and add 1. Repeat this process until n is 1. Write a recursive version of hailstone that prints out the values of the sequence and returns the number of steps.
defhailstone(n):"""Print out the hailstone sequence starting at n, and return the number of elements in the sequence.>>> a = hailstone(10) 10 5 16 8 4 2 1>>> a 7>>> # Do not use while/for loops!>>> from construct_check import check>>> check(this_file, 'hailstone',... ['While', 'For']) True """"*** YOUR CODE HERE ***"
Q2: Solution
defhailstone(n,count=0):"""Print out the hailstone sequence starting at n, and return the number of elements in the sequence.>>> a = hailstone(10) 10 5 16 8 4 2 1>>> a 7>>> # Do not use while/for loops!>>> from construct_check import check>>> check(this_file, 'hailstone',... ['While', 'For']) True """"*** YOUR CODE HERE ***"print(n)if n ==1:return1elif n %2==0:return1+hailstone(n //2)else:return1+hailstone(n*3+1)
Q3: Summation
Now, write a recursive implementation of summation, which takes a positive integer n and a function term. It applies term to every number from 1 to n including n and returns the sum of the results.
defsummation(n,term):"""Return the sum of the first n terms in the sequence defined by term. Implement using recursion!>>> summation(5, lambda x: x * x * x) # 1^3 + 2^3 + 3^3 + 4^3 + 5^3 225>>> summation(9, lambda x: x + 1) # 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 54>>> summation(5, lambda x: 2**x) # 2^1 + 2^2 + 2^3 + 2^4 + 2^5 62>>> # Do not use while/for loops!>>> from construct_check import check>>> check(this_file, 'summation',... ['While', 'For']) True """assert n >=1"*** YOUR CODE HERE ***"
Q3: Solution
defsummation(n,term):"""Return the sum of the first n terms in the sequence defined by term. Implement using recursion!>>> summation(5, lambda x: x * x * x) # 1^3 + 2^3 + 3^3 + 4^3 + 5^3 225>>> summation(9, lambda x: x + 1) # 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 54>>> summation(5, lambda x: 2**x) # 2^1 + 2^2 + 2^3 + 2^4 + 2^5 62>>> # Do not use while/for loops!>>> from construct_check import check>>> check(this_file, 'summation',... ['While', 'For']) True """assert n >=1"*** YOUR CODE HERE ***"if n ==1:returnterm(n)else:returnterm(n)+summation(n -1, term)