diff options
| author | Csaba Simandi <20855193+x1mandi@users.noreply.github.com> | 2020-07-24 09:38:42 -0400 |
|---|---|---|
| committer | Csaba Simandi <20855193+x1mandi@users.noreply.github.com> | 2020-07-24 09:38:42 -0400 |
| commit | 1557cbe3c012d4484b8b6d3c00b09ca1e87f4e09 (patch) | |
| tree | ac7cd1f9da13a9ad8dd3a9cca31862c5259e362e | |
| parent | 3599289f28f0d47a37fa0f348d63516791f07c08 (diff) | |
| download | perlweeklychallenge-club-1557cbe3c012d4484b8b6d3c00b09ca1e87f4e09.tar.gz perlweeklychallenge-club-1557cbe3c012d4484b8b6d3c00b09ca1e87f4e09.tar.bz2 perlweeklychallenge-club-1557cbe3c012d4484b8b6d3c00b09ca1e87f4e09.zip | |
Solution for challenge-070 taks1 in Perl by x1mandi.
| -rw-r--r-- | challenge-070/x1mandi/README | 1 | ||||
| -rw-r--r-- | challenge-070/x1mandi/perl/ch-1.pl | 44 |
2 files changed, 45 insertions, 0 deletions
diff --git a/challenge-070/x1mandi/README b/challenge-070/x1mandi/README new file mode 100644 index 0000000000..f579b0b193 --- /dev/null +++ b/challenge-070/x1mandi/README @@ -0,0 +1 @@ +Solution by Csaba Simandi. diff --git a/challenge-070/x1mandi/perl/ch-1.pl b/challenge-070/x1mandi/perl/ch-1.pl new file mode 100644 index 0000000000..29bfe36675 --- /dev/null +++ b/challenge-070/x1mandi/perl/ch-1.pl @@ -0,0 +1,44 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use diagnostics; +use utf8; +use 5.26.0; +#promt for a string from user (keyboard input) + +my $S = $ARGV[0] || "Perlmongers"; +my $C = $ARGV[1] || 3; +my $O = $ARGV[2] || 4; + +print character_swapping($S, $C, $O)."\n"; + +sub character_swapping { + my ($s, $c, $o) = @_; + my $N = length($s); + + die "ERROR: Missing string.\n" unless defined $s; + die "ERROR: Missing swap count.\n" unless defined $c; + die "ERROR: Missing offset.\n" unless defined $o; + die "ERROR: Invalid swap count [$c].\n" unless ( $c >= 1 ); + die "ERROR: Invalid offset [$o].\n" unless ( $o >= 1 ); + #Why swap count cannot be greater that offset? Checked $S=abcd, $C=2, $O=1 and the output is "bcda" + die "ERROR: Swap count [$c] is greater than offset [$o].\n" unless ( $c <= $o ); + # When $N = $c-1 + die "ERROR: Invalid string length [$N] for given swap count and offset [$c + $o].\n" + unless ( ($c + $o) <= $N); + + my @chars = split //, $s; + + #Why is swapping not applied to the first character at index 0? + for (1..$c) { + my $firstchar = $chars[$_ % $N]; + my $secondchar = $chars[ ($_+$o) % $N]; + + $chars[($_+$o) % $N] = $firstchar; + $chars[$_ % $N] = $secondchar; + } + + $s = join('', @chars); + return $s; +}
\ No newline at end of file |
