From 41729a1f195643c2e70f76894080fba89076ede7 Mon Sep 17 00:00:00 2001 From: Asher Harvey-Smith Date: Mon, 13 May 2024 17:56:47 +0100 Subject: ch-1 and some of ch-2 --- challenge-269/asherbhs/apl/ch-1.apl | 5 +++++ challenge-269/asherbhs/apl/ch-2.apl | 1 + challenge-269/asherbhs/bqn/ch-1.bqn | 5 +++++ challenge-269/asherbhs/haskell/ch-1.hs | 8 ++++++++ challenge-269/asherbhs/haskell/ch-2.hs | 21 +++++++++++++++++++++ challenge-269/asherbhs/hy/ch-1.hy | 15 +++++++++++++++ challenge-269/asherbhs/j/ch-1.ijs | 7 +++++++ challenge-269/asherbhs/nu/ch-1.nu | 7 +++++++ challenge-269/asherbhs/raku/ch-1.raku | 7 +++++++ challenge-269/asherbhs/raku/ch-2.raku | 9 +++++++++ 10 files changed, 85 insertions(+) create mode 100644 challenge-269/asherbhs/apl/ch-1.apl create mode 100644 challenge-269/asherbhs/apl/ch-2.apl create mode 100644 challenge-269/asherbhs/bqn/ch-1.bqn create mode 100644 challenge-269/asherbhs/haskell/ch-1.hs create mode 100644 challenge-269/asherbhs/haskell/ch-2.hs create mode 100644 challenge-269/asherbhs/hy/ch-1.hy create mode 100644 challenge-269/asherbhs/j/ch-1.ijs create mode 100644 challenge-269/asherbhs/nu/ch-1.nu create mode 100644 challenge-269/asherbhs/raku/ch-1.raku create mode 100644 challenge-269/asherbhs/raku/ch-2.raku diff --git a/challenge-269/asherbhs/apl/ch-1.apl b/challenge-269/asherbhs/apl/ch-1.apl new file mode 100644 index 0000000000..46e080f5d3 --- /dev/null +++ b/challenge-269/asherbhs/apl/ch-1.apl @@ -0,0 +1,5 @@ +BitwiseOr←{2≤+/~2|⍵} + +⎕←BitwiseOr 1 2 3 4 5 +⎕←BitwiseOr 2 3 8 16 +⎕←BitwiseOr 1 2 5 7 9 diff --git a/challenge-269/asherbhs/apl/ch-2.apl b/challenge-269/asherbhs/apl/ch-2.apl new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/challenge-269/asherbhs/apl/ch-2.apl @@ -0,0 +1 @@ + diff --git a/challenge-269/asherbhs/bqn/ch-1.bqn b/challenge-269/asherbhs/bqn/ch-1.bqn new file mode 100644 index 0000000000..3587c9fd99 --- /dev/null +++ b/challenge-269/asherbhs/bqn/ch-1.bqn @@ -0,0 +1,5 @@ +BitwiseOr←{2≤+´¬2|𝕩} + +•Show BitwiseOr 1‿2‿3‿4‿5 +•Show BitwiseOr 2‿3‿8‿16 +•Show BitwiseOr 1‿2‿5‿7‿9 diff --git a/challenge-269/asherbhs/haskell/ch-1.hs b/challenge-269/asherbhs/haskell/ch-1.hs new file mode 100644 index 0000000000..e3da858f34 --- /dev/null +++ b/challenge-269/asherbhs/haskell/ch-1.hs @@ -0,0 +1,8 @@ +bitwiseOr :: [Int] -> Bool +bitwiseOr = (>= 2) . length . filter (\x -> mod x 2 == 0) + +main :: IO () +main = do + print $ bitwiseOr [1, 2, 3, 4, 5] + print $ bitwiseOr [2, 3, 8, 16] + print $ bitwiseOr [1, 2, 5, 7, 9] diff --git a/challenge-269/asherbhs/haskell/ch-2.hs b/challenge-269/asherbhs/haskell/ch-2.hs new file mode 100644 index 0000000000..dee299060c --- /dev/null +++ b/challenge-269/asherbhs/haskell/ch-2.hs @@ -0,0 +1,21 @@ +import Data.List (foldl') + +distributeElements :: [Int] -> [Int] +distributeElements (a : b : xs) = + let + (ys, zs) = foldl' + (\(y : ys, z : zs) x -> + if y > z + then (x : y : ys, z : zs) + else ( y : ys, x : z : zs) + ) + ([a], [b]) + xs + in + reverse ys ++ reverse zs + +main :: IO () +main = do + print $ distributeElements [2, 1, 3, 4, 5] + print $ distributeElements [3, 2, 4] + print $ distributeElements [5, 4, 3 ,8] diff --git a/challenge-269/asherbhs/hy/ch-1.hy b/challenge-269/asherbhs/hy/ch-1.hy new file mode 100644 index 0000000000..8cdbf70711 --- /dev/null +++ b/challenge-269/asherbhs/hy/ch-1.hy @@ -0,0 +1,15 @@ +(import + hyrule [pprint] + itertools [count] +) + +(defn bitwise-or [ints] + (>= + (sum (gfor n ints (= (% n 2) 0))) + 2 + ) +) + +(pprint (bitwise-or [1 2 3 4 5])) +(pprint (bitwise-or [2 3 8 16])) +(pprint (bitwise-or [1 2 5 7 9])) diff --git a/challenge-269/asherbhs/j/ch-1.ijs b/challenge-269/asherbhs/j/ch-1.ijs new file mode 100644 index 0000000000..2072c137b7 --- /dev/null +++ b/challenge-269/asherbhs/j/ch-1.ijs @@ -0,0 +1,7 @@ +BitwiseOr=.{{2<:+/-.2|y}} + +echo BitwiseOr 1 2 3 4 5 +echo BitwiseOr 2 3 8 16 +echo BitwiseOr 1 2 5 7 9 + +exit '' diff --git a/challenge-269/asherbhs/nu/ch-1.nu b/challenge-269/asherbhs/nu/ch-1.nu new file mode 100644 index 0000000000..56c5a86066 --- /dev/null +++ b/challenge-269/asherbhs/nu/ch-1.nu @@ -0,0 +1,7 @@ +def bitwise-or [ints: list] { + ($ints | where { 0 == $in mod 2 } | length) >= 2 +} + +print (bitwise-or [1 2 3 4 5]) +print (bitwise-or [2 3 8 16]) +print (bitwise-or [1 2 5 7 9]) diff --git a/challenge-269/asherbhs/raku/ch-1.raku b/challenge-269/asherbhs/raku/ch-1.raku new file mode 100644 index 0000000000..7b5d06c0fe --- /dev/null +++ b/challenge-269/asherbhs/raku/ch-1.raku @@ -0,0 +1,7 @@ +sub bitwise-or(Int:D @ints --> Bool) { + @ints.map(* %% 2).sum ≥ 2 +} + +say bitwise-or Array[Int:D].new: 1, 2, 3, 4, 5; +say bitwise-or Array[Int:D].new: 2, 3, 8, 16; +say bitwise-or Array[Int:D].new: 1, 2, 5, 7, 9; diff --git a/challenge-269/asherbhs/raku/ch-2.raku b/challenge-269/asherbhs/raku/ch-2.raku new file mode 100644 index 0000000000..60cd8401f7 --- /dev/null +++ b/challenge-269/asherbhs/raku/ch-2.raku @@ -0,0 +1,9 @@ +sub distribute-elements(Int:D @ints) { + my @arr1 = @ints[0]; + my @arr2 = @ints[1]; + +} + +say distribute-elements Array[Int:D].new: 2, 1, 3, 4, 5; +say distribute-elements Array[Int:D].new: 3, 2, 4; +say distribute-elements Array[Int:D].new: 5, 4, 3 ,8; -- cgit From dc632388f15e4f5f9fdfd5806de3f621fd3d7a12 Mon Sep 17 00:00:00 2001 From: Asher Harvey-Smith Date: Mon, 13 May 2024 21:58:04 +0100 Subject: ch-2 --- challenge-269/asherbhs/apl/ch-2.apl | 6 ++++++ challenge-269/asherbhs/bqn/ch-2.bqn | 5 +++++ challenge-269/asherbhs/hy/ch-2.hy | 20 ++++++++++++++++++++ challenge-269/asherbhs/j/ch-2.ijs | 15 +++++++++++++++ challenge-269/asherbhs/nu/ch-1.nu | 2 +- challenge-269/asherbhs/nu/ch-2.nu | 16 ++++++++++++++++ challenge-269/asherbhs/raku/ch-2.raku | 11 +++++++++-- 7 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 challenge-269/asherbhs/bqn/ch-2.bqn create mode 100644 challenge-269/asherbhs/hy/ch-2.hy create mode 100644 challenge-269/asherbhs/j/ch-2.ijs create mode 100644 challenge-269/asherbhs/nu/ch-2.nu diff --git a/challenge-269/asherbhs/apl/ch-2.apl b/challenge-269/asherbhs/apl/ch-2.apl index 8b13789179..69411bdf96 100644 --- a/challenge-269/asherbhs/apl/ch-2.apl +++ b/challenge-269/asherbhs/apl/ch-2.apl @@ -1 +1,7 @@ +⎕IO←0 +DistributeElements←{⊃,/⊃{(⊂⍺,⍨⊃)@(≤/⊃⍤⌽¨⍵)⊢⍵}/(⌽2↓⍵),⊂,¨2↑⍵} + +⎕←DistributeElements 2 1 3 4 5 +⎕←DistributeElements 3 2 4 +⎕←DistributeElements 5 4 3 8 diff --git a/challenge-269/asherbhs/bqn/ch-2.bqn b/challenge-269/asherbhs/bqn/ch-2.bqn new file mode 100644 index 0000000000..a56bfe267e --- /dev/null +++ b/challenge-269/asherbhs/bqn/ch-2.bqn @@ -0,0 +1,5 @@ +DistributeElements←{∾´(⋈¨2↑𝕩){∾⟜𝕨⌾((≤´¯1⊑¨𝕩)⊸⊑)𝕩}´⌽2↓𝕩} + +•Show DistributeElements 2‿1‿3‿4‿5 +•Show DistributeElements 3‿2‿4 +•Show DistributeElements 5‿4‿3‿8 diff --git a/challenge-269/asherbhs/hy/ch-2.hy b/challenge-269/asherbhs/hy/ch-2.hy new file mode 100644 index 0000000000..86c01386ec --- /dev/null +++ b/challenge-269/asherbhs/hy/ch-2.hy @@ -0,0 +1,20 @@ +(import hyrule [pprint]) + +(defn distribute-elements [ints] (do + (setv + arr1 [(get ints 0)] + arr2 [(get ints 1)] + ) + (for [n (cut ints 2 None)] + (if (> (get arr1 -1) (get arr2 -1)) + (arr1.append n) + (arr2.append n) + ) + ) + (+ arr1 arr2) +)) + +(pprint (distribute-elements [2 1 3 4 5])) +(pprint (distribute-elements [3 2 4])) +(pprint (distribute-elements [5 4 3 8])) + diff --git a/challenge-269/asherbhs/j/ch-2.ijs b/challenge-269/asherbhs/j/ch-2.ijs new file mode 100644 index 0000000000..775341a468 --- /dev/null +++ b/challenge-269/asherbhs/j/ch-2.ijs @@ -0,0 +1,15 @@ +DistributeElements=.{{ + F=.{{ + NB. couldn't get structural under to work :( + a=.>y + i=.<:/{:@>a + x),~>i{a + }} + ,&>/>F/(<"0|.2}.y),<<"0]2{.y +}} + +echo DistributeElements 2 1 3 4 5 +echo DistributeElements 3 2 4 +echo DistributeElements 5 4 3 8 + +exit '' diff --git a/challenge-269/asherbhs/nu/ch-1.nu b/challenge-269/asherbhs/nu/ch-1.nu index 56c5a86066..b4cfa52c7f 100644 --- a/challenge-269/asherbhs/nu/ch-1.nu +++ b/challenge-269/asherbhs/nu/ch-1.nu @@ -1,4 +1,4 @@ -def bitwise-or [ints: list] { +def bitwise-or [ints: list] -> bool { ($ints | where { 0 == $in mod 2 } | length) >= 2 } diff --git a/challenge-269/asherbhs/nu/ch-2.nu b/challenge-269/asherbhs/nu/ch-2.nu new file mode 100644 index 0000000000..5babaf4244 --- /dev/null +++ b/challenge-269/asherbhs/nu/ch-2.nu @@ -0,0 +1,16 @@ +def distribute-elements [ints: list] -> list { + mut arr1 = [$ints.0] + mut arr2 = [$ints.1] + for n in ($ints | skip 2) { + if ($arr1 | last) > ($arr2 | last) { + $arr1 ++= $n + } else { + $arr2 ++= $n + } + } + $arr1 ++ $arr2 +} + +print (distribute-elements [2 1 3 4 5]) +print (distribute-elements [3 2 4]) +print (distribute-elements [5 4 3 8]) diff --git a/challenge-269/asherbhs/raku/ch-2.raku b/challenge-269/asherbhs/raku/ch-2.raku index 60cd8401f7..05acbb0884 100644 --- a/challenge-269/asherbhs/raku/ch-2.raku +++ b/challenge-269/asherbhs/raku/ch-2.raku @@ -1,7 +1,14 @@ -sub distribute-elements(Int:D @ints) { +sub distribute-elements(Int:D @ints --> Array:D[Int:D]) { my @arr1 = @ints[0]; my @arr2 = @ints[1]; - + for @ints.tail(* - 2) -> $n { + if @arr1[* - 1] > @arr2[* - 1] { + @arr1.push($n) + } else { + @arr2.push($n) + } + } + (|@arr1, |@arr2).Array } say distribute-elements Array[Int:D].new: 2, 1, 3, 4, 5; -- cgit From 9d3aea024416f4d76405ed2809c4fe558834f0cf Mon Sep 17 00:00:00 2001 From: Asher Harvey-Smith Date: Tue, 14 May 2024 13:21:33 +0100 Subject: tweak --- challenge-269/asherbhs/hy/ch-1.hy | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/challenge-269/asherbhs/hy/ch-1.hy b/challenge-269/asherbhs/hy/ch-1.hy index 8cdbf70711..82745b41ec 100644 --- a/challenge-269/asherbhs/hy/ch-1.hy +++ b/challenge-269/asherbhs/hy/ch-1.hy @@ -1,7 +1,4 @@ -(import - hyrule [pprint] - itertools [count] -) +(import hyrule [pprint]) (defn bitwise-or [ints] (>= -- cgit From 909c81342b4c0ff0132c8e560c12b8e3f1e295c9 Mon Sep 17 00:00:00 2001 From: Asher Harvey-Smith Date: Tue, 14 May 2024 13:25:15 +0100 Subject: tweak --- challenge-269/asherbhs/raku/ch-1.raku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-269/asherbhs/raku/ch-1.raku b/challenge-269/asherbhs/raku/ch-1.raku index 7b5d06c0fe..33e1adef5b 100644 --- a/challenge-269/asherbhs/raku/ch-1.raku +++ b/challenge-269/asherbhs/raku/ch-1.raku @@ -1,4 +1,4 @@ -sub bitwise-or(Int:D @ints --> Bool) { +sub bitwise-or(Int:D @ints --> Bool:D) { @ints.map(* %% 2).sum ≥ 2 } -- cgit