''' Lecture 17 Examples ''' import random def randcard(): ''' returns a random card as a string from a standard deck of 52 cards ''' # Get the value card_value = random.randint(1,13) if (card_value == 1): card_value = "Ace" elif (card_value == 11): card_value = "Jack" elif (card_value == 12): card_value = "Queen" elif (card_value == 13): card_value = "King" # Get the suit card_suit = random.choice(["Spades", "Hearts", "Diamonds", "Clubs"]) # Return card as string return "Card: " + str(card_value) + " of " + card_suit def sallyize(your_string): ''' This function returns your_string with every two letters swapped. e.g. "test string" -> "ettss rtnig" ''' # TODO: Below are two examples of code that would complete this exercise. # There are some parts left out though, so you'll have to uncomment the # solution and solve for the parts marked with TODO comments. # # NOTE: The solutions done here are complicated becuase strings # are immutable! There may be a mutable data structure that we recently # learned about that might be easier to deal with. Of course you'll # have to do some data conversions to get that solution to work. ''' # Example solution #1: # make an initial copy of string new_string = your_string # Swapping characters in a string, if done manually we would do the following: # newString = newString[0:0] + newString[1] + newString[0] + newString[2:] # newString = newString[0:2] + newString[3] + newString[2] + newString[4:] # newString = newString[0:4] + newString[5] + newString[4] + newString[6:] # newString = newString[...] + newString[...] + newString[...] + newString[...] # newString = newString[0:n*2] + newString[(n*2)+1] + newString[n*2] + newString[(n*2+2):] # so we're doing this 'n' times, we need to do a loop! # # but how many times!? if we're swapping 2 letters at a time: # # for i in range(0, len(yourString)//2): # swap 2 letters at a time # newString = newString[0:X] + newString[Y] + newString[X] + newString[Z:] # Where: # X - reprensts the index of the first character being swapped # Y - represents the index of the second character being swapped # Z - represents the index after Y # So: # Y = X+1 # Z = X+2 # X is the first chacter being swapped. The index can be calculated # based on the iteration. X would be: # 0 during the 1st iteration, # 2 the 2nd iteration, # 4 the third iteration, etc. # # Adjusting our X,Y,Z variables to better represent the data: # Replacing X with "swap_index" # Y with "swap_index+1" # and Z with "swap_index+2" # # Hint: Remember that we are swapping different letters with each iteration # of the loop. So the swap_index is based on our iterator # Complete the following code by replaceing the "#TODO" comments with the # proper code: for i in range(0, len(yourString)//2): # swap two letters each iteration swap_index = #TODO # index of first character to swap (based on iterator) c1 = new_string[swap_index] # letter of first character to swap c2 = new_string[swap_index+1] # letter of second character to swap # Build a new string by appending the following: # - string slice from before the swap index # - the two characters in opposite positions # - string slice from after the swapped characters new_string = # TODO: complete the comments above # return the string sallyized return new_string ''' ''' # Alternate solution: # start with an empty string newString = "" # for every index in the string for i in range(0,len(your_string)): #TODO: This code is almost right, but it has two bugs: # 1) It could index past the end of some strings # 2) It could potentially leave off the last character # Fix these bugs if you choose this solution. # if the index is even (so every other index) if (i%2 == 0): # swap it with the following index new_string += your_string[i+1] + your_string[i] # return the string return new_string) ''' # Here is a function that is using a similar technique to swap letter def reverse(string): ''' returns a new copy of string in reverse order ''' # start with the string the same rev_string = string # reverse the string for i in range(0, len(rev_string)//2): # swap 2 letters each time # index values front_letter_index = i # index of letter in front half to swap back_letter_index = len(rev_string)-1-i # index of letter in back half to swap start_slice = front_letter_index+1 # letters inbetween stay the same so end_slice = back_letter_index # figure out where that slices starts and ends # values to swap c1 = rev_string[front_letter_index] c2 = rev_string[back_letter_index] # build a new string with letters swapped concat = "" # start with an empty string and append to it concat += rev_string[:front_letter_index] # keep letters from the front concat += c2 # place letter form back half in front half concat += rev_string[start_slice:end_slice] # keep the middle letters the same concat += c1 # place letter form front half in back half concat += rev_string[back_letter_index+1:] # keep letters from the back rev_string = concat # Return the string reversed return rev_string def main(): ''' Main program driver ''' print("You drew the " + randcard()) your_string = input("Enter a string: ") print("Your string reversed: " + reverse(your_string)) # Sallyize code needs updating before you can run this: #print("Your string sallyized: " + sallyize(your_string)) # Run the program main()