Created
June 18, 2018 01:41
-
-
Save connormcmk/0d7d5212771084e0d9d0e30d935e9635 to your computer and use it in GitHub Desktop.
An unsolicited solution to How to Lose an IT Job in 10 Minutes
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 distinct_value(word): | |
""" | |
add up the hash value of each letter of the given word to create a unique id | |
for that entry | |
""" | |
# make word lower-case | |
word = word.lower() | |
hash_val = 0 | |
for letter in word: | |
hash_val += hash(letter) | |
return hash_val | |
def match_distinct_value(word, _list): | |
""" | |
check the first word in each sub_list of _list to see if it matches the word | |
that's passed in | |
if it matches, return the index where it matches | |
if it doesn't match any of the existing members return None | |
""" | |
for index, item in enumerate(_list): | |
if distinct_value(item[0]) == distinct_value(word)h: | |
return index | |
return None | |
def main(): | |
""" | |
for each city in the given_list | |
check if it has a matching distinct_value in the city_list | |
if it does, append within the sub_list at the returned index | |
otherwise, append a new sub_list with it as the member | |
""" | |
given_list = ['Tokyo', 'London', 'Rome', 'Donlon', 'Kyoto', 'Paris', 'Ondlon', 'Apris'] | |
city_list = [] | |
for city in given_list: | |
index_result = match_distinct_value(city, city_list) | |
if index_result is not None: | |
city_list[index_result].append(city) | |
else: | |
city_list.append([city]) | |
print(city_list) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment