From bd51393f97bbd1f964490420e0d40352129e7932 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 12 Dec 2022 08:48:22 +0100 Subject: Task 1 done --- challenge-195/luca-ferrari/raku/ch-1.p6 | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 challenge-195/luca-ferrari/raku/ch-1.p6 diff --git a/challenge-195/luca-ferrari/raku/ch-1.p6 b/challenge-195/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..15012afdf2 --- /dev/null +++ b/challenge-195/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,13 @@ +#!raku + +# Perl Weekly Challenge 195 + +sub MAIN( Int $n where { $n > 0 }, Bool :$verbose = False ) { + my @special-integers; + for 1 .. $n { + @special-integers.push: $_ if $_.comb.Bag.values.max <= 1; + } + + @special-integers.join( ',' ).say if ( $verbose ); + @special-integers.elems.say; +} -- cgit From 77d6f3b11a9014e30e505808ab9a24a40ca3873f Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 12 Dec 2022 08:54:02 +0100 Subject: Task 2 done --- challenge-195/luca-ferrari/raku/ch-2.p6 | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 challenge-195/luca-ferrari/raku/ch-2.p6 diff --git a/challenge-195/luca-ferrari/raku/ch-2.p6 b/challenge-195/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..707f429ed1 --- /dev/null +++ b/challenge-195/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,15 @@ +#!raku + +# Perl Weekly Challenge 195 + +sub MAIN( *@list where { @list.grep( * ~~ Int ).elems == @list.elems } ) { + my $bag = @list.grep( * %% 2 ).Bag; + my $most-frequency = $bag.values.max; + my @most-frequent-evens; + for $bag.keys { + next if $bag{ $_ } != $most-frequency; + @most-frequent-evens.push: $_; + } + + @most-frequent-evens.min.say; +} -- cgit From 786befb39e7048cf385e1b9c5c37a24238e05334 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 12 Dec 2022 09:11:27 +0100 Subject: Task 1 PL/Perl done --- challenge-195/luca-ferrari/postgresql/ch-1.plperl | 40 +++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 challenge-195/luca-ferrari/postgresql/ch-1.plperl diff --git a/challenge-195/luca-ferrari/postgresql/ch-1.plperl b/challenge-195/luca-ferrari/postgresql/ch-1.plperl new file mode 100644 index 0000000000..3ea8594add --- /dev/null +++ b/challenge-195/luca-ferrari/postgresql/ch-1.plperl @@ -0,0 +1,40 @@ +-- Perl Weekly Challenge 195 +-- Task 1 + +CREATE SCHEMA IF NOT EXISTS pwc195; + +CREATE OR REPLACE FUNCTION +pwc195.task1_plperl( int, bool default true ) +RETURNS int +AS $CODE$ + my ( $n, $verbose ) = @_; + my @special_integers; + + my $baggify = sub { + my ( $n ) = @_; + my $bag = {}; + for my $digit ( split '', $n ) { + $bag->{ $digit }++; + } + + return $bag; + }; + + my $has_no_repetitions = sub { + my ( $bag ) = @_; + for ( keys $bag->%* ) { + return 0 if $bag->{ $_ } != 1; + } + + return 1; + }; + + for ( 1 .. $n ) { + push @special_integers, $_ if ( $has_no_repetitions->( $baggify->( $_ ) ) ); + } + + elog( INFO, "Found: " . join( ',', @special_integers ) ) if ( $ verbose ); + return scalar @special_integers; + +$CODE$ +LANGUAGE plperl; -- cgit From f3d7ddcb540bb7ab77ff433fc94cb738ee38cf39 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 12 Dec 2022 09:26:40 +0100 Subject: Task 2 plperl done --- challenge-195/luca-ferrari/postgresql/ch-2.plperl | 29 +++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 challenge-195/luca-ferrari/postgresql/ch-2.plperl diff --git a/challenge-195/luca-ferrari/postgresql/ch-2.plperl b/challenge-195/luca-ferrari/postgresql/ch-2.plperl new file mode 100644 index 0000000000..b15f6b215b --- /dev/null +++ b/challenge-195/luca-ferrari/postgresql/ch-2.plperl @@ -0,0 +1,29 @@ +-- Perl Weekly Challenge 195 +-- Task 2 + +CREATE SCHEMA IF NOT EXISTS pwc195; + +CREATE OR REPLACE FUNCTION +pwc195.task2_plperl( int[] ) +RETURNS int +AS $CODE$ + my ( $array ) = @_; + # extract only evens + my @evens = grep { $_ % 2 == 0 } $array->@*; + # classify frequency + my $bag = {}; + $bag->{ $_ }++ for ( @evens ); + + + # sort by frequency and value + my @sorted_bag = + map { $_->[1] } + sort { $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] } + map { [ $bag->{ $_ }, $_ ] } keys $bag->%*; + + + # the first value in the list is the + # one with the max frequency and the lowest value + return $sorted_bag[ 0 ]; +$CODE$ +LANGUAGE plperl; -- cgit From 733a94071f895afa1fe066133eb6010245bc4031 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 12 Dec 2022 09:38:10 +0100 Subject: Task 1 plpgsql done --- challenge-195/luca-ferrari/postgresql/ch-1.sql | 30 ++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 challenge-195/luca-ferrari/postgresql/ch-1.sql diff --git a/challenge-195/luca-ferrari/postgresql/ch-1.sql b/challenge-195/luca-ferrari/postgresql/ch-1.sql new file mode 100644 index 0000000000..a736df90b1 --- /dev/null +++ b/challenge-195/luca-ferrari/postgresql/ch-1.sql @@ -0,0 +1,30 @@ +-- Perl Weekly Challenge 195 +-- Task 1 + +CREATE SCHEMA IF NOT EXISTS pwc195; + +CREATE OR REPLACE FUNCTION +pwc195.task1_plpgsql( n int ) +RETURNS int +AS $CODE$ +DECLARE + i int; + freq int; + counter int := 0; +BEGIN + FOR i IN 1 .. n LOOP + SELECT count(*) + INTO freq + FROM regexp_split_to_table( i::text, '' ) as n(d) + GROUP BY d; + + IF freq = 1 THEN + counter := counter + 1; + END IF; + + END LOOP; + + RETURN counter; +END +$CODE$ +LANGUAGE plpgsql; -- cgit From e14d4da9dddbf81d1e0cb9ba83a3b95c6584b6dc Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 12 Dec 2022 09:43:34 +0100 Subject: Task 2 plpgsql done --- challenge-195/luca-ferrari/postgresql/ch-2.sql | 33 ++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 challenge-195/luca-ferrari/postgresql/ch-2.sql diff --git a/challenge-195/luca-ferrari/postgresql/ch-2.sql b/challenge-195/luca-ferrari/postgresql/ch-2.sql new file mode 100644 index 0000000000..c6b8cf318a --- /dev/null +++ b/challenge-195/luca-ferrari/postgresql/ch-2.sql @@ -0,0 +1,33 @@ +-- Perl Weekly Challenge 195 +-- Task 2 + +CREATE SCHEMA IF NOT EXISTS pwc195; + +CREATE OR REPLACE FUNCTION +pwc195.task2_plpgsql( list int[] ) +RETURNS int +AS $CODE$ +DECLARE + current int; +BEGIN + CREATE TEMPORARY TABLE IF NOT EXISTS nums( v int, f int default 1, primary key( v ) ); + TRUNCATE TABLE nums; + + FOREACH current IN ARRAY list LOOP + INSERT INTO nums AS frequency + SELECT current, 1 + ON CONFLICT (v) + DO UPDATE SET f = frequency.f + 1; + END LOOP; + + SELECT v + INTO current + FROM nums + WHERE v % 2 = 0 + ORDER BY f DESC, v ASC + LIMIT 1; + + RETURN current; +END +$CODE$ +LANGUAGE plpgsql; -- cgit From cecf189f37fcf86a24862bf40283975e73392ca9 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 12 Dec 2022 10:33:18 +0100 Subject: plpgsql fixes --- challenge-195/luca-ferrari/postgresql/ch-1.sql | 3 ++- challenge-195/luca-ferrari/postgresql/ch-2.sql | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/challenge-195/luca-ferrari/postgresql/ch-1.sql b/challenge-195/luca-ferrari/postgresql/ch-1.sql index a736df90b1..c6cbc28437 100644 --- a/challenge-195/luca-ferrari/postgresql/ch-1.sql +++ b/challenge-195/luca-ferrari/postgresql/ch-1.sql @@ -16,7 +16,8 @@ BEGIN SELECT count(*) INTO freq FROM regexp_split_to_table( i::text, '' ) as n(d) - GROUP BY d; + GROUP BY d + ORDER BY 1 DESC; IF freq = 1 THEN counter := counter + 1; diff --git a/challenge-195/luca-ferrari/postgresql/ch-2.sql b/challenge-195/luca-ferrari/postgresql/ch-2.sql index c6b8cf318a..633ffc8c4f 100644 --- a/challenge-195/luca-ferrari/postgresql/ch-2.sql +++ b/challenge-195/luca-ferrari/postgresql/ch-2.sql @@ -3,6 +3,15 @@ CREATE SCHEMA IF NOT EXISTS pwc195; +/* +testdb=> select pwc195.task2_plpgsql( array[1,2,2,3,4,4,4,5,6,6,6,6]::int[] ); +NOTICE: relation "nums" already exists, skipping + task2_plpgsql +--------------- + 6 + +*/ + CREATE OR REPLACE FUNCTION pwc195.task2_plpgsql( list int[] ) RETURNS int -- cgit From e9c10736a2e8811abb8e8cd8bd787071131fceb2 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 12 Dec 2022 10:37:59 +0100 Subject: Blog references --- challenge-195/luca-ferrari/blog-1.txt | 1 + challenge-195/luca-ferrari/blog-2.txt | 1 + challenge-195/luca-ferrari/blog-3.txt | 1 + challenge-195/luca-ferrari/blog-4.txt | 1 + challenge-195/luca-ferrari/blog-5.txt | 1 + challenge-195/luca-ferrari/blog-6.txt | 1 + 6 files changed, 6 insertions(+) create mode 100644 challenge-195/luca-ferrari/blog-1.txt create mode 100644 challenge-195/luca-ferrari/blog-2.txt create mode 100644 challenge-195/luca-ferrari/blog-3.txt create mode 100644 challenge-195/luca-ferrari/blog-4.txt create mode 100644 challenge-195/luca-ferrari/blog-5.txt create mode 100644 challenge-195/luca-ferrari/blog-6.txt diff --git a/challenge-195/luca-ferrari/blog-1.txt b/challenge-195/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..2afdd83ab8 --- /dev/null +++ b/challenge-195/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/12/12/PerlWeeklyChallenge195.html#task1 diff --git a/challenge-195/luca-ferrari/blog-2.txt b/challenge-195/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..124f33a97c --- /dev/null +++ b/challenge-195/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/12/12/PerlWeeklyChallenge195.html#task2 diff --git a/challenge-195/luca-ferrari/blog-3.txt b/challenge-195/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..fb3572a632 --- /dev/null +++ b/challenge-195/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/12/12/PerlWeeklyChallenge195.html#task1plperl diff --git a/challenge-195/luca-ferrari/blog-4.txt b/challenge-195/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..d8193434ac --- /dev/null +++ b/challenge-195/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/12/12/PerlWeeklyChallenge195.html#task2plperl diff --git a/challenge-195/luca-ferrari/blog-5.txt b/challenge-195/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..1864572ac2 --- /dev/null +++ b/challenge-195/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/12/12/PerlWeeklyChallenge195.html#task1plpgsql diff --git a/challenge-195/luca-ferrari/blog-6.txt b/challenge-195/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..bc5e6b6762 --- /dev/null +++ b/challenge-195/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/12/12/PerlWeeklyChallenge195.html#task2plpgsql -- cgit From 2eb11689346f8bead46931a658ccd30bdd52c921 Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Mon, 12 Dec 2022 13:03:23 -0500 Subject: jacoby finishes Ch 195 --- challenge-195/dave-jacoby/perl/ch-1.pl | 26 ++++++++++++++++++++++++++ challenge-195/dave-jacoby/perl/ch-2.pl | 29 +++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 challenge-195/dave-jacoby/perl/ch-1.pl create mode 100644 challenge-195/dave-jacoby/perl/ch-2.pl diff --git a/challenge-195/dave-jacoby/perl/ch-1.pl b/challenge-195/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..189613873e --- /dev/null +++ b/challenge-195/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,26 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ fc say postderef signatures state }; + +my @examples = ( 15, 35 ); +for my $n (@examples) { + my $o = special_list($n); + say <<"END"; + Input: \$n = $n + Output: $o +END +} + +sub special_list ( $n ) { + return scalar grep { is_special($_) } 1 .. $n; +} + +sub is_special ( $n ) { + my %hash; + for my $i ( split //, $n ) { + return 0 if ++$hash{$i} > 1; + } + return 1; +} diff --git a/challenge-195/dave-jacoby/perl/ch-2.pl b/challenge-195/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..9a704a2c63 --- /dev/null +++ b/challenge-195/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,29 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use List::Util qw{ min max }; + +my @examples = ( [ 1, 1, 2, 6, 2 ], [ 1, 3, 5, 7 ], [ 6, 4, 4, 6, 1 ] ); + +for my $e (@examples) { + my $list = join ',', $e->@*; + my $o = most_frequent_even( $e->@* ); + say <<"END"; + Input: \@list = $list + Output: $o +END +} + +sub most_frequent_even( @list ) { + my %hash; + map { $hash{$_}++ } grep { 0 == $_ % 2 } @list; + if ( scalar keys %hash ) { + my $max = max values %hash; + return min grep { $hash{$_} == $max } keys %hash; + } + return -1; +} + -- cgit From 5f5c979bb278b989fbff1473ed44aefd2861cba9 Mon Sep 17 00:00:00 2001 From: David Ferrone Date: Mon, 12 Dec 2022 13:12:35 -0500 Subject: week 195 --- challenge-195/zapwai/perl/ch-1.pl | 28 ++++++++++++++++++++++++++++ challenge-195/zapwai/perl/ch-2.pl | 27 +++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100755 challenge-195/zapwai/perl/ch-1.pl create mode 100755 challenge-195/zapwai/perl/ch-2.pl diff --git a/challenge-195/zapwai/perl/ch-1.pl b/challenge-195/zapwai/perl/ch-1.pl new file mode 100755 index 0000000000..606dbdd0d1 --- /dev/null +++ b/challenge-195/zapwai/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/bin/env perl +use feature 'say'; +my $n = $ARGV[0] || 35; +my @unspecial; # An integer is special when all of its digits are unique. +my $count = 0; +foreach (1 .. $n) { + if (is_special($_)) { + $count++; + } else { + push @unspecial, $_; + } +} +sub is_special() { + my $num = shift; + my @digits = split //, $num; + my @freq; + for (0 .. 9) { + foreach my $d (@digits) { + $freq[$_]++ if ($d == $_); + } + } + for (0 .. 9) { + return 0 if ($freq[$_] > 1); + } + return 1; +} +say "Input: \$n = $n"; +say "Output: $count as except for {@unspecial} all others are special."; diff --git a/challenge-195/zapwai/perl/ch-2.pl b/challenge-195/zapwai/perl/ch-2.pl new file mode 100755 index 0000000000..590dc00aa0 --- /dev/null +++ b/challenge-195/zapwai/perl/ch-2.pl @@ -0,0 +1,27 @@ +#!/usr/bin/env perl +my @list = (1,1,4,2,6); +my @freq; +my $ans; +foreach my $num (@list) { + next if ($num % 2 == 1); + $freq[$num]++; +} +if (!@freq) { + $ans = -1; +} +my $max_freq; +for (1 .. $#freq/2) { + my $i = 2*$_; + if ($max_freq < $freq[$i]) { + $max_freq = $freq[$i] + } +} +for my $num (1 .. $#freq/2) { + my $i = 2*$num; + if ( $freq[$i] == $max_freq ) { + $ans = $i; + last; + } +} +print "Input: \@list = @list\n"; +print "Output: $ans\n"; -- cgit From f863f9659a7a0209ba8c2ef2b9f052cc422471ee Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 12 Dec 2022 20:37:20 +0000 Subject: Challenge 195 Solutions (Raku) --- challenge-195/mark-anderson/raku/ch-1.raku | 18 ++++++++++++++++++ challenge-195/mark-anderson/raku/ch-2.raku | 11 +++++++++++ 2 files changed, 29 insertions(+) create mode 100644 challenge-195/mark-anderson/raku/ch-1.raku create mode 100644 challenge-195/mark-anderson/raku/ch-2.raku diff --git a/challenge-195/mark-anderson/raku/ch-1.raku b/challenge-195/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..32539e82ae --- /dev/null +++ b/challenge-195/mark-anderson/raku/ch-1.raku @@ -0,0 +1,18 @@ +#!/usr/bin/env raku +use Test; + +is special-integers(15), 14; +is special-integers(35), 32; +is special-integers(9_999_999), 712890; + +sub special-integers($n) +{ + .elems given gather for (^10).combinations(1..$n.chars) + { + for .permutations + { + next unless .head; + .take unless $_ > $n given .join + } + } +} diff --git a/challenge-195/mark-anderson/raku/ch-2.raku b/challenge-195/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..574f857543 --- /dev/null +++ b/challenge-195/mark-anderson/raku/ch-2.raku @@ -0,0 +1,11 @@ +#!/usr/bin/env raku +use Test; + +is most-frequent-even(1,1,2,6,2), 2; +is most-frequent-even(1,3,5,7), Inf; +is most-frequent-even(6,4,4,6,1), 4; + +sub most-frequent-even(*@list) +{ + @list.grep(* %% 2).Bag.maxpairs>>.key.min +} -- cgit From ffc4d8685185f4c0a80863f6337ac5ccbf9ef535 Mon Sep 17 00:00:00 2001 From: Thomas Köhler Date: Mon, 12 Dec 2022 22:12:31 +0100 Subject: Add jeanluc2020's solution to challenge 195. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Köhler --- challenge-195/jeanluc2020/perl/ch-1.pl | 31 +++++++++++++++++++++++++++++++ challenge-195/jeanluc2020/perl/ch-2.pl | 30 ++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100755 challenge-195/jeanluc2020/perl/ch-1.pl create mode 100755 challenge-195/jeanluc2020/perl/ch-2.pl diff --git a/challenge-195/jeanluc2020/perl/ch-1.pl b/challenge-195/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..79573aa3fc --- /dev/null +++ b/challenge-195/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,31 @@ +#!/usr/bin/perl +use strict; +use warnings; + +foreach my $input (1..40) { + print "Input $input, output " . count_specials($input) . "\n"; +} + +sub count_specials { + my $number = shift; + my $specials = 0; + foreach my $i (1..$number) { + $specials += is_special($i); + } + return $specials; +} + +sub is_special { + my $number = shift; + my $map; + # count all digits by adding 1 to the corresponding key in %$map for each digit + # the count is the value + map { $map->{$_}++ } split //, $number; + # sort values descending + my @values = reverse sort { $a <=> $b } values %$map; + # if the first element is 1, the number is special + if($values[0] == 1) { + return 1; + } + return 0; +} diff --git a/challenge-195/jeanluc2020/perl/ch-2.pl b/challenge-195/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..ec92ca072b --- /dev/null +++ b/challenge-195/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,30 @@ +#!/usr/bin/perl + +my @list = (1,1,2,6,2); +print "(" . join(",", @list) . ") => " . smallest_even_integer(@list) . "\n"; +@list = (1,3,5,7); +print "(" . join(",", @list) . ") => " . smallest_even_integer(@list) . "\n"; +@list = (6,4,4,6,1); +print "(" . join(",", @list) . ") => " . smallest_even_integer(@list) . "\n"; + +sub smallest_even_integer { + # we need only even numbers + my @list = only_even(@_); + return -1 unless @list; # no even numbers found + my $map; + # count each number in %$map, key is the number, value its count + map { $map->{$_}++ } @list; + # sort @list by the count for each number descending + # if that's equal sort by the number ascending + my @sorted = sort { $map->{$b} <=> $map->{$a} || $a <=> $b } @list; + # solution is in the first element of the sorted array + return $sorted[0]; +} + +sub only_even { + my @result = (); + foreach my $elem (@_) { + push @result,$elem unless $elem % 2; + } + return @result; +} -- cgit From 6930d0c30b8c5cea8a81cfd809fbbd1894ad2148 Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Mon, 12 Dec 2022 21:23:46 +0000 Subject: Solutions for challenge #195 --- challenge-195/roger-bell-west/javascript/ch-1.js | 37 +++++ challenge-195/roger-bell-west/javascript/ch-2.js | 45 ++++++ challenge-195/roger-bell-west/kotlin/ch-1.kt | 30 ++++ challenge-195/roger-bell-west/kotlin/ch-2.kt | 38 +++++ challenge-195/roger-bell-west/lua/ch-1.lua | 34 +++++ challenge-195/roger-bell-west/lua/ch-2.lua | 51 +++++++ challenge-195/roger-bell-west/perl/ch-1.pl | 26 ++++ challenge-195/roger-bell-west/perl/ch-2.pl | 30 ++++ challenge-195/roger-bell-west/postscript/ch-1.ps | 66 +++++++++ challenge-195/roger-bell-west/postscript/ch-2.ps | 180 +++++++++++++++++++++++ challenge-195/roger-bell-west/python/ch-1.py | 28 ++++ challenge-195/roger-bell-west/python/ch-2.py | 30 ++++ challenge-195/roger-bell-west/raku/ch-1.p6 | 22 +++ challenge-195/roger-bell-west/raku/ch-2.p6 | 26 ++++ challenge-195/roger-bell-west/ruby/ch-1.rb | 29 ++++ challenge-195/roger-bell-west/ruby/ch-2.rb | 34 +++++ challenge-195/roger-bell-west/rust/ch-1.rs | 34 +++++ challenge-195/roger-bell-west/rust/ch-2.rs | 38 +++++ 18 files changed, 778 insertions(+) create mode 100755 challenge-195/roger-bell-west/javascript/ch-1.js create mode 100755 challenge-195/roger-bell-west/javascript/ch-2.js create mode 100644 challenge-195/roger-bell-west/kotlin/ch-1.kt create mode 100644 challenge-195/roger-bell-west/kotlin/ch-2.kt create mode 100755 challenge-195/roger-bell-west/lua/ch-1.lua create mode 100755 challenge-195/roger-bell-west/lua/ch-2.lua create mode 100755 challenge-195/roger-bell-west/perl/ch-1.pl create mode 100755 challenge-195/roger-bell-west/perl/ch-2.pl create mode 100644 challenge-195/roger-bell-west/postscript/ch-1.ps create mode 100644 challenge-195/roger-bell-west/postscript/ch-2.ps create mode 100755 challenge-195/roger-bell-west/python/ch-1.py create mode 100755 challenge-195/roger-bell-west/python/ch-2.py create mode 100755 challenge-195/roger-bell-west/raku/ch-1.p6 create mode 100755 challenge-195/roger-bell-west/raku/ch-2.p6 create mode 100755 challenge-195/roger-bell-west/ruby/ch-1.rb create mode 100755 challenge-195/roger-bell-west/ruby/ch-2.rb create mode 100755 challenge-195/roger-bell-west/rust/ch-1.rs create mode 100755 challenge-195/roger-bell-west/rust/ch-2.rs diff --git a/challenge-195/roger-bell-west/javascript/ch-1.js b/challenge-195/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..728340ef1c --- /dev/null +++ b/challenge-195/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,37 @@ +#! /usr/bin/node + +"use strict" + +function specialintegers(n) { + let o = 0; + for (let i = 1; i <= n; i++) { + let f = new Set(); + let s = true; + for (let c of i.toString()) { + if (f.has(c)) { + s = false; + break; + } else { + f.add(c); + } + } + if (s == 1) { + o++; + } + } + return o; +} + +if (specialintegers(15) == 14) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); + +if (specialintegers(35) == 32) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-195/roger-bell-west/javascript/ch-2.js b/challenge-195/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..0fdfce7898 --- /dev/null +++ b/challenge-195/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,45 @@ +#! /usr/bin/node + +"use strict" + +function mostfrequenteven(l) { + let f = new Map(); + for (let n of l) { + if (n % 2 == 0) { + if (f.has(n)) { + f.set(n, f.get(n) + 1); + } else { + f.set(n, 1); + } + } + } + if (f.size > 0) { + let m = Math.max(...f.values()); + let l = Array.from(f.keys()).filter(i => f.get(i) == m); + l.sort(); + return l[0]; + } else { + return -1; + } +} + +if (mostfrequenteven([1, 1, 2, 6, 2]) == 2) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); + +if (mostfrequenteven([1, 3, 5, 7]) == -1) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); + +if (mostfrequenteven([6, 4, 4, 6, 1]) == 4) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-195/roger-bell-west/kotlin/ch-1.kt b/challenge-195/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..205f2994ac --- /dev/null +++ b/challenge-195/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,30 @@ +fun specialintegers(n: Int): Int { + var o = 0 + for (i in 1..n) { + var f = mutableMapOf() + for (c in i.toString()) { + var l = f.getOrDefault(c, 0) + f.set(c, l + 1) + } + if (f.values.maxOrNull()!! == 1) { + o += 1 + } + } + return o +} + +fun main() { + if (specialintegers(15) == 14) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + + if (specialintegers(35) == 32) { + print("Pass") + } else { + print("FAIL") + } + println("") +} diff --git a/challenge-195/roger-bell-west/kotlin/ch-2.kt b/challenge-195/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..14d82ddc2a --- /dev/null +++ b/challenge-195/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,38 @@ +fun mostfrequenteven(l: List): Int { + var f = mutableMapOf() + for (n in l) { + if (n % 2 == 0) { + var k = f.getOrDefault(n, 0); + f.set(n, k + 1); + } + } + if (f.size > 0) { + val m = f.values.maxOrNull()!! + var ll = ArrayList(f.keys.filter{f[it] == m}) + ll.sort() + return ll[0] + } else { + return -1 + } +} + +fun main() { + if (mostfrequenteven(listOf(1, 1, 2, 6, 2)) == 2) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + if (mostfrequenteven(listOf(1, 3, 5, 7)) == -1) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + if (mostfrequenteven(listOf(6, 4, 4, 6, 1)) == 4) { + print("Pass") + } else { + print("FAIL") + } + println("") +} diff --git a/challenge-195/roger-bell-west/lua/ch-1.lua b/challenge-195/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..598da06a7b --- /dev/null +++ b/challenge-195/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,34 @@ +#! /usr/bin/lua + +function specialintegers(n) + local o = 0 + for i = 1, n do + local f = {} + local s = true + for c in string.gmatch(i, ".") do + if f[c] == nil then + f[c] = 1 + else + s = false + break + end + end + if s then + o = o + 1 + end + end + return o +end + +if specialintegers(15) == 14 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") +if specialintegers(35) == 32 then + io.write("Pass") +else + io.write("FAIL") +end +print("") diff --git a/challenge-195/roger-bell-west/lua/ch-2.lua b/challenge-195/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..f661eb83d5 --- /dev/null +++ b/challenge-195/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,51 @@ +#! /usr/bin/lua + +function mostfrequenteven(l) + local f = {} + local mx + for i, n in ipairs(l) do + if n % 2 == 0 then + if f[n] == nil then + f[n] = 1 + else + f[n] = f[n]+1 + end + if mx == nil or f[n] > mx then + mx = f[n] + end + end + end + if mx ~= nil then + local l = {} + for k, v in pairs(f) do + if v == mx then + table.insert(l, k) + end + end + table.sort(l) + return l[1] + else + return -1 + end +end + +if mostfrequenteven({1, 1, 2, 6, 2}) == 2 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if mostfrequenteven({1, 3, 5, 7}) == -1 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if mostfrequenteven({6, 4, 4, 6, 1}) == 4 then + io.write("Pass") +else + io.write("FAIL") +end +print("") diff --git a/challenge-195/roger-bell-west/perl/ch-1.pl b/challenge-195/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..92f77b56ad --- /dev/null +++ b/challenge-195/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,26 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 2; + +is(specialintegers(15), 14, 'example 1'); +is(specialintegers(35), 32, 'example 2'); + +use List::Util qw(max); + +sub specialintegers($n) { + my $o = 0; + foreach my $i (1..$n) { + my %f; + foreach my $c (split('', $i)) { + $f{$c}++; + } + if (max(values %f) == 1) { + $o++; + } + } + return $o; +} diff --git a/challenge-195/roger-bell-west/perl/ch-2.pl b/challenge-195/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..6c5d99c7f7 --- /dev/null +++ b/challenge-195/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,30 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is(mostfrequenteven([1, 1, 2, 6, 2]), 2, 'example 1'); +is(mostfrequenteven([1, 3, 5, 7]), -1, 'example 2'); +is(mostfrequenteven([6, 4, 4, 6, 1]), 4, 'example 3'); + +use List::Util qw(max); + +sub mostfrequenteven($l) { + my %f; + foreach my $n (@{$l}) { + if ($n % 2 == 0) { + $f{$n}++; + } + } + if (scalar %f > 0) { + my $m = max(values %f); + my @l = grep {$f{$_} == $m} keys %f; + @l = sort @l; + return $l[0]; + } else { + return -1; + } +} diff --git a/challenge-195/roger-bell-west/postscript/ch-1.ps b/challenge-195/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..26f4c3669f --- /dev/null +++ b/challenge-195/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,66 @@ +%!PS + +% begin included library code +% see https://github.com/Firedrake/postscript-libraries/ +/test { + /test.count test.count 1 add def + { + /test.pass test.pass 1 add def + } { + ( ) print + test.count (....) cvs print + (-fail) print + } ifelse +} bind def + +/test.end { + ( ) print + test.count 0 gt { + (Passed ) print + test.pass (...) cvs print + (/) print + test.count (...) cvs print + ( \() print + test.pass 100 mul test.count idiv (...) cvs print + (%\)) print + (\r\n) print + } if +} bind def + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + + +% end included library code + +/specialintegers { + 4 dict begin + /n exch def + /o 0 def + 1 1 n { + /f 0 dict def + /s true def + dup log cvi 1 add string cvs { + /c exch def + f c known { + /s false def + exit + } { + f c true put + } ifelse + } forall + s { + /o o 1 add def + } if + } for + o + end +} bind def + +(specialintegers) test.start +15 specialintegers 14 eq test +35 specialintegers 32 eq test +test.end diff --git a/challenge-195/roger-bell-west/postscript/ch-2.ps b/challenge-195/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..8324605895 --- /dev/null +++ b/challenge-195/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,180 @@ +%!PS + +% begin included library code +% see https://github.com/Firedrake/postscript-libraries/ +/quicksort.partition { + 3 dict begin + /pivot arr hi lo add 2 idiv get def + /i lo 1 sub def + /j hi 1 add def + { + { + /i i 1 add def + arr i get pivot ge { + exit + } if + } loop + { + /j j 1 sub def + arr j get pivot le { + exit + } if + } loop + i j ge { + j + exit + } if + i j quicksort.swap + } loop + end +} bind def + +/test.end { + ( ) print + test.count 0 gt { + (Passed ) print + test.pass (...) cvs print + (/) print + test.count (...) cvs print + ( \() print + test.pass 100 mul test.count idiv (...) cvs print + (%\)) print + (\r\n) print + } if +} bind def + +/quicksort { % [ a c b ] -> [ a b c ] + 1 dict begin + /arr exch def + arr length 0 gt { + 0 arr length 1 sub quicksort.main + } if + arr + end +} bind def + +/test { + /test.count test.count 1 add def + { + /test.pass test.pass 1 add def + } { + ( ) print + test.count (....) cvs print + (-fail) print + } ifelse +} bind def + +/quicksort.main { % lo hi -> (null) + 3 dict begin + /hi exch def + /lo exch def + /xit false def + lo 0 lt { + /xit true def + } if + hi 0 lt { + /xit true def + } if + lo hi ge { + /xit true def + } if + xit not { + /p quicksort.partition def + lo p quicksort.main + p 1 add hi quicksort.main + } if + end +} bind def + +/quicksort.swap { + 2 dict begin + /bi exch def + /ai exch def + arr ai get + arr bi get + arr exch ai exch put + arr exch bi exch put + end +} bind def + +/reduce { % array proc -> value + 2 dict begin + /p exch def + /a exch def + a 0 get + 1 1 a length 1 sub { + a exch get + p + } for + end +} bind def + +/listmax { + { max } reduce +} bind def + +/keys { % dict -> array of dict keys + [ exch + { + pop + } forall + ] +} bind def + +/values { % dict -> array of dict values + [ exch + { + exch pop + } forall + ] +} bind def + +/filter { % array proc(bool) -> array + 1 dict begin + /p exch def + [ exch + { + dup p not + { + pop + } if + } forall + ] + end +} bind def + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + + +% end included library code + +/mostfrequenteven { + 2 dict begin + /l exch def + /f 0 dict def + l { 2 mod 0 eq } filter { + dup f exch known { + dup f exch get 1 add f 3 1 roll put + } { + f exch 1 put + } ifelse + } forall + f length 0 gt { + /m f values listmax def + f keys { f exch get m eq } filter + quicksort 0 get + } { + -1 + } ifelse + end +} bind def + +(mostfrequenteven) test.start +[ 1 1 2 6 2 ] mostfrequenteven 2 eq test +[ 1 3 5 7 ] mostfrequenteven -1 eq test +[ 6 4 4 6 1 ] mostfrequenteven 4 eq test +test.end diff --git a/challenge-195/roger-bell-west/python/ch-1.py b/challenge-195/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..d246c2b66d --- /dev/null +++ b/challenge-195/roger-bell-west/python/ch-1.py @@ -0,0 +1,28 @@ +#! /usr/bin/python3 + +import unittest + +def specialintegers(n): + o = 0 + for i in range(1, n+1): + s = True + f = set() + for c in str(i): + if c in f: + s = False + break + else: + f.add(c) + if s: + o += 1 + return o + +class TestSpecialIntegers(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(specialintegers(15), 14, 'example 1') + + def test_ex2(self): + self.assertEqual(specialintegers(35), 32, 'example 2') + +unittest.main() diff --git a/challenge-195/roger-bell-west/python/ch-2.py b/challenge-195/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..d5f1259e14 --- /dev/null +++ b/challenge-195/roger-bell-west/python/ch-2.py @@ -0,0 +1,30 @@ +#! /usr/bin/python3 + +import unittest +from collections import defaultdict + +def mostfrequenteven(l): + f = defaultdict(lambda: 0) + for n in l: + if n % 2 == 0: + f[n] += 1 + if len(f) > 0: + m = max(f.values()) + l = [i for i in f.keys() if f[i] == m] + l.sort() + return l[0] + else: + return -1 + +class TestMostfrequenteven(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(mostfrequenteven([1, 1, 2, 6, 2]), 2, 'example 1') + + def test_ex2(self): + self.assertEqual(mostfrequenteven([1, 3, 5, 7]), -1, 'example 2') + + def test_ex3(self): + self.assertEqual(mostfrequenteven([6, 4, 4, 6, 1]), 4, 'example 3') + +unittest.main() diff --git a/challenge-195/roger-bell-west/raku/ch-1.p6 b/challenge-195/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..192c1803d1 --- /dev/null +++ b/challenge-195/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,22 @@ +#! /usr/bin/perl6 + +use Test; + +plan 2; + +is(specialintegers(15), 14, 'example 1'); +is(specialintegers(35), 32, 'example 2'); + +sub specialintegers($n) { + my $o = 0; + for (1..$n) -> $i { + my %f; + for $i.Str.comb -> $c { + %f{$c}++; + } + if (%f.values.max == 1) { + $o++; + } + } + return $o; +} diff --git a/challenge-195/roger-bell-west/raku/ch-2.p6 b/challenge-195/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..c3370ef9d6 --- /dev/null +++ b/challenge-195/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,26 @@ +#! /usr/bin/perl6 + +use Test; + +plan 3; + +is(mostfrequenteven([1, 1, 2, 6, 2]), 2, 'example 1'); +is(mostfrequenteven([1, 3, 5, 7]), -1, 'example 2'); +is(mostfrequenteven([6, 4, 4, 6, 1]), 4, 'example 3'); + +sub mostfrequenteven(@l) { + my %f; + for @l -> $n { + if ($n % 2 == 0) { + %f{$n}++; + } + } + if (%f.elems > 0) { + my $m = %f.values.max; + my @l = grep {%f{$_} == $m}, %f.keys; + @l = @l.sort; + return @l[0]; + } else { + return -1; + } +} diff --git a/challenge-195/roger-bell-west/ruby/ch-1.rb b/challenge-195/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..2adacafc59 --- /dev/null +++ b/challenge-195/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,29 @@ +#! /usr/bin/ruby + +require 'test/unit' + +def specialintegers(n) + o = 0 + 1.upto(n) do |i| + f = Hash.new(0) + for c in i.to_s.chars do + f[c] += 1 + end + if f.values.max == 1 then + o += 1 + end + end + return o +end + +class TestSpecialintegers < Test::Unit::TestCase + + def test_ex1 + assert_equal(14, specialintegers(15)) + end + + def test_ex2 + assert_equal(32, specialintegers(35)) + end + +end diff --git a/challenge-195/roger-bell-west/ruby/ch-2.rb b/challenge-195/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..bd9dba8347 --- /dev/null +++ b/challenge-195/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,34 @@ +#! /usr/bin/ruby + +require 'test/unit' + +def mostfrequenteven(l) + f = Hash.new(0) + l.each do |n| + if n % 2 == 0 then + f[n] += 1 + end + end + if f.length > 0 then + m = f.values.max + l = f.keys.find_all { |x| f[x] == m}.sort + return l[0] + else + return -1 + end +end + +class TestMostFrequentEven < Test::Unit::TestCase + + def test_ex1 + assert_equal(2, mostfrequenteven([1, 1, 2, 6, 2])) + end + + def test_ex2 + assert_equal(-1, mostfrequenteven([1, 3, 5, 7])) + end + + def test_ex3 + assert_equal(4, mostfrequenteven([6, 4, 4, 6, 1])) + end +end diff --git a/challenge-195/roger-bell-west/rust/ch-1.rs b/challenge-195/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..ff432e7607 --- /dev/null +++ b/challenge-195/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,34 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x --nocapture; rm -f ${0}x ; exit + +use std::collections::HashSet; + +#[test] +fn test_ex1() { + assert_eq!(specialintegers(15), 14); +} + +#[test] +fn test_ex2() { + assert_eq!(specialintegers(35), 32); +} + +fn specialintegers(n: usize) -> usize { + let mut o = 0; + for i in 1..=n { + let mut s = true; + let mut f: HashSet = HashSet::new(); + for c in i.to_string().chars() { + if f.contains(&c) { + s = false; + break; + } else { + f.insert(c); + } + } + if s { + o += 1; + } + } + o +} diff --git a/challenge-195/roger-bell-west/rust/ch-2.rs b/challenge-195/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..0588db1309 --- /dev/null +++ b/challenge-195/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,38 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x --nocapture; rm -f ${0}x ; exit + +use std::collections::HashMap; + +#[test] +fn test_ex1() { + assert_eq!(mostfrequenteven(vec![1, 1, 2, 6, 2]), 2); +} + +#[test] +fn test_ex2() { + assert_eq!(mostfrequenteven(vec![1, 3, 5, 7]), -1); +} + +#[test] +fn test_ex3() { + assert_eq!(mostfrequenteven(vec![6, 4, 4, 6, 1]), 4); +} + +fn mostfrequenteven(l: Vec) -> isize { + let mut f: HashMap = HashMap::new(); + for n in &l { + if *n % 2 == 0 { + let en = f.entry(*n).or_insert(0); + *en += 1; + } + } + if f.len() > 0 { + let m = f.values().max().unwrap(); + let mut l = + f.keys().copied().filter(|i| f[i] == *m).collect::>(); + l.sort(); + l[0] as isize + } else { + -1 + } +} -- cgit From cce1735620dd943802adc0c1a16d59758dde79f1 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 12 Dec 2022 21:41:25 +0000 Subject: Challenge 195 Solutions (Raku) --- challenge-195/mark-anderson/raku/ch-1.raku | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/challenge-195/mark-anderson/raku/ch-1.raku b/challenge-195/mark-anderson/raku/ch-1.raku index 32539e82ae..f71c30c3b8 100644 --- a/challenge-195/mark-anderson/raku/ch-1.raku +++ b/challenge-195/mark-anderson/raku/ch-1.raku @@ -7,12 +7,16 @@ is special-integers(9_999_999), 712890; sub special-integers($n) { - .elems given gather for (^10).combinations(1..$n.chars) + my $i; + + for (^10).combinations(1..$n.chars) { for .permutations { next unless .head; - .take unless $_ > $n given .join + $i++ unless $_ > $n given .join } } + + $i } -- cgit From 24ebe044fcc947417212a49b55aabaeac8d50695 Mon Sep 17 00:00:00 2001 From: rir Date: Mon, 12 Dec 2022 17:24:48 -0500 Subject: 195 --- challenge-195/0rir/raku/ch-1.raku | 52 ++++++++++++++++++++++++++++++++++ challenge-195/0rir/raku/ch-2.raku | 59 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 challenge-195/0rir/raku/ch-1.raku create mode 100644 challenge-195/0rir/raku/ch-2.raku diff --git a/challenge-195/0rir/raku/ch-1.raku b/challenge-195/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..2c7de79d23 --- /dev/null +++ b/challenge-195/0rir/raku/ch-1.raku @@ -0,0 +1,52 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅ ≡ ∩ ≢ ∈ «␤» +use v6.d; +use Test; + +=begin comment +195-1: Special Integers Submitted by: Mohammad S Anwar + +An integer is special when all of its digits are unique. +Given a positive integer, $n > 0, print the count of all special integers +between 1 and $n. + +Example 1: +Input: $n = 15 +Output: 14 as except 11 all other integers between 1 and 15 are spcial. +Example 2: +Input: $n = 35 +Output: 32 as except 11, 22, 33 all others are special. +=end comment + +constant @special-int-ct = gather { + take Nil; + loop { + state ( $i, $prev) = 0, 0; + ++$i; + take $i.Str.comb.elems == $i.Str.comb.unique.elems ?? ++$prev !! $prev; + } +} + +multi MAIN ( 'test' ) { + my @Test = + { in => 15, exp => 14, }, + { in => 35, exp => 32, }, + { in => 99, exp => 90, }, + { in => 200, exp => 162, }, + { in => 180, exp => 147, }, + { in => 1_000, exp => 738, }, + { in => 10_000, exp => 5_274, }, + { in => 100_000, exp => 32_490, }, + { in => 1_000_000, exp => 168_570, }, + ; + plan +@Test; + for @Test -> %t { + is @special-int-ct[%t], %t, " %t <- 1..%t"; + } + done-testing; + exit; +} + +multi MAIN( $n = 180) { + say "Input: \$n = 180\nOutput: @special-int-ct[$n]"; +} diff --git a/challenge-195/0rir/raku/ch-2.raku b/challenge-195/0rir/raku/ch-2.raku new file mode 100644 index 0000000000..2356b48bec --- /dev/null +++ b/challenge-195/0rir/raku/ch-2.raku @@ -0,0 +1,59 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅ ≡ ∩ ≢ ∈ «␤» +use v6.d; +use Test; + +=begin comment +195-2: Most Frequent Even Submitted by: Mohammad S Anwar + +Given a list of numbers, @list, find the most frequent even numbers in the +list. In case you get more than one even number then return the smallest +even integer. For all other case, return -1. + +Example 1 +Input: @list = (1,1,2,6,2) +Output: 2 as there are only 2 even numbers 2 and 6 and of those 2 appears the most. +Example 2 +Input: @list = (1,3,5,7) +Output: -1 since no even numbers found in the list +Example 3 +Input: @list = (6,4,4,6,1) +Output: 4 since there are only two even numbers 4 and 6. They both appears the equal number of times, so pick the smallest. +=end comment + +sub most-freqy-even( @l where * !~~ () --> Int) { + my %h = Bag.new( @l.grep( * %% 2)); + %h = grep { .key == %h.keys.min}, %h.grep( { .value ~~ %h.values.max}); + return (Int) if %h ~~ {}; + %h.keys[0].Int; +} + +multi MAIN ( 'test' ) { + my @Die = { in => (), exp => (Int), }, ; + + my @Test = + { in => (1,1,2,2,6,6), exp => 2, }, + { in => (1,1,2,6,2), exp => 2, }, + { in => (1,3,5,7), exp => (Int), }, + { in => (6,4,4,6,1), exp => 4, }, + { in => (6,6,6,6,6), exp => 6, }, + { in => (1,2,3,4,5), exp => 2, }, + { in => (2,2,3,3,6,6), exp => 2, }, + ; + plan +@Test + @Die; + for @Die -> %t { + dies-ok { most-freqy-even( @(%t))}, + "most-freqy-even("~ (%t//"(Int)") ~") dies."; + } + for @Test -> %t { + quietly is most-freqy-even( @(%t) ), %t, + (%t // '(Int)') ~" <- %t"; + } + done-testing; +} + +multi MAIN() { + my @list = (1,2,2,2,2,2,3,6,6,6,6,6); + say "Input: \@list = @list.join(', ');\nOutput: ", + &most-freqy-even(@list) // -1; +} -- cgit From 9270d933f799a9cdaabebb3ae599fa4662311c0f Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Mon, 12 Dec 2022 21:44:55 -0600 Subject: Solve PWC195 --- challenge-195/wlmb/blog.txt | 1 + challenge-195/wlmb/perl/ch-1.pl | 48 +++++++++++++++++++++++++++++++++++++++++ challenge-195/wlmb/perl/ch-2.pl | 10 +++++++++ 3 files changed, 59 insertions(+) create mode 100644 challenge-195/wlmb/blog.txt create mode 100755 challenge-195/wlmb/perl/ch-1.pl create mode 100755 challenge-195/wlmb/perl/ch-2.pl diff --git a/challenge-195/wlmb/blog.txt b/challenge-195/wlmb/blog.txt new file mode 100644 index 0000000000..a8e038bba3 --- /dev/null +++ b/challenge-195/wlmb/blog.txt @@ -0,0 +1 @@ +https://wlmb.github.io/2022/12/12/PWC195/ diff --git a/challenge-195/wlmb/perl/ch-1.pl b/challenge-195/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..d131e286e3 --- /dev/null +++ b/challenge-195/wlmb/perl/ch-1.pl @@ -0,0 +1,48 @@ +#!/usr/bin/env perl +# Perl weekly challenge 195 +# Task 1: Special Integers +# +# See https://wlmb.github.io/2022/12/12/PWC195/#task-1-special-integers +use v5.36; +use integer; +use POSIX qw(lround); +say(<<"FIN"), exit unless @ARGV; +Usage: $0 N1 [N2...] +to get the number of special numbers between 1 and Ni +FIN +say "$_->", special($_) for @ARGV; +sub fact($n){ # factorial + my $r=1; + $r*=$_ for (1..$n); + $r +} +sub special($n){ + my $k=0; + my $count=0; + while($k<=9 && 10**($k+1) < $n){ + # Count from 1 to 9, then from 10 to 99, then from 100 to 999, etc. + $count += 9*fact(9)/fact(9-$k); + ++$k; + } + return $count if $k>9; # Nothing else to do eleven digit numbers or larger + #Count from 100.. to q00..-1 + my $q=lround($n/10**$k); + $count += ($q-1)*fact(9)/fact(9-$k); + $count += final($q, $k-1, $n); # count from q00... upto $n=qxy... + return $count; +} +sub final($left, $power, $n){ # final approach from left 0 0 0 to n=x y z + my $count=0; + my $fixed=length $left; # leftward fixed digits + my %fixed; + ++$fixed{$_} for my @fixed=split '', $left; # actual fixed digits. + $_>1 && return 0 for(values %fixed); # nothing to add if fixed part is not special + return 1 if $power < 0; # Found last string + my $target=substr($n, $fixed, 1); # Next digit + for(0..$target-1){ # count upwards to target + next if $fixed{$_}; # skip seen digits + $count += fact(9-$fixed)/fact(9-$fixed-$power); # add rightwards contribution + } + $count += final($left.$target, $power-1, $n); # add digit to leftmost and recurse. + return $count; +} diff --git a/challenge-195/wlmb/perl/ch-2.pl b/challenge-195/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..c4dfa29c27 --- /dev/null +++ b/challenge-195/wlmb/perl/ch-2.pl @@ -0,0 +1,10 @@ +#!/usr/bin/env perl +# Perl weekly challenge 195 +# Task 2: Most Frequent Even +# +# See https://wlmb.github.io/2022/12/12/PWC195/#task-2-most-frequent-even +use v5.36; +my %count; +$count{$_}++ for grep {!($_&1)} @ARGV; # Filter evens and count them +my @sorted=sort {$count{$b} <=> $count{$a} || $a<=>$b} keys %count; +say join " ", @ARGV, " -> ", @sorted?shift @sorted:-1 -- cgit From 76b0a9e0d5694fed4df1d08a97cbc0eb0c71f702 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Tue, 13 Dec 2022 11:50:51 +0000 Subject: - Added solutions by Roger Bell_West. --- stats/pwc-challenge-194.json | 631 +++ stats/pwc-current.json | 638 +-- stats/pwc-language-breakdown-summary.json | 62 +- stats/pwc-language-breakdown.json | 7595 +++++++++++++++-------------- stats/pwc-leaders.json | 312 +- stats/pwc-summary-1-30.json | 42 +- stats/pwc-summary-121-150.json | 96 +- stats/pwc-summary-151-180.json | 92 +- stats/pwc-summary-181-210.json | 96 +- stats/pwc-summary-211-240.json | 98 +- stats/pwc-summary-241-270.json | 46 +- stats/pwc-summary-271-300.json | 54 +- stats/pwc-summary-31-60.json | 40 +- stats/pwc-summary-61-90.json | 108 +- stats/pwc-summary-91-120.json | 46 +- stats/pwc-summary.json | 602 +-- 16 files changed, 5324 insertions(+), 5234 deletions(-) create mode 100644 stats/pwc-challenge-194.json diff --git a/stats/pwc-challenge-194.json b/stats/pwc-challenge-194.json new file mode 100644 index 0000000000..3bad2186d6 --- /dev/null +++ b/stats/pwc-challenge-194.json @@ -0,0 +1,631 @@ +{ + "plotOptions" : { + "series" : { + "dataLabels" : { + "format" : "{point.y}", + "enabled" : 1 + }, + "borderWidth" : 0 + } + }, + "tooltip" : { + "followPointer" : 1, + "headerFormat" : "{series.name}
", + "pointFormat" : "{point.name}: {point.y:f}
" + }, + "yAxis" : { + "title" : { + "text" : "Total Solutions" + } + }, + "title" : { + "text" : "The Weekly Challenge - 194" + }, + "subtitle" : { + "text" : "[Champions: 33] Last updated at 2022-12-13 11:25:18 GMT" + }, + "drilldown" : { + "series" : [ + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "name" : "Alexander Pankoff", + "id" : "Alexander Pankoff" + }, + { + "name" : "Arne Sommer", + "data" : [ + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Arne Sommer" + }, + { + "id" : "Athanasius", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ] + ], + "name" : "Athanasius" + }, + { + "id" : "Bob Lied", + "name" : "Bob Lied", + "data" : [ + [ + "Perl", + 2 + ] + ] + }, + { + "id" : "Bruce Gray", + "name" : "Bruce Gray", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ] + }, + { + "id" : "Cheok-Yin Fung", + "name" : "Cheok-Yin Fung", + "data" : [ + [ + "Perl", + 2 + ] + ] + }, + { + "name" : "Colin Crain", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Colin Crain" + }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "name" : "Dave Jacoby", + "id" : "Dave Jacoby" + }, + { + "id" : "David Ferrone", + "name" : "David Ferrone", + "data" : [ + [ + "Perl", + 2 + ] + ] + }, + { + "id" : "Duncan C. White", + "data" : [ + [ + "Perl", + 2 + ] + ], + "name" : "Duncan C. White" + }, + { + "id" : "E. Choroba", + "name" : "E. Choroba", + "data" : [ + [ + "Perl", + 2 + ] + ] + }, + { + "id" : "Feng Chang", + "data" : [ + [ + "Raku", + 2 + ] + ], + "name" : "Feng Chang" + }, + { + "id" : "Flavio Poletti", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 2 + ] + ], + "name" : "Flavio Poletti" + }, + { + "name" : "Jaldhar H. Vyas", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Jaldhar H. Vyas" + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "name" : "James Smith", + "id" : "James Smith" + }, + { + "name" : "Jan Krnavek", + "data" : [ + [ + "Raku", + 2 + ] + ], + "id" : "Jan Krnavek" + }, + { + "id" : "Jorg Sommrey", + "name" : "Jorg Sommrey", + "data" : [ + [ + "Perl", + 2 + ] + ] + }, + { + "name" : "Laurent Rosenfeld", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Laurent Rosenfeld" + }, + { + "name" : "Luca Ferrari", + "data" : [ + [ + "Raku", + 2 + ], + [ + "Blog", + 6 + ] + ], + "id" : "Luca Ferrari" + }, + { + "data" : [ + [ + "Raku", + 2 + ] + ], + "name" : "Mark Anderson", + "id" : "Mark Anderson" + }, + { + "name" : "Niels van Dijke", + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "Niels van Dijke" + }, + { + "name" : "Peter Campbell Smith", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Peter Campbell Smith" + }, + { + "id" : "Robbie Hatley", + "data" : [ + [ + "Perl", + 2 + ] + ], + "name" : "Robbie Hatley" + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ] + ], + "name" : "Robert DiCicco", + "id" : "Robert DiCicco" + }, + { + "data" : [ + [ + "Raku", + 2 + ] + ], + "name" : "Robert Ransbottom", + "id" : "Robert Ransbottom" + }, + { + "id" : "Roger Bell_West", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ], + "name" : "Roger Bell_West" + }, + { + "name" : "Simon Green", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Simon Green" + }, + { + "id" : "Solathian", + "data" : [ + [ + "Perl", + 2 + ] + ], + "name" : "Solathian" + }, + { + "id" : "Stephen G. Lynn", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ], + "name" : "Stephen G. Lynn" + }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "name" : "Thomas Kohler", + "id" : "Thomas Kohler" + }, + { + "id" : "Ulrich Rieke", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ] + ], + "name" : "Ulrich Rieke" + }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "name" : "Vamsi Meenavilli", + "id" : "Vamsi Meenavilli" + }, + { + "id" : "W. Luis Mochan", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "name" : "W. Luis Mochan" + } + ] + }, + "chart" : { + "type" : "column" + }, + "xAxis" : { + "type" : "category" + }, + "series" : [ + { + "colorByPoint" : 1, + "name" : "The Weekly Challenge - 194", + "data" : [ + { + "drilldown" : "Alexander Pankoff", + "y" : 2, + "name" : "Alexander Pankoff" + }, + { + "drilldown" : "Arne Sommer", + "y" : 3, + "name" : "Arne Sommer" + }, + { + "y" : 4, + "drilldown" : "Athanasius", + "name" : "Athanasius" + }, + { + "y" : 2, + "drilldown" : "Bob Lied", + "name" : "Bob Lied" + }, + { + "name" : "Bruce Gray", + "y" : 5, + "drilldown" : "Bruce Gray" + }, + { + "drilldown" : "Cheok-Yin Fung", + "y" : 2, + "name" : "Cheok-Yin Fung" + }, + { + "name" : "Colin Crain", + "y" : 3, + "drilldown" : "Colin Crain" + }, + { + "name" : "Dave Jacoby", + "y" : 2, + "drilldown" : "Dave Jacoby" + }, + { + "drilldown" : "David Ferrone", + "y" : 2, + "name" : "David Ferrone" + }, + { + "drilldown" : "Duncan C. White", + "y" : 2, + "name" : "Duncan C. White" + }, + { + "name" : "E. Choroba", + "y" : 2, + "drilldown" : "E. Choroba" + }, + { + "y" : 2, + "drilldown" : "Feng Chang", + "name" : "Feng Chang" + }, + { + "name" : "Flavio Poletti", + "drilldown" : "Flavio Poletti", + "y" : 6 + }, + { + "name" : "Jaldhar H. Vyas", + "drilldown" : "Jaldhar H. Vyas", + "y" : 5 + }, + { + "y" : 3, + "drilldown" : "James Smith", + "name" : "James Smith" + }, + { + "name" : "Jan Krnavek", + "drilldown" : "Jan Krnavek", + "y" : 2 + }, + { + "name" : "Jorg Sommrey", + "drilldown" : "Jorg Sommrey", + "y" : 2 + }, + { + "name" : "Laurent Rosenfeld", + "drilldown" : "Laurent Rosenfeld", + "y" : 5 + }, + { + "name" : "Luca Ferrari", + "drilldown" : "Luca Ferrari", + "y" : 8 + }, + { + "drilldown" : "Mark Anderson", + "y" : 2, + "name" : "Mark Anderson" + }, + { + "name" : "Niels van Dijke", + "y" : 2, + "drilldown" : "Niels van Dijke" + }, + { + "y" : 3, + "drilldown" : "Peter Campbell Smith", + "name" : "Peter Campbell Smith" + }, + { + "name" : "Robbie Hatley", + "y" : 2, + "drilldown" : "Robbie Hatley" + }, + { + "y" : 4, + "drilldown" : "Robert DiCicco", + "name" : "Robert DiCicco" + }, + { + "drilldown" : "Robert Ransbottom", + "y" : 2, + "name" : "Robert Ransbottom" +