Notes and Assignments

2017-03-24

posted Mar 24, 2017, 5:37 AM by Samuel Konstantinovich

Do Now:
Write two functions:
1. makeSentence(L) Take a list of strings, and join them in one string with spaces BETWEEN the words. do not use the function join(). Assume no spaces in the strings.
       
makeSentence(["coding","you","must","do"]) returns:  'coding you must do'  (no space at end) 
makeSentence( [])   returns ''  (empty string)

2. makeFibList(n) takes a non-negative integer n, and returns a list of the first Fibonacci numbers up to the n'th number counting 0 as the 0th number. (do NOT use a separate fib function for this.)
makeFibList(0) returns  [0]
makeFibList(5) returns  [ 0, 1, 1, 2, 3, 5]


2017-03-23 HW

posted Mar 23, 2017, 6:08 AM by Samuel Konstantinovich   [ updated Mar 23, 2017, 8:59 AM ]

Work in class, complete at home:

http://codingbat.com/python/List-1  
(These are just to see if you understand the use of list basic functions)
1. make_pi
2. same_first_last
3. common_end
4. has23

5. count_evens
6. big_diff
7. centered_average

8. makeListNEvens(this was old hw)
9. makeHailList (this was old hw)
*the others in this section will be assigned later. 

2017-03-22 HWList

posted Mar 22, 2017, 7:00 AM by Samuel Konstantinovich   [ updated Mar 22, 2017, 8:49 AM ]

Do Now:
1. On paper, write a hailLen(n) function, that calculates the length of a hailstone sequence starting with n.
hailLen(1) -> 1
hailLen(2) -> 2
hailLen(3) ->  8   //because 3 10 5 16 8 4 2 1

Recall that the next number hailstone sequence is calculating by looking at the current number Hn
When Hn is 1:
     there is no next number.
When Hn is even:
    H(n+1) = Hn / 2
When Hn is odd:
    H(n+1) = 3 * Hn + 1 

Write a recursive method to solve this problem in your notebook. Then compare with your neighbors.


Lists
A list object is a collection of values. The list can contain 0 or more elements, and it can be modified (unlike strings).
We use [] square brackets to designate lists. 
The len() primitive works on lists just like strings.
We place a zero-based index inside the brackets to access/modify an individual position in the list.

y = []

     store the empty list into y.

x = [ 1, 5, 3, 9, 2]

    Store a list in variable x.

print x
    displays [1,5,3,9,2]     

print x[1]      (access a single value)
    displays 5 

x[2]=18         (modify a single value)
    Updates/modifies the list such that the 3 is replaced with 18.

Slices work the same way as before, except they return a COPY of the sub-list. 


x = [ 1, 5, 3, 9, 2]

print x[1:3]     
    displays [5,3]

print x[:4]     
    displays [1,5,3,9]

print x[::2]     
    displays [1,3,2]

You CAN assign to a slice or replace a slice with another list/slice! (examples tomorrow)


List Methods

A list object has a number of member methods.  

The following list transformation functions update a list object. In the case of the pop method, it both returns information as well as updates/modifies the list.

Classwork: (complete for homework)
In a python terminal test each of the five methods. Then in your notebook, write the method and describe exactly what it does, you may write an example of how it is used.

l.append ( object )
l.extend ( list )
l.insert ( index , object )
l.pop ( index ) → item
l.remove ( value ) 

Discuss with your neighbors so you can see if you have the right idea about how these work. 
Did you test many cases? (empty lists, 0's, values that aren't there, etc)
You MUST see what parameters are valid or invalid through experimentation.


HW LIST: (you will need a working copy in class tomorrow) 
1. Make a function that returns a list of n even integers starting with 0. Do not use the range function.
makeEvenList(n) 

makeEvenList(2) -> [0, 2]
makeEvenList(5) -> [0, 2, 4, 6, 8]

2. Make a function that returns a list of the hailstone sequence starting with n. This does not have to be recursive. 

makeHailList(n)
makeHailList(3) -> [ 3, 10, 5, 16, 8, 4, 2, 1]
makeHailList(16) -> [16, 8, 4, 2, 1]

2017-03-21 Hanoi

posted Mar 21, 2017, 6:42 AM by Samuel Konstantinovich   [ updated Mar 21, 2017, 7:05 AM ]

