Introduction to Looping
Objectives
- Learn how to write loops
- Understand how loops differ from and are similar to recursion
Looping for a while…
Repetition is what computers do best—and what people enjoy not having to do!
To get into the design mindset of using loops, paste this function (and comments) into a new file named loops.py. This is a factorial function that uses a python for loop:
Problem 1: power(b,p)
To start, read over and run the above starter file. You should see the tests pass with the expected results.
Then, either copy and alter that function, or create a new function, based on that model, named power(b, p), which should
- Accept any numeric value (base)
b - Accept any nonnegative integer (power)
p(pcan be 0) - Return the value of
b**p. - The function must use a
forloop! (writing returnb**pis not in the spirit of this practice…) - For this problem,
power(0, 0)should return 1.0 (we realize this might push some mathematicians’ tolerance past its limit)
Here are a few tests to paste and use.
The value of n in factorial is analogous to the value of p (the power) in the power function. (The base is simply used as the multiplied value…)
Problem 2: summed_odds(L)
Here is a loop-based summed function we looked at—or will look at—in class. Include this in your file (and try it out):
Either by using this as a template or by copy-paste-and-alter, create a loop-based function named summed_odds(L), which should
- Accept any list of integers L.
- You may assume it includes only integers.
- It should return the sum of all of the odd elements in L
- …using a loop!
Use an if statement inside the loop and only add to result under the correct conditions
Problem 3: until_a_repeat(high)
This function will let you explore what is sometimes called the birthday paradox:
- Even in a very small group of people (23 or more), there is greater than a 50-50 chance that a pair of those people share a birthday.
- This is explained well here, including a nice discussion of why it seems paradoxical.
First, paste and try out the number-guessing while-loop example from class:
Then, using countGuesses as a starting point (feel free to copy and alter it), write a variant of this function named until_a_repeat(high), which
- Keeps a list
Lof all of the integers guessed. Start withL = []! - Keeps looping as long as all of the elements in
Lare unique (no repeats).- Use a
whileloop! - Use the
unique(L)function that’s provided below—it returns a Boolean.
- Use a
- Within the
whileloop:- Make a guess in the
range(0, high). - Count the number of guesses (add one each time to some kind of counting variable).
- Add the guess on to the end of the list
L(see below for a hint on this…).
- Make a guess in the
- When the
whileloop finishes, the function should return the number of guesses needed until it gets a repeat.
Hints:
Remember that you can add 1 to a variable with
count += 1Similarly, you can add a new element 42 to the end of a list
Lwith the lineL = L + [42]The element needs to be in brackets, because only lists can be added to lists.
Feel free to use this
unique(L)function provided here.
Once you’ve written your until_a_repeat function, you should confirm that until_a_repeat(365) produces surpringingly small numbers! For example,
…and then try running 10,000 times with a list comprehension:
You might find the average, maximum, and minimum of that list comprehension L:
See if these values match your intuition.
Sometimes it’s tricky to know whether it’s one’s program—or intuition—that needs refinement!
Optional Bonus Challenge
Problem 1 Write the integer_div function. This function takes two arguments (numer, denom) and returns the result of performing integer division like so: numer // denom. You cannot use floating point division ( / ) or integer division ( // ) in your implementation.
Here is some example output:
What happens in your function when you try to divide by 0?
Problem 2 Write the my_mod function. This function takes two arguments (numer, mod) and returns the result of performing mod like so: numer % mod. You cannot use the mod operator ( % ) or any division ( / or // ) in your implementation.
Here is some example output: