Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
luhn in julia, 4 styles
using BenchmarkTools
dbl2nd(da::Array{Int64,1}) = for i in 1:2:(length(da))
da[i] *= 2
end
mod9(da::Array{Int64,1}) = for i in 1:2:length(da)
if da[i] > 9
da[i] -= 9
end
end
function dblmod9(da::Array{Int64,1})
for i in length(da) - 1:-2:1
da[i] = (da[i] * 2) % 9
end
return da
end
#################################
## Version 1
# adapted from https://gist.github.com/larryfox/7f573afc0be4be74370d
function luhn(n::Int64)
d = digits(n, 10)
s = 0
for i = 1:length(d)
isodd(i) || (d[i] *= 2)
(d[i] < 10) || (d[i] = sum(digits(d[i])))
s += d[i]
end
s % 10 == 0
end
# v1
n1 = 4242424242424242
m1 = 4539148803436467
luhn(n1)
@benchmark luhn(m1)
#################################
# Version 2
function luhn(f::Function, n::Int64)
da = reverse(digits(n, 10))
f(da)
mod9(da)
return sum(da) % 10 == 0
end
# style 2
n2 = 4242424242424242
m2 = 4539148803436467
luhn(dbl2nd, n2)
@benchmark luhn(dbl2nd, m2)
#################################
# Version 3
function luhn(f::Function, f2::Function, n::Int64)
da = reverse(digits(n, 10))
f(da)
f2(da)
return sum(da) % 10 == 0
end
# style 3
n3 = 4242424242424242
m3 = 4539148803436467
luhn(dbl2nd, mod9, n3)
@benchmark luhn(dbl2nd, mod9, m3)
#################################
# Version 4
function luhn_two(n::Int64)
da = reverse(digits(n, 10))
dbl2nd(da)
mod9(da)
return sum(da) % 10 == 0
end
# style 4
n4 = 4242424242424242
m4 = 4539148803436467
luhn_two(n4)
@benchmark luhn_two(m4)
#################################
## version 5
function luhn_three(n::Int64)
da = reverse(digits(n, 10))
sum(
dblmod9(da)
) % 10 == 0
end
# style 5
n5 = 4242424242424242
m5 = 4539148803436467
luhn_three(n5)
@benchmark luhn_three(m5)
#################################
#v5.1
function luhn_four(n::Int64)
da = reverse(digits(n, 10))
for i in length(da) - 1:-2:1
da[i] = (da[i] * 2) % 9
end
return sum(da) % 10 == 0
end
# style 5.1
n51 = 4242424242424242
m51 = 4539148803436467
luhn_four(n51)
@benchmark luhn_four(m51)
#BenchmarkTools.Trial:
# memory estimate: 416 bytes
# allocs estimate: 2
# --------------
# minimum time: 433.369 ns (0.00% GC)
# median time: 517.288 ns (0.00% GC)
# mean time: 529.690 ns (1.31% GC)
# maximum time: 3.831 μs (69.11% GC)
# --------------
# samples: 10000
# evals/sample: 198
#################################
## Version 6
# taken from https://github.com/exercism/julia/blob/master/exercises/luhn/example.jl
function luhn(id::AbstractString)
id = split(replace(id, ' ', ""), "")
length(id) < 2 && return false
all(all(isdigit, s) for s in id) || return false
numbers = Int[]
for val in id
push!(numbers, parse(Int, val))
end
for i in length(numbers) - 1:-2:1
numbers[i] = (numbers[i] * 2) % 9
end
return sum(numbers) % 10 == 0
end
# style 6
n6 = "4242424242424242"
m6 = "4539148803436467"
luhn(n6)
@benchmark luhn(m6)
#################################
## version 7
# a lisp-like style
function luhn(n::Array{Int64,1})
sum(
dblmod9(n)
) % 10 == 0
end
# style 7
n7 = 4242424242424242
m7 = 4539148803436467
dn7 = reverse(digits(n7, 10))
dm7 = reverse(digits(m7, 10))
luhn(dn7)
@benchmark luhn(dm7)
#################################
# Version 8
## pass in functions and data
function luhn(f::Function, da::Array{Int64,1})
f(da)
mod9(da)
return sum(da) % 10 == 0
end
# stle 8
n8 = 4242424242424242
m8 = 4539148803436467
dn8 = reverse(digits(n8, 10))
dm8 = reverse(digits(m8, 10))
luhn(dbl2nd, dn8)
@benchmark luhn(dbl2nd, dm8)
#################################
# Version 9
function luhn(f::Function, f2::Function, da::Array{Int64,1})
f(da)
f2(da)
return sum(da) % 10 == 0
end
# style 9
n9 = 4242424242424242
m9 = 4539148803436467
dn9 = reverse(digits(n9, 10))
dm9 = reverse(digits(m9, 10))
luhn(dbl2nd, mod9, dn9)
@benchmark luhn(dbl2nd, mod9, dm9)
##########################
##########################
##########################
### print out the steps if you want to see
#mm = 4539148803436467
#dm = reverse(digits(mm))
#function see(numbers)
# for i in length(numbers) - 1:-2:1
# @printf("index: %d, integer: %d, by2: %d mod9: %d\n",i, numbers[i], (numbers[i] * 2), ((numbers[i] * 2) % 9))
# numbers[i] = (numbers[i] * 2) % 9
# end
#end
#
#see(dm)
#println(dm)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.