aboutsummaryrefslogtreecommitdiff
path: root/challenge-280/athanasius/raku/ch-1.raku
blob: f03a8b68ef5f46746c3813f2830be1aad0ea9606 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use v6d;

################################################################################
=begin comment

Perl Weekly Challenge 280
=========================

TASK #1
-------
*Twice Appearance*

Submitted by: Mohammad Sajid Anwar

You are given a string, $str, containing lowercase English letters only.

Write a script to print the first letter that appears twice.

Example 1

  Input: $str = "acbddbca"
  Output: "d"

Example 2

  Input: $str = "abccd"
  Output: "c"

Example 3

  Input: $str = "abcdabbb"
  Output: "a"

=end comment
################################################################################

#--------------------------------------#
# Copyright © 2024 PerlMonk Athanasius #
#--------------------------------------#

#===============================================================================
=begin comment

Interface
---------
1. If no command-line arguments are given, the test suite is run.
Otherwise:
2. The input string is entered on the command-line.

=end comment
#===============================================================================

use Test;

#-------------------------------------------------------------------------------
BEGIN
#-------------------------------------------------------------------------------
{
    "\nChallenge 280, Task #1: Twice Appearance (Raku)\n".put;
}

#===============================================================================
multi sub MAIN
(
    Str:D $str where / ^ <[ a .. z ]>* $ /     #= A string of lower case letters
)
#===============================================================================
{
    qq[Input:  \$str = "$str"].put;

    my Str $frl = find-first-repeated-letter( $str );

    "Output: %s\n".printf: $frl eq '' ?? '<none>' !! qq["$frl"];
}

#===============================================================================
multi sub MAIN()                                  # No input: run the test suite
#===============================================================================
{
    run-tests();
}

#-------------------------------------------------------------------------------
sub find-first-repeated-letter
(
    Str:D $str where / ^ <[ a .. z ]>* $ /
--> Str:D
)
#-------------------------------------------------------------------------------
{
    my Str  $frl = '';
    my UInt %dict{Str};

    for $str.split( '', :skip-empty ) -> Str $letter
    {
        if ++%dict{ $letter } > 1
        {
            $frl = $letter;
            last;
        }
    }

    return $frl;
}

#-------------------------------------------------------------------------------
sub run-tests()
#-------------------------------------------------------------------------------
{
    'Running the test suite'.put;

    for test-data.lines -> Str $line
    {
        my Str ($test-name, $str, $expected) = $line.split: / \| /;

        for     $test-name, $str, $expected
        {
            s/ ^ \s+   //;
            s/   \s+ $ //;
        }

        my Str $frl = find-first-repeated-letter( $str );

        is $frl, $expected, $test-name;
    }

    done-testing;
}

#-------------------------------------------------------------------------------
sub error( Str:D $message )
#-------------------------------------------------------------------------------
{
    "ERROR: $message".put;

    USAGE();

    exit 0;
}

#-------------------------------------------------------------------------------
sub USAGE()
#-------------------------------------------------------------------------------
{
    my Str $usage = $*USAGE;

    $usage ~~ s:g/ ($*PROGRAM-NAME) /raku $0/;

    $usage.put;
}

#-------------------------------------------------------------------------------
sub test-data( --> Str:D )
#-------------------------------------------------------------------------------
{
    return q:to/END/;
        Example 1 |acbddbca        |d
        Example 2 |abccd           |c
        Example 3 |abcdabbb        |a
        Empty     |                |
        Singletons|abcdefghijklmnop|
        END
}

################################################################################