Sign InNuggetsTutorials CodePadCheatsheet

Daily Exercise

Day 1: Define a function that checks whether a number is even, in O(1) time.
def is_even(num): print is_even(520), is_even(31)
Day 2: Define a function that returns the minimum in a list of positive numbers in O(n) time.
def find_min(list): my_list = [5, 4, 6, 2, 7, 1, 8, 10, 9, 3] print find_min(my_list)
Day 3: Define a function that returns the product of consecutive numbers from 1 to n, in O(n) time.
def product(n): print product(6)
Day 4: Define a function that checks whether an element occurs in a list, in O(n) time.
def is_in(list, elem): print is_in([3, 5, 6, 2], 5)
Day 5: Define a function that computes and returns the total of a list of integers, in O(n) time.
def total(list): print total([4, 2, 6, 5, 10])

Charting the Growth Rate

Modify y1, y2, y3 at the bottom of the program to experiment with different growth rate.

import turtle s = turtle.Screen() s.setup(420, 420) SIZE = 400 half_size = SIZE/2 tx = turtle.Turtle() tx.speed(0) tx.up() tx.goto(-half_size, -half_size) tx.down() tx.forward(SIZE) ty = turtle.Turtle() ty.speed(0) ty.up() ty.goto(-half_size, -half_size) ty.down() ty.left(90) ty.forward(SIZE) t1 = turtle.Turtle() t2 = turtle.Turtle() t3 = turtle.Turtle() t1.color('red') t2.color('green') t3.color('blue') t1.up() t2.up() t3.up() t1.speed(0) t2.speed(0) t3.speed(0) t1.goto(-half_size, -half_size) t2.goto(-half_size, -half_size) t3.goto(-half_size, -half_size) t1.down() t2.down() t3.down() for x in range(0, SIZE + 1, 10): y1 = x / 10 y2 = x*x/1000 y3 = x*x*x/100000 t1.goto(x - half_size, y1 - half_size) if y2 < SIZE: t2.goto(x - half_size, y2 - half_size) if y3 < SIZE: t3.goto(x - half_size, y3 - half_size)
© CS Wonders·Home·About·Contact·Classes·Gallery·Glossary·Fun Facts