• + 93 comments

    Here is how I broke this down:

    1. We need to read the first line of input to get it out of the way (it can be used to write a parser for the next line, but it isn't useful)
    n = raw_input()
    
    1. Read in the second line -- this is just a single string ex. '1 2 3 4'
    input_line = raw_input()
    
    1. Split the string by whitespace into a list
    input_list = input_line.split()
    
    • Note: This is a list of strings ex. ['1', '2', '3', '4'] But we want a list of integers ex. [1, 2, 3, 4]
      1. We need to convert the list elements from strings to ints
    • [Simple] Option 1 (since we know the number of elements, n):
        for i in xrange(n):
            input_list[i] = int(input_list[i])
    
    • [Simple] Option 2 (if we are ignoring the first input line for some reason):
        for i in xrange(len(input_list)):
            input_list[i] = int(input_list[i])
    
    • [More Advanced] Option 3:
        input_list = map(int, input_list)
    
    • [More Advanced] Option 4 (List composition):
        input_list = [int(x) for x in input_list]
    

    Now input_list looks like this: [1, 2, 3, 4]

    1. We need to convert our list of ints to a tuple of ints (as a list is not hashable, but a tuple is)
    t = tuple(input_list)
    
    1. print the result
    print hash(t)
    

    Other Optimizations (Not necessary for credit here):

    • If the first input line == '0' then bail, there isn't anything interesting to parse
    • Call strip() on the input to remove trailing and leading whitespace.
    raw_input().strip()
    
    • These commands can obviously be combined