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]`
@dbgarf
Copy link

dbgarf commented Sep 11, 2016

some snippets and idioms that might help clean this up. it's not as bad you think it is though.

  • 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.
sorted_last_names = sorted([item[1] for item in records.keys()]
  • this line is really truly bogus if str([key + ('', records[key])]) not in itered_record]: . i'm really not sure what you're trying to do there but here's the code smell: if you can't even say in English what the condition is checking, then you wrote it wrong. if you have to type cast something in order to get it to work, then you wrote it wrong. the general principle is that complicated tricky code is never correct under any circumstances. there are a vanishingly small number of good exceptions to that rule of thumb and nearly all of them are related to really twitchy optimization of low level code in languages that are close to the hardware. in a high level language like Python I've never once seen a good reason to except that rule of thumb.
  • 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?
  • here's a code snippet that might help. this is populating a list with tuples where the items in the last are (last_name, [list, of, tags]) and the list is sorted by last name
output_list = []
for key, val in records.items():
    last_name =  key[1]
    output_tuple = (last_name, val)
    output_list.append(output_tuple)
output_list = sorted(output_list, key=lambda tup: tup[0])
  • here's a variation of it where you keep the (first_name, last_name) format in your output_list
for key, val in records.items():
    output_tuple = (key, val)
    output_list.append(output_tuple)

# tup[0] is the whole name tuple (first_name, last_name)
# tup[0][1] then is just the last name
output_list = sorted(output_list, key=lambda tup: tup[0][1])

sorry if I missed the point of the exercise. I'm not entirely sure what you were supposed to be doing with this. it looks like its just some data munging though, massaging the contents of the records dictionary into a sorted list, right? if there's something more besides that let me know and I'll take another look.

@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