# We always start with this!
%pylab inline
Oh no! I wrote a simple little function to remove all instances of the number 6 from a list, and it doesn't work! There must be something terribly evil about the number 6.
Can you figure out the problem, and fix my function?
def remove_all_6s(lst):
"""Removes all 6s in a list, and returns the list."""
for n in lst:
if n == 6:
lst.remove(n)
return lst
remove_all_6s([1,6,6,3,3,6,4,6,5])
This is actually a rather deep and challenging problem that comes up in many languages and in many forms. Do not be discouraged if you have trouble solving it!
Modify the code in a hundred different ways, just to see what happens!
Try using print inside the function to see what is happening at each iteration.
Think about what happens when you remove an item from a list in the middle of a for loop. How could you avoid this?
This is an example of a more general problem known as "iterator invalidation," and a bit of internet searching may prove to be illuminating...
The zip function allows you to iterate through 2 or more lists at the same time:
my_list1 = [1, 2, 3, 4]
my_list2 = [10, 20, 30, 40]
for n,m in zip(my_list1, my_list2):
print(n, '+', m, '=', n + m)
How is this different from "nesting" for loops? What happens if I run the following code? How is this different from using zip like above?
my_list1 = [1, 2, 3, 4]
my_list2 = [10, 20, 30, 40]
for n in my_list1:
for m in my_list2:
print(n, '+', m, '=', n + m)
It is fall season - time to bake a pumkin pie! Because I don't really remember what goes into a pumpkin pie, I looked up a recipe. We can keep the needed ingredients in a python list of strings.
ingredients = ["pumpkin","sweetened condensed milk","eggs","cinnamon","ginger","nutmeg","salt","pie crust"]
It turns out I have a bunch of stuff in stock, but not all of it can be used for making my pie.
stock = ["chair","salt","milk","eggs","flour","lemon juice","frozen fish","apples","sugar","dish washing soap","sweetened condensed milk","ginger","garlic","pumpkin","potatoes"]
Write a function shoppinglist(ingredients, stock) that will return a list with all the items that I need for the pie but I don't have in stock.
Write a function update_stock that removes all items from stock that I will be using for the pie. The function should return an updated stock list. Assume that an item gets used up entirely when I bake my pie.
def draw_circle(radius, npoints=100):
angles = linspace(0, 2*pi, npoints)
xs = cos(angles) * radius
ys = sin(angles) * radius
plot(xs, ys, 'k-')
axis('equal')
axis('off')
draw_circle(2.0)
def draw_circle(radius, center_x=0, center_y=0, start_angle=0.0, end_angle=2*pi, npoints=100):
# Your code here
You can use a circle for the edge of the face, a circle for each of the eyes, and a partial circle (with start_angle and end_angle different from 0 and $2 \pi$) for the smile.
I made the following one before class:
In class we defined the Heavside function in python as step(x). Unfortunaley it does not work on arrays yet, meaning if the input x is an array python will return an error. Use your method of choice (smart indexing or for loops) to redefine step(x) as better_step(x) such that it will work on arrays too. You may use your own definition of step(x) or you can use the one provided below.
def step(x):
"This is the Heaviside step function. It returns 0 for x values below 0, 1/2 for x = 0, and 1 for x > 0."
if x < 0:
return 0
elif x == 0:
return 0.5
else:
return 1
x = linspace(-1,1,num = 1000)
y = better_step(x)
plot(x,y,'o')
Your function should return a list of all the numbers less than $n$ that $n$ is divisible by.
>>> get_factors(7)
[1, 7]
>>> get_factors(12)
[1, 2, 3, 4, 6, 12]
You can use for i in range(n, m+1): to loop over all numbers between n and m inclusively.
To check if x is divisible by y, you can use the modulo (or remainder) operator:
>>> 12 % 3 == 0
True
>>> 12 % 5 == 0
False
Remember that a number is prime if its only two factors are 1 and itself.
>>> is_prime(7)
True
>>> is_prime(12)
False
You can use the get_factors function we defined above!
2
3
5
7
11
13
17
19
...