aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-05-14 13:58:08 +0100
committerGitHub <noreply@github.com>2024-05-14 13:58:08 +0100
commit3cf0a1b043c8f6c098e57675cacc11d3210df8c2 (patch)
treea0224b3cf5bfe20a314e6f15bacfaa8ef9e84034
parent7fa75c11a751d16a4706c4e1c9448b6122d54d02 (diff)
parent909c81342b4c0ff0132c8e560c12b8e3f1e295c9 (diff)
downloadperlweeklychallenge-club-3cf0a1b043c8f6c098e57675cacc11d3210df8c2.tar.gz
perlweeklychallenge-club-3cf0a1b043c8f6c098e57675cacc11d3210df8c2.tar.bz2
perlweeklychallenge-club-3cf0a1b043c8f6c098e57675cacc11d3210df8c2.zip
Merge pull request #10097 from asherbhs/challenge-269
Challenge 269
-rw-r--r--challenge-269/asherbhs/apl/ch-1.apl5
-rw-r--r--challenge-269/asherbhs/apl/ch-2.apl7
-rw-r--r--challenge-269/asherbhs/bqn/ch-1.bqn5
-rw-r--r--challenge-269/asherbhs/bqn/ch-2.bqn5
-rw-r--r--challenge-269/asherbhs/haskell/ch-1.hs8
-rw-r--r--challenge-269/asherbhs/haskell/ch-2.hs21
-rw-r--r--challenge-269/asherbhs/hy/ch-1.hy12
-rw-r--r--challenge-269/asherbhs/hy/ch-2.hy20
-rw-r--r--challenge-269/asherbhs/j/ch-1.ijs7
-rw-r--r--challenge-269/asherbhs/j/ch-2.ijs15
-rw-r--r--challenge-269/asherbhs/nu/ch-1.nu7
-rw-r--r--challenge-269/asherbhs/nu/ch-2.nu16
-rw-r--r--challenge-269/asherbhs/raku/ch-1.raku7
-rw-r--r--challenge-269/asherbhs/raku/ch-2.raku16
14 files changed, 151 insertions, 0 deletions
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..69411bdf96
--- /dev/null
+++ b/challenge-269/asherbhs/apl/ch-2.apl
@@ -0,0 +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-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/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/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..82745b41ec
--- /dev/null
+++ b/challenge-269/asherbhs/hy/ch-1.hy
@@ -0,0 +1,12 @@
+(import hyrule [pprint])
+
+(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/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-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/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
+ <a i}~<(>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
new file mode 100644
index 0000000000..b4cfa52c7f
--- /dev/null
+++ b/challenge-269/asherbhs/nu/ch-1.nu
@@ -0,0 +1,7 @@
+def bitwise-or [ints: list<int>] -> bool {
+ ($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/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<int>] -> list<int> {
+ 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-1.raku b/challenge-269/asherbhs/raku/ch-1.raku
new file mode 100644
index 0000000000..33e1adef5b
--- /dev/null
+++ b/challenge-269/asherbhs/raku/ch-1.raku
@@ -0,0 +1,7 @@
+sub bitwise-or(Int:D @ints --> Bool:D) {
+ @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..05acbb0884
--- /dev/null
+++ b/challenge-269/asherbhs/raku/ch-2.raku
@@ -0,0 +1,16 @@
+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;
+say distribute-elements Array[Int:D].new: 3, 2, 4;
+say distribute-elements Array[Int:D].new: 5, 4, 3 ,8;