diff options
| author | Leo Manfredi <manfredi@cpan.org> | 2023-05-03 15:49:23 +0000 |
|---|---|---|
| committer | Leo Manfredi <manfredi@cpan.org> | 2023-05-03 15:49:23 +0000 |
| commit | 38c03c48a29682bb4a56f50c3bd3dbe8a89f443a (patch) | |
| tree | 87ea87f76eb072f03dc5f6eaf5eac19e35b0a20a /challenge-215 | |
| parent | fd65867cfebbaf4ebdd5aed901c3c71d20e31d16 (diff) | |
| download | perlweeklychallenge-club-38c03c48a29682bb4a56f50c3bd3dbe8a89f443a.tar.gz perlweeklychallenge-club-38c03c48a29682bb4a56f50c3bd3dbe8a89f443a.tar.bz2 perlweeklychallenge-club-38c03c48a29682bb4a56f50c3bd3dbe8a89f443a.zip | |
Perl Solution for Task #1
Diffstat (limited to 'challenge-215')
| -rwxr-xr-x | challenge-215/manfredi/perl/ch-1.pl | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/challenge-215/manfredi/perl/ch-1.pl b/challenge-215/manfredi/perl/ch-1.pl new file mode 100755 index 0000000000..5fabadd90b --- /dev/null +++ b/challenge-215/manfredi/perl/ch-1.pl @@ -0,0 +1,51 @@ +#!/usr/bin/env perl + +use v5.36; + +say "challenge-215-task1"; + +# Task 1: Odd one Out +# You are given a list of words (alphabetic characters only) of same size. +# Write a script to remove all words not sorted alphabetically and print the number of words in the list that are not alphabetically sorted. + +# Example 1 +# Input: @words = ('abc', 'xyz', 'tsu') +# Output: 1 + +# Example 2 +# Input: @words = ('rat', 'cab', 'dad') +# Output: 3 + +# Example 3 +# Input: @words = ('x', 'y', 'z') +# Output: 0 + +sub odd_one_out { + my @words = @{+shift}; + my $char = ''; + my $out = 0; + say "Input: @words"; + for my $word (@words) { + for my $c (split '', $word) { + if ($char lt $c) { + $char = $c; + } else { + $out += 1; + last; + } + } + } + say "Output: $out"; +} + +while (<DATA>) { + chomp; + my @words = split ','; + odd_one_out(\@words); +} + + +__DATA__ +abc,xyz,tsu +rat, cab,dad +x,y,z |
