diff options
| author | E7-87-83 <fungcheokyin@gmail.com> | 2022-05-01 10:59:43 +0800 |
|---|---|---|
| committer | E7-87-83 <fungcheokyin@gmail.com> | 2022-05-01 10:59:43 +0800 |
| commit | b1f7bf280c5ff285012778f8226dc41bb606c866 (patch) | |
| tree | d4a22fe41ff51a0658b69fec5582872fb5049574 /challenge-162 | |
| parent | 2bb8a6d519ee62fd9e85fb2a86a7e75e2b331ba6 (diff) | |
| download | perlweeklychallenge-club-b1f7bf280c5ff285012778f8226dc41bb606c866.tar.gz perlweeklychallenge-club-b1f7bf280c5ff285012778f8226dc41bb606c866.tar.bz2 perlweeklychallenge-club-b1f7bf280c5ff285012778f8226dc41bb606c866.zip | |
check digit cases for Task 1 Perl script; Task 2 Partial Julia solution
Diffstat (limited to 'challenge-162')
| -rw-r--r-- | challenge-162/cheok-yin-fung/julia/ch-2.jl | 100 | ||||
| -rw-r--r-- | challenge-162/cheok-yin-fung/perl/ch-1.pl | 6 |
2 files changed, 105 insertions, 1 deletions
diff --git a/challenge-162/cheok-yin-fung/julia/ch-2.jl b/challenge-162/cheok-yin-fung/julia/ch-2.jl new file mode 100644 index 0000000000..12d3a012e6 --- /dev/null +++ b/challenge-162/cheok-yin-fung/julia/ch-2.jl @@ -0,0 +1,100 @@ +# The Weekly Challenge 162 +# Task 2 Wheatstone-Playfair cipher +# Julia Solution, +# Partial (only encrypt has been implemented yet) +# -- 1st May 2022 11AM HKT +# Usage: codify( key , word-to-be-encrypted) + +using LinearAlgebra + +function only_special(c) + if c == "J" + return 'I' + else + return only(c) + end +end + + + +function preprocess(word) + word = replace(uppercase(word), " " => "", "J" => "I") + word_a = split(word, "") + new_word_a = [] + for i in 1:(length(word_a)-1) + push!(new_word_a, word_a[i]) + if word_a[i] == word_a[i+1] + push!(new_word_a, "X") + end + end + new_word = join(new_word_a,"")*word_a[end] + return new_word +end + + + +function pairup(word) + if length(word) % 2 == 1 + word *= "X" + end + word_h = [] + word_t = [] + flip = true + for c in word + if flip + push!(word_h, c) + else + push!(word_t, c) + end + flip = !flip + end + return zip(word_h, word_t) +end + + + +function playfair_board(key) + letters = unique(Base.vcat( + filter( c -> c != ' ' , + only_special.(split(uppercase(key), "")) + ), + Char.(vcat(codepoint('A'):codepoint('I'), + codepoint('K'):codepoint('Z')) + ) + )) + + board_vv = [ letters[ 1:5], + letters[ 6:10], + letters[11:15], + letters[16:20], + letters[21:25] ] + return Char.(transpose(codepoint.(reduce(hcat,board_vv)))) + # transpose(reduce(hcat,board_vv)) does not work! +end + + + +function codify(key, word) + board = playfair_board(key) + zipped_words = pairup(preprocess(word)) + encrypted_v = [] + for p in collect(zipped_words) + (x1, y1) = Tuple(findfirst(c->c==first(p), board)) + (x2, y2) = Tuple(findfirst(c->c== last(p), board)) + if x1 == x2 + push!(encrypted_v, board[x1, 1+y1%5] * board[x2, 1+y2%5]) + continue + end + if y1 == y2 + push!(encrypted_v, board[1+x1%5, y1] * board[1+x2%5, y2]) + continue + end + push!(encrypted_v, board[x1,y2] * board[x2,y1]) + end + + return join(encrypted_v, "") +end + + + +# codify("playfair example", "hide the gold in the tree stump") diff --git a/challenge-162/cheok-yin-fung/perl/ch-1.pl b/challenge-162/cheok-yin-fung/perl/ch-1.pl index 95c0df5b10..b54c0590b8 100644 --- a/challenge-162/cheok-yin-fung/perl/ch-1.pl +++ b/challenge-162/cheok-yin-fung/perl/ch-1.pl @@ -34,9 +34,13 @@ sub valid { -use Test::More tests => 5; +use Test::More tests => 9; 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"; +ok 7==lookup("978-0-306-40615-?"), "(check digit) task example"; +ok 8==lookup("978-1-492-04503-?"), "(check digit) Think Julia"; +ok 9==lookup("978-1-59327-666-?"), "(check digit) How Software Works"; +ok 4==lookup("978-1-260-08450-?"), "(check digit) Database System Concepts"; |
