aboutsummaryrefslogtreecommitdiff
path: root/challenge-074/e-choroba/perl/ch-2.pl
blob: 05055a10610017c3684a26586fc905bec9897341 (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
#! /usr/bin/perl
use warnings;
use strict;

sub fnr_character {
    my ($s) = @_;
    my $r;
    my %count;
    my @order;
    for my $i (0 .. length($s) - 1) {
        my $ch = substr $s, $i, 1;
        my $first;
        if (1 == ++$count{$ch}) {
            unshift @order, $first = $ch;
        } else {
            $first = '#';
            for my $o (@order) {
                $first = $o, last if 1 == $count{$o};
            }
        }
        $r .= $first;
    }
    return $r
}

use Test::More tests => 2;

is fnr_character('ababc'),  'abb#c',  'example 1';
is fnr_character('xyzzyx'), 'xyzyx#', 'example 2';