2017-02-13 HW

posted Feb 13, 2017, 6:42 AM by Samuel Konstantinovich   [ updated Feb 14, 2017, 6:26 AM ]
Today's Homework:
     Complete Codingbat set3 (hailstone)

OLD HOMEWORK REMINDER: (I will not remind you in the future to keep up with codingbat)
To avoid penalties and understand the work we are doing next, you MUST complete ALL codingbat problems assigned. 
This includes the following currently:
1) Warmup-1:
    make10,    sleep_in,    monkey_trouble,    diff21,    
    parrot_trouble,    makes10,    near_hundred,    pos_neg
2) Logic-1:
  sorta_sum,      near_ten,      love6
3) Logic-2:
  lone_sum,      lucky_sum,    no_teen_sum
4) Custom Problems: *edit* you can access set2 any way you want. It works both ways.
http://codingbat.com/home/konstans@stuy.edu/set1
http://codingbat.com/home/konstans@stuy.edu/set2
http://codingbat.com/home/konstans@stuy.edu/set3  * this is new as of today *



Goal: Generating a sequence with a loop.

The hailstone sequence starts at any natural number, and always terminates with the value 1.
To generate the sequence you follow a simple set of rules:

if n = 1 : done
if n is even: the next number is n / 2
if n is odd: the next number is 3 * n + 1

So hailstone sequences starting with the first 6 values:
1
2 1 
3 10 5 16 8 4 2 1
4 2 1
5 16 8 4 2 1
3 10 5 16 8 4 2 1
...

Write a function printHail(n) that will print the hailstone sequence (on one line for readability)
You can print with a comma at the end of the line to prevent the new line from printing
e.g.
print 5,
print 6

outputs:
5 6

Normally we don't print out things, instead we return them. We know the starting/ending number of each sequence, so that isn't a useful thing to return. Something we cannot predict is the length of the sequence!

Write a function that calculates the length of a hailstone sequence that starts with n: 
(look at the example sequences above to see why!) 
n ->  hailLen(n)
1 -> 1
2 -> 2
3 -> 8
4 -> 3
5 -> 6
6 -> 9
...






Comments