Goal: Towers of Hanoi 2: To understand recursion you must first... understand recursion.

define a function: hanoi(n, start, temp, end)
-N is the number of disks.
-start temp and end are strings of the names of the towers.
-You are printing the set of moves.



hanoi(2, "A","B","C")
move from A to B
move from A to C
move from B to C

hanoi(3, "X","temp","Y")
move from X to Y
move from X to temp
move from Y to temp
move from X to Y
move from temp to X
move from temp to Y
move from X to Y

Think of this recursively, and remember that a base case that is "do nothing" just means you can write something like this with no else:

if you_have_more_to_do : 
   do recursion step(s)
   do recursion step(s)




The number of moves to solve a towers of hanoi with n disks is easily calculated.
Explicit definition:
tn = 2n - 1 
Recursive definition:
tn = 2 * t(n-1) + 1  [twice the previous result plus one]

Since this number is huge, computers cannot realistically compute results for any large values of n.


Remember Moore's Law:

Moore's law refers to an observation made by Intel co-founder Gordon Moore in 1965. He noticed that the number of transistors per square inch on integrated circuits had doubled every year since their invention. Moore's law predicts that this trend will continue into the foreseeable future

"I've heard the death of Moore's law more times than anything else in my career, and I'm here today to really show you and tell you that Moore's Law is alive and well and flourishing." Intel CEO Brian Krzanich (2017)

Moore's law does seem to predict speed increases as we can see from this chart of calculations per second per $1000:

And this chart of calculations per second (also normalized by dollars)


2017-03-20 Hanoi

posted Mar 20, 2017, 7:15 AM by Samuel Konstantinovich

Goal: Towers of Hanoi Part 1: Super Saiyan Ninja Monks.

1. How many fibs could you calculate in 5 seconds?
2. What was the largest you tried and completed?
3. Is your computer smarter than a 5th grader? Why/Why not?


2017-03-16 Fractal Tree

posted Mar 16, 2017, 6:10 AM by Samuel Konstantinovich   [ updated Mar 16, 2017, 6:17 AM ]

Goal: 2 Tree 4 no reason.

do now:
1. Write a Netlogo procedure square n (non-reporting) to make a turtle draw a square of size n.
2. Write a Netlogo function sum n   that takes an integer n, and calculates the sum of the numbers from 1 to n inclusive. Functions use (to-report)! (Remember the repeat x[ ]  command!)

Notes: ( Diagrams and notes from class are on the board only ) sorry!

Classwork/Homework 
Complete:

tree [ n s a ]   
n : number of levels
s : length of each branch
a : angle of the split

2017-03-15 HW

posted Mar 15, 2017, 12:16 AM by Samuel Konstantinovich   [ updated Mar 15, 2017, 6:07 AM ]

Goal: Writing a function using loops, and using recursion

Complete the two GCF functions at home if you do not complete them in class.

Euclidean algorithm for Greatest Common Factor
This algorithm is based on the fact that G.C.F. of two numbers divides their difference as well. The math behind it is not important, but the algorithm is:

Calculate the remainder of the greater divided by the smaller. 
This remainder becomes the new smaller number (as it is the smallest of all 3 numbers)
The original smaller number is now the "larger"

Repeat until the remainder is 0.

For example, if we want to find the G.C.F. of 54 and 24:
Divide 54 by 24. The remainder is 6
24 is the larger
6 is the smaller
Repeat the process:
Now, we divide 24 by 6 and the remainder is 0. Hence, 6 is the required G.C.F


Can you easily find the G.C.F of 40 and 192 this way?
192,40 -> 32
40,32  -> 8
32,8 -> 0
8,0 STOP!

gcf(54,24) results in 6
gcf(192,40) results in 8

1. Write this using a loop.  gcfLoop(a,b)
Think:
-When should the loop stop?

2. Write this using recursion: gcfRec(a,b)  , make sure you call gcfRec, NOT gcfLoop in your function!!
Think of these two "rules" for recursion:
-If I can solve the problem now, without recursion, the function simply returns a value. (base case) 
-If I cannot solve the problem now without recursion, the function reduces the problem to something smaller and similar and calls itself to solve the problem. (recursive case)



2017-03-13

