diff options
| author | Thomas Köhler <jean-luc@picard.franken.de> | 2024-08-19 19:01:47 +0200 |
|---|---|---|
| committer | Thomas Köhler <jean-luc@picard.franken.de> | 2024-08-19 19:01:47 +0200 |
| commit | f2c7f31b9a8deb5f3831ca0111c249ba830cdaab (patch) | |
| tree | 9dcc1592643a2f86fa7663923174d7cf254ca153 | |
| parent | 094015310b8f295a6d758b8f39788958f9687730 (diff) | |
| download | perlweeklychallenge-club-f2c7f31b9a8deb5f3831ca0111c249ba830cdaab.tar.gz perlweeklychallenge-club-f2c7f31b9a8deb5f3831ca0111c249ba830cdaab.tar.bz2 perlweeklychallenge-club-f2c7f31b9a8deb5f3831ca0111c249ba830cdaab.zip | |
Add solution 283
Signed-off-by: Thomas Köhler <jean-luc@picard.franken.de>
| -rw-r--r-- | challenge-283/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-283/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-283/jeanluc2020/perl/ch-1.pl | 61 | ||||
| -rwxr-xr-x | challenge-283/jeanluc2020/perl/ch-2.pl | 61 |
4 files changed, 124 insertions, 0 deletions
diff --git a/challenge-283/jeanluc2020/blog-1.txt b/challenge-283/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..47b29ccd4c --- /dev/null +++ b/challenge-283/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-283-1.html diff --git a/challenge-283/jeanluc2020/blog-2.txt b/challenge-283/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..ce04150f47 --- /dev/null +++ b/challenge-283/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-283-2.html diff --git a/challenge-283/jeanluc2020/perl/ch-1.pl b/challenge-283/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..620d2b4be9 --- /dev/null +++ b/challenge-283/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,61 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-283/#TASK1 +# +# Task 1: Unique Number +# ===================== +# +# You are given an array of integers, @ints, where every elements appears more +# than once except one element. +# +# Write a script to find the one element that appears exactly one time. +# +## Example 1 +## +## Input: @ints = (3, 3, 1) +## Output: 1 +# +## Example 2 +## +## Input: @ints = (3, 2, 4, 2, 4) +## Output: 3 +# +## Example 3 +## +## Input: @ints = (1) +## Output: 1 +# +## Example 4 +## +## Input: @ints = (4, 3, 1, 1, 1, 4) +## Output: 3 +# +############################################################ +## +## discussion +## +############################################################ +# +# We first count how often each integer appears in the array, then +# we just return the first one that appears exactly once as a way +# to short-circuit that part of the execution. + +use strict; +use warnings; + +unique_number(3, 3, 1); +unique_number(3, 2, 4, 2, 4); +unique_number(1); +unique_number(4, 3, 1, 1, 1, 4); + +sub unique_number { + my @ints = @_; + print "Input: (", join(", ", @ints), ")\n"; + my $found; + foreach my $n (@ints) { + $found->{$n}++; + } + foreach my $key (keys %$found) { + return print "Output: $key\n" if $found->{$key} == 1; + } + return "Error: No number found exactly once!\n"; +} diff --git a/challenge-283/jeanluc2020/perl/ch-2.pl b/challenge-283/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..0721e1b3ea --- /dev/null +++ b/challenge-283/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,61 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-283/#TASK2 +# +# Task 2: Digit Count Value +# ========================= +# +# You are given an array of positive integers, @ints. +# +# Write a script to return true if for every index i in the range 0 <= i < size +# of array, the digit i occurs exactly the $ints[$i] times in the given array +# otherwise return false. +# +## Example 1 +## +## Input: @ints = (1, 2, 1, 0) +## Ouput: true +## +## $ints[0] = 1, the digit 0 occurs exactly 1 time. +## $ints[1] = 2, the digit 1 occurs exactly 2 times. +## $ints[2] = 1, the digit 2 occurs exactly 1 time. +## $ints[3] = 0, the digit 3 occurs 0 time. +# +## Example 2 +## +## Input: @ints = (0, 3, 0) +## Ouput: false +## +## $ints[0] = 0, the digit 0 occurs 2 times rather than 0 time. +## $ints[1] = 3, the digit 1 occurs 0 time rather than 3 times. +## $ints[2] = 0, the digit 2 occurs exactly 0 time. +# +############################################################ +## +## discussion +## +############################################################ +# +# This requires two passes: +# 1. count the digits +# 2. for each index of the array, check whether the digit count value +# for this index matches the value in the array. + +use strict; +use warnings; + +digit_count_value(1, 2, 1, 0); +digit_count_value(0, 3, 0); + +sub digit_count_value { + my @ints = @_; + my $digits_counts = {}; + print "Input: (" . join(", ", @ints) . ")\n"; + foreach my $digit (@ints) { + $digits_counts->{$digit}++; + } + foreach my $i (0..$#ints) { + $digits_counts->{$i} //= 0; + return print "Output: false\n" unless $digits_counts->{$i} == $ints[$i]; + } + return print "Output: true\n"; +} |
