aboutsummaryrefslogtreecommitdiff
path: root/challenge-215/jo-37/perl/ch-1.pl
blob: 2f8236a666793ec7276002de3c7523e5855498ea (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
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/perl -s

use v5.24;
use Test2::V0 '!float';
use PDL;
use PDL::Char;

our ($tests, $examples);

run_tests() if $tests || $examples;	# does not return

die <<EOS unless @ARGV;
usage: $0 [-examples] [-tests] [WORD...]

-examples
    run the examples from the challenge
 
-tests
    run some tests

WORD...
    list of words

EOS


### Input and Output

say count_unsorted(@ARGV);


### Implementation

# Compare each word with its sorted bytes, find any divergences and
# count the concerned words.

sub count_unsorted {
    my $w = PDL::Char->new(@_);

    which(($w <=> $w->qsort)->orover)->dim(0);
}


### Examples and tests

sub run_tests {
    SKIP: {
        skip "examples" unless $examples;

        is count_unsorted('abc', 'xyz', 'tsu'), 1, 'example 1';
        is count_unsorted('rat', 'cab', 'dad'), 3, 'example 2';
        is count_unsorted('x', 'y', 'z'), 0, 'example 3';
    }

    SKIP: {
        skip "tests" unless $tests;
	}

    done_testing;
    exit;
}