diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-01-31 02:13:55 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-01-31 02:13:55 +0000 |
| commit | 27eaafb596073707f124d33df55757ca87bd8040 (patch) | |
| tree | 37ce4428a81d4a8f821efa192cebee4d47e711fb /challenge-097 | |
| parent | 5eac7e73d5082ef138a8203632c893ffd9c701e7 (diff) | |
| parent | 91cec7a8eecae534830b2d1b78f2acbfa9b03729 (diff) | |
| download | perlweeklychallenge-club-27eaafb596073707f124d33df55757ca87bd8040.tar.gz perlweeklychallenge-club-27eaafb596073707f124d33df55757ca87bd8040.tar.bz2 perlweeklychallenge-club-27eaafb596073707f124d33df55757ca87bd8040.zip | |
Merge pull request #3419 from tylerw/tw/challenge-097
Challenge 097
Diffstat (limited to 'challenge-097')
| -rw-r--r-- | challenge-097/tyler-wardhaugh/clojure/src/tw/weekly/c97/core.clj | 12 | ||||
| -rw-r--r-- | challenge-097/tyler-wardhaugh/clojure/src/tw/weekly/c97/t1.clj | 28 | ||||
| -rw-r--r-- | challenge-097/tyler-wardhaugh/clojure/src/tw/weekly/c97/t2.clj | 25 | ||||
| -rw-r--r-- | challenge-097/tyler-wardhaugh/clojure/test/tw/weekly/c97_test.clj | 14 | ||||
| -rw-r--r-- | challenge-097/tyler-wardhaugh/lua/README.md | 6 | ||||
| -rwxr-xr-x | challenge-097/tyler-wardhaugh/lua/ch-1.lua | 34 | ||||
| -rwxr-xr-x | challenge-097/tyler-wardhaugh/lua/ch-2.lua | 40 | ||||
| -rwxr-xr-x | challenge-097/tyler-wardhaugh/lua/run.lua | 9 | ||||
| -rwxr-xr-x | challenge-097/tyler-wardhaugh/lua/test.lua | 20 | ||||
| -rwxr-xr-x | challenge-097/tyler-wardhaugh/python/ch1.py | 32 | ||||
| -rwxr-xr-x | challenge-097/tyler-wardhaugh/python/ch2.py | 35 | ||||
| -rw-r--r-- | challenge-097/tyler-wardhaugh/python/requirements.txt | 0 | ||||
| -rwxr-xr-x | challenge-097/tyler-wardhaugh/python/test_ch1.py | 16 | ||||
| -rwxr-xr-x | challenge-097/tyler-wardhaugh/python/test_ch2.py | 15 |
14 files changed, 283 insertions, 3 deletions
diff --git a/challenge-097/tyler-wardhaugh/clojure/src/tw/weekly/c97/core.clj b/challenge-097/tyler-wardhaugh/clojure/src/tw/weekly/c97/core.clj new file mode 100644 index 0000000000..4981804890 --- /dev/null +++ b/challenge-097/tyler-wardhaugh/clojure/src/tw/weekly/c97/core.clj @@ -0,0 +1,12 @@ +(ns tw.weekly.c97.core + (:require [tw.weekly.c97.t1 :as t1]) + (:require [tw.weekly.c97.t2 :as t2]) + (:gen-class)) + +(defn -main + "Run all tasks" + [& _] + (println "Task #1:") + (t1/-main) + (println "\nTask #2:") + (t2/-main)) diff --git a/challenge-097/tyler-wardhaugh/clojure/src/tw/weekly/c97/t1.clj b/challenge-097/tyler-wardhaugh/clojure/src/tw/weekly/c97/t1.clj new file mode 100644 index 0000000000..61e17f1c3c --- /dev/null +++ b/challenge-097/tyler-wardhaugh/clojure/src/tw/weekly/c97/t1.clj @@ -0,0 +1,28 @@ +(ns tw.weekly.c97.t1 + (:require [clojure.edn :as edn] + [clojure.string :as str])) + +;;; +; Task description for TASK #1 › Caesar Cipher +;;; + +(def DEFAULT-INPUT ["THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG" 3]) +(def ALPHABET "ABCDEFGHIJKLMNOPQRSTUVWXYZ") +(def ALEN (count ALPHABET)) + +(defn caesar + [s n] + (let [cipher (->> (repeat ALPHABET) + (mapcat seq) + (drop (- ALEN n)) + (take ALEN) + (zipmap ALPHABET))] + (str/escape s cipher))) + +(defn -main + "Run Task 1 using a string S and shift size S, defaulting to the example + given in the task description." + [& args] + (let [[S N] (or (some->> args (take 2) edn/read-string) DEFAULT-INPUT) + ciphertext (caesar S N)] + (println "Plaintext: " S "\nCiphertext: " ciphertext))) diff --git a/challenge-097/tyler-wardhaugh/clojure/src/tw/weekly/c97/t2.clj b/challenge-097/tyler-wardhaugh/clojure/src/tw/weekly/c97/t2.clj new file mode 100644 index 0000000000..abbef4ace9 --- /dev/null +++ b/challenge-097/tyler-wardhaugh/clojure/src/tw/weekly/c97/t2.clj @@ -0,0 +1,25 @@ +(ns tw.weekly.c97.t2 + (:require [clojure.edn :as edn])) + +;;; +; Task description for TASK #2 › Binary Substrings +;;; + +(def DEFAULT-INPUT ["101100101" 3]) + +(defn binary-substrings + [bstr size] + (let [parts (partition size bstr) + tranches (apply map vector parts) + xf (comp + (map (partial filter #{\0})) + (map (juxt count #(- size (count %)))) + (map (partial apply min)))] + (transduce xf + tranches))) + +(defn -main + "Run Task 2 using string B and size S, defaulting to the example given in + the task description." + [& args] + (let [[B S] (or (some->> args (take 2) (map edn/read-string)) DEFAULT-INPUT)] + (println (binary-substrings B S)))) diff --git a/challenge-097/tyler-wardhaugh/clojure/test/tw/weekly/c97_test.clj b/challenge-097/tyler-wardhaugh/clojure/test/tw/weekly/c97_test.clj new file mode 100644 index 0000000000..6f0d7f7ffb --- /dev/null +++ b/challenge-097/tyler-wardhaugh/clojure/test/tw/weekly/c97_test.clj @@ -0,0 +1,14 @@ +(ns tw.weekly.c97-test + (:require [clojure.test :refer [deftest is testing]] + [tw.weekly.c97.t1 :refer [caesar]] + [tw.weekly.c97.t2 :refer [binary-substrings]])) + +(deftest task-1 + (testing "Task 1, Caesar Cipher" + (is (= "QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD" + (caesar "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG" 3))))) + +(deftest task-2 + (testing "Task 2, Binary Substrings" + (is (= 1 (binary-substrings "101100101" 3))) + (is (= 2 (binary-substrings "10110111" 4))))) diff --git a/challenge-097/tyler-wardhaugh/lua/README.md b/challenge-097/tyler-wardhaugh/lua/README.md index b916c3c458..27612d81d8 100644 --- a/challenge-097/tyler-wardhaugh/lua/README.md +++ b/challenge-097/tyler-wardhaugh/lua/README.md @@ -1,17 +1,17 @@ # The Weekly Challenge -The Weekly Challenge - #096 - Tyler Wardhaugh +The Weekly Challenge - #097 - Tyler Wardhaugh ## Usage Run Task 1: - $ ./run.lua ch-1 S + $ ./run.lua ch-1 S N Run Task 2: - $ ./run.lua ch-2 S1 S2 + $ ./run.lua ch-2 B S Run the project's tests (all the samples from the task descriptions plus some others): diff --git a/challenge-097/tyler-wardhaugh/lua/ch-1.lua b/challenge-097/tyler-wardhaugh/lua/ch-1.lua new file mode 100755 index 0000000000..7d3c9dbcd5 --- /dev/null +++ b/challenge-097/tyler-wardhaugh/lua/ch-1.lua @@ -0,0 +1,34 @@ +#!/usr/bin/env lua + +local t1 = {} +t1.DEFAULT_INPUT = {'THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG', 3} + +function t1.caesar(s, n) + local aleph = ('A'):byte() + local zed = ('Z'):byte() + local diff = zed - aleph + + local cipher = {} + local c + for i = aleph, zed do + c = i - n + if c < aleph then c = c + diff + 1 end + cipher[string.char(i)] = string.char(c) + end + + return s:gsub('.', cipher) +end + +function t1.run(args) + local s, n + if #args > 0 then + s, n = args[1], tonumber(args[2]) + else + s, n = table.unpack(t1.DEFAULT_INPUT) + end + + print('Plaintext: ' .. s) + print('Ciphertext: ' .. t1.caesar(s, n)) +end + +return t1 diff --git a/challenge-097/tyler-wardhaugh/lua/ch-2.lua b/challenge-097/tyler-wardhaugh/lua/ch-2.lua new file mode 100755 index 0000000000..babbd8b8c8 --- /dev/null +++ b/challenge-097/tyler-wardhaugh/lua/ch-2.lua @@ -0,0 +1,40 @@ +local t2 = {} +t2.DEFAULT_INPUT = {'101100101', 3} + +function t2.binary_substrings(bstr, size) + local parts = {} + local s, e + for i = 0, (#bstr // size) - 1 do + s = (i * size) + 1 + e = s + size - 1 + table.insert(parts, bstr:sub(s, e)) + end + + local sum = 0 + local zeros, ones + for i = 1, size do + zeros = 0 + for _, p in ipairs(parts) do + if p:sub(i, i) == '0' then + zeros = zeros + 1 + end + end + ones = size - zeros + sum = sum + math.min(zeros, ones) + end + + return sum +end + +function t2.run(args) + local b, s + if #args > 0 then + b, s = args[1], tonumber(args[2]) + else + b, s = table.unpack(t2.DEFAULT_INPUT) + end + + print(t2.binary_substrings(b, s)) +end + +return t2 diff --git a/challenge-097/tyler-wardhaugh/lua/run.lua b/challenge-097/tyler-wardhaugh/lua/run.lua new file mode 100755 index 0000000000..c6e7473bee --- /dev/null +++ b/challenge-097/tyler-wardhaugh/lua/run.lua @@ -0,0 +1,9 @@ +#!/usr/bin/env lua + +local filename = arg[1] +local run_args = table.move(arg, 2, #arg, 1, {}) + +io.write(string.format("Running task from '%s' with {%s}:\n", + filename, table.concat(run_args, ", "))) + +require(filename).run(run_args) diff --git a/challenge-097/tyler-wardhaugh/lua/test.lua b/challenge-097/tyler-wardhaugh/lua/test.lua new file mode 100755 index 0000000000..d218876b01 --- /dev/null +++ b/challenge-097/tyler-wardhaugh/lua/test.lua @@ -0,0 +1,20 @@ +#!/usr/bin/env lua + +require 'busted.runner'() + +describe("Task 1, Caesar Cipher", function() + local t1 = require'ch-1' + it("produces correct results for the examples", function() + assert.are.same("QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD", + t1.caesar("THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG", 3)) + end) +end) + + +describe("Task 2, Binary Substrings", function() + local t2 = require'ch-2' + it("produces correct results for the examples", function() + assert.are.same(1, t2.binary_substrings("101100101", 3)) + assert.are.same(2, t2.binary_substrings("10110111", 4)) + end) +end) diff --git a/challenge-097/tyler-wardhaugh/python/ch1.py b/challenge-097/tyler-wardhaugh/python/ch1.py new file mode 100755 index 0000000000..e32abf63e7 --- /dev/null +++ b/challenge-097/tyler-wardhaugh/python/ch1.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 +"""Challenge 97, Task 1""" + +import string +import sys + + +DEFAULT_INPUT = ('THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG', 3) +ALPHABET = [ord(c) for c in string.ascii_uppercase] +ALEN = len(ALPHABET) + +def caesar(s, n): + aleph = ALPHABET[0] + def f(oc): + newoc = oc - n + return newoc if newoc >= aleph else newoc + ALEN + + cipher = {oc: f(oc) for oc in ALPHABET} + return s.translate(cipher) + + +def main(args=None): + """Run the task""" + if args is None: + args = sys.argv[1:] + + s, n = args[0:1] if args else DEFAULT_INPUT + print(caesar(s, n)) + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/challenge-097/tyler-wardhaugh/python/ch2.py b/challenge-097/tyler-wardhaugh/python/ch2.py new file mode 100755 index 0000000000..a9e13404e1 --- /dev/null +++ b/challenge-097/tyler-wardhaugh/python/ch2.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 +"""Challenge 97, Task 2""" + +import sys +from collections import Counter + +DEFAULT_INPUT = ('101100101', 3) + + +def _minflips(part): + cnt = Counter({'0': 0, '1': 0}) + cnt.update(part) + return min(*cnt.values()) + + +def binary_substrings(bstr, size): + numparts = len(bstr) // size + return sum(_minflips(bstr[i::size]) for i in range(numparts)) + + +def main(args=None): + """Run the task""" + if args is None: + args = sys.argv[1:] + + if args: + b, s = args[0:1] + else: + b, s = DEFAULT_INPUT + + print(binary_substrings(b, s)) + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/challenge-097/tyler-wardhaugh/python/requirements.txt b/challenge-097/tyler-wardhaugh/python/requirements.txt new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/challenge-097/tyler-wardhaugh/python/requirements.txt diff --git a/challenge-097/tyler-wardhaugh/python/test_ch1.py b/challenge-097/tyler-wardhaugh/python/test_ch1.py new file mode 100755 index 0000000000..147b96e5d9 --- /dev/null +++ b/challenge-097/tyler-wardhaugh/python/test_ch1.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python3 + +import unittest +from ch1 import caesar + +class TestTask1(unittest.TestCase): + + + def test_example_cases(self): + self.assertEqual( + 'QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD', + caesar('THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG', 3)) + + +if __name__ == '__main__': + unittest.main() diff --git a/challenge-097/tyler-wardhaugh/python/test_ch2.py b/challenge-097/tyler-wardhaugh/python/test_ch2.py new file mode 100755 index 0000000000..f7f6ab56a1 --- /dev/null +++ b/challenge-097/tyler-wardhaugh/python/test_ch2.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python3 + +import unittest +from ch2 import binary_substrings + +class TestTask2(unittest.TestCase): + + + def test_example_cases(self): + self.assertEqual(1, binary_substrings('101100101', 3)) + self.assertEqual(2, binary_substrings('10110111', 4)) + + +if __name__ == '__main__': + unittest.main() |
