From 0bd9bbf298022fe22ad49d1f589db3e1ffeb805e Mon Sep 17 00:00:00 2001 From: echoaj Date: Fri, 20 Nov 2020 00:55:32 -0800 Subject: [PATCH 1/3] Implemented minimum steps to one using tabulation. --- dynamic_programming/minimum_steps_to_one.py | 69 +++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 dynamic_programming/minimum_steps_to_one.py diff --git a/dynamic_programming/minimum_steps_to_one.py b/dynamic_programming/minimum_steps_to_one.py new file mode 100644 index 000000000000..6ebbec9edbb5 --- /dev/null +++ b/dynamic_programming/minimum_steps_to_one.py @@ -0,0 +1,69 @@ +""" +YouTube Explanation: https://www.youtube.com/watch?v=f2xi3c1S95M + +Given an integer n, return the minimum steps to 1 + +AVAILABLE STEPS: + * Decrement by 1 + * if n is divisible by 2, divide by 2 + * if n is divisible by 3, divide by 3 + + +Example 1: n = 10 +10 -> 9 -> 3 -> 1 +Result: 3 steps + +Example 2: n = 15 +15 -> 5 -> 4 -> 2 -> 1 +Result: 4 steps + +Example 3: n = 6 +6 -> 2 -> 1 +Result: 2 step +""" + +from __future__ import annotations + +__author__ = "Alexander Joslin" + + +def min_steps_to_one(n: int) -> int: + """ + Implemented using tabulation. + + DocTests + >>> min_steps_to_one(10) + 3 + + >>> min_steps_to_one(15) + 4 + + >>> min_steps_to_one(6) + 2 + + :param n: + :return int: + """ + + if n <= 0: + raise ValueError(f"n must be greater than 0. Got n = {n}") + + table = [n + 1] * (n + 1) + + # starting position + table[1] = 0 + for i in range(1, n): + table[i + 1] = min(table[i + 1], table[i] + 1) + # check if out of bounds + if i * 2 <= n: + table[i * 2] = min(table[i * 2], table[i] + 1) + # check if out of bounds + if i * 3 <= n: + table[i * 3] = min(table[i * 3], table[i] + 1) + return table[n] + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 99e0bec2e9efe5879f77062fd4d7d5cb50661a12 Mon Sep 17 00:00:00 2001 From: Alex Joslin Date: Sun, 6 Dec 2020 20:28:30 -0800 Subject: [PATCH 2/3] Update minimum_steps_to_one.py Made the parameter "n" more descriptive. Changed it to number --- dynamic_programming/minimum_steps_to_one.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/dynamic_programming/minimum_steps_to_one.py b/dynamic_programming/minimum_steps_to_one.py index 6ebbec9edbb5..292da26c4263 100644 --- a/dynamic_programming/minimum_steps_to_one.py +++ b/dynamic_programming/minimum_steps_to_one.py @@ -27,7 +27,7 @@ __author__ = "Alexander Joslin" -def min_steps_to_one(n: int) -> int: +def min_steps_to_one(number: int) -> int: """ Implemented using tabulation. @@ -45,22 +45,22 @@ def min_steps_to_one(n: int) -> int: :return int: """ - if n <= 0: - raise ValueError(f"n must be greater than 0. Got n = {n}") + if number <= 0: + raise ValueError(f"n must be greater than 0. Got n = {number}") - table = [n + 1] * (n + 1) + table = [number + 1] * (number + 1) # starting position table[1] = 0 - for i in range(1, n): + for i in range(1, number): table[i + 1] = min(table[i + 1], table[i] + 1) # check if out of bounds - if i * 2 <= n: + if i * 2 <= number: table[i * 2] = min(table[i * 2], table[i] + 1) # check if out of bounds - if i * 3 <= n: + if i * 3 <= number: table[i * 3] = min(table[i * 3], table[i] + 1) - return table[n] + return table[number] if __name__ == "__main__": From 65c27abc6a394f9ce0e4eade9291d0c6efbb4940 Mon Sep 17 00:00:00 2001 From: John Law Date: Wed, 9 Dec 2020 16:34:35 +0800 Subject: [PATCH 3/3] `n` to `number` --- dynamic_programming/minimum_steps_to_one.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/dynamic_programming/minimum_steps_to_one.py b/dynamic_programming/minimum_steps_to_one.py index 292da26c4263..f4eb7033dd20 100644 --- a/dynamic_programming/minimum_steps_to_one.py +++ b/dynamic_programming/minimum_steps_to_one.py @@ -29,19 +29,15 @@ def min_steps_to_one(number: int) -> int: """ - Implemented using tabulation. - - DocTests + Minimum steps to 1 implemented using tabulation. >>> min_steps_to_one(10) 3 - >>> min_steps_to_one(15) 4 - >>> min_steps_to_one(6) 2 - :param n: + :param number: :return int: """