diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-10-20 23:42:02 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-10-20 23:42:02 +0100 |
| commit | 520cb5dc89e105c9ff67c2ca2ae08e4d9ae6a4f4 (patch) | |
| tree | d22d6eb878fd8cf857dc38ca79e0aa2a954c1928 | |
| parent | 6b6f12298c9a8e248d2f4f685c2e6f39cb9db313 (diff) | |
| parent | e8ca81af2740fa9eb395b3476755bad1f665cde9 (diff) | |
| download | perlweeklychallenge-club-520cb5dc89e105c9ff67c2ca2ae08e4d9ae6a4f4.tar.gz perlweeklychallenge-club-520cb5dc89e105c9ff67c2ca2ae08e4d9ae6a4f4.tar.bz2 perlweeklychallenge-club-520cb5dc89e105c9ff67c2ca2ae08e4d9ae6a4f4.zip | |
Merge pull request #11056 from asherbhs/challenge-291
ch-1 in several, ch-2 in raku
| -rw-r--r-- | challenge-291/asherbhs/apl/ch-1.apl | 8 | ||||
| -rw-r--r-- | challenge-291/asherbhs/bqn/ch-1.bqn | 5 | ||||
| -rw-r--r-- | challenge-291/asherbhs/j/ch-1.ijs | 7 | ||||
| -rw-r--r-- | challenge-291/asherbhs/raku/ch-1.raku | 13 | ||||
| -rw-r--r-- | challenge-291/asherbhs/raku/ch-2.raku | 27 |
5 files changed, 60 insertions, 0 deletions
diff --git a/challenge-291/asherbhs/apl/ch-1.apl b/challenge-291/asherbhs/apl/ch-1.apl new file mode 100644 index 0000000000..159479f8a4 --- /dev/null +++ b/challenge-291/asherbhs/apl/ch-1.apl @@ -0,0 +1,8 @@ +⎕IO←0 + +MiddleIndex1←{¯1⊣⍣(i=≢⍵)i←1⍳⍨=∘⌽⍥(0,¯1↓+\)∘⌽⍨⍵} +MiddleIndex2←{ ⊃¯1,⍨⍸=∘⌽⍥(0,¯1↓+\)∘⌽⍨⍵} + +⎕←(MiddleIndex1,MiddleIndex2) 2 3 ¯1 8 4 +⎕←(MiddleIndex1,MiddleIndex2) 1 ¯1 4 +⎕←(MiddleIndex1,MiddleIndex2) 2 5 diff --git a/challenge-291/asherbhs/bqn/ch-1.bqn b/challenge-291/asherbhs/bqn/ch-1.bqn new file mode 100644 index 0000000000..a7ef4420fb --- /dev/null +++ b/challenge-291/asherbhs/bqn/ch-1.bqn @@ -0,0 +1,5 @@ +MiddleIndex←{⊑¯1∾˜/=⟜⌽○(»+`)⟜⌽˜𝕩} + +•Show MiddleIndex 2‿3‿¯1‿8‿4 +•Show MiddleIndex 1‿¯1‿4 +•Show MiddleIndex 2‿5 diff --git a/challenge-291/asherbhs/j/ch-1.ijs b/challenge-291/asherbhs/j/ch-1.ijs new file mode 100644 index 0000000000..e78b9f696c --- /dev/null +++ b/challenge-291/asherbhs/j/ch-1.ijs @@ -0,0 +1,7 @@ +MiddleIndex=:{{{._1,~I.((=|.)&(0,[:}:+/\)|.)~y}} + +echo MiddleIndex 2 3 _1 8 4 +echo MiddleIndex 1 _1 4 +echo MiddleIndex 2 5 + +exit '' diff --git a/challenge-291/asherbhs/raku/ch-1.raku b/challenge-291/asherbhs/raku/ch-1.raku new file mode 100644 index 0000000000..7921a94cad --- /dev/null +++ b/challenge-291/asherbhs/raku/ch-1.raku @@ -0,0 +1,13 @@ +sub middle-index(Int:D @ints --> Int:D) { + my ($l, $r) = 0, @ints.sum; + for @ints.kv -> $i, $n { + $r -= $n; + return $i if $l == $r; + $l += $n; + } + -1 +} + +say middle-index Array[Int:D].new: 2, 3, -1, 8, 4; +say middle-index Array[Int:D].new: 1, -1, 4; +say middle-index Array[Int:D].new: 2, 5; diff --git a/challenge-291/asherbhs/raku/ch-2.raku b/challenge-291/asherbhs/raku/ch-2.raku new file mode 100644 index 0000000000..981acf8b71 --- /dev/null +++ b/challenge-291/asherbhs/raku/ch-2.raku @@ -0,0 +1,27 @@ +my @suits = flat <♠ ♣ ♥ ♦> »xx» 13; +my @values = flat (1 .. 13) xx 4; +my @hands = (^52).combinations: 5; +say +@hands; # 2,598,960 +my @counts = 0 xx 10; +for @hands -> @hand { + my @v = @values[@hand]; + my @c = @v.Bag.values; + my @s = @suits[@hand]; + my $straight = ((@v.min «+« ^5) ⊆ @v) || (1 ∈ @v) && (10 .. 13 ⊆ @v); + my $flush = [eq] @s; + when 5 ∈ @c { @counts[0]++ } # five of a kind + when $straight and $flush { @counts[1]++ } # straight flush + when 4 ∈ @c { @counts[2]++ } # four of a kind + when 3 & 2 ∈ @c { @counts[3]++ } # full house + when $flush { @counts[4]++ } # flush + when $straight { @counts[5]++ } # straight + when 3 ∈ @c { @counts[6]++ } # three of a kind + when 2 == @c.grep: 2 { @counts[7]++ } # two pair + when 2 ∈ @c { @counts[8]++ } # one pair + @counts[9]++ # high card + # it's a shame that + # @counts[i]++ when ... + # doesn't work because the statement modifier doesn't short-circuit :( +} +say @counts; # 0 40 624 3744 5108 10200 54912 123552 1098240 1302540 +say @hands == @counts.sum; |
