blob: 6ae4cc6640cd6080d0b9abf72e84c9230a32436b (
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
|
#!/usr/bin/env perl
# https://theweeklychallenge.org/blog/perl-weekly-challenge-255/#TASK1
#
# Task 1: Odd Character
# =====================
#
# You are given two strings, $s and $t. The string $t is generated using the
# shuffled characters of the string $s with an additional character.
#
# Write a script to find the additional character in the string $t..
#
## Example 1
##
## Input: $s = "Perl" $t = "Preel"
## Output: "e"
#
## Example 2
##
## Input: $s = "Weekly" $t = "Weeakly"
## Output: "a"
#
## Example 3
##
## Input: $s = "Box" $t = "Boxy"
## Output: "y"
#
############################################################
##
## discussion
##
############################################################
#
# Split the word into its character, store them in a hash table, then
# count the result for each character in both the table for the original
# word and for the new word.
use strict;
use warnings;
odd_character("Perl", "Preel");
odd_character("Weekly", "Weeakly");
odd_character("Box", "Boxy");
sub odd_character {
my ($s, $t) = @_;
print "Input: '$s', '$t'\n";
my $s_hash = {};
my $t_hash = {};
foreach my $char (split//,$s) {
$s_hash->{$char}++;
}
foreach my $char (split//,$t) {
$t_hash->{$char}++;
}
foreach my $found (keys %$t_hash) {
$s_hash->{$found} //= 0;
if($t_hash->{$found} > $s_hash->{$found}) {
print "Output: $found\n";
return;
}
}
}
|