﻿ List Exercises

### List Exercises

Looping with lists: adding up the numbers in a list of numbers:

``````
def listsum(L):
total = 0
for i in range(len(L)):
total += L[i]

# By the way, there is already a built-in function in Python called "sum()",
#   and sum(L) will return the sum of the numbers in a list L
``````
Looping with lists: Creating a list, element by element:
``````# Create a list with many copies of a given number
def dups(this_number, this_many_times):
for i in range(this_many_times):

# By the way, Python can create a list of duplicates this way as well:
#  [this_number]*this_many_times
``````

CodingBat Exercises:

Do the following problems from the CodingBat section "List related problems"...
In these exercises do all of your looping using "while" and not "for"

1. makeListOneToN
2. makeListNEvens
3. makeEvenListToN
4. mmakeSentence

The Fibonacci Sequence:

The sequence usually starts at at 1:   1  1  2  3  5  8  ... etc.  but we can also start it at 0:   0   1   1   2   3   5   8.
So let's define the function fib(n) such that fib(0) = 0, and fib(1) = 1, and fib(2) = 1, and fib(3) = 2, etc.

1. Create the function fib(n).  Calculate fib(6).  It should return 8.  Calculate fib(1000).  Put the answer into Comments-to-Teacher  and upload the code (along with the code for the functions from all of the exercises below).  Those of you who want to use recursion for this exercise, do it carefully.

2. Create the function fibSeq(n) which will create and return the list of all Fibonacci numbers from positions 0 through n.  For instance fibSeq(7) = [0,1,1,2,3,5,8,13]. Do not use fib(n)  (think of why using fib(n) would be inefficient -- in other words, it would be asking the computer to do a lot of extra, unnecessary work.)

The Collatz Conjecture in mathematics (see video) uses the "Hailstone Sequence" of numbers, which is explained in the video.

Start with a positive whole number as a "seed" and then generate the next number according to the following rule, if the number is even, divide it by 2, and if it's odd, multiply it by 3 and add 1.  Then go until you get to 1.
Suppose the "seed" or starting number is 5, then the sequence, as a list would be [5, 16, 8, 4, 2, 1], and its length would be 6.
If the seed is 7, then the sequence would be [7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2 ,1] and its length would be 17.

1. Create the Hailstone(seed) function that returns the Hailstone sequence given a seed.  So, Hailstone(5) = [5, 16, 8, 4, 2, 1].  Report the resulting list for Hailstone(26) in the Comments-to-Teacher, and upload the code.

2. Create the HailstoneSummary(low_seed, high_seed), which will return a list of 2 numbers: the seed, and the length of the Hailstone sequence starting from that seed, which is the longest sequence for any seed between low_seed and high_seed.  So, HailstoneSummary(2,10) = [9, 20]  because of all the seeds between 2 and 10, the seed = 9 generates the longest sequence ([9, 28, 14, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]) and its length is 20.  DO NOT USE the Hail() function, because it is inefficient (you do not need to create actual list of any sequence starting from a seed -- you only need to find its length).  Report the result of HailstoneSummary(2,100).

3. Challenge calculation: report the seed and the length of the longest hailstone sequence with a seed less than a million.  Please inform your computer that if it overheats, then that is its problem, and it should do a better job of self-cooling in the future.