Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(python)Triangular Numbers (two parts!): (3 pts) Part 1 (2pts) Write a function called triang(n) that uses recursion to return the sum +(1)+(2)++1.n+(n1)+(n2)++1. In [ ]:

(python)Triangular Numbers (two parts!): (3 pts)

Part 1 (2pts) Write a function called triang(n) that uses recursion to return the sum

+(1)+(2)++1.n+(n1)+(n2)++1.

In [ ]:

def triang(n):

 # YOUR CODE HERE
 raise NotImplementedError()

In [ ]:

assert triang(3) == 6

assert triang(10) == 55

(python)Part 2 (1pt- manually graded) Add a docstring to the code of triang(n) so that when you call help(triang) it explains what the function does.

In [ ]:

# YOUR CODE HERE

raise NotImplementedError()

Use recursion to write a function printBackwards(x) that takes in a string and prints it backwards.

In [ ]:

def printBackwards(x):

 # YOUR CODE HERE
 raise NotImplementedError()

In [ ]:

assert printBackwards('taco') == 'ocat'

assert printBackwards('Was it a cat i saW') == 'Was i tac a ti saW'

Use two arguments, where the second argument is the number you're checking divisibility by. Give this one a default value as in the code below.

def isPrime(n,i=2): if i> n/2: return True # Your Solution here

(python)Complete the function isPrime below.

In [ ]:

 
def isPrime(n,i=2):
 # YOUR CODE HERE
 raise NotImplementedError()

In [ ]:

assert isPrime(3) == True

assert isPrime(17) == True
assert isPrime(20,6) == False

(python)Complete the following test function testPrime(n) that illustrates the result of calling isPrime(i) on i=1,...,n

In [ ]:

def testisPrime(n):

 # YOUR CODE HERE
 raise NotImplementedError()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions