The Forum > Technology > NP Python Olympiad
I've created a #npcc on IRC. I'll be watching this channel for any clarifying questions or technical issues you may have. Contest begins in 27 minutes. |
gws said: Depending on the problem set, they can also be very good for learning to estimate. You'll typically find at least one problem which is impossible, and one that is possible but will likely take so long that you'd be better off working on something else. The one I wasted all of my time on was the stupid ransom note one, which I eventually did correctly, but not without more debugging than I wanted to over spacing. :/ If anyone is interested in seeing the code I wrote, here are my solutions: "Go the distance" #!/usr/bin/python import sys import math data = sys.stdin.readlines() coords = [] for line in data[1:]: coords.append(line.split("\t")) length=0 for i in range(int(data[0])-1): length += math.sqrt((int(coords[i][0])-int(coords[i+1][0]))**2 + (int(coords[i][1])-int(coords[i+1][1]))**2) print length Claustrophobia (unfinished) #!/usr/bin/python import sys data = sys.stdin.readlines() x = data[0] y = data[1] edges = [] for line in data[2:-1]: edges.append(line.split("\t")) interior = data[-1].split("\t") size=1 visited=[interior] tested=[] keepgoing = False while (keepgoing): for i in visited: if i[0] #at this point I gave up because I was wasting time "Piecing together a ransom note" #!/usr/bin/python import sys data = sys.stdin.readlines() letters = [] for i in data: letters.append(i.split("\t")) for i in range(len(letters)): if len(letters[i]) > 1: letters[i][0] = int(letters[i][0]) letters[i][1] = int(letters[i][1]) letters[i][2] = letters[i][2][:-1] else: letters = letters[:i] break maxx = 0 maxy = 0 for i in letters: if i[0] > maxx: maxx = i[0]+1 if i[1] > maxy: maxy = i[1]+1 coords = [ [" "] * (maxx + 1) for i in range(maxy+1)] for i in letters: coords[i[1]][i[0]] = i[2] message = "" for i in coords: lastspace = False firstletter = True for j in i: if j != " ": message += j lastspace = False firstletter = False elif (not lastspace and not firstletter): message += " " lastspace = True print message Prime means first #!/usr/bin/python import sys import math data = sys.stdin.readlines() for i in data: try: i = int(i) factors = [] for j in range(i): j+=2 while (i%j == 0): factors.append(j) i /= j for r in range(len(factors)): if r+1 < len(factors): print "%d," % factors[r], else: print factors[r] except: pass "Omni will like this one" For the record, this is the one I had the most failed attempts on. :( #!/usr/bin/python import sys def makecollatz(a): result = [a] value = a while(value != 1): if (value % 2 == 0): value = value/2 result.append(value) else: value = 3*value+1 result.append(value) return result def main(): data = sys.stdin.readlines() tests = [] for i in data: tests.append(i.split("\t")) for i in tests: if len(i) > 1: a = makecollatz(int(i[0])) b = makecollatz(int(i[1])) k = -1 while (a[k] == b[k] and -1*k != len(a) and -1*k != len(b)): k -= 1 if (-1*k == len(a) or -1*k == len(b)): print a[k] else: print a[k+1] if __name__ == "__main__": main() "This one's easy, too" #!/usr/bin/python import sys def main(): data = sys.stdin.readlines() for i in range(len(data)-1): for j in data[i+1:]: if (j == data[i]): print j return if __name__ == "__main__": main() "No Sally, I don't want your stinkin beans" #!/usr/bin/python import sys data = sys.stdin.readlines() for i in data: ops = i.split(" ") if len(ops) > 1: result = int(ops[0]) for j in range((len(ops)-1)/2): if ops[j*2+1] == "+": result += int(ops[j*2+2]) elif ops[j*2+1] == "-": result -= int(ops[j*2+2]) elif ops[j*2+1] == "*": result *= int(ops[j*2+2]) elif ops[j*2+1] == "/": result /= int(ops[j*2+2]) print result Any questions I'll explain. |
gws said: Cosman246 said: Currently, I'm making a text-based adventure in Python. It's a scene-branching style, mainly because I don't know how to make a parser or want to make one Parsers are a very interesting field to get into. I'm sure they are, but this is just a small project done in a bottom-up style. I suppose that if I figure out how to, I could write an elementary parser for basic tasks, like checking the inventory, but not much for now. |
gws said: Cosman246 said: Currently, I'm making a text-based adventure in Python. It's a scene-branching style, mainly because I don't know how to make a parser or want to make one Parsers are a very interesting field to get into. Very hard. Time flies like an arrow. Fruit flies like a banana. Is "Time" an adjective or noun? How do we know that? What about "Fruit"? Is "flies" a noun or a verb? Is "like" a verb or the beginning part of a simile phrase? Time flies like a banana. |
The Forum > Technology > NP Python Olympiad
