Skip to content

Commit

Permalink
Add kattis prob pizzahawaii in python3 + notes
Browse files Browse the repository at this point in the history
  • Loading branch information
iandioch committed Apr 16, 2018
1 parent b4211f2 commit 4f7bada
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
9 changes: 9 additions & 0 deletions kattis/pizzahawaii/notes.md
@@ -0,0 +1,9 @@
A sets/dictionaries problem.

Build up a dictionary `d` of possible translations for each foreign word.

Whenever a new pizza is read in, the possible translations for each `w` in the foreign language should be the set intersection of the previous translations (`d[w]`) and the english ingredients in this pizza.

After this is done for all pizzas, you need to run through each `w` in `d` and remove all possible candidate translations that appear in other pizzas without `w`. ie. For every input pizza, if `w` is not in the foreign ingredients, then you should remove all the english ingredients for that pizza from `d[w]`.

This runs in 0.02s in python3 (the fastest python3 submission on kattis!).
33 changes: 33 additions & 0 deletions kattis/pizzahawaii/solution.py
@@ -0,0 +1,33 @@
def main():
t = int(input())
for _ in range(t):
n = int(input())
d = {}
p = []
for _ in range(n):
input() # pizza name
_, *u = input().split()
_, *v = input().split()
u = set(u)
v = set(v)
for w in u:
if w in d:
d[w] &= v
else:
d[w] = set(v)
p.append((u, v))
for u, v in p:
for w in d:
if w not in u:
d[w] -= v
ans = []
for k in d:
for o in d[k]:
ans.append((k, o))
ans.sort()
for a in ans:
print('({}, {})'.format(*a))
print()

if __name__ == '__main__':
main()

0 comments on commit 4f7bada

Please sign in to comment.