Skip to content

Instantly share code, notes, and snippets.

@jcuffe
Created October 13, 2018 03:31
Show Gist options
  • Save jcuffe/280df510597b95d349f504a8b5f7866c to your computer and use it in GitHub Desktop.
Save jcuffe/280df510597b95d349f504a8b5f7866c to your computer and use it in GitHub Desktop.
diff --git a/hash-tables/ex1/ex1.py b/hash-tables/ex1/ex1.py
index 3039cf3..e7e289d 100644
--- a/hash-tables/ex1/ex1.py
+++ b/hash-tables/ex1/ex1.py
@@ -1,14 +1,14 @@
def get_indices_of_item_weights(weights, limit):
- hash_table = { i : weights[i] for i in range(0, len(weights) ) }
- hash_table2 = {y:x for x,y in hash_table.items()}
+ hash_table = { weights[i] : i for i in range(len(weights)) }
- for weight in hash_table2:
+ for i in range(len(weights)):
try:
- twoWeights = hash_table2[limit-weight]
- return (hash_table2[limit-weight], hash_table2[weight])
- except KeyError:
- print('Try again...')
+ return (hash_table[limit-weights[i]], i)
+ except KeyError:
+ pass
+
+ return ()
if __name__ == '__main__':
# You can write code here to test your implementation using the Python repl
diff --git a/hash-tables/ex2/ex2.py b/hash-tables/ex2/ex2.py
index 67b7390..61bb7e5 100644
--- a/hash-tables/ex2/ex2.py
+++ b/hash-tables/ex2/ex2.py
@@ -13,6 +13,8 @@ def reconstruct_trip(tickets):
route.append(current)
if current in hash_table:
current = hash_table[current]
+ else:
+ return []
return route
if __name__ == '__main__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment