SHARE
TWEET

codehs

a guest Jun 8th, 2017 8,273 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ###6.1.7 fix this tuple###
  2. my_tuple = (0, 1, 2, "hi", 4, 5)
  3.  
  4. my_tuple = (0, 1, 2) + (3,) + (4, 5)
  5.  
  6. print my_tuple
  7.  
  8.  
  9. ###6.1.8 citation###
  10. author_name = ("Martin", "Luther", "King, Jr.")
  11.  
  12. print author_name[2] + ", " + author_name[0] + " " + author_name[1]
  13.  
  14.  
  15. ###6.1.9 coordinate pairs###
  16. import math
  17.  
  18. def distance(point1, point2):
  19.     # find difference between x and y values and square them
  20.     first = point2[0] - point1[0]
  21.     nice = pow(first, 2)
  22.     second = point2[1] - point1[1]
  23.     okay = pow(second, 2)
  24.     # add squares
  25.     result = nice + okay
  26.     # take square root and return answer
  27.     all_done = math.sqrt(result)
  28.     return all_done
  29.  
  30. # This should print 5.0
  31. print distance((1, 1), (4, 5))
  32.  
  33.  
  34. ###6.2.11 how many names?###
  35. my_list = []
  36. names = int(input("How many names do you have? "))
  37. for i in range(names):
  38.     nnn = input("Name: ")
  39.     my_list = my_list + [nnn]
  40.    
  41. print "First name: " + str(my_list[0])
  42. print "Middle name(s): " + str(my_list[1:-1])
  43. print "Last name: " + str(my_list[-1])
  44.  
  45.  
  46. ###6.2.12 max in list###
  47. def max_int_in_list(x):
  48.     max_num = x[0]
  49.     for num in x:
  50.         if max_num < num:
  51.             max_num = num
  52.     return max_num
  53.  
  54.  
  55. print max_int_in_list([77,48,19,17,94,90])
  56.  
  57.  
  58. ###6.2.13 owls###
  59. def check_for_owl():
  60.     my_num = 0
  61.     text = input("Enter some text: ")
  62.     new_list = text.split()
  63.     for word in new_list:
  64.         if "owl" in word.lower():
  65.             my_num = my_num + 1
  66.     print "You said \"owl\" " + str(my_num) + " times."
  67.    
  68. check_for_owl()
  69.  
  70.  
  71. ###6.2.14v exclamation po!nts###
  72. #ask user to enter text and convert each character to item in list
  73. original = input("Enter text: ")
  74. new_list = list(original)
  75.  
  76. #every time there's an i, replace with !
  77. for i, item in enumerate(new_list):
  78.     if item == "i":
  79.         new_list[i] = "!"
  80.  
  81. # make the list back into a string and print
  82. new_string = "".join(new_list)
  83. print new_string
  84.  
  85.  
  86. ###6.2.15 word ladder###
  87. # retrieves index
  88. def get_index():
  89.     while True:
  90.         try:
  91.             index = int(input("Enter an index. "))
  92.             if index >= 0 and index <= len(original):
  93.                 return index
  94.         except ValueError:
  95.             print "Your index must be an integer."
  96.  
  97. # retrieves letter        
  98. def get_letter():
  99.     while True:
  100.         letter = input("Enter a letter. ")
  101.         if len(letter) > 1:
  102.             print "Please enter only one letter."
  103.         elif not letter.islower():
  104.             print "Please enter a lowercase letter."
  105.         else:
  106.             return letter
  107.  
  108. # user enters word & it is converted to list
  109. original = input("Enter a word: ")
  110. new_list = list(original)
  111.  
  112. while True:
  113.     index = get_index()
  114.     letter = get_letter()
  115.     # replace letter and print new word
  116.     new_list[index] = letter
  117.     print "".join(new_list)
  118.  
  119.  
  120. ###6.2.16 owls, part 2###
  121. def check_for_owl():
  122.     my_num = 0
  123.     indices_list = []
  124.     # user input
  125.     text = input("Enter some text: ")
  126.     # convert text to list
  127.     new_list = text.split()
  128.     for i, word in enumerate(new_list):
  129.         # check for "owl" at each index and record index
  130.         if "owl" in word.lower():
  131.             my_num = my_num + 1
  132.             indices_list = indices_list + [i]
  133.     # regurgitate
  134.     print "You said \"owl\" " + str(my_num) + " times."
  135.     print "You said it at indices " + str(indices_list)
  136.    
  137. check_for_owl()
  138.  
  139.  
  140. ###6.3.8 five numbers###
  141. my_list = []
  142. summ = 0
  143. for i in range(5):
  144.     num = int(input("Number: "))
  145.     my_list.append(num)
  146.     print my_list
  147.     summ = summ + my_list[i]
  148. print "Sum: " + str(summ)
  149.  
  150.  
  151. ###6.3.9 librarian###
  152. # establish list
  153. my_list = []
  154. # gather names
  155. for i in range(5):
  156.     name = input("Name: ")
  157.     my_list.extend([name])
  158. #sort & print    
  159. my_list.sort()
  160. print my_list
  161.  
  162.  
  163. ###6.3.10 fruits and vegetables###
  164. my_list = ["broccoli", "apple", "zucchini", "rambutan", "grapefruit"]
  165.  
  166.  
  167. my_list.remove("grapefruit")
  168. my_list.sort()
  169. my_list.reverse()
  170.  
  171. print my_list
  172.  
  173. ###6.3.11 librarian, part 2###
  174. collect = []
  175. list_form = []
  176.  
  177. # ask for names and collect in a list
  178. for i in range(5):
  179.     name = input("Name: ")
  180.     list_form = name.split()
  181.     # last names in a list
  182.     last_name = list_form[-1]
  183.     collect.extend([last_name])
  184. # print last names
  185. print collect
  186.  
  187.  
  188. ###6.4.6 checkerboard, v1###
  189. # Pass this function a list of lists, and it will
  190. # print it such that it looks like the grids in
  191. # the exercise instructions.
  192. def print_board(board):
  193.     for i in range(len(board)):
  194.        
  195.         # This line uses some Python you haven't
  196.         # learned yet. You'll learn about this
  197.         # part in a future lesson:
  198.         #
  199.         # [str(x) for x in board[i]]
  200.         print " ".join([str(x) for x in board[i]])
  201.  
  202. # Your code here...
  203. board = []
  204. for i in range(3):
  205.     board.append([1] * 8)
  206. for i in range(2):
  207.     board.append([0] * 8)
  208. for i in range(3):
  209.     board.append([1] *8)
  210.    
  211. print_board(board)
  212.  
  213.  
  214. ###6.4.7 checkerboard, v2###
  215. # Pass this function a list of lists, and it will
  216. # print it such that it looks like the grids in
  217. # the exercise instructions.
  218. def print_board(board):
  219.     for i in range(len(board)):
  220.        
  221.         # This line uses some Python you haven't
  222.         # learned yet. You'll learn about this
  223.         # part in the next lesson:
  224.         #
  225.         # [str(x) for x in board[i]]
  226.         print " ".join([str(x) for x in board[i]])
  227.  
  228. # Your code here...
  229.  
  230. board = []
  231. board.append(["1 0 1 0 1 0 1 0"])
  232. board.append(["0 1 0 1 0 1 0 1"])
  233. board.append(["1 0 1 0 1 0 1 0"])
  234. board.append(["0 1 0 1 0 1 0 1"])
  235. board.append(["1 0 1 0 1 0 1 0"])
  236. board.append(["0 1 0 1 0 1 0 1"])
  237. board.append(["1 0 1 0 1 0 1 0"])
  238. board.append(["0 1 0 1 0 1 0 1"])
  239.  
  240. print_board(board)
  241.  
  242.  
  243. ###6.4.8 checkerboard, v3###
  244. # Pass this function a list of lists, and it will
  245. # print it such that it looks like the grids in
  246. # the exercise instructions.
  247. def print_board(board):
  248.     for i in range(len(board)):
  249.        
  250.         # This line uses some Python you haven't
  251.         # learned yet. You'll learn about this
  252.         # part in the next lesson:
  253.         #
  254.         # [str(x) for x in board[i]]
  255.         print " ".join([str(x) for x in board[i]])
  256.  
  257. # Your code here...
  258.  
  259. board = []
  260. board.append([0, 1] * 4)
  261. board.append([1, 0] * 4)
  262. board.append([0, 1] * 4)
  263. board.append([0] * 8)
  264. board.append([0] * 8)
  265. board.append([1, 0] * 4)
  266. board.append([0, 1] * 4)
  267. board.append([1, 0] * 4)
  268.  
  269.  
  270. print_board(board)
  271.  
  272.  
  273. ###6.4.9 tic tac toe###
  274. # get_valid_index
  275. # -----
  276. # Get row or column from user
  277. def get_valid_index(prompt):
  278.     while True:
  279.         try:
  280.             index = int(input(prompt))
  281.             if index >= 0 and index <= 2:
  282.                 return index
  283.             print "Must be 0 - 2 inclusive!"
  284.         except ValueError:
  285.             print "Must be an integer!"
  286.  
  287. # game_is_over
  288. # -----
  289. # Return True if the game is over and False
  290. # otherwise. Print a message indicating who
  291. # won or whether there was a tie.
  292. def game_is_over(board):
  293.     for i in range(3):
  294.         # Check horizontal
  295.         if board[i][0] == board[i][1] == board[i][2] \
  296.             and board[i][0] != " ":
  297.             print board[i][0] + " wins!"
  298.             return True
  299.        
  300.         # Check vertical
  301.         if board[0][i] == board[1][i] == board[2][i] \
  302.             and board[0][i] != " ":
  303.             print board[0][i] + " wins!"
  304.             return True
  305.        
  306.     # Check diagonals
  307.     if board[0][0] == board[1][1] == board[2][2] \
  308.         and board[0][0] != " ":
  309.         print board[0][0] + " wins!"
  310.         return True
  311.    
  312.     if board[2][0] == board[1][1] == board[0][2] \
  313.         and board[2][0] != " ":
  314.         print board[2][0] + " wins!"
  315.         return True
  316.    
  317.     # Check tie
  318.     if " " not in board[0] and " " not in board[1] \
  319.         and " " not in board[2]:
  320.         print "Tie game!"
  321.         return True
  322.    
  323.     # Not over yet!
  324.     return False
  325.    
  326. # print_board
  327. # -----
  328. # Print the board.
  329. def print_board(board):
  330.     for i in range(len(board)):
  331.         print ([str(x) for x in board[i]])
  332.  
  333. # Set up board
  334. board = []
  335. # TODO: Set up the board as a 3x3 grid of spaces here...
  336. for i in range(3):
  337.     board.append([' '] * 3)
  338.  
  339. # x goes first
  340. turn = "x"
  341.  
  342. # Play tic tac toe
  343. while True:
  344.     print_board(board)
  345.     turn = "x"
  346.     print "It's " + turn + "'s turn."
  347.     row = get_valid_index("Row: ")
  348.     col = get_valid_index("Col: ")
  349.    
  350.     if board[row][col] == ' ':
  351.         board[row][col] = turn
  352.     game_is_over(board)
  353.     if game_is_over(board) == True:
  354.         break
  355.    
  356.     print_board(board)
  357.     turn = "o"
  358.     print "It's " + turn + "'s turn."
  359.     row = get_valid_index("Row: ")
  360.     col = get_valid_index("Col: ")
  361.    
  362.     if board[row][col] == ' ':
  363.         board[row][col] = turn
  364.     game_is_over(board)
  365.     if game_is_over(board) == True:
  366.         break
  367.    
  368. print_board(board)
  369.  
  370.  
  371. ###6.5.7 last names###
  372. names = [
  373.     "Maya Angelou",
  374.     "Chimamanda Ngozi Adichie",
  375.     "Tobias Wolff",
  376.     "Sherman Alexie",
  377.     "Aziz Ansari"
  378. ]
  379.  
  380. new_list = [names[x].split() for x in range(len(names))]
  381. for i in range(len(new_list)):
  382.     new_list[i] = new_list[i][-1]
  383. print new_list
  384.  
  385.  
  386. ###6.5.8 strings to integers###
  387. # Write your function here...
  388.  
  389.  
  390. list_of_strings = ["a", "2", "7", "zebra"]
  391.  
  392. # Your code here...
  393.  
  394.  
  395. def safe_int(x):
  396.     try:
  397.         return int(x)
  398.     except ValueError:
  399.         return 0
  400.    
  401. print [safe_int(x) for x in list_of_strings]
  402.  
  403.  
  404. ###6.6.7 coordinate pair###
  405. first = int(input("Enter a number: "))
  406. second = int(input("Enter another number: "))
  407.  
  408. my_tuple = first, second
  409. print my_tuple
  410.  
  411.  
  412. ###6.6.8 slopes###
  413. my_list = []
  414. for i in range(5):
  415.     first = int(input("First coordinate: "))
  416.     second = int(input("Second coordinate: "))
  417.     new_tuple = (first, second)
  418.     my_list = my_list + [new_tuple]
  419.    
  420. print my_list
  421.  
  422. answer = (my_list[1][1] - my_list[0][1]) / (my_list[1][0] - my_list[0][0])
  423. print "Slope between " + str(my_list[0]) + " and " + str(my_list[1]) + ": " + str(answer)
  424.  
  425. answer = (my_list[2][1] - my_list[1][1]) / (my_list[2][0] - my_list[1][0])
  426. print "Slope between " + str(my_list[1]) + " and " + str(my_list[2]) + ": " + str(answer)
  427.  
  428. answer = (my_list[3][1] - my_list[2][1]) / (my_list[3][0] - my_list[2][0])
  429. print "Slope between " + str(my_list[2]) + " and " + str(my_list[3]) + ": " + str(answer)
  430.  
  431. answer = (my_list[4][1] - my_list[3][1]) / (my_list[4][0] - my_list[3][0])
  432. print "Slope between " + str(my_list[3]) + " and " + str(my_list[4]) + ": " + str(answer)
  433.  
  434.  
  435. ###6.7.5 phone book###
  436. names_and_numbers = {}
  437. names_and_numbers["John"] = 5551021
  438. names_and_numbers["Sarah"] = 5554689
  439.  
  440. entered = input("Enter a name: ")
  441. if entered == "":
  442.     exit
  443. elif entered in names_and_numbers:
  444.     print names_and_numbers[entered]
  445. else:
  446.     number = input("Enter a phone number: ")
  447.     names_and_numbers[entered] = number
  448.  
  449.  
  450. ###6.7.6 word counts###
  451. words = input("Enter some text: ")
  452. new_list = words.split()
  453. my_dict = {}
  454. for word in new_list:
  455.     if word not in my_dict:
  456.         my_dict[word] = 1
  457.     else:
  458.         my_dict[word] = my_dict[word] + 1
  459.        
  460. print my_dict
  461.  
  462.  
  463. ###6.8.6 swapping, part 2###
  464. def swap_lists(first, second):
  465.     if len(first) != len(second):
  466.         print "Lengths must be equal!"
  467.         return
  468.     else:
  469.         for i in range(len(first)):
  470.             first[i], second[i] = second[i], first[i]
  471.            
  472. list_one = [1, 6, 3]
  473. list_two = [4, 5, 6]
  474.  
  475. print "Before swap"
  476. print "list_one: " + str(list_one)
  477. print "list_two: " + str(list_two)
  478.  
  479. swap_lists(list_one, list_two)
  480.  
  481. print "After swap"
  482. print "list_one: " + str(list_one)
  483. print "list_two: " + str(list_two)
  484.  
  485.  
  486. ###6.8.7 word counts, part 2###
  487. words = input("Enter some text: ")
  488. my_list = words.split()
  489. my_dict = {}
  490.  
  491. def update_counts():
  492.     for word in my_list:
  493.         if word not in my_dict:
  494.             my_dict[word] = 1
  495.         else:
  496.             my_dict[word] = my_dict[word] + 1
  497.        
  498.     print my_dict
  499.  
  500. update_counts()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top