diff options
| -rw-r--r-- | challenge-224/0rir/raku/ch-1.raku | 65 | ||||
| -rw-r--r-- | challenge-224/0rir/raku/ch-2.raku | 135 | ||||
| -rw-r--r-- | challenge-224/tyler-wardhaugh/clojure/README.md | 17 | ||||
| -rw-r--r-- | challenge-224/tyler-wardhaugh/clojure/bb.edn | 2 | ||||
| -rw-r--r-- | challenge-224/tyler-wardhaugh/clojure/build.clj | 19 | ||||
| -rw-r--r-- | challenge-224/tyler-wardhaugh/clojure/deps.edn | 7 | ||||
| -rw-r--r-- | challenge-224/tyler-wardhaugh/clojure/src/c224/t1.clj | 18 | ||||
| -rw-r--r-- | challenge-224/tyler-wardhaugh/clojure/src/c224/t2.clj | 39 | ||||
| -rw-r--r-- | challenge-224/tyler-wardhaugh/clojure/test/c224/t1_test.clj | 9 | ||||
| -rw-r--r-- | challenge-224/tyler-wardhaugh/clojure/test/c224/t2_test.clj | 9 |
10 files changed, 307 insertions, 13 deletions
diff --git a/challenge-224/0rir/raku/ch-1.raku b/challenge-224/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..88c3b06806 --- /dev/null +++ b/challenge-224/0rir/raku/ch-1.raku @@ -0,0 +1,65 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ « » ∴ +use v6.d; +use Test; + +=begin comment +224-1: Special Notes Submitted by: Mohammad S Anwar + +Given two strings, $source and $target, find out if using the characters +(only once) from source, a target string can be created. + +Example 1 +Input: $source = "abc" + $target = "xyz" +Output: false +Example 2 +Input: $source = "scriptinglanguage" + $target = "perl" +Output: true +Example 3 +Input: $source = "aabbcc" + $target = "abc" +Output: true +=end comment + +=begin consideration + It seems that "aabb" would allow 'aab' to be True. Depends on the + definition of 'character'. +=end consideration + +my @Test = + # source target set bag + "aabbcc", "abc", True, True, + "scriptinglanguage", "perl", True, True, + "abc", "xyz", False, False, + 'a', 'a', True, True, + 'aa', 'a', True, True, + 'aa', 'aa', False, True, + 'aa', 'ab', False, False, +; +plan @Test ÷ 2; + +sub func( $src, $trg, Bool :$bag -->Bool) { + my ($s, $t) = ($src.comb.Bag, $trg.comb.Bag); + return $t ⊆ $s if $bag; + return $t ⊆ $s.Set; +} + +for @Test -> $src, $trg, $set, $bag { + is func($src, $trg ), $set, "$trg <- $src (set)"; + is func($src, $trg, :bag), $bag, "$trg <- $src (bag)"; +} +done-testing; + +my $source = 'apple'; +my $dest = 'leap'; +say qq{\n\$source = "$source"\n\$target = "$dest"}; +say "Output: &func($source, $dest) set required"; +my $dest = 'apple'; +say qq{\n\$source = "$source"\n\$target = "$dest"}; +say "Output: &func($source, $dest) set required"; +say qq{\n\$source = "$source"\n\$target = "$dest"}; +say "Output: &func($source, $dest, :bag) dupes per source"; +exit; + diff --git a/challenge-224/0rir/raku/ch-2.raku b/challenge-224/0rir/raku/ch-2.raku new file mode 100644 index 0000000000..a46445ca61 --- /dev/null +++ b/challenge-224/0rir/raku/ch-2.raku @@ -0,0 +1,135 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ « » ∴ +use v6.d; +use lib $?FILE.IO.cleanup.parent(2).add("lib"); +use Test; + +=begin comment +224-2: Additive Number Submitted by: Mohammad S Anwar +Given a string containing digits 0-9 only, find if the given string is an +additive number. An additive number is one whose digits form an additive +sequence. + A valid additive sequence should contain at least 3 numbers. Except the +first 2 numbers, each subsequent number in the sequence must be the sum of +the preceding two. + +Example 1: +Input: $string = "112358" +Output: true + +The additive sequence can be created using the given string digits: 1,1,2,3,5,8 +1 + 1 => 2 +1 + 2 => 3 +2 + 3 => 5 +3 + 5 => 8 +Example 2: +Input: $string = "12345" +Output: false + +No additive sequence can be created using the given string digits. +Example 3: +Input: $string = "199100199" +Output: true + +The additive sequence can be created using the given string digits: 1,99,100,199 + 1 + 99 => 100 +99 + 100 => 199 + +=end comment + +my @Test = + # simple special case +=begin comment +=end comment + "0 0 0", False, + "000000000000000000", False, + "1", False, + "12", False, + # ill-formed + "012", False, + "01123", False, + "00001123", False, + # fouled + "1203", False, + "222 555 0777 1332", False, + # Insolvable + "12345", False, + "112336", False, + # + "1 99 100 199", True, + "1 1 2 3 5 8", True, + "224 466 690 1156", True, + '5510 234 5744 5978', True, + '123 123 246 369', True, + "11 23 34", True, + (10,23, *+*…* > 10¹⁰).join, True, + (1238,111, *+*…* > 10¹⁰).join, True, +; + +plan @Test ÷ 2; + +sub zed-fouled( $str -->Bool) { $str.starts-with('0') and +$str ≠ 0 } + +sub first-three( $a -->Array) { + my @return; + my $a-len = $a.chars; + ONE: + for 1..$a-len -> $i { + my $one = $a.substr( 0, $i); + next if zed-fouled( $one); # No leading zeds. + TWO: for 1..($a-len - $i -1 ) -> $ii { + my $two = $a.substr( $i, $ii); + next TWO if zed-fouled( $two); # No leading zeds. + my $three = ~( $one + $two); + if $three eq $a.substr( $i+$ii, $three.chars) { + my $tail = $a.substr( $i+$ii+$three.chars); + # No leading zeds. + next TWO if $tail.starts-with('0') and $three > 0; + @return.push: [ $two, $three, $tail]; + } + } + } + return @return; +} + +sub the-rest( @aoa -->Bool) { + FOR: + for @aoa -> @a { + my ( $deuce, $trey, $tail ) = @a; + return True if $tail eq ''; # Triplet only. + while $tail { + my $next = ~($deuce + $trey); + if $next eq $tail.substr( 0, $next.chars) { + $tail ~~ s/ ^ $next//; + if $tail eq '' { + return True + } + $deuce = $trey; + $trey = $next; + next; + } else { + next FOR; + } + } + } + return False; +} + +sub additive( $s -->Bool){ + my $s-len = $s.chars; + return False if $s-len < 3; + return False if $s.starts-with: '0'; + return the-rest( first-three( $s)); +} + +for @Test -> $in, $r { + my $s = $in.subst: ' ', '', :g ; + is additive($s), $r, "$r <- $in"; +} +done-testing; + +my $string = "2244666901156"; +say "\nInput: \$string = $string\n Output: ", additive( $string); + +exit; + diff --git a/challenge-224/tyler-wardhaugh/clojure/README.md b/challenge-224/tyler-wardhaugh/clojure/README.md index 7b86c0068b..1fc0d33f84 100644 --- a/challenge-224/tyler-wardhaugh/clojure/README.md +++ b/challenge-224/tyler-wardhaugh/clojure/README.md @@ -1,6 +1,6 @@ -# c223 +# c224 -The Weekly Challenge — #223 — Tyler Wardhaugh +The Weekly Challenge — #224 — Tyler Wardhaugh ## Usage @@ -8,18 +8,21 @@ Clojure ([installation instructions](https://clojure.org/guides/getting_started# Run Task #1: - $ clojure -M:t1 N + $ clojure -M:t1 SOURCE TARGET # ... or ... - $ bb run task-1 N + $ bb run task-1 SOURCE TARGET + + # Alternatively, to run it via Babashka: + $ bb run task-1-bb SOURCE TARGET Run Task #2: - $ clojure -M:t2 COLL + $ clojure -M:t2 N # ... or ... - $ bb run task-2 COLL + $ bb run task-2 N # Alternatively, to run it via Babashka: - $ bb run task-2-bb COLL + $ bb run task-2-bb N Run the project's tests (which are samples from the task descriptions): diff --git a/challenge-224/tyler-wardhaugh/clojure/bb.edn b/challenge-224/tyler-wardhaugh/clojure/bb.edn index 09ef39da1e..3c885d11a6 100644 --- a/challenge-224/tyler-wardhaugh/clojure/bb.edn +++ b/challenge-224/tyler-wardhaugh/clojure/bb.edn @@ -1,6 +1,6 @@ { :paths ["src" "resources"] - :deps {c223/c223 {:local/root "."}} + :deps {c224/c224 {:local/root "."}} :tasks { diff --git a/challenge-224/tyler-wardhaugh/clojure/build.clj b/challenge-224/tyler-wardhaugh/clojure/build.clj new file mode 100644 index 0000000000..67267daa7d --- /dev/null +++ b/challenge-224/tyler-wardhaugh/clojure/build.clj @@ -0,0 +1,19 @@ +(ns build + (:refer-clojure :exclude [test]) + (:require [org.corfield.build :as bb])) + +(def lib 'net.clojars.c224/c224) +(def version "0.1.0-SNAPSHOT") +(def main 'c224.c224) + +(defn test "Run the tests." [opts] + (bb/run-tests opts)) + +(def clean bb/clean) + +(defn ci "Run the CI pipeline of tests (and build the uberjar)." [opts] + (-> opts + (assoc :lib lib :version version :main main) + (bb/run-tests) + (bb/clean) + (bb/uber))) diff --git a/challenge-224/tyler-wardhaugh/clojure/deps.edn b/challenge-224/tyler-wardhaugh/clojure/deps.edn index f3cec366e5..ddb4ddaca3 100644 --- a/challenge-224/tyler-wardhaugh/clojure/deps.edn +++ b/challenge-224/tyler-wardhaugh/clojure/deps.edn @@ -1,12 +1,9 @@ {:paths ["src" "resources"] :deps {org.clojure/clojure {:mvn/version "1.11.1"} - clojure/math.numeric-tower {:git/url "https://github.com/clojure/math.numeric-tower" - :git/sha "3e98b31da229d7d3a533f1cee0c509e9b349aa17"} - com.hypirion/primes {:mvn/version "0.2.2"} net.cgrand/xforms {:mvn/version "0.19.4"}} :aliases - {:t1 {:main-opts ["-m" "c223.t1"]} - :t2 {:main-opts ["-m" "c223.t2"]} + {:t1 {:main-opts ["-m" "c224.t1"]} + :t2 {:main-opts ["-m" "c224.t2"]} :build {:deps {io.github.seancorfield/build-clj {:git/tag "v0.8.3" :git/sha "7ac1f8d" ;; since we're building an app uberjar, we do not diff --git a/challenge-224/tyler-wardhaugh/clojure/src/c224/t1.clj b/challenge-224/tyler-wardhaugh/clojure/src/c224/t1.clj new file mode 100644 index 0000000000..ea41f23a17 --- /dev/null +++ b/challenge-224/tyler-wardhaugh/clojure/src/c224/t1.clj @@ -0,0 +1,18 @@ +(ns c224.t1) + +(def DEFAULT-INPUT ["abc" "xyz"]) + +(defn special-notes? + [source target] + (let [s (-> source frequencies) + t (-> target frequencies (update-vals -))] + (->> (merge-with + s t) + vals + (every? (complement neg?))))) + +(defn -main + "Run Task 1 with a given input N, defaulting to the first example from the + task description." + [& args] + (let [[SOURCE TARGET] (or args DEFAULT-INPUT)] + (println (if (special-notes? SOURCE TARGET) "true" "false")))) diff --git a/challenge-224/tyler-wardhaugh/clojure/src/c224/t2.clj b/challenge-224/tyler-wardhaugh/clojure/src/c224/t2.clj new file mode 100644 index 0000000000..ac50b8cb16 --- /dev/null +++ b/challenge-224/tyler-wardhaugh/clojure/src/c224/t2.clj @@ -0,0 +1,39 @@ +(ns c224.t2 + (:require + [clojure.edn :as edn] + [net.cgrand.xforms :as x])) + +(def DEFAULT-INPUT ["112358"]) + +(defn coll->n + [coll] + (->> coll (x/str identity) parse-long)) + +(defn additive-number? + [s] + (let [sv (into [] (map #(Character/digit % 10)) s) + len (count sv) + xf (x/for [i % + j (range 1 (- len i)) + :let [n1 (-> sv (subvec 0 i) coll->n) + n2 (-> sv (subvec i (+ i j)) coll->n) + rsv (-> sv (subvec (+ i j) len))]] + [n1 n2 rsv]) + f (fn f [_ [n1 n2 rsv]] + (let [sum (+ n1 n2) + sumlen (-> sum str count)] + (when (<= sumlen (count rsv)) + (let [n3 (-> rsv (subvec 0 sumlen) coll->n) + rrsv (-> rsv (subvec sumlen (count rsv)))] + (when (= sum n3) + (if (zero? (count rrsv)) + (reduced true) + (f nil [n2 n3 rrsv])))))))] + (boolean (transduce xf (completing f) nil (range 1 len))))) + +(defn -main + "Run Task 2 with a given input COLL, defaulting to the first example from the + task description." + [& args] + (let [[N] (or args DEFAULT-INPUT)] + (println (if (additive-number? N) "true" "false")))) diff --git a/challenge-224/tyler-wardhaugh/clojure/test/c224/t1_test.clj b/challenge-224/tyler-wardhaugh/clojure/test/c224/t1_test.clj new file mode 100644 index 0000000000..c64576d21d --- /dev/null +++ b/challenge-224/tyler-wardhaugh/clojure/test/c224/t1_test.clj @@ -0,0 +1,9 @@ +(ns c224.t1-test + (:require [clojure.test :refer [deftest is testing]] + [c224.t1 :refer [special-notes?]])) + +(deftest task-1 + (testing "Task 1 produces the correct results from examples in the description" + (is (false? (special-notes? "abc" "xyz"))) + (is (true? (special-notes? "scriptinglanguage" "perl"))) + (is (true? (special-notes? "aabbcc" "abc"))))) diff --git a/challenge-224/tyler-wardhaugh/clojure/test/c224/t2_test.clj b/challenge-224/tyler-wardhaugh/clojure/test/c224/t2_test.clj new file mode 100644 index 0000000000..af887c72f3 --- /dev/null +++ b/challenge-224/tyler-wardhaugh/clojure/test/c224/t2_test.clj @@ -0,0 +1,9 @@ +(ns c224.t2-test + (:require [clojure.test :refer [deftest is testing]] + [c224.t2 :refer [additive-number?]])) + +(deftest task-1 + (testing "Task 2 produces the correct results from examples in the description" + (is (true? (additive-number? "112358"))) + (is (false? (additive-number? "12345"))) + (is (true? (additive-number? "199100199"))))) |
