# Siri.py # The CS1110 profs, spring 2016 (cs-1110profs-L@cornell.edu) # Feb 2016 """ Assignment 2, Siri problems""" def SiriSez(x): """ Returns the associated intersection instruction string. PreC: x is an intersection string. """ if x[0]==x[1]: M = 'Continue Straight' elif x=='ES' or x == 'SW' or x == 'WN' or x=='NE': M = 'Turn Right' elif x=='EN' or x == 'NW' or x == 'WS' or x=='SE': M = 'Turn Left' elif x=='EW' or x == 'WE' or x == 'NS' or x=='SN': M = 'Make a U-Turn' else: M = 'Pull Over' return M # Helper function for alternate implementation of SiriSez def code(d): """ d is a direction string 'N', 'E, 'S', 'W'. code(d) returns 0, 1, 2, or 3, respectively, to allow for "direction arithmetic". Returns None otherwise.""" if d == 'N': return 0 elif d == 'E': return 1 elif d == 'S': return 2 elif d == 'W': return 3 def SiriSez2(x): """Returns the associated intersection instruction string. PreC: x is an intersection string.""" # Alternate implementation # convert directions into numbers in {0,1,2,3} dir1=code(x[0]) dir2=code(x[1]) if dir1 == dir2: return 'Continue Straight' elif dir2 == ((dir1 + 1) % 4): # use modular arithmetic return 'Turn Right' elif dir2 == ((dir1 + 2) % 4): return 'Make a U-Turn' else: return 'Turn Left' def TripAdvisor(Route): print print Route if Route[0] == 'N': print 'Start Driving North' elif Route[0] == 'W': print 'Start Driving West' elif Route[0] == 'S': print 'Start Driving South' else: print 'Start Driving East' print SiriSez(Route[0:2]) print SiriSez(Route[1:3]) print SiriSez(Route[2:4]) print SiriSez(Route[3:5]) print SiriSez(Route[4:6]) def Fix(Route): n = len(Route) if n >= 6: return Route[:6] if n < 6: return Route + (6-n)*'W' if __name__ == '__main__': Route = raw_input('Enter a length-6 route string: ') #Route = Fix(Route) TripAdvisor(Route)