Skip to content

Instantly share code, notes, and snippets.

@funkotron
Created February 15, 2014 03:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save funkotron/9014341 to your computer and use it in GitHub Desktop.
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
#!/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()
@harshil93r
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment