some fixes for v0.5

This commit is contained in:
cormullion
2017-04-28 11:06:03 +01:00
parent 4e02a36b07
commit 8c3a152225
5 changed files with 13 additions and 17 deletions

View File

@@ -4,7 +4,7 @@ function letters_only(text)
end
function rotateRight(arr, n)
# implementation of the Mathematica function RotateRight
# implementation of the Mathematica function RotateRight - or you could try circshift()?
ans = copy(arr)
for i in 1:length(arr)
ans[i] = arr[((2*length(ans)+i-n-1) % length(ans)) + 1]

View File

@@ -1,5 +1,5 @@
function keystr_to_dict(keystr::AbstractString)
Dict{Char, Char}(map(x -> (x[1]+64, x[2]), enumerate(uppercase(keystr))))
Dict{Char, Char}(map(x -> (Char(x[1]+64), x[2]), enumerate(uppercase(keystr))))
end
"""
@@ -47,7 +47,7 @@ function decrypt_monoalphabetic(ciphertext, key::AbstractString)
# working in lowercase; key is assumed only to have each element appearing once
# and to be in lowercase
# so decrypt_monoalphabetic("cb", "cbade…") is "ab"
dict = [(a => Char(96 + search(lowercase(key), a))) for a in lowercase(key)]
dict = Dict(a => Char(96 + search(lowercase(key), a)) for a in lowercase(key))
encrypt_monoalphabetic(lowercase(ciphertext), dict)
end
@@ -174,4 +174,3 @@ function crack_monoalphabetic(ciphertext; starting_key="",
end
(decrypt_monoalphabetic(ciphertext, key), key)
end

View File

@@ -12,9 +12,8 @@ function playfair_key_to_square(key::AbstractString, replacement)
key_sanitised = union(uppercase(letters_only(key)))
# construct key square
remaining = collect(filter(x -> (x != replacement[2] && findfirst(key_sanitised, x) == 0), 'A':'Z'))
keysquare = transpose(reshape([key_sanitised; remaining], 5, 5))
keysquare
keysquare = reshape([key_sanitised; remaining], 5, 5)
return permutedims(keysquare, (2, 1)) # transpose() is deprecated
end
function encrypt_playfair(plaintext, key::AbstractString; combined=('I','J'))
@@ -126,6 +125,6 @@ Does not attempt to delete X's inserted as padding for double letters.
function decrypt_playfair(ciphertext, key::Array{Char, 2}; combined=('I', 'J'))
# to obtain the decrypting keysquare, reverse every row and every column
keysquare = mapslices(reverse, key, 2)
keysquare = transpose(mapslices(reverse, transpose(keysquare), 2))
keysquare = permutedims(mapslices(reverse, permutedims(keysquare, (2, 1)), 2), (2, 1))
lowercase(encrypt_playfair(ciphertext, keysquare, combined=combined))
end

View File

@@ -1,4 +1,4 @@
include("common.jl")
# include("common.jl")
function next_solitaire(deckIn)
# performs one round of Solitaire on the given deck

View File

@@ -4,10 +4,8 @@ For example, encrypt_vigenere("ab", [0, 1]) returns "AC".
"""
function encrypt_vigenere(plaintext, key::Array)
# plaintext: string; key: vector of integer offsets, so [0, 1] encrypts "ab" as "ac"
ans = [encrypt_caesar(chr, key[(i-1) % length(key)+1]) for (i, chr) in enumerate(letters_only(plaintext))]
join(ans, "")
end
"""