Created
February 15, 2014 03:57
-
-
Save funkotron/9014341 to your computer and use it in GitHub Desktop.
Solution to "All Your Base" Google Codejam contest in Python http://code.google.com/codejam/contest/189252/dashboard#s=p0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
def distinct(j): | |
seen = set() | |
for i in j: | |
if i not in seen: | |
seen.add(i) | |
return seen | |
def next_index(c): | |
if c == 0: | |
return 1 | |
if c == 1: | |
return 0 | |
return c | |
def solve(j): | |
alphabet = distinct(j) | |
base = max(2, len(alphabet)) | |
code = {} | |
result = 0 | |
total = len(j) | |
for k, i in enumerate(j): | |
if i in code: | |
new_val = code[i] | |
else: | |
new_val = next_index | |
result += ((base ^ (total - k)) * new_val) | |
code[i]=new_val | |
if __name__ == "__main__": | |
f = open("A-large-practice.in") | |
l = f.readlines()[1:] | |
f.close() | |
o = open("A-large-py.out", "w") | |
for i, j in enumerate(l): | |
o.write("Case #%d: %d\n"% (i, solve(j))) | |
o.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
at line 6 why would you check if it exists in
set
? The whole purpose of using set is that you don't check while adding and be sure that it will return unique element.