posted Mar 13, 2017, 6:50 AM by Samuel Konstantinovich   [ updated Mar 13, 2017, 11:30 PM ]

Homework:
1. Refresh your memory of basic turtle movement commands, and NetLogo functions/procedures.
2. Complete your fib(n) function in python.  
3. What is the largest n you can calculate in fib(n) that completes in less than 5 seconds? (start with lower numbers and then try larger as you test)

Describe the following problems using both:
  -recursive definition(s)
  -base case(s)
Do not write the function, just the rules for when you recurse, and when you stop.
You need at least 2 parts to each answer, the 3rd and 4th require more parts.

ALL QUESTIONS SHOULD BE ANSWERED ON PAPER in your notebooks. You do not have to copy the entire question, just enough to understand what you are writing about. (e.g. function name, or answer with complete sentences.)
0- Whatever you do not complete, must be completed at home!
1. countDigits(n)  - how many digits are in the number
2. myLen(s) - how many characters are in the string (without using len)
3. countFives(n)  - how many fives are in the number
4. countMatching(s)   -   how many consecutive matching characters in a string. e.g.  'aabcccd' has 3 matching (overlap counts twice)
Fibonacci 

5. Calculate the first 15 elements of the sequence, and time yourself. (Starts with 0)
6. Extrapolate: How long would it take to calculate the 50th term? (estimate based on your data)
7. How long would it take the computer? (guess)




2017-03-10 - HW Fib

posted Mar 10, 2017, 6:58 AM by Samuel Konstantinovich   [ updated Mar 10, 2017, 7:19 AM ]

Goal: Recursion day 2

DO NOW: Open your exp(b,e) function and do the rest of the work on paper.

Consider the following scheme functions:
(define (foo x)
 (if (< x 1)
   1
   (* x (foo (- x 1)))))

(define (bar x y)
  (cond
    (( > x y) 0)#base case
    (( = x y) x)#base case
    (else (+ x y (bar (+ x 1) (- y 1))))))

1. ON PAPER - Evaluate:
(foo 6)        (bar 1 3)        (bar 1  10)

2. ON PAPER - Write the function bunnyEars(n)

BEFORE WRITING IT: 
a) What is the base case?
b) How can you describe this recursively? (not with code)
We have a number of bunnies and each bunny has two big floppy ears. 
We want to compute the total number of ears across all the bunnies recursively (without loops or multiplication).
bunnyEars(0) → 0
bunnyEars(1) → 2
bunnyEars(2) → 4

NOW consider the python function:
def what(s):
    total = 0
    while len(s)>1:
        if s[:2]=="hi":
            total+=1
        s = s[1:]
    return total

3. ON PAPER - Evaluate:
what("Hihihi") what("hi-hihihi")

4. Write a recursive version of the what() function. 
**This should use no loops, and no other methods that count.
What is the base case? (easiest thing to count)
What about all other strings, how can you reduce them to be closer to the base case?





Homework - Bring in a printed physical copy, with your header on top. 
I WILL NOT count hand written work.
Consider the Fibonacci sequence:
0 1 1 2 3 5 8 13 21 34 55 . . .

Or:

n    nth term of the sequence
0    0
1    1
2    1
3    2
4    3
5    5
...

The first two terms are 0 and 1. Each subsequent term is the sum of the previous two.
1
0 + 1 -> 1
1 + 1 -> 2
1 + 2 -> 3
2 + 3 -> 5
...

QUESTIONS (Write complete answers.)
1. Describe how this function has a recursive relationship
2. Describe the relationship of fib(n) in terms of fib() 
3. What must the base case(s) be?
4. Draw a tree function calls when you want to evaluate: fib(5) ?
5. How many times you need to call a fib() function for fib(5)?
6. What happens to this diagram if you want to evaluate fib(6) ? 
Do NOT write the function.

2017-03-09

posted Mar 9, 2017, 8:05 AM by Samuel Konstantinovich   [ updated Mar 9, 2017, 8:46 AM ]

Goal: What is recursion? See recursion.

HW: 
1. Complete exp(base,exponent) and have it ready in python (email it if you need to)
    base can be any non-zero number.
    exponent can be any integer. (negative exponents are allowed)

2. Complete myInt() on codingbat.

1-10 of 28