mirror of
https://github.com/Smaug123/ClassicalCiphers.jl
synced 2025-10-05 01:18:48 +00:00
Merge pull request #35 from Smaug123/str-rotate-optimise
Rotating functions optimise
This commit is contained in:
@@ -3,30 +3,18 @@ function letters_only(text::AbstractString)
|
|||||||
return filter(x -> ('A' <= x <= 'Z' || 'a' <= x <= 'z'), text)
|
return filter(x -> ('A' <= x <= 'Z' || 'a' <= x <= 'z'), text)
|
||||||
end
|
end
|
||||||
|
|
||||||
function rotate_right(arr::AbstractVector, n::Integer)
|
rotate_left(arr::AbstractVector, n::Integer) = circshift(arr, -n)
|
||||||
# implementation of the Mathematica function rotate_right - or you could try circshift()?
|
rotate_right(arr::AbstractVector, n::Integer) = circshift(arr, n)
|
||||||
ans = copy(arr)
|
|
||||||
for i in 1:length(arr)
|
function rotate_string(str::AbstractString, n::Integer)
|
||||||
ans[i] = arr[((2*length(ans)+i-n-1) % length(ans)) + 1]
|
i = mod(n, length(str))
|
||||||
end
|
return str[i+1:end] * str[1:i]
|
||||||
|
|
||||||
return ans
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function rotate_left(arr::AbstractVector, n::Integer)
|
rotate_left(str::AbstractString, n::Integer) =
|
||||||
# implementation of the Mathematica function rotate_left
|
rotate_string(str, n)
|
||||||
ans = copy(arr)
|
rotate_right(str::AbstractString, n::Integer) =
|
||||||
for i in 1:length(arr)
|
rotate_string(str, -n)
|
||||||
ans[i] = arr[((i + n - 1) % length(ans)) + 1]
|
|
||||||
end
|
|
||||||
|
|
||||||
return ans
|
|
||||||
end
|
|
||||||
|
|
||||||
rotate_left_str(st::AbstractString, n::Integer) =
|
|
||||||
join(rotate_left(collect(st), n))
|
|
||||||
rotate_right_str(st::AbstractString, n::Integer) =
|
|
||||||
join(rotate_right(collect(st), n))
|
|
||||||
|
|
||||||
function split_by(arr::AbstractVector, func::Function)
|
function split_by(arr::AbstractVector, func::Function)
|
||||||
# implementation of the Mathematica function split_by
|
# implementation of the Mathematica function split_by
|
||||||
|
19
test/common.jl
Normal file
19
test/common.jl
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
@test ClassicalCiphers.letters_only("Se9Wj8NK2:'n") == "SeWjNKn"
|
||||||
|
|
||||||
|
@test ClassicalCiphers.rotate_left([78, 18, 53, 96, 15, 35, 72, 29, 34, 26], 3) == [96, 15, 35, 72, 29, 34, 26, 78, 18, 53]
|
||||||
|
@test ClassicalCiphers.rotate_right([78, 18, 53, 96, 15, 35, 72, 29, 34, 26], 3) == [29, 34, 26, 78, 18, 53, 96, 15, 35, 72]
|
||||||
|
|
||||||
|
@test ClassicalCiphers.rotate_string("Se9Wj8NK2:'n", 5) == "8NK2:'nSe9Wj"
|
||||||
|
@test ClassicalCiphers.rotate_left("Se9Wj8NK2:'n", 3) == "Wj8NK2:'nSe9"
|
||||||
|
@test ClassicalCiphers.rotate_right("Se9Wj8NK2:'n", 3) == ":'nSe9Wj8NK2"
|
||||||
|
|
||||||
|
@test ClassicalCiphers.split_by([78, 18, 53, 96, 15, 35, 72, 29, 34, 26], x -> x > 52) == [[78], [18], [53, 96], [15, 35], [72], [29, 34, 26]]
|
||||||
|
|
||||||
|
# @test get_trigram_fitness() # I don't think we need this test because the function in question is immediately called and used in ../src/common.jl
|
||||||
|
|
||||||
|
@test ClassicalCiphers.string_fitness("Se9Wj8NK2:'n") == 11.640550700535616
|
||||||
|
|
||||||
|
# @test ClassicalCiphers.frequencies("hello") == Dict('h' => 1, 'e' => 1, 'l' => 2, 'o' => 1)
|
||||||
|
|
||||||
|
@test ClassicalCiphers.index_of_coincidence("hello world") == 0.5777777777777777
|
||||||
|
@test ClassicalCiphers.index_of_coincidence("smaug123classicalciphers") == 0.6190476190476191
|
@@ -2,6 +2,7 @@ include(joinpath(dirname(@__DIR__), "src", "ClassicalCiphers.jl")); using .Class
|
|||||||
using Test
|
using Test
|
||||||
|
|
||||||
tests = [
|
tests = [
|
||||||
|
"common",
|
||||||
"playfair",
|
"playfair",
|
||||||
"vigenere",
|
"vigenere",
|
||||||
"monoalphabetic",
|
"monoalphabetic",
|
||||||
|
Reference in New Issue
Block a user