diff options
| -rw-r--r-- | challenge-096/cristian-heredia/perl/ch-1.pl | 57 | ||||
| -rw-r--r-- | challenge-096/cristian-heredia/python/ch-1.py | 40 |
2 files changed, 97 insertions, 0 deletions
diff --git a/challenge-096/cristian-heredia/perl/ch-1.pl b/challenge-096/cristian-heredia/perl/ch-1.pl new file mode 100644 index 0000000000..6bc1bcf43f --- /dev/null +++ b/challenge-096/cristian-heredia/perl/ch-1.pl @@ -0,0 +1,57 @@ +=begin + +TASK #1 › Reverse Words +Submitted by: Mohammad S Anwar +You are given a string $S. + +Write a script to reverse the order of words in the given string. The string may contain leading/trailing spaces. The string may have more than one space between words in the string. Print the result without leading/trailing spaces and there should be only one space between words. + + Example 1: + Input: $S = "The Weekly Challenge" + Output: "Challenge Weekly The" + Example 2: + Input: $S = " Perl and Raku are part of the same family " + Output: "family same the of part are Raku and Perl" + +=end +=cut + +use strict; +use warnings; + +my $input = " Perl and Raku are part of the same family "; + +my @wordsReverse; +my $sentence; + +fixSpaces(); +reverseString(); + +sub fixSpaces { + $input =~ s/^\s+|\s+$//g; + $input =~ s/\s{2,}/ /g; +} + +sub reverseString { + my @words = split / /, $input; + for (my $i=0; $i<@words; $i++) { + unshift(@wordsReverse, $words[$i]); + unshift(@wordsReverse, " "); + } + fixSpaces(); + createString(); +} + +sub createString { + for (my $j=0; $j<@wordsReverse; $j++) { + $sentence = $sentence.$wordsReverse[$j] + } + fixSpaces(); + print "$sentence\n"; +} + + + + + + diff --git a/challenge-096/cristian-heredia/python/ch-1.py b/challenge-096/cristian-heredia/python/ch-1.py new file mode 100644 index 0000000000..51b8e54cff --- /dev/null +++ b/challenge-096/cristian-heredia/python/ch-1.py @@ -0,0 +1,40 @@ +''' + +TASK #1 › Reverse Words +Submitted by: Mohammad S Anwar +You are given a string $S. + +Write a script to reverse the order of words in the given string. The string may contain leading/trailing spaces. The string may have more than one space between words in the string. Print the result without leading/trailing spaces and there should be only one space between words. + + Example 1: + Input: $S = "The Weekly Challenge" + Output: "Challenge Weekly The" + Example 2: + Input: $S = " Perl and Raku are part of the same family " + Output: "family same the of part are Raku and Perl" + +''' +import re + +input = " Perl and Raku are part of the same family "; +listReverse = [] +separator = " " + +def fixSpaces(a): + inputNew = a.strip() + inputFinal = re.sub(' +', separator,inputNew) + reverseString(inputFinal) + +def reverseString(x): + words = x.split(separator) + for i in range(len(words)): + listReverse.append(words[-1-i]) + sentence = separator.join(listReverse) + print (sentence) + +fixSpaces(input) + + + + + |
