aboutsummaryrefslogtreecommitdiff
path: root/challenge-064/manfredi
diff options
context:
space:
mode:
authorLeo Manfredi <manfredi@cpan.org>2020-06-12 18:46:24 +0200
committerLeo Manfredi <manfredi@cpan.org>2020-06-12 18:46:24 +0200
commit3326f44c45080935ed26e168c9b6e5b0d2dec867 (patch)
treed3799f72b5af6bd528f5b5b38a9d6b1288df28bb /challenge-064/manfredi
parent4cb5dab951b195c73af94b60a5fb1063158fdde0 (diff)
downloadperlweeklychallenge-club-3326f44c45080935ed26e168c9b6e5b0d2dec867.tar.gz
perlweeklychallenge-club-3326f44c45080935ed26e168c9b6e5b0d2dec867.tar.bz2
perlweeklychallenge-club-3326f44c45080935ed26e168c9b6e5b0d2dec867.zip
Solution for Task #2
Diffstat (limited to 'challenge-064/manfredi')
-rwxr-xr-xchallenge-064/manfredi/perl/ch-2.pl30
-rwxr-xr-xchallenge-064/manfredi/python/ch-2.py32
2 files changed, 62 insertions, 0 deletions
diff --git a/challenge-064/manfredi/perl/ch-2.pl b/challenge-064/manfredi/perl/ch-2.pl
new file mode 100755
index 0000000000..5bb0204612
--- /dev/null
+++ b/challenge-064/manfredi/perl/ch-2.pl
@@ -0,0 +1,30 @@
+#!/usr/bin/env perl
+
+# Perl Week Challenge 064 - Task #2
+# You are given a string $S and an array of words @W.
+# Write a script to find out if $S can be split into
+# sequence of one or more words as in the given @W.
+# Print the all the words if found otherwise print 0.
+
+use strict;
+my (@words, $words, $string, %pos);
+
+$string = 'perlweeklychallenge';
+@words = ("weekly", "challenge", "perl");
+
+# $string = 'perlandraku';
+# @words = ("python", "ruby", "haskell");
+
+$words = join '', @words;
+
+print 0 and exit if length($string) != length($words);
+
+for my $word (@words) {
+ my $i = index($string, $word);
+ print 0 and exit if $i == -1;
+ $pos{chr($i)} = $word;
+}
+
+my @result = map { $pos{$_} } sort keys %pos;
+print "@result\n";
+
diff --git a/challenge-064/manfredi/python/ch-2.py b/challenge-064/manfredi/python/ch-2.py
new file mode 100755
index 0000000000..8b83956291
--- /dev/null
+++ b/challenge-064/manfredi/python/ch-2.py
@@ -0,0 +1,32 @@
+#!/usr/bin/env python3
+
+# Perl Week Challenge 064 - Task #2
+# You are given a string $S and an array of words @W.
+# Write a script to find out if $S can be split into
+# sequence of one or more words as in the given @W.
+# Print the all the words if found otherwise print 0.
+
+string = 'perlweeklychallenge'
+words = ["weekly", "challenge", "perl"]
+
+# string = 'perlandraku'
+# words = ["python", "ruby", "haskell"]
+
+slurp = ''.join(words)
+
+if len(string) != len(slurp):
+ print(0)
+ exit()
+
+pos = {}
+
+for word in words:
+ i = string.find(word)
+ if i == -1:
+ print(0)
+ exit()
+ pos[i] = word
+
+result = [ pos[x] for x in sorted(pos) ]
+print(result)
+