fork download
  1. # Mechanical_MOOC_Exercise_2_10.py
  2. # MIT OCW 6.189 Homework 2 Exercise 2.10
  3. # http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-189-a-gentle-introduction-to-programming-using-python-january-iap-2011/assignments/MIT6_189IAP11_hw2.pdf
  4. # G@R
  5. # July 8, 2013
  6. """
  7. 1. Write a list comprehension that prints a list of the cubes of the numbers 1 through 10.
  8.  
  9. 2. Write a list comprehension that prints out the possible results
  10. of two coin flips (one result would be 'ht'). (Hint - how many results should there be?)
  11.  
  12. 3. Write a function that takes in a string and uses a list comprehension
  13. to return all the vowels in the string.
  14.  
  15. 4. Run this list comprehension in your prompt:
  16. [x+y for x in [10,20,30] for y in [1,2,3]]
  17. Figure out what is going on here, and write a nested for loop
  18. that gives you the same result. Make sure what
  19. is going on makes sense to you!
  20. """
  21. # 1
  22. print [n ** 3 for n in range(1, 11)]
  23. # 2
  24. print ['ht'[i] + 'ht'[j] for i in range(0, 2) for j in range(0,2)]
  25. print [s1 + s2 for s1 in 'ht' for s2 in 'ht']
  26. # 3
  27. def vowelLister(st):
  28. return [v for v in st if v.lower() in 'aeiouy']
  29. # Longest word coined by a major author,
  30. # the longest word ever to appear in literature.
  31. # Source: http://en.wikipedia.org/wiki/Longest_word_in_English
  32. # "name of a dish compounded of all kinds of dainties, fish, flesh, fowl, and sauces."
  33. 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"))
  34. # 4
  35. xPlusY = []
  36. for x in range(10, 40, 10):
  37. for y in range(1, 4):
  38. xPlusY.append(x + y)
  39. print xPlusY
  40.  
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]