Advertisement
jspill

webinar-python-exam-review-2022-06-18

Jun 18th, 2022
1,190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.92 KB | None | 0 0
  1. # Exam Review 2022 June 18
  2.  
  3. # LABS
  4. # Ch 2-14... all Labs!
  5. # Ch 21-31 just ADDITIONAL LABS, but important practice!
  6. # Use Submit Mode!!!
  7.  
  8. # Watch your string input and output
  9. # # 1
  10. # myVar = input().strip() # myVar = input().rstrip()
  11. # # 2
  12. # print("some stuff", end=" ") # if you ever override end
  13. # print() # print(end="\n")
  14.  
  15. # Comp 1: Basic syntax and knowledge: operators, data types, etc
  16. # Comp 2: Control Flow
  17. # Comp 3: Modules and Files
  18.  
  19. # Comp 1: Basic syntax and knowledge: operators, data types, etc
  20. # Data Types
  21. # str
  22. # int
  23. # float
  24. # list
  25. # dict
  26. # tuple # immutable, any series x, y, z -> interpreted as a tuple, return x, y -> return (x,y)
  27. # sets # all unique/no duplicates, no order -> no index, no slicing
  28.  
  29. # operators
  30. # = # assigning a value
  31. # == # asking, "equality operator"... use in conditional
  32. # +
  33. # -
  34. # *
  35. # /
  36. # % # modulo... whole number remainder, "How many whole things are left over?"
  37. # // # floor division... int(x/y) or really like math.floor(x,y)
  38. # <
  39. # >
  40. # <=
  41. # >=
  42. # !=
  43. # += # increment... x += 1 same as x = x + 1
  44. # -=
  45. # ** # raise to power... similar to math.pow()
  46. # # # keywords used like operators
  47. # in # if __ in __
  48. # not # if not __ in __
  49. # and
  50. # or # any one True means whole condition True... limit OR to 2 conditions
  51.  
  52. # Comp 2
  53. # Basic control structures
  54. # IF statements... if, if/else, if/elif, if/elif/else, etc
  55. # LOOPS
  56. # WHILE - an IF that repeats
  57. # FOR - looping over some container, or a known number of times (range())
  58. # for __ in __:
  59. # for item in myList:
  60. # for n in range(0, 5): # [0, 1, 2, 3, 4]
  61. # for i in range(len(myList)): # i for the index, myList[i] for the value
  62. # for k in myDictionary: # value for the key is myDictionary[k]
  63.  
  64. # FUNCTIONS
  65. # defining/writing vs calling
  66. # parameters vs outside or "regular" variables
  67. # parameters vs arguments
  68. # a good function has ONE job, modular
  69. # return vs print()... # do whichever the question
  70. # methods are functions that belong to a type/class
  71.  
  72. # def someFunction(x, y):
  73. #     return x + y
  74. #
  75. # if "__name__" == "__main__":  # am I running from this particular file that I'm writing?
  76. #     myInput = int(input().strip())
  77. #     num = someFunction(myInput, someOtherNum)
  78. #     print(num)
  79.  
  80. # See "tasks" in the last section of Ch 10, 11, 13, 14 for function writing practice
  81. # CodingBat also has good function-based Python questions:
  82. # https://codingbat.com/python
  83.  
  84. # BUILT-IN FUNCTIONS
  85. # print()
  86. # input()
  87. # len()
  88. # sum()
  89. # min()
  90. # max()
  91. # range()
  92. # str()
  93. # list()
  94. # dict()
  95. # tuple()
  96. # set()
  97. # sorted() # compare to list.sort()
  98. # reversed() # compare to list.reverse()
  99. # enumerate()
  100. # round() # cousins math.ceil(), math.floor()
  101. # open() # IO/file methods: .read(), .readlines(), .write()
  102. #
  103. # help()
  104. # dir()
  105. # help(str)
  106. # print(dir(str))
  107. # help(str.isspace)
  108. # for item in dir(str):
  109. #     if not item.startswith("_"):
  110. #         print("{}()".format(item))
  111.  
  112. # STRINGS
  113. # be able to slice like it's 2nd nature... myString[start:stop:step]
  114. # myString = "abc"
  115. # myRevString = myString[::-1]
  116. # print(myRevString)
  117.  
  118. # KNOW YOUR WHITESPACE
  119. # # " " # ... and a lot of other Unicode spaces
  120. # # "\n"
  121. # # "\r"
  122. # # "\b"
  123. # # "\t"
  124. # # "\f"
  125.  
  126. # # STRING METHODS
  127. # myString.format()
  128. # myString.strip() # myString.rstrip()
  129. # myString.split() # returns a list of smaller string
  130. # ",".join(someListOfStrings)
  131. # myString.replace(oldSubStr, newSubStr) # remove... myString.replace(oldSubStr, "")
  132. # myString.find(subStr) # return index of first occurrences, compare myString.index()
  133. # myString.count(subStr) # return int number of occurrences
  134. # # is/Boolean methods: isupper(), islower(), isdigit(), isnumeric(), isspace(), isalphanum()
  135. #
  136. # # LISTS
  137. # # again be able to slice
  138. #
  139. # # LIST METHODS
  140. # # +
  141. # myList.append(item)
  142. # myList.insert(i, item)
  143. # myList.extend(anotherList)
  144. # # -
  145. # myList.pop() # myList.pop(i), pop by index...
  146. # myList.remove(item) # remove by value...
  147. # # other
  148. # myList.count(item)
  149. # myList.sort()
  150. # myList.reverse()
  151. # # not as important
  152. # myList.index() # be careful, could be in there more than once
  153. # myList.copy()
  154. # myList.clear()
  155.  
  156. # DICT
  157. # # # use the key like an index
  158. # # # you can use the dict KEY to get its VALUE
  159. # myDict[key] # retrieve the value for that key
  160. # myDict[key] = new value
  161. # myDict.items() # for k, v in myDict.items()
  162. # myDict.keys()
  163. # myDict.values()
  164.  
  165. # SETS
  166. # mySet.add()
  167. # mySet.remove(item) # by value
  168. # mySet.pop() # removes a random item
  169.  
  170. # MODULES
  171. # # MATH MODULE
  172. # import math
  173. # math.factorial(x)
  174. # math.ceil(x.yz)
  175. # math.floor(x.yz)
  176. # math.pow(x, y) # similar to **
  177. # math.sqrt(x)
  178. # math.fabs() # abs()
  179. # math.e
  180. # math.pi
  181.  
  182. # different import types
  183. # full import: math.factorial()
  184. # partial import:
  185. # from math import factorial
  186. # --> factorial()
  187. # from math import factorial, sqrt
  188. # from math import *
  189. # aliased imports
  190. # import math as m --> m.factorial()
  191.  
  192.  
  193. # # OPENING FILES
  194. #  good practice in Ch 14 Task 4, 7, 8
  195.  
  196. # with open("filename.txt", "r") as f:
  197. #     # grab contents and get out of the open block
  198. #     contents = f.read() # whole file as one big string
  199. #     contents = f.readlines() # a list of line by line strings
  200.  
  201. with open("test.txt", "r") as f:
  202.     contents = f.readlines()
  203. # print(contents)
  204. # for line in contents:
  205. #     line = line.strip()
  206. #     print(line) # print(line, end="\n")
  207.  
  208. with open("mock_data.csv", "r") as f:
  209.     # contents = f.readlines() # ["1,Flossie,Levey...\n"...]
  210.  
  211.     import csv
  212.     contents = list(csv.reader(f)) # csv.reader(f, delimiter="\t")
  213.     # does one more step for us...
  214.     # ['1', 'Flossie', 'Levey', 'flevey0@jimdo.com', '129.134.226.30']
  215.     print(contents)
  216.  
  217. with open("mock_data2.csv", "w") as f2:
  218.     for line in contents:
  219.         if line[3].endswith(".org"): # if line[3][-4] == ".org"
  220.             f2.write("{}\n".format(",".join(line)))
  221.  
  222. # Student Questions
  223. # Seats (or columns) within rows in a room is a good use case for nested loops...
  224. rows = 5
  225. seats = 10 # "columns" within the rows
  226. for row in range(0, rows):
  227.     for seat in range(0, seats):
  228.         print("{}-{}".format(row, seat))
  229.  
  230.  
  231. # The Simon Says game in Ch 6.10 is a good example where
  232. # you might THINK it needs a nested loop... but it doesn't!
  233. '''
  234. "Simon Says" is a memory game where "Simon" outputs a sequence of 10 characters (R, G, B, Y) and the user must repeat the sequence. Create a for loop that compares each character of the two strings. For each matching character, add one point to user_score. Upon a mismatch, end the loop.
  235.  
  236. Sample output with inputs: 'RRGBRYYBGY' 'RRGBBRYBGY'
  237. '''
  238. user_score = 0
  239. simon_pattern = "RRGBRYYBGY" #input()
  240. user_pattern  = "RRGBBRYBGY" # input()
  241.  
  242. ''' Your solution goes here '''
  243. # NOT a nested loop! We don't need to compare all the values, just corresponding ones
  244. for i in range(len(simon_pattern)):
  245.     if simon_pattern[i] == user_pattern[i]:
  246.         user_score += 1
  247.     else:
  248.         break
  249. print('User score:', user_score)
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement