fork download
# Mechanical_MOOC_Exercise_2_10.py
# MIT OCW 6.189 Homework 2 Exercise 2.10
# http://o...content-available-to-author-only...t.edu/courses/electrical-engineering-and-computer-science/6-189-a-gentle-introduction-to-programming-using-python-january-iap-2011/assignments/MIT6_189IAP11_hw2.pdf
# 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: http://e...content-available-to-author-only...a.org/wiki/Longest_word_in_English
# "name of a dish compounded of all kinds of dainties, fish, flesh, fowl, and sauces."
print(vowelLister("Lopado-temacho-selacho-galeo-kranio-leipsano-drim-hypo-trimmato-silphio-parao-melito-katakechy-meno-kichl-epi-kossypho-phatto-perister-alektryon-opte-kephallio-kigklo-peleio-lagoio-siraio-baphe-tragano-pterygon"))
# 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
stdin
Standard input is empty
stdout
[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]