Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dyspop/484f09eed4cee61f5d7901db0a621fb0 to your computer and use it in GitHub Desktop.
Save dyspop/484f09eed4cee61f5d7901db0a621fb0 to your computer and use it in GitHub Desktop.
py sort challenge
records = {('wendy', 'sanchez'): ['wunderkind','extraordinaire'], ('dan', 'black'): ['hacker','wannabe'], ('tim', 'black'): ['mad', 'genius', 'dontchaknow'], ('dan', 'garfield'): ['porg', 'rammer', 'snake charmer']}
sorted_last = ['black', 'black', 'garfield', 'sanchez']
itered_records = []
for last_name in sorted_last:
for key in records:
if key[1] == last_name:
if str([key + ('', records[key])]) not in itered_records:
itered_records.append(str([key + ('', records[key])]))
print key, records[key]`
@dyspop
Copy link
Author

dyspop commented Sep 12, 2016

to get the sorted list of keys from records, what you have as sorted_last, it should be generated with code. hard coding data like that would probably be rejected as a solution because it's impossible to do for data sets of non-trivial size.

This is a microcosm of the real problem, so I just made a dummy set to illustrate the data

if you can't even say in English what the condition is checking, then you wrote it wrong. if str([key + ('', records[key])]) not in itered_record]:

English: "If the stringified key value pair is not in the list of iterated records"

what is being checked in that conditional? checking if you've seen the record yet? I don't know why you need to make that check. what is the specification of the problem?

Yes, exactly that is what's being checked. The reason is because of the nested loop, the output has duplicates. We don't want duplicates.

it looks like its just some data munging

precisely.

Your solution works pretty well! 🙇

I knew it had bad code smell and we tried doing sorted and we tried list comprehension but just a lambda sorted didn't occur to us. Thanks!

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