Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.06 KB | None | 0 0
  1. #(1) Write Python expressions that correspond to each of the following statements:
  2. #The sum of the five even integers from 2 to 10.
  3. startingNumber = 2
  4. endingNumber = 10
  5. sum = startingNumber
  6.  
  7. nextEven = 4
  8. while nextEven <= 10:
  9. sum = sum + nextEven
  10. nextEven = nextEven + 2
  11. print(sum)
  12.  
  13. #The average of this group of test scores: 75%, 83.5%, 61%, 43%
  14. scores = [75.00,83.5,61.00,43.00]
  15. sumScores = 0
  16. for i in range(len(scores)):
  17. sumScores = sumScores + scores[i]
  18. average = sumScores / len(scores)
  19. print(average)
  20.  
  21. #2 to the 10th power
  22. base = 2
  23. power = 10
  24. result = 0
  25. for i in range(power):
  26. if i == 0:
  27. result = base
  28. else:
  29. result = result * base
  30. print(base, 'to the power of ', power, ' will equal ', result)
  31.  
  32. #A moving anteater has a mass of 50 kg and a velocity (speed) of 15 meters per second.
  33. #Compute its kinetic energy using this formula: 1/2 times the mass times the velocity squared.
  34. mass = 50.0
  35. velocity = 15.0
  36. kineticEnergy = 0.0
  37.  
  38. print('The Kinetic Energy of a Moving Anteater is: ',(mass/2) * (velocity**2))
  39.  
  40.  
  41. #(2) We're designing a game where players can create castle defenses against invaders. Each side of the castle consists of a sequence of wall parts and cannons: We represent a six-segment side with no cannons as 'wwwwww'; a five-segment side with one cannon in the middle would be 'wwcww'.
  42. #Define these two variables:
  43.  
  44. wall = 'w'
  45. cannon = 'c'
  46. #Using the variables wall and cannon, the string operators + and *, and parentheses, write string expressions that evaluate to:
  47. def wallNoCannon():
  48. return wall*6
  49. def wallWithCannon():
  50. return (wall*2+cannon+wall*2)
  51. #"wc"
  52. print(wall+cannon)
  53. #"wcw"
  54. print(wall+cannon+wall)
  55. #"wwwcwww"
  56. print(wall*3+cannon+wall*3)
  57. #"wccwccwccwcc"
  58. print((wall+cannon*2)*4)
  59. #"wwwcwwwcwwwcwwwcw"
  60. print((wall*3+cannon)*4+wall)
  61. #"wwwwcwwwwcwwwwcwwwwcwwww"
  62. print((wall*4+cannon)*3+wall*4)
  63.  
  64. #(3) Scores on a quiz range from 0 to 5. Suppose we have the following assignment
  65. #statement (which represents the quiz scores of all the students in a class):
  66.  
  67. #Using the variable test_scores and the indexing operators, write four
  68. #separate expressions whose values are each of the following: quiz score
  69. #for the 1st student, the 5th student, the 10th student, and the 16th student.
  70. #Remember zero-based indexing.
  71.  
  72. test_scores = "4325220523455023"
  73.  
  74. print(test_scores[3])
  75. print(test_scores[4])
  76. print(test_scores[9])
  77. print(test_scores[15])
  78.  
  79. #(4) Suppose we have the following assignment statement:
  80.  
  81. s = "anteater"
  82. #For each of the following, write a boolean expression that represents the
  83. #English statement:
  84.  
  85. #The first character of string s is "a"
  86. print(s[0] == 'a')
  87. #The last character of string s is "r"
  88. print(s[len(s)-1] == 'r')
  89. #The fourth character of string s is "x"
  90. print(s[3] == 'x')
  91. #The first three characters of string s match the string "zot"
  92. print(s[0:2] == 'zot')
  93.  
  94. #(5) Write Python assignment statements that correspond to each of the following:
  95.  
  96. #Assign 3.14159 to variable pi
  97. pi = 3.14159
  98. print(pi)
  99. #Assign the values "Toyota", "Camry", and 2014 to the variables make, model,
  100. #and year
  101. make, model, year = 'Toyota','Camry','2014'
  102. print(make,model,year)
  103. #Assign a list containing strings "Computer Science", "Informatics", and
  104. #"Computer Game Science" to the variable ICS_majors
  105. ICS_majors= ['Computer Science','Informatics','Computer Game Science']
  106. #Assign the variable a to be the average of the odd numbers from 3 to 9.
  107. a = 3
  108. numbers = []
  109. average = 0
  110. sumNumbers = 0
  111. while a <= 9:
  112. if a == 3:
  113. numbers.append(a)
  114. average = average + 1
  115. if(a + 2 <= 9):
  116. average = average + 1
  117. a = a + 2
  118. numbers.append(a)
  119. else:
  120. break
  121. for i in range(len(numbers)):
  122. sumNumbers = numbers[i] + sumNumbers
  123. a = float(sumNumbers/average)
  124. print(a)
  125.  
  126. #(6) Write boolean expressions corresponding to each of the following statements:
  127.  
  128. #20 plus 35 is greater than 2 to the power of 4
  129. print((20+35)>(2**4))
  130. #The string "hello" is not equivalent to the string "goodbye"
  131. print('hello' == 'goodbye')
  132. #The remainder when 10 is divided by 3 is less or equal to 1.
  133. print((10%3) <= 1)
  134. #The list ["apple", "orange", "banana", "mango"] contains 5 elements.
  135. fruits = ["apple", "orange", "banana", "mango"]
  136. print(len(fruits)==5)
  137. #The number 63 is an even number.
  138. print((63%2)==0)
  139.  
  140. #(7) Start with the following assignment statement:
  141. s = "abcdefghijklmnopqrstuvwxyz"
  142.  
  143. #Using only string concatenation and the indexing operator on the string s,
  144. #write Python expressions that result in the following:
  145.  
  146. #"dog"
  147. print(s[s.index('d')]+s[s.index('o')]+s[s.index('g')])
  148. #"tv"
  149. print(s[s.index('t')]+s[s.index('v')])
  150. #"ics"
  151. print(s[s.index('i')]+s[s.index('c')]+s[s.index('s')])
  152. #"uci"
  153. print(s[s.index('u')]+s[s.index('c')]+s[s.index('i')])
  154. #function version of previous problem
  155. def makeAWord(wordString):
  156. s = "abcdefghijklmnopqrstuvwxyz"
  157. returnWord = ''
  158. for i in range(len(wordString)):
  159. returnWord = returnWord + s[s.index(wordString[i])]
  160. print(returnWord)
  161.  
  162. makeAWord('wowthisisreallycoolhellowow')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement