Sign InNuggetsCoursesCodePadCheatsheet

Daily Exercise

Day 1: Write a recursive function that returns the sum of the first n positive integers.
def sum(n): # result should be 55 print sum(10)
Day 2: Write a recursive function that reverses a string and returns it. (hint: use s[1:])
def reverse(s): # result should be "olleH" print reverse("Hello")
Day 3: Write a recursive function that returns the maximum element in a list of positive numbers. (hint: use list[1:])
def find_max(list): # should return 26 print find_max([4, 3, 16, 15, 26, 1, 9])
Day 4: Define a recursive function that checks whether an element occurs in a list.
def is_in(list, elem): # should return True print is_in([3, 5, 6, 2], 5)
Day 5: Define a recursive function that computes and returns the total of a list of integers.
def total(list): # should return 27 print total([4, 2, 6, 5, 10])

Recursive Art

# Design your own recursive art import turtle screen = turtle.Screen() screen.setup(800, 600) def draw_art(t, size): # base case if size < 5: return # TODO: draw your art here # recursively draws your art draw_art(t, size - 10) t = turtle.Turtle() t.speed(0) # TODO: call draw_art here to create your art
© CS Wonders·Home·About·Contact·Classes·Gallery·Glossary·Fun Facts