fork download
# MIT OCW 6.189 Homework 2 Exercise 2.10
# G@R
# July 8, 2013
1. Write a list comprehension that prints a list of the cubes of the numbers 1 through 10.

2. Write a list comprehension that prints out the possible results
of two coin flips (one result would be 'ht'). (Hint - how many results should there be?)

3. Write a function that takes in a string and uses a list comprehension
to return all the vowels in the string.

4. Run this list comprehension in your prompt:
[x+y for x in [10,20,30] for y in [1,2,3]]
Figure out what is going on here, and write a nested for loop
that gives you the same result. Make sure what
is going on makes sense to you!
# 1
print [n ** 3 for n in range(1, 11)]
# 2
print ['ht'[i] + 'ht'[j] for i in range(0, 2) for j in range(0,2)]
print [s1 + s2 for s1 in 'ht' for s2 in 'ht']
# 3
def vowelLister(st):
    return [v for v in st if v.lower() in 'aeiouy']
# Longest word coined by a major author,
# the longest word ever to appear in literature.
# Source:
# "name of a dish compounded of all kinds of dainties, fish, flesh, fowl, and sauces."
# 4
xPlusY = []
for x in range(10, 40, 10):
    for y in range(1, 4):
        xPlusY.append(x + y)
print xPlusY
Success #stdin #stdout 0.01s 7728KB
Standard input is empty
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
['hh', 'ht', 'th', 'tt']
['hh', 'ht', 'th', 'tt']
['o', 'a', 'o', 'e', 'a', 'o', 'e', 'a', 'o', 'a', 'e', 'o', 'a', 'i', 'o', 'e', 'i', 'a', 'o', 'i', 'y', 'o', 'i', 'a', 'o', 'i', 'i', 'o', 'a', 'a', 'o', 'e', 'i', 'o', 'a', 'a', 'e', 'y', 'e', 'o', 'i', 'e', 'i', 'o', 'y', 'o', 'a', 'o', 'e', 'i', 'e', 'a', 'e', 'y', 'o', 'o', 'e', 'e', 'a', 'i', 'o', 'i', 'o', 'e', 'e', 'i', 'o', 'a', 'o', 'i', 'o', 'i', 'a', 'i', 'o', 'a', 'e', 'a', 'a', 'o', 'e', 'y', 'o']
[11, 12, 13, 21, 22, 23, 31, 32, 33]