Skip to content

Commit

Permalink
chore: add check_format.yml (#217)
Browse files Browse the repository at this point in the history
* style: use proper formatting

* chore: add `check_format.yml`

* fix: format new code

* style: mark end of the document

Co-authored-by: David Leal <halfpacho@gmail.com>

* chore: use julia 1.9

Co-authored-by: David Leal <halfpacho@gmail.com>

---------

Co-authored-by: David Leal <halfpacho@gmail.com>
  • Loading branch information
vil02 and Panquesito7 authored Jan 15, 2024
1 parent 6046d53 commit 384424b
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 42 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/check_format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
name: check_format

# yamllint disable-line rule:truthy
on:
workflow_dispatch:
push:
branches:
- main
pull_request:

jobs:
check_format:
name: Check format
runs-on: ubuntu-latest
steps:
- uses: julia-actions/setup-julia@v1
with:
version: 1.9
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Install JuliaFormatter
run: |
julia -e 'using Pkg; Pkg.add(PackageSpec(name="JuliaFormatter"))'
- name: Format code
run: |
git clean -f -x -d
julia -e 'using JuliaFormatter; format(".", verbose=true)'
- name: Fail if needs reformatting
run: |
if [[ $(git status --porcelain) ]]; then
echo "please reformat these files:"
git status --porcelain=v1
exit 1
fi
...
14 changes: 7 additions & 7 deletions src/cipher/vigenere.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"


"""
encrypt_vigenere(text, key)
Expand All @@ -27,16 +26,16 @@ function encrypt_vigenere(text::String, key::String)::String
num += findfirst(isequal(key[key_index]), LETTERS) - 1
num %= length(LETTERS)
num = num == 0 ? 26 : num
encrypted *= isuppercase(symbol) ? LETTERS[num] : lowercase(LETTERS[num])
encrypted *=
isuppercase(symbol) ? LETTERS[num] : lowercase(LETTERS[num])
key_index = key_index == length(key) ? 1 : key_index + 1
else
encrypted *= symbol
end
end
encrypted
return encrypted
end


"""
decrypt_vigenere(text, key)
Expand All @@ -63,13 +62,14 @@ function decrypt_vigenere(text::String, key::String)::String
num -= findfirst(isequal(key[key_index]), LETTERS) - 1
num %= length(LETTERS)
num = num <= 0 ? num + length(LETTERS) : num
decrypted *= isuppercase(symbol) ? LETTERS[num] : lowercase(LETTERS[num])
decrypted *=
isuppercase(symbol) ? LETTERS[num] : lowercase(LETTERS[num])
key_index = key_index == length(key) ? 1 : key_index + 1
else
decrypted *= symbol
end
end
decrypted
return decrypted
end

println(encrypt_vigenere("QUICKBROWNFOX", "LAZYDOG"))
println(encrypt_vigenere("QUICKBROWNFOX", "LAZYDOG"))
2 changes: 1 addition & 1 deletion src/longest_increasing_subsequence/binary_search.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ https://cp-algorithms.com/sequences/longest_increasing_subsequence.html
- [Igor Malheiros](https://github.com/igormalheiros)
"""

function lis(arr::Array{T}, ::Val{:bs}) where T <: Integer
function lis(arr::Array{T}, ::Val{:bs}) where {T<:Integer}
len = length(arr)
memo = ones(T, len)
p = ones(Int, len)
Expand Down
2 changes: 1 addition & 1 deletion src/longest_increasing_subsequence/dynamic_programming.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ https://cp-algorithms.com/sequences/longest_increasing_subsequence.html
- [Igor Malheiros](https://github.com/igormalheiros)
"""

function lis(arr::Array{T}, ::Val{:dp}) where T <: Integer
function lis(arr::Array{T}, ::Val{:dp}) where {T<:Integer}
len = length(arr)
memo = ones(Int, len)
p = zeros(Int, len)
Expand Down
18 changes: 12 additions & 6 deletions src/math/runge_kutta_integration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,25 @@ julia> exp.([0.0, 0.1])
# Contributors:
- [E-W-Jones](https://github.com/E-W-Jones)
"""
function runge_kutta_integration(f::Function, x0::Real, y0::Real, h::Real, x_stop::Real)
function runge_kutta_integration(
f::Function,
x0::Real,
y0::Real,
h::Real,
x_stop::Real,
)
h > 0 || throw(DomainError(h, "The step size `h` should be >0."))

x = Float64(x0)
y = Float64(y0)
output_x = [x]
output_y = [y]

while x < x_stop
k1 = f(x, y)
k2 = f(x + h/2, y + k1*h/2)
k3 = f(x + h/2, y + k2*h/2)
k4 = f(x + h , y + k3*h )
k2 = f(x + h / 2, y + k1 * h / 2)
k3 = f(x + h / 2, y + k2 * h / 2)
k4 = f(x + h, y + k3 * h)

y += h * (k1 + 2k2 + 2k3 + k4) / 6
x += h
Expand All @@ -80,4 +86,4 @@ function runge_kutta_integration(f::Function, x0::Real, y0::Real, h::Real, x_sto
end

return output_x, output_y
end
end
2 changes: 1 addition & 1 deletion src/sorts/heap_sort.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ After the largest element has been extracted, the tree is updated to maintain th
function heap_sort!(arr::Vector{T}, gt = >, N::Int = length(arr)) where {T}
if isempty(arr)
return
end
end
n = N
i = div(n, 2)
t = -1
Expand Down
4 changes: 2 additions & 2 deletions src/strings/naive_pattern_search.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ julia> naive_pattern_search("Hello world!", "eggs")
"""

function naive_pattern_search(text, pattern)
for index in 0:(length(text)-length(pattern) + 1)
for index in 0:(length(text)-length(pattern)+1)
matches = 0
for character in eachindex(pattern)
if pattern[character] == text[index + character]
if pattern[character] == text[index+character]
matches += 1

if matches == length(pattern)
Expand Down
2 changes: 1 addition & 1 deletion test/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ using TheAlgorithms.Basic
@test Basic.prefix_sum([1, 1, 1]) == [1, 2, 3]
@test Basic.prefix_sum([1, 2, 3]) == [1, 3, 6]
@test Basic.prefix_sum(BigInt[]) == BigInt[]
@test Basic.prefix_sum([0., 0., 0.]) == [0., 0., 0.]
@test Basic.prefix_sum([0.0, 0.0, 0.0]) == [0.0, 0.0, 0.0]
@test Basic.prefix_sum([1 + 2im, 2 - 3im]) == [1 + 2im, 3 - 1im]
end

Expand Down
18 changes: 2 additions & 16 deletions test/graph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,14 @@ using TheAlgorithms.Graph
end

@testset "bfs" begin
graph = [
[2, 3, 6],
[3, 4],
[4],
[1, 2, 5],
[2],
[1, 5]
]
graph = [[2, 3, 6], [3, 4], [4], [1, 2, 5], [2], [1, 5]]

@test bfs(graph, 4) == [4, 1, 2, 5, 3, 6]
@test bfs(graph) == [1, 2, 3, 6, 4, 5]
end

@testset "dfs" begin
graph = [
[2, 3, 6],
[3, 4],
[4],
[1, 2, 5],
[2],
[1, 5]
]
graph = [[2, 3, 6], [3, 4], [4], [1, 2, 5], [2], [1, 5]]

@test dfs(graph, 6) == [6, 5, 2, 4, 3, 1]
@test dfs(graph) == [1, 6, 5, 3, 4, 2]
Expand Down
28 changes: 26 additions & 2 deletions test/longest_increasing_subsequence.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,19 @@ using TheAlgorithms.LongSubSeq
# Boolean array
@test lis([true, false, false, true], Val(:dp)) == [false, true]
# other Integer subtypes
for T in [UInt128, UInt16, UInt32, UInt64, UInt8, BigInt, Int128, Int16, Int32, Int64, Int8]
for T in [
UInt128,
UInt16,
UInt32,
UInt64,
UInt8,
BigInt,
Int128,
Int16,
Int32,
Int64,
Int8,
]
@test lis(T[3, 10, 2, 1, 20], Val(:dp)) == T[3, 10, 20]
end
end
Expand All @@ -48,7 +60,19 @@ using TheAlgorithms.LongSubSeq
# Boolean array
@test lis([true, false, false, true], Val(:bs)) == [false, true]
# other Integer subtypes
for T in [UInt128, UInt16, UInt32, UInt64, UInt8, BigInt, Int128, Int16, Int32, Int64, Int8]
for T in [
UInt128,
UInt16,
UInt32,
UInt64,
UInt8,
BigInt,
Int128,
Int16,
Int32,
Int64,
Int8,
]
@test lis(T[3, 10, 2, 1, 20], Val(:bs)) == T[3, 10, 20]
end
end
Expand Down
16 changes: 12 additions & 4 deletions test/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -378,12 +378,20 @@ using TheAlgorithms.Math
end

@testset "Math: Runge_Kutta Integration" begin
@test runge_kutta_integration((x, y)->1, 0, 0, 1, 3) == ([0.0, 1.0, 2.0, 3.0], [0.0, 1.0, 2.0, 3.0])
@test runge_kutta_integration((x, y) -> 1, 0, 0, 1, 3) ==
([0.0, 1.0, 2.0, 3.0], [0.0, 1.0, 2.0, 3.0])
@test begin
x, y = runge_kutta_integration((x, y)->cos(x), 0, 0, 1e-4, π/2)
isapprox(x[end], π/2; rtol=1e-4) && isapprox(y[end], 1; rtol=1e-4)
x, y = runge_kutta_integration((x, y) -> cos(x), 0, 0, 1e-4, π / 2)
isapprox(x[end], π / 2; rtol = 1e-4) &&
isapprox(y[end], 1; rtol = 1e-4)
end
@test_throws DomainError runge_kutta_integration((x, y)->(), 0, 0, 0, 0)
@test_throws DomainError runge_kutta_integration(
(x, y) -> (),
0,
0,
0,
0,
)
end

@testset "Math: Sum of Arithmetic progression" begin
Expand Down
2 changes: 1 addition & 1 deletion test/sorts.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using TheAlgorithms.Sorts

@testset "Sorts" begin
@testset "bogo_sort" begin
@testset "bogo_sort" begin
x = [3, 1, 4, 2]
bogo_sort!(x)
@test x == [1, 2, 3, 4]
Expand Down

0 comments on commit 384424b

Please sign in to comment.