aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorE7-87-83 <fungcheokyin@gmail.com>2022-05-01 07:57:20 +0800
committerE7-87-83 <fungcheokyin@gmail.com>2022-05-01 07:57:20 +0800
commit2bb8a6d519ee62fd9e85fb2a86a7e75e2b331ba6 (patch)
tree6239c3b90a32a61f76fae30320a6ecca441e7bd1
parentc06e9274949519c37a5b740c5148c4f8fbb9c7e7 (diff)
downloadperlweeklychallenge-club-2bb8a6d519ee62fd9e85fb2a86a7e75e2b331ba6.tar.gz
perlweeklychallenge-club-2bb8a6d519ee62fd9e85fb2a86a7e75e2b331ba6.tar.bz2
perlweeklychallenge-club-2bb8a6d519ee62fd9e85fb2a86a7e75e2b331ba6.zip
Julia soln for task 1 and negative test for the Perl script
-rw-r--r--challenge-162/cheok-yin-fung/julia/ch-1.jl40
-rw-r--r--challenge-162/cheok-yin-fung/perl/ch-1.pl3
2 files changed, 42 insertions, 1 deletions
diff --git a/challenge-162/cheok-yin-fung/julia/ch-1.jl b/challenge-162/cheok-yin-fung/julia/ch-1.jl
new file mode 100644
index 0000000000..b832af7a4d
--- /dev/null
+++ b/challenge-162/cheok-yin-fung/julia/ch-1.jl
@@ -0,0 +1,40 @@
+# The Weekly Challenge 162
+# Task 1 ISBN-13
+# Julia Solution
+
+using IterTools
+
+function p_int(s)
+ return parse(Int64, s)
+end
+
+
+
+function lookup(partialisbn)
+ return only(filter( d -> valid(partialisbn * string(d)), 0:9) )
+end
+
+
+function valid(isbn)
+ weight = push!(repeat( [1,3], 6), 1)
+ arr = p_int.(collect(
+ takestrict(
+ (filter(d-> (tryparse(Int64,d) isa Number),
+ split(isbn, ""))),
+ 13)))
+ return sum(Base.splat(*),zip(weight,arr)) % 10 == 0
+end
+
+
+#= testing
+valid("978-0-306-40615-7") # "task example"
+valid("978-1-492-04503-8") # "Think Julia"
+valid("978-1-59327-666-9") # "How Software Works"
+valid("978-1-260-08450-4") # "Database System Concepts"
+!valid("1234567891234") # random sequence of digits
+
+lookup("978-0-306-40615-?") # "task example"
+lookup("978-1-492-04503-?") # "Think Julia"
+lookup("978-1-59327-666-") # "How Software Works"
+lookup("978-1-260-08450-") # "Database System Concepts"
+=#
diff --git a/challenge-162/cheok-yin-fung/perl/ch-1.pl b/challenge-162/cheok-yin-fung/perl/ch-1.pl
index f9a10d1e5b..95c0df5b10 100644
--- a/challenge-162/cheok-yin-fung/perl/ch-1.pl
+++ b/challenge-162/cheok-yin-fung/perl/ch-1.pl
@@ -34,8 +34,9 @@ sub valid {
-use Test::More tests => 4;
+use Test::More tests => 5;
ok valid("978-0-306-40615-7"), "task example";
ok valid("978-1-492-04503-8"), "Think Julia";
ok valid("978-1-59327-666-9"), "How Software Works";
ok valid("978-1-260-08450-4"), "Database System Concepts";
+ok !valid("123-4-567-89123-4"), "random strings of digits";