aboutsummaryrefslogtreecommitdiff
path: root/challenge-295
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-295')
-rw-r--r--challenge-295/sgreen/README.md4
-rw-r--r--challenge-295/sgreen/blog.txt1
-rwxr-xr-xchallenge-295/sgreen/perl/ch-1.pl23
-rwxr-xr-xchallenge-295/sgreen/perl/ch-2.pl38
-rwxr-xr-xchallenge-295/sgreen/python/ch-1.py22
-rwxr-xr-xchallenge-295/sgreen/python/ch-2.py34
-rwxr-xr-xchallenge-295/sgreen/python/test.py23
7 files changed, 143 insertions, 2 deletions
diff --git a/challenge-295/sgreen/README.md b/challenge-295/sgreen/README.md
index 1abaa6d74b..2b801f7763 100644
--- a/challenge-295/sgreen/README.md
+++ b/challenge-295/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 293
+# The Weekly Challenge 295
-Blog: [Similar boomerang](https://dev.to/simongreennet/similar-boomerang-2h88)
+Blog: [The Break Game](https://dev.to/simongreennet/the-break-game-2jp8)
diff --git a/challenge-295/sgreen/blog.txt b/challenge-295/sgreen/blog.txt
new file mode 100644
index 0000000000..586067928a
--- /dev/null
+++ b/challenge-295/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/the-break-game-2jp8 \ No newline at end of file
diff --git a/challenge-295/sgreen/perl/ch-1.pl b/challenge-295/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..931319325e
--- /dev/null
+++ b/challenge-295/sgreen/perl/ch-1.pl
@@ -0,0 +1,23 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub main (@words) {
+ my $str = shift @words;
+
+ # Turn the words into a regular expression
+ my $pattern = '^(' . join( '|', map { quotemeta } @words ) . ')+$';
+
+ # Check if the string matches the pattern
+ if ( $str =~ $pattern ) {
+ say 'true';
+ }
+ else {
+ say 'false';
+ }
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-295/sgreen/perl/ch-2.pl b/challenge-295/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..84b8e8d60c
--- /dev/null
+++ b/challenge-295/sgreen/perl/ch-2.pl
@@ -0,0 +1,38 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub jump_game( $ints, $moves ) {
+ # Calculate the minimum number of moves from this point
+ my $min_moves = undef;
+
+ for ( my $i = $ints->[0] ; $i > 0 ; $i-- ) {
+ # We can complete the jump with this move
+ if ( $i >= $#$ints ) {
+ return $moves;
+ }
+
+ # Skip if we can't move forward
+ if ( $ints->[$i] == 0 ) {
+ next;
+ }
+
+ # Call the function again jumping 'i' positions
+ my $m = jump_game( [ $ints->[ $i .. $#$ints ] ], $moves + 1 );
+ if ( defined($m) and ( not defined($min_moves) or $min_moves > $m ) ) {
+ $min_moves = $m;
+ }
+ }
+
+ return $min_moves;
+}
+
+sub main (@ints) {
+ my $moves = jump_game( \@ints, 1 );
+ say $moves // -1;
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-295/sgreen/python/ch-1.py b/challenge-295/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..bed823f512
--- /dev/null
+++ b/challenge-295/sgreen/python/ch-1.py
@@ -0,0 +1,22 @@
+#!/usr/bin/env python3
+
+import re
+import sys
+
+
+def word_break(s: str, words: list) -> bool:
+ # Turn the words into a regular expression
+ pattern = '^(' + '|'.join(map(re.escape, words)) + ')+$'
+
+ # Check if the string matches the pattern
+ return True if re.search(pattern, s) else False
+
+
+def main():
+ # The first value is the string, the rest are the words
+ result = word_break(sys.argv[1], sys.argv[2:])
+ print('true' if result else 'false')
+
+
+if __name__ == '__main__':
+ main()
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()
diff --git a/challenge-295/sgreen/python/test.py b/challenge-295/sgreen/python/test.py
new file mode 100755
index 0000000000..33117a6d85
--- /dev/null
+++ b/challenge-295/sgreen/python/test.py
@@ -0,0 +1,23 @@
+#!/usr/bin/env python3
+
+import unittest
+ch_1 = __import__('ch-1')
+ch_2 = __import__('ch-2')
+
+
+class TestClass(unittest.TestCase):
+ def test_ch_1(self):
+ self.assertTrue(ch_1.word_break(
+ 'weeklychallenge', ["challenge", "weekly"]))
+ self.assertTrue(ch_1.word_break('perlrakuperl', ["raku", "perl"]))
+ self.assertFalse(ch_1.word_break(
+ 'sonsanddaughters', ["sons", "sand", "daughters"]))
+
+ def test_ch_2(self):
+ self.assertEqual(ch_2.jump_game([2, 3, 1, 1, 4]), 2)
+ self.assertEqual(ch_2.jump_game([2, 3, 0, 4]), 2)
+ self.assertEqual(ch_2.jump_game([2, 0, 0, 4]), None)
+
+
+if __name__ == '__main__':
+ unittest.main()