Recursion Algorithms

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/7

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

8 Terms

1
New cards

How many times does the following code run and what is the output of the code given the input of 21?

def fun2(n):

----if(n == 0):

--------return

----fun2(n / 2)

----print(n % 2, end="")

5 times, 10101

https://www.geeksforgeeks.org/practice-questions-for-recursion-set-2/?ref=rp

2
New cards

What is the output of the following code?

def adder(n):

----if(n < 10):

--------adder(n + 1)

--------print(n,end=" ")

adder(5)

9 8 7 6 5

Verify code here:

http://www.pythontutor.com/visualize.html#mode=display

3
New cards

def trips(n):

----if n <= 0:

--------return 0

----else:

----return (n n n) + trips(n-1)

# driver code

print(trips(3))

36

http://www.pythontutor.com/visualize.html#mode=display

4
New cards

What is the output of the following code for n=5?

def dataAlgo(n):

----if n == 1:

--------return 2

----else:

--------return 2 * dataAlgo(n-1)

print(dataAlgo(5))

32

http://www.pythontutor.com/visualize.html#mode=display

5
New cards

What is the output of the following code with the given input of

[55, -22, 7, 33, 99, 30, 42]?

def dataSequence(listData):

----data = listData[0]

----for i in listData:

--------if i > data:

------------data = i

----return data

print(dataSequence([55, -22, 7, 33, 99, 30, 42]))

99

Verify code at:

http://www.pythontutor.com/visualize.html#mode=display

6
New cards

What is the output of the following code for n = 4?

def recursiveAlgo(n):

----if n == 0:

--------return 0

----else:

--------return 3 n - 2 recursiveAlgo(n - 1)

print(recursiveAlgo(4))

-6

Verify output:

http://www.pythontutor.com/visualize.html#mode=display

7
New cards

What is the output of the following code for n = 5?

def recursiveAlgo(n):

----if n == 0:

--------return 0

----else:

--------return 2 * recursiveAlgo(n - 1)

print(recursiveAlgo(5))

What is the output of the following code for n = 5?

def recursiveAlgo(n):

----if n == 0:

--------return 0

----else:

--------return 2 * recursiveAlgo(n - 1)

print(recursiveAlgo(5))

8
New cards

What is the output of the following code for n = 5?

def recursionAlgo2(n):

----if n == 0:

--------return 1

----else:

--------return n * recursionAlgo2(n - 1)

print(recursionAlgo2(5))

120

Verify output:

http://www.pythontutor.com/visualize.html#mode=display