Last active
January 31, 2019 10:10
-
-
Save rec/431b768c746e1e2c9e6a6e0a32fa51c1 to your computer and use it in GitHub Desktop.
Correction to https://gist.github.com/alexgolec/155256f50cca40d26c5af8e2285dadbf#file-part_1_solution-py
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
def synonym_queries(synonym_words, queries): | |
''' | |
synonym_words: iterable of pairs of strings representing synonymous words | |
queries: iterable of pairs of strings representing queries to be tested for | |
synonymous-ness | |
''' | |
synonyms = defaultdict(set) | |
for w1, w2 in synonym_words: | |
synonyms[w1].add(w2) | |
def synonymous(w1, w2): | |
return w1 == w2 or w2 in synonyms.get(w1, ()) or w1 in synonyms.get(w2, ())) | |
def synonymous_phrase(q1, q2): | |
q1, q2 = q1.split(), q2.split() | |
return len(q1) == len(q2) and all(synonymous(w1, w2) for w1, w2 in zip(q1, q2)) | |
return [synonymous_phrase(q1, q2) for q1, q2 in queries] | |
# See https://medium.com/@alexgolec/google-interview-problems-synonymous-queries-36425145387c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment