From 3e01b8b6eacd42f3befb1c286e240a36197d7919 Mon Sep 17 00:00:00 2001 From: Pranav Rajpurkar Date: Wed, 7 Sep 2022 02:41:05 -0400 Subject: [PATCH 1/9] Add vscode settings to .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c6f9a44 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vscode/settings.json From e1127bc5b61b9943291ab21cec6ae9da023ec34a Mon Sep 17 00:00:00 2001 From: Pranav Rajpurkar Date: Wed, 7 Sep 2022 11:08:04 -0400 Subject: [PATCH 2/9] Update number_of_ways.py --- number_of_ways.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/number_of_ways.py b/number_of_ways.py index 5197515..bebe306 100644 --- a/number_of_ways.py +++ b/number_of_ways.py @@ -57,7 +57,7 @@ def test_number_of_ways(): - 1 -> 0 -> 1 -> 2. It can be proven that no other way is possible, so we return 3. """ - numberOfWays(1, 2, 3) + print(numberOfWays(1, 2, 3)) if __name__ == "__main__": test_number_of_ways() From db0ae6668584193e9dc964e98d0b42d8081525dd Mon Sep 17 00:00:00 2001 From: Pranav Rajpurkar Date: Wed, 7 Sep 2022 15:43:15 -0400 Subject: [PATCH 3/9] Delete a.py --- a.py | 79 ------------------------------------------------------------ 1 file changed, 79 deletions(-) delete mode 100644 a.py diff --git a/a.py b/a.py deleted file mode 100644 index 55183f5..0000000 --- a/a.py +++ /dev/null @@ -1,79 +0,0 @@ -from turtle import right -from typing import DefaultDict -from tqdm import tqdm - -def numberOfWays3(startPos: int, endPos: int, k: int, cache: dict = None) -> int: - if cache is None: - cache = {} - if k == 0: - if startPos == endPos: - return 1 - return 0 - dist = endPos - startPos - if dist > k: - return 0 - if dist in cache and k in cache[dist]: - return cache[dist][k] - left_ways = numberOfWays3(startPos-1, endPos, k -1, cache) - right_ways = numberOfWays3(startPos+1, endPos, k -1, cache) - ways = left_ways + right_ways - if dist not in cache: - cache[dist] = {} - cache[dist][k] = ways - return left_ways + right_ways - - -def numberOfWays2(startPos: int, endPos: int, k: int) -> int: - if k == 0: - if startPos == endPos: - return 1 - return 0 - if endPos - startPos > k: - return 0 - return ( - numberOfWays2(startPos-1, endPos, k -1) - #numberOfWays2(startPos+1, endPos, k -1) - + numberOfWays2(startPos+1, endPos, k -1) - ) - -def numberOfWays(startPos: int, endPos: int, k: int) -> int: - #paths = [startPos] - paths = [[startPos]] - for i in tqdm(range(k)): - #for path in paths: - # new_path = path.copy() - # last_position = new_path[-1] - # new_path_left = new_path + [last_position - 1] - # new_path_right = new_path + [last_position - 1] - # paths.append(new_path_left) - # paths.append(new_path_right) - for j in range(len(paths)): - path = paths.pop(0) - last_position = path[-1] - # better error - # if endPos - last_position > (k - i - 1): - # continue - # if last_position - endPos > (k - i): - # continue - if endPos - last_position > (k - i): - continue - new_path_left = path + [last_position - 1] - new_path_right = path + [last_position + 1] - paths.append(new_path_left) - paths.append(new_path_right) - num_ways = 0 - for path in paths: - if path[-1] == endPos: - #new_ways += 1 - num_ways += 1 - return num_ways -#print(numberOfWays2(startPos=1, endPos=2, k=3)) -#print(numberOfWays2(startPos = 2, endPos = 5, k = 10)) -# print(numberOfWays(startPos=2, endPos=5, k=10)) - - -print(numberOfWays3(startPos=264, endPos=198, k=68, cache={})) -198 -264 -198 -68 \ No newline at end of file From 9dcaaff07c1009c87af8e37c70d6bba664552bf8 Mon Sep 17 00:00:00 2001 From: Ethan Hellman Date: Mon, 16 Jan 2023 18:50:06 -0800 Subject: [PATCH 4/9] Add environment.yml Earlier the repository did not specify how to get number_of_ways.py to work well. --- environment.yml | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 environment.yml diff --git a/environment.yml b/environment.yml new file mode 100644 index 0000000..02ea824 --- /dev/null +++ b/environment.yml @@ -0,0 +1,7 @@ +name: cs197 +channels: + - defaults +dependencies: + - python=3.9 + - tqdm +prefix: /Users/ethan/opt/anaconda3/envs/cs197 From d05fd293aa09e1807169f15740a36a1fd861f779 Mon Sep 17 00:00:00 2001 From: Ethan Hellman Date: Mon, 16 Jan 2023 18:53:29 -0800 Subject: [PATCH 5/9] Add environment.yaml Earlier this repository did not specify how to get number_of_ways.py to work well. --- environment.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/environment.yml b/environment.yml index 02ea824..03cff00 100644 --- a/environment.yml +++ b/environment.yml @@ -4,4 +4,5 @@ channels: dependencies: - python=3.9 - tqdm + - numpy prefix: /Users/ethan/opt/anaconda3/envs/cs197 From d28530db6048fdc63dce95fe9803a5fb92c82ccb Mon Sep 17 00:00:00 2001 From: Ethan Hellman Date: Mon, 16 Jan 2023 18:58:44 -0800 Subject: [PATCH 6/9] Fix initialization of paths Paths should have been a list of one list element, rather than a list of one int element. --- number_of_ways.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/number_of_ways.py b/number_of_ways.py index bebe306..fcd47be 100644 --- a/number_of_ways.py +++ b/number_of_ways.py @@ -16,7 +16,7 @@ def numberOfWays(startPos: int, endPos: int, k: int) -> int: perform exactly k steps. """ # start with path of length 1 - paths = [startPos] + paths = [[startPos]] # loop k times for i in tqdm(range(k)): From b32a2442d6c78c7557fb9d1470f2dc8e8790f7e9 Mon Sep 17 00:00:00 2001 From: Ethan Hellman Date: Mon, 16 Jan 2023 19:14:43 -0800 Subject: [PATCH 7/9] fixed a couple of errors --- number_of_ways.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/number_of_ways.py b/number_of_ways.py index fcd47be..d24bd4b 100644 --- a/number_of_ways.py +++ b/number_of_ways.py @@ -20,18 +20,18 @@ def numberOfWays(startPos: int, endPos: int, k: int) -> int: # loop k times for i in tqdm(range(k)): - for path in paths: - new_path = path.copy() + for _ in range(len(paths)): + new_path = paths.pop(0) last_position = new_path[-1] # exist fast if not going to make to end - if endPos - last_position > (k - i - 1): + if endPos - last_position > (k - i): continue # path that goes to the left new_path_left = new_path + [last_position - 1] # path that goes to the right - new_path_right = new_path + [last_position - 1] + new_path_right = new_path + [last_position + 1] # add paths to the left and right paths.append(new_path_left) From d58415988b69fc180558bc38387933a967ee7893 Mon Sep 17 00:00:00 2001 From: Ethan Hellman Date: Mon, 16 Jan 2023 19:18:13 -0800 Subject: [PATCH 8/9] changed name of new_ways to num_ways --- number_of_ways.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/number_of_ways.py b/number_of_ways.py index d24bd4b..6295dd5 100644 --- a/number_of_ways.py +++ b/number_of_ways.py @@ -40,7 +40,7 @@ def numberOfWays(startPos: int, endPos: int, k: int) -> int: num_ways = 0 for path in paths: if path[-1] == endPos: - new_ways += 1 + num_ways += 1 return num_ways From 26b2674f0343bb473cc44235efaf74d36de2a606 Mon Sep 17 00:00:00 2001 From: Ethan Hellman Date: Mon, 16 Jan 2023 19:19:14 -0800 Subject: [PATCH 9/9] added bs line to code --- number_of_ways.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/number_of_ways.py b/number_of_ways.py index 6295dd5..7096bf3 100644 --- a/number_of_ways.py +++ b/number_of_ways.py @@ -43,6 +43,8 @@ def numberOfWays(startPos: int, endPos: int, k: int) -> int: num_ways += 1 return num_ways +##adding some bs line here + def test_number_of_ways(): """