diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-11-30 21:56:17 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-11-30 21:56:17 +0000 |
| commit | d3f8a4ffd028c6c532ee4e97a434bcb351364120 (patch) | |
| tree | c4fa27d353a84276c941852a40da641748381d6d | |
| parent | ff7bffcc79350291e6f05e1aa2cde7b20f457f04 (diff) | |
| parent | 8bbf8ae2baa476b9937a7ba780d81ee5cc200795 (diff) | |
| download | perlweeklychallenge-club-d3f8a4ffd028c6c532ee4e97a434bcb351364120.tar.gz perlweeklychallenge-club-d3f8a4ffd028c6c532ee4e97a434bcb351364120.tar.bz2 perlweeklychallenge-club-d3f8a4ffd028c6c532ee4e97a434bcb351364120.zip | |
Merge pull request #2892 from seaker/master
Feng Chang's Challenge #089 raku solutions
| -rwxr-xr-x | challenge-087/feng-chang/raku/ch-1b.raku | 33 | ||||
| -rwxr-xr-x | challenge-087/feng-chang/raku/ch-2b.raku | 55 | ||||
| -rwxr-xr-x | challenge-089/feng-chang/raku/ch-1.raku | 27 | ||||
| -rwxr-xr-x | challenge-089/feng-chang/raku/ch-1b.raku | 31 | ||||
| -rwxr-xr-x | challenge-089/feng-chang/raku/ch-2.raku | 32 |
5 files changed, 178 insertions, 0 deletions
diff --git a/challenge-087/feng-chang/raku/ch-1b.raku b/challenge-087/feng-chang/raku/ch-1b.raku new file mode 100755 index 0000000000..83629e7906 --- /dev/null +++ b/challenge-087/feng-chang/raku/ch-1b.raku @@ -0,0 +1,33 @@ +#!/bin/env raku + +=begin Challenge +087, Task #1 + +You are given an unsorted array of integers @N. +Write a script to find the longest consecutive sequence. Print 0 if none sequence found. + +Example 1: + Input: @N = (100, 4, 50, 3, 2) + Output: (2, 3, 4) + +Example 2: + Input: @N = (20, 30, 10, 40, 50) + Output: 0 + +Example 3: + Input: @N = (20, 19, 9, 11, 10) + Output: (9, 10, 11) + +=end Challenge + +sub MAIN(*@N) { + @N .= sort; + my \宽 = @N.elems - 1; + + my @a = (0..宽-1 X 1..宽) + .grep({ $_[1] > $_[0] }) + .grep({ $_[1] - $_[0] == @N[$_[1]] - @N[$_[0]] }); + put do { + @a.elems > 0 ?? @a.max({ $_[1] - $_[0] }).map({ @N[$^a..$^b] }) !! 0; + } +} diff --git a/challenge-087/feng-chang/raku/ch-2b.raku b/challenge-087/feng-chang/raku/ch-2b.raku new file mode 100755 index 0000000000..457119c152 --- /dev/null +++ b/challenge-087/feng-chang/raku/ch-2b.raku @@ -0,0 +1,55 @@ +#!/bin/env raku + +=begin Challenge + +You are given matrix m x n with 0 and 1. +Write a script to find the largest rectangle containing only 1. Print 0 if none found. + +Example 1: +Input: + [ 0 0 0 1 0 0 ] + [ 1 1 1 0 0 0 ] + [ 0 0 1 0 0 1 ] + [ 1 1 1 1 1 0 ] + [ 1 1 1 1 1 0 ] +Output: + [ 1 1 1 1 1 ] + [ 1 1 1 1 1 ] + +Example 2: +Input: + [ 1 0 1 0 1 0 ] + [ 0 1 0 1 0 1 ] + [ 1 0 1 0 1 0 ] + [ 0 1 0 1 0 1 ] +Output: 0 + +Example 3: +Input: + [ 0 0 0 1 1 1 ] + [ 1 1 1 1 1 1 ] + [ 0 0 1 0 0 1 ] + [ 0 0 1 1 1 1 ] + [ 0 0 1 1 1 1 ] +Output: + [ 1 1 1 1 ] + [ 1 1 1 1 ] + +=end Challenge + +my @N; +for $*IN.lines -> $line { + @N.push($line.words».Int); +} + +my \高 = @N.elems; +my \宽 = @N[0].elems; + +my $a = ((0 ..^ 高-1 X 0 ..^ 宽-1) X (1 ..^ 高 X 1 ..^ 宽)) + .grep({ $_[1;0] > $_[0;0] and $_[1;1] > $_[0;1] }) + .grep({ @N[$_[0;0]..$_[1;0]; $_[0;1]..$_[1;1]].all == 1 }) + .max({ ($_[1;0] - $_[0;0] + 1) * ($_[1;1] - $_[0;1] + 1) }); + +if $a.elems > 1 { + put (1) xx ($a[1;1] - $a[0;1] + 1) for ^($a[1;0] - $a[0;0] + 1); +} diff --git a/challenge-089/feng-chang/raku/ch-1.raku b/challenge-089/feng-chang/raku/ch-1.raku new file mode 100755 index 0000000000..4bfaf7bc8c --- /dev/null +++ b/challenge-089/feng-chang/raku/ch-1.raku @@ -0,0 +1,27 @@ +#!/bin/env raku + +=begin Challenge + +You are given a positive integer $N. + +Write a script to sum GCD of all possible unique pairs between 1 and $N. + +Example 1: + +Input: 3 +Output: 3 + +gcd(1,2) + gcd(1,3) + gcd(2,3) + +Example 2: + +Input: 4 +Output: 7 + +gcd(1,2) + gcd(1,3) + gcd(1,4) + gcd(2,3) + gcd(2,4) + gcd(3,4) + +=end Challenge + +sub MAIN(UInt:D $N) { + (1..$N-1 X 2..$N).grep({.[1] > .[0]}).map({.[0] gcd .[1]}).sum.put; +} diff --git a/challenge-089/feng-chang/raku/ch-1b.raku b/challenge-089/feng-chang/raku/ch-1b.raku new file mode 100755 index 0000000000..833a76c8bb --- /dev/null +++ b/challenge-089/feng-chang/raku/ch-1b.raku @@ -0,0 +1,31 @@ +#!/bin/env raku + +=begin Challenge + +You are given a positive integer $N. + +Write a script to sum GCD of all possible unique pairs between 1 and $N. + +Example 1: + +Input: 3 +Output: 3 + +gcd(1,2) + gcd(1,3) + gcd(2,3) + +Example 2: + +Input: 4 +Output: 7 + +gcd(1,2) + gcd(1,3) + gcd(1,4) + gcd(2,3) + gcd(2,4) + gcd(3,4) + +=end Challenge + +sub MAIN(UInt:D $N) { + (1..$N-1) X (2..$N) ==> + grep { .[1] > .[0] } ==> + map { .[0] gcd .[1] } ==> + sum() ==> + put(); +} diff --git a/challenge-089/feng-chang/raku/ch-2.raku b/challenge-089/feng-chang/raku/ch-2.raku new file mode 100755 index 0000000000..6fe22b02a8 --- /dev/null +++ b/challenge-089/feng-chang/raku/ch-2.raku @@ -0,0 +1,32 @@ +#!/bin/env raku + +=begin Challenge + +Write a script to display matrix as below with numbers 1 - 9. Please make sure numbers are used once. + +[ a b c ] +[ d e f ] +[ g h i ] + +So that it satisfies the following: + +a + b + c = 15 +d + e + f = 15 +g + h + i = 15 +a + d + g = 15 +b + e + h = 15 +c + f + i = 15 +a + e + i = 15 +c + e + g = 15 + +=end Challenge + +(1..9).permutations + .grep({ all( + .[0]+.[1]+.[2], .[3]+.[4]+.[5], .[6]+.[7]+.[8], + .[0]+.[3]+.[6], .[1]+.[4]+.[7], .[2]+.[5]+.[8], + .[0]+.[4]+.[8], .[2]+.[4]+.[6] + ) == 15 }) + .map({ "{.[0]} {.[1]} {.[2]}\n{.[3]} {.[4]} {.[5]}\n{.[6]} {.[7]} {.[8]}" }) + .join("\n\n") + .put; |
