From 3326f44c45080935ed26e168c9b6e5b0d2dec867 Mon Sep 17 00:00:00 2001 From: Leo Manfredi Date: Fri, 12 Jun 2020 18:46:24 +0200 Subject: Solution for Task #2 --- challenge-064/manfredi/perl/ch-2.pl | 30 ++++++++++++++++++++++++++++++ challenge-064/manfredi/python/ch-2.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100755 challenge-064/manfredi/perl/ch-2.pl create mode 100755 challenge-064/manfredi/python/ch-2.py 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) + -- cgit