codehs 1.12.6 answers ###6.1.7 fix this tuple### my_tuple = (0, 1, 2, "hi", 4, 5) my_tuple = (0, 1, 2) + (3,) + (4, 5) print my_tuple ###6.1.8 citation### author_name = ("Martin", "Luther", "King, Jr.") codehs 1.12.6 answers PasteShr codehs 1.12.6 answers print author_name[2] + ", " + author_name[0] + " " + author_name[1] ###6.1.9 coordinate pairs### import math def distance(point1, point2): # find difference between x and y values and square them first = point2[0] - point1[0] nice = pow(first, 2) codehs 1.12.6 answers How to get it for free? codehs 1.12.6 answers second = point2[1] - point1[1] okay = pow(second, 2) # add squares result = nice + okay # take square root and return answer all_done = math.sqrt(result) return all_done # This should print 5.0 print distance((1, 1), (4, 5)) codehs 1.12.6 answers How to dowload it? codehs 1.12.6 answers ###6.2.11 how many names?### my_list = [] names = int(input("How many names do you have? ")) for i in range(names): nnn = input("Name: ") my_list = my_list + [nnn] print "First name: " + str(my_list[0]) codehs 1.12.6 answers How to get it for free? codehs 1.12.6 answers print "Middle name(s): " + str(my_list[1:-1]) print "Last name: " + str(my_list[-1]) ###6.2.12 max in list### def max_int_in_list(x): max_num = x[0] for num in x: if max_num < num: max_num = num codehs 1.12.6 answers How to use it? codehs 1.12.6 answers return max_num print max_int_in_list([77,48,19,17,94,90]) ###6.2.13 owls### def check_for_owl(): my_num = 0 text = input("Enter some text: ") codehs 1.12.6 answers How to get it? codehs 1.12.6 answers new_list = text.split() for word in new_list: if "owl" in word.lower(): my_num = my_num + 1 print "You said \"owl\" " + str(my_num) + " times." check_for_owl() ###6.2.14v exclamation po!nts### codehs 1.12.6 answers How to get it for free? codehs 1.12.6 answers #ask user to enter text and convert each character to item in list original = input("Enter text: ") new_list = list(original) #every time there's an i, replace with ! for i, item in enumerate(new_list): if item == "i": new_list[i] = "!" # make the list back into a string and print codehs 1.12.6 answers How to dowload it? codehs 1.12.6 answers new_string = "".join(new_list) print new_string ###6.2.15 word ladder### # retrieves index def get_index(): while True: try: index = int(input("Enter an index. ")) codehs 1.12.6 answers How to get it for free? codehs 1.12.6 answers if index >= 0 and index <= len(original): return index except ValueError: print "Your index must be an integer." # retrieves letter def get_letter(): while True: letter = input("Enter a letter. ") if len(letter) > 1: codehs 1.12.6 answers How to use it? codehs 1.12.6 answers print "Please enter only one letter." elif not letter.islower(): print "Please enter a lowercase letter." else: return letter # user enters word & it is converted to list original = input("Enter a word: ") new_list = list(original) codehs 1.12.6 answers How to dowload it? codehs 1.12.6 answers while True: index = get_index() letter = get_letter() # replace letter and print new word new_list[index] = letter print "".join(new_list) ###6.2.16 owls, part 2### def check_for_owl(): codehs 1.12.6 answers How to dowload it? codehs 1.12.6 answers my_num = 0 indices_list = [] # user input text = input("Enter some text: ") # convert text to list new_list = text.split() for i, word in enumerate(new_list): # check for "owl" at each index and record index if "owl" in word.lower(): my_num = my_num + 1 codehs 1.12.6 answers How to use it? codehs 1.12.6 answers indices_list = indices_list + [i] # regurgitate print "You said \"owl\" " + str(my_num) + " times." print "You said it at indices " + str(indices_list) check_for_owl() ###6.3.8 five numbers### my_list = [] codehs 1.12.6 answers How to use it? codehs 1.12.6 answers summ = 0 for i in range(5): num = int(input("Number: ")) my_list.append(num) print my_list summ = summ + my_list[i] print "Sum: " + str(summ) ###6.3.9 librarian### codehs 1.12.6 answers How to use it? codehs 1.12.6 answers # establish list my_list = [] # gather names for i in range(5): name = input("Name: ") my_list.extend([name]) #sort & print my_list.sort() print my_list codehs 1.12.6 answers How to dowload it? codehs 1.12.6 answers ###6.3.10 fruits and vegetables### my_list = ["broccoli", "apple", "zucchini", "rambutan", "grapefruit"] my_list.remove("grapefruit") my_list.sort() my_list.reverse() print my_list codehs 1.12.6 answers PasteShr codehs 1.12.6 answers ###6.3.11 librarian, part 2### collect = [] list_form = [] # ask for names and collect in a list for i in range(5): name = input("Name: ") list_form = name.split() # last names in a list codehs 1.12.6 answers How to use it? codehs 1.12.6 answers last_name = list_form[-1] collect.extend([last_name]) # print last names print collect ###6.4.6 checkerboard, v1### # Pass this function a list of lists, and it will # print it such that it looks like the grids in # the exercise instructions. codehs 1.12.6 answers How to get it for free? codehs 1.12.6 answers def print_board(board): for i in range(len(board)): # This line uses some Python you haven't # learned yet. You'll learn about this # part in a future lesson: # # [str(x) for x in board[i]] print " ".join([str(x) for x in board[i]]) codehs 1.12.6 answers PasteShr codehs 1.12.6 answers # Your code here... board = [] for i in range(3): board.append([1] * 8) for i in range(2): board.append([0] * 8) for i in range(3): board.append([1] *8) print_board(board) codehs 1.12.6 answers How to get it for free? codehs 1.12.6 answers ###6.4.7 checkerboard, v2### # Pass this function a list of lists, and it will # print it such that it looks like the grids in # the exercise instructions. def print_board(board): for i in range(len(board)): # This line uses some Python you haven't codehs 1.12.6 answers How to get it? codehs 1.12.6 answers # learned yet. You'll learn about this # part in the next lesson: # # [str(x) for x in board[i]] print " ".join([str(x) for x in board[i]]) # Your code here... board = [] board.append(["1 0 1 0 1 0 1 0"]) codehs 1.12.6 answers How to use it? codehs 1.12.6 answers board.append(["0 1 0 1 0 1 0 1"]) board.append(["1 0 1 0 1 0 1 0"]) board.append(["0 1 0 1 0 1 0 1"]) board.append(["1 0 1 0 1 0 1 0"]) board.append(["0 1 0 1 0 1 0 1"]) board.append(["1 0 1 0 1 0 1 0"]) board.append(["0 1 0 1 0 1 0 1"]) print_board(board) codehs 1.12.6 answers How to get it for free? codehs 1.12.6 answers ###6.4.8 checkerboard, v3### # Pass this function a list of lists, and it will # print it such that it looks like the grids in # the exercise instructions. def print_board(board): for i in range(len(board)): # This line uses some Python you haven't # learned yet. You'll learn about this codehs 1.12.6 answers How to get it for free? codehs 1.12.6 answers # part in the next lesson: # # [str(x) for x in board[i]] print " ".join([str(x) for x in board[i]]) # Your code here... board = [] board.append([0, 1] * 4) board.append([1, 0] * 4) codehs 1.12.6 answers How to dowload it? codehs 1.12.6 answers board.append([0, 1] * 4) board.append([0] * 8) board.append([0] * 8) board.append([1, 0] * 4) board.append([0, 1] * 4) board.append([1, 0] * 4) print_board(board) codehs 1.12.6 answers How to get it? codehs 1.12.6 answers ###6.4.9 tic tac toe### # get_valid_index # ----- # Get row or column from user def get_valid_index(prompt): while True: try: index = int(input(prompt)) if index >= 0 and index <= 2: codehs 1.12.6 answers How to get it for free? codehs 1.12.6 answers return index print "Must be 0 - 2 inclusive!" except ValueError: print "Must be an integer!" # game_is_over # ----- # Return True if the game is over and False # otherwise. Print a message indicating who # won or whether there was a tie. codehs 1.12.6 answers How to use it? codehs 1.12.6 answers def game_is_over(board): for i in range(3): # Check horizontal if board[i][0] == board[i][1] == board[i][2] \ and board[i][0] != " ": print board[i][0] + " wins!" return True # Check vertical if board[0][i] == board[1][i] == board[2][i] \ codehs 1.12.6 answers How to dowload it? codehs 1.12.6 answers and board[0][i] != " ": print board[0][i] + " wins!" return True # Check diagonals if board[0][0] == board[1][1] == board[2][2] \ and board[0][0] != " ": print board[0][0] + " wins!" return True codehs 1.12.6 answers PasteShr codehs 1.12.6 answers if board[2][0] == board[1][1] == board[0][2] \ and board[2][0] != " ": print board[2][0] + " wins!" return True # Check tie if " " not in board[0] and " " not in board[1] \ and " " not in board[2]: print "Tie game!" return True codehs 1.12.6 answers PasteShr codehs 1.12.6 answers # Not over yet! return False # print_board # ----- # Print the board. def print_board(board): for i in range(len(board)): print ([str(x) for x in board[i]]) codehs 1.12.6 answers PasteShr codehs 1.12.6 answers # Set up board board = [] # TODO: Set up the board as a 3x3 grid of spaces here... for i in range(3): board.append([' '] * 3) # x goes first turn = "x" codehs 1.12.6 answers How to get it? codehs 1.12.6 answers # Play tic tac toe while True: print_board(board) turn = "x" print "It's " + turn + "'s turn." row = get_valid_index("Row: ") col = get_valid_index("Col: ") if board[row][col] == ' ': board[row][col] = turn codehs 1.12.6 answers How to use it? codehs 1.12.6 answers game_is_over(board) if game_is_over(board) == True: break print_board(board) turn = "o" print "It's " + turn + "'s turn." row = get_valid_index("Row: ") col = get_valid_index("Col: ") codehs 1.12.6 answers How to get it for free? codehs 1.12.6 answers if board[row][col] == ' ': board[row][col] = turn game_is_over(board) if game_is_over(board) == True: break print_board(board) ###6.5.7 last names### codehs 1.12.6 answers How to get it? codehs 1.12.6 answers names = [ "Maya Angelou", "Chimamanda Ngozi Adichie", "Tobias Wolff", "Sherman Alexie", "Aziz Ansari" ] new_list = [names[x].split() for x in range(len(names))] for i in range(len(new_list)): codehs 1.12.6 answers How to use it? codehs 1.12.6 answers new_list[i] = new_list[i][-1] print new_list ###6.5.8 strings to integers### # Write your function here... list_of_strings = ["a", "2", "7", "zebra"] codehs 1.12.6 answers How to dowload it? codehs 1.12.6 answers # Your code here... def safe_int(x): try: return int(x) except ValueError: return 0 print [safe_int(x) for x in list_of_strings] codehs 1.12.6 answers How to dowload it? codehs 1.12.6 answers ###6.6.7 coordinate pair### first = int(input("Enter a number: ")) second = int(input("Enter another number: ")) my_tuple = first, second print my_tuple codehs 1.12.6 answers PasteShr codehs 1.12.6 answers ###6.6.8 slopes### my_list = [] for i in range(5): first = int(input("First coordinate: ")) second = int(input("Second coordinate: ")) new_tuple = (first, second) my_list = my_list + [new_tuple] print my_list codehs 1.12.6 answers How to dowload it? codehs 1.12.6 answers answer = (my_list[1][1] - my_list[0][1]) / (my_list[1][0] - my_list[0][0]) print "Slope between " + str(my_list[0]) + " and " + str(my_list[1]) + ": " + str(answer) answer = (my_list[2][1] - my_list[1][1]) / (my_list[2][0] - my_list[1][0]) print "Slope between " + str(my_list[1]) + " and " + str(my_list[2]) + ": " + str(answer) answer = (my_list[3][1] - my_list[2][1]) / (my_list[3][0] - my_list[2][0]) print "Slope between " + str(my_list[2]) + " and " + str(my_list[3]) + ": " + str(answer) answer = (my_list[4][1] - my_list[3][1]) / (my_list[4][0] - my_list[3][0]) codehs 1.12.6 answers How to get it? codehs 1.12.6 answers print "Slope between " + str(my_list[3]) + " and " + str(my_list[4]) + ": " + str(answer) ###6.7.5 phone book### names_and_numbers = {} names_and_numbers["John"] = 5551021 names_and_numbers["Sarah"] = 5554689 entered = input("Enter a name: ") if entered == "": codehs 1.12.6 answers How to get it for free? codehs 1.12.6 answers exit elif entered in names_and_numbers: print names_and_numbers[entered] else: number = input("Enter a phone number: ") names_and_numbers[entered] = number ###6.7.6 word counts### words = input("Enter some text: ") codehs 1.12.6 answers PasteShr codehs 1.12.6 answers new_list = words.split() my_dict = {} for word in new_list: if word not in my_dict: my_dict[word] = 1 else: my_dict[word] = my_dict[word] + 1 print my_dict codehs 1.12.6 answers How to use it? codehs 1.12.6 answers ###6.8.6 swapping, part 2### def swap_lists(first, second): if len(first) != len(second): print "Lengths must be equal!" return else: for i in range(len(first)): first[i], second[i] = second[i], first[i] codehs 1.12.6 answers How to get it for free? codehs 1.12.6 answers list_one = [1, 6, 3] list_two = [4, 5, 6] print "Before swap" print "list_one: " + str(list_one) print "list_two: " + str(list_two) swap_lists(list_one, list_two) print "After swap" codehs 1.12.6 answers How to use it? codehs 1.12.6 answers print "list_one: " + str(list_one) print "list_two: " + str(list_two) ###6.8.7 word counts, part 2### words = input("Enter some text: ") my_list = words.split() my_dict = {} def update_counts(): codehs 1.12.6 answers How to get it? codehs 1.12.6 answers for word in my_list: if word not in my_dict: my_dict[word] = 1 else: my_dict[word] = my_dict[word] + 1 print my_dict update_counts() codehs 1.12.6 answers