diff options
| author | James Smith <js5@sanger.ac.uk> | 2022-12-15 01:57:55 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-12-15 01:57:55 +0000 |
| commit | e2b33f546bda365fe677e14e725bb53a5f23798c (patch) | |
| tree | a99ca3ed8e82e367b0aba5acd5cec50e5b48757f | |
| parent | 2b99b6e28c2a3b672e50d7b22cd3e1c6998670c7 (diff) | |
| parent | c1d3932971f399789d4ee01cb886bb1e984f2563 (diff) | |
| download | perlweeklychallenge-club-e2b33f546bda365fe677e14e725bb53a5f23798c.tar.gz perlweeklychallenge-club-e2b33f546bda365fe677e14e725bb53a5f23798c.tar.bz2 perlweeklychallenge-club-e2b33f546bda365fe677e14e725bb53a5f23798c.zip | |
Merge branch 'manwar:master' into master
88 files changed, 4764 insertions, 2146 deletions
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<in>], %t<exp>, " %t<exp> <- 1..%t<in>"; + } + 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<in>))}, + "most-freqy-even("~ (%t<exp>//"(Int)") ~") dies."; + } + for @Test -> %t { + quietly is most-freqy-even( @(%t<in>) ), %t<exp>, + (%t<exp> // '(Int)') ~" <- %t<in>"; + } + 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; +} diff --git a/challenge-195/carlos-oliveira/README b/challenge-195/carlos-oliveira/README new file mode 100644 index 0000000000..f8182f8301 --- /dev/null +++ b/challenge-195/carlos-oliveira/README @@ -0,0 +1 @@ +Solution by Carlos Eduardo de Oliveira. diff --git a/challenge-195/carlos-oliveira/perl/ch-1.pl b/challenge-195/carlos-oliveira/perl/ch-1.pl new file mode 100644 index 0000000000..eb1c905189 --- /dev/null +++ b/challenge-195/carlos-oliveira/perl/ch-1.pl @@ -0,0 +1,11 @@ +use strict; +use warnings; + +sub specialIntegers { + my ($limit) = @_; + return scalar grep { $_ !~ /(.).*\1/; } 1..$limit; +} + +print specialIntegers(15), "\n"; # Output: 14 +print specialIntegers(35), "\n"; # Output: 32 + diff --git a/challenge-195/carlos-oliveira/perl/ch-2.pl b/challenge-195/carlos-oliveira/perl/ch-2.pl new file mode 100644 index 0000000000..c0a63c7f94 --- /dev/null +++ b/challenge-195/carlos-oliveira/perl/ch-2.pl @@ -0,0 +1,30 @@ +use strict; +use warnings; + +sub mostFrequentEven { + my @numbers = sort @_; + my $currentNumber = $numbers[0]; + my $maxEven = -1; + my $maxCount = 0; + my $currentCount = 0; + for my $elem (@numbers) { + next if $elem % 2; + if ($currentNumber == $elem) { + $currentCount++; + } else { + $currentCount = 1; + $currentNumber = $elem; + } + if ($currentCount > $maxCount) { + $maxEven = $elem; + $maxCount = $currentCount; + } + } + return $maxEven; +} + +print mostFrequentEven(1,1,2,6,2), "\n"; +print mostFrequentEven(1,3,5,7), "\n"; +print mostFrequentEven(6,4,4,6,1), "\n"; +print mostFrequentEven(6,4,4,6,1,8,8,8), "\n"; + 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; +} + diff --git a/challenge-195/eric-cheung/python/ch-1.py b/challenge-195/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..4839f6f0fc --- /dev/null +++ b/challenge-195/eric-cheung/python/ch-1.py @@ -0,0 +1,30 @@ +
+def IsNumSpecial(nInput):
+
+ if nInput < 11:
+ return True
+
+ arrList = list(map(int, str(nInput)))
+ arrUniqList = list(set(arrList))
+
+ for nLoop in arrUniqList:
+ if arrList.count(nLoop) > 1:
+ return False
+
+ return True
+
+def nCountNumSpecial(nNum):
+
+ arrOutputList = []
+
+ for nVar in range(1, nNum + 1):
+ if IsNumSpecial(nVar):
+ arrOutputList.append(nVar)
+
+ return len(arrOutputList)
+
+
+## nGivenInput = 15 ## Example 1
+nGivenInput = 35 ## Example 2
+
+print (nCountNumSpecial(nGivenInput))
diff --git a/challenge-195/eric-cheung/python/ch-2.py b/challenge-195/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..b141abef13 --- /dev/null +++ b/challenge-195/eric-cheung/python/ch-2.py @@ -0,0 +1,20 @@ +
+nArrList = [1, 1, 2, 6, 2] ## Example 1
+## nArrList = [1, 3, 5, 7] ## Example 2
+## nArrList = [6, 4, 4, 6, 1] ## Example 3
+
+nArrEvenList = [nLoop for nLoop in nArrList if nLoop % 2 == 0]
+nArrUniqEvenList = list(set(nArrEvenList))
+
+nSmallEvenNum = -1
+nEvenNumCount = 0
+
+for nLoop in nArrUniqEvenList:
+
+ nCount = nArrEvenList.count(nLoop)
+
+ if nCount > nEvenNumCount and (nLoop < nSmallEvenNum or nSmallEvenNum < 0):
+ nSmallEvenNum = nLoop
+ nEvenNumCount = nCount
+
+print (nSmallEvenNum)
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 descen |
