Compare commits
No commits in common. "031dbf06c95a3a0f6f273f05085d8aeac82e6a63" and "9b6fb22a41c164538d0fa4b609946a09ca9126e1" have entirely different histories.
031dbf06c9
...
9b6fb22a41
4 changed files with 68 additions and 16 deletions
|
|
@ -26,6 +26,53 @@ const BASE30_ALPHABET = UInt8.([
|
||||||
87:90..., #W-Z
|
87:90..., #W-Z
|
||||||
])
|
])
|
||||||
|
|
||||||
|
"""
|
||||||
|
rebase(v::Integer, base::Integer)
|
||||||
|
|
||||||
|
Rebase `v` into the base system of `base`. `v` can be any integer type, and can
|
||||||
|
be represented in base 10 (decimal) or base 16 (hexadecimal) based on Julia's
|
||||||
|
treatment of that integer type. Returns a `Vector{UInt8}` containing the
|
||||||
|
**indices** of each digit in the rebased number. Since Julia is 1-indexed, this
|
||||||
|
means that `rebase(0, base)[1]` actually returns a value of `UInt8[1]`, since
|
||||||
|
the first index of a numeral system's alphabet is assumed to be its ``0`` value.
|
||||||
|
|
||||||
|
# Examples
|
||||||
|
|
||||||
|
```jldoctest
|
||||||
|
julia> rebase(0, 10)
|
||||||
|
1-element Vector{UInt8}:
|
||||||
|
0x01
|
||||||
|
|
||||||
|
julia> # Two plus two is ten... IN BASE FOUR! I'M FINE!
|
||||||
|
|
||||||
|
julia> rebase(2 + 2, 4)
|
||||||
|
2-element Vector{UInt8}:
|
||||||
|
0x02
|
||||||
|
0x01
|
||||||
|
|
||||||
|
julia> base_4_alphabet = ['0', '1', '2', '3'];
|
||||||
|
|
||||||
|
julia> String([base_4_alphabet[i] for i in rebase(2 + 2, 4)])
|
||||||
|
"10"
|
||||||
|
```
|
||||||
|
|
||||||
|
!!! warning
|
||||||
|
|
||||||
|
This method is not tested on negative integers and may produce incorrect
|
||||||
|
output when passed negative numbers
|
||||||
|
"""
|
||||||
|
function rebase(v::Integer, base::Integer)
|
||||||
|
remainder = UInt8((v % base) + 1)
|
||||||
|
quotient = div(v, base)
|
||||||
|
|
||||||
|
if quotient == 0
|
||||||
|
return [remainder]
|
||||||
|
else
|
||||||
|
return [rebase(quotient, base)..., remainder]
|
||||||
|
end #if
|
||||||
|
|
||||||
|
end #function
|
||||||
|
|
||||||
"""
|
"""
|
||||||
base30encode(v::Integer)
|
base30encode(v::Integer)
|
||||||
base30encode(v::UUID)
|
base30encode(v::UUID)
|
||||||
|
|
@ -44,9 +91,14 @@ julia> base30encode(30)
|
||||||
julia> using UUIDs; base30encode(uuid1())
|
julia> using UUIDs; base30encode(uuid1())
|
||||||
"7ZK00JECBDF2H7GNBXN59C6S9S"
|
"7ZK00JECBDF2H7GNBXN59C6S9S"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
!!! warning
|
||||||
|
|
||||||
|
Just like [`rebase`](@ref), this method is not tested on negative integers
|
||||||
|
and may produce incorrect output when passed negative numbers
|
||||||
"""
|
"""
|
||||||
function base30encode(v::Integer)
|
function base30encode(v::Integer)
|
||||||
return String([BASE30_ALPHABET[i] for i in reverse(digits(v; base = 30) .+ 1)])
|
return String([BASE30_ALPHABET[i] for i in rebase(v, 30)])
|
||||||
end #function
|
end #function
|
||||||
|
|
||||||
function base30encode(v::UUID)
|
function base30encode(v::UUID)
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,6 @@ using UUIDs: UUID
|
||||||
|
|
||||||
include("Base30.jl")
|
include("Base30.jl")
|
||||||
include("ResultCodeOption.jl")
|
include("ResultCodeOption.jl")
|
||||||
include("Minting.jl")
|
|
||||||
|
|
||||||
export ResultCode
|
export ResultCode
|
||||||
export ResultCodeOption
|
export ResultCodeOption
|
||||||
|
|
@ -15,6 +14,7 @@ export has_duplicate
|
||||||
export has_fixed
|
export has_fixed
|
||||||
export has_not_fixable
|
export has_not_fixable
|
||||||
export has_valid
|
export has_valid
|
||||||
|
export rebase
|
||||||
|
|
||||||
function _create_arg_table()
|
function _create_arg_table()
|
||||||
s = ArgParseSettings(autofix_names = true)
|
s = ArgParseSettings(autofix_names = true)
|
||||||
|
|
|
||||||
|
|
@ -1,11 +0,0 @@
|
||||||
"""
|
|
||||||
_hamming(str1::AbstractString, str2::AbstractString)
|
|
||||||
|
|
||||||
Computes the [Hamming distance](https://en.wikipedia.org/wiki/Hamming_distance) between
|
|
||||||
`str1` and `str2`.
|
|
||||||
"""
|
|
||||||
function _hamming(str1::AbstractString, str2::AbstractString)
|
|
||||||
length(str1) != length(str2) &&
|
|
||||||
throw(DimensionMismatch("strings must be of equal length"))
|
|
||||||
return sum(c -> first(c) != last(c), zip(str1, str2))
|
|
||||||
end #function
|
|
||||||
|
|
@ -1,10 +1,21 @@
|
||||||
using CualerID: base30encode, rebase
|
using CualerID: base30encode, rebase
|
||||||
|
|
||||||
|
@testset "rebase" begin
|
||||||
|
@test rebase(0, 10) == UInt8[1]
|
||||||
|
@test rebase(0, 2) == UInt8[1]
|
||||||
|
@test rebase(0, 32) == UInt8[1]
|
||||||
|
|
||||||
|
@test rebase(10, 10) == UInt8[2, 1]
|
||||||
|
@test rebase(2, 2) == UInt8[2, 1]
|
||||||
|
@test rebase(32, 32) == UInt8[2, 1]
|
||||||
|
|
||||||
|
@test rebase(11, 10) == UInt8[2, 2]
|
||||||
|
@test rebase(3, 2) == UInt8[2, 2]
|
||||||
|
@test rebase(33, 32) == UInt8[2, 2]
|
||||||
|
end #@testset
|
||||||
|
|
||||||
@testset "base30encode" begin
|
@testset "base30encode" begin
|
||||||
@test base30encode(0) == "0"
|
@test base30encode(0) == "0"
|
||||||
@test base30encode(1) == "1"
|
@test base30encode(1) == "1"
|
||||||
@test base30encode(29) == "Z"
|
|
||||||
@test base30encode(30) == "10"
|
@test base30encode(30) == "10"
|
||||||
@test base30encode(31) == "11"
|
|
||||||
@test base30encode(59) == "1Z"
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue