Created
October 13, 2018 03:30
-
-
Save jcuffe/74376174e042d2ba278b825cb74ff629 to your computer and use it in GitHub Desktop.
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
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