aboutsummaryrefslogtreecommitdiff
path: root/challenge-215/manfredi/perl/ch-1.pl
blob: 5fabadd90b8648ea4450ab276827534c0615d171 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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