aboutsummaryrefslogtreecommitdiff
path: root/challenge-295/sgreen/python/ch-2.py
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-11-17 16:36:08 +0000
committerGitHub <noreply@github.com>2024-11-17 16:36:08 +0000
commit7309ac414326e436ee729a71db0a1cb127041ead (patch)
tree2b7fa45a2ff970bc91249bb6b2783c6dc38a3e4e /challenge-295/sgreen/python/ch-2.py
parent889332989f865e782e6727abc8a85107b2439946 (diff)
parentfe00ecfb7362652b3d450000cd099f4044775cfd (diff)
downloadperlweeklychallenge-club-7309ac414326e436ee729a71db0a1cb127041ead.tar.gz
perlweeklychallenge-club-7309ac414326e436ee729a71db0a1cb127041ead.tar.bz2
perlweeklychallenge-club-7309ac414326e436ee729a71db0a1cb127041ead.zip
Merge pull request #11167 from simongreen-net/master
sgreen solutions to challenge 295
Diffstat (limited to 'challenge-295/sgreen/python/ch-2.py')
-rwxr-xr-xchallenge-295/sgreen/python/ch-2.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/challenge-295/sgreen/python/ch-2.py b/challenge-295/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..5aa6999eb9
--- /dev/null
+++ b/challenge-295/sgreen/python/ch-2.py
@@ -0,0 +1,34 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def jump_game(ints: list, moves=1) -> int:
+ # Calculate the minimum number of moves from this point
+ min_moves = None
+ for i in range(ints[0], 0, -1):
+ # We can complete the jump with this move
+ if i >= len(ints)-1:
+ return moves
+
+ # Skip if we can't move forward
+ if ints[i] == 0:
+ continue
+
+ # Call the function again jumping 'i' positions
+ m = jump_game(ints[i:], moves+1)
+ if m is not None and (min_moves is None or min_moves > m):
+ min_moves = m
+
+ return min_moves
+
+
+def main():
+ # Convert input into integers
+ array = [int(n) for n in sys.argv[1:]]
+ result = jump_game(array)
+ print(-1 if result is None else result)
+
+
+if __name__ == '__main__':
+ main()