Skip to content

Commit 7712892

Browse files
Merge branch 'main' into directory_writer
2 parents 3c4032d + b5269b4 commit 7712892

18 files changed

+175
-32
lines changed

.gitpod.dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM gitpod/workspace-base:2023-10-04-18-52-46
1+
FROM gitpod/workspace-base:2023-10-19-14-24-02
22

33
ENV JULIA_TMP="julia_tmp.tar.gz"
44

src/basic/difference_arr.jl

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111
- print array after any numbers of changes - O(N)
1212
1313
# Functions
14-
- create_diff_arr(original::Array{T}) - Create difference array for array 'original'
15-
- calculate_arr(diff_arr::Array{T}) - Create a original array from the given difference array
16-
- add_to_arr(diff_arr::Array{T}, l::Int, r::Int, x::Number) - Add x to all elements with index from [l, r]
14+
- create_diff_arr(original::Array{<:Number})
15+
* Create difference array for array 'original'
16+
- calculate_arr(diff_arr::Array{<:Number})
17+
* Recreate the original array from the given difference array
18+
- add_to_arr(diff_arr::Array{<:Number}, l::Int, r::Int, x::Number)
19+
* Add x to all elements with index from [l, r]
1720
1821
1922
# Contributed by: [Nikola Mircic](https://github.com/Nikola-Mircic)
@@ -25,7 +28,7 @@ module DifferenceArray
2528
function create_diff_arr(original::Array{T}) where {T<:Number}
2629
n = length(original)
2730

28-
diff_arr = Array(original)
31+
diff_arr = copy(original)
2932

3033
for i in 2:n
3134
diff_arr[i] = original[i] - original[i-1]
@@ -40,7 +43,7 @@ end
4043
function calculate_arr(diff_arr::Array{T}) where {T<:Number}
4144
n = length(diff_arr)
4245

43-
arr = Array(diff_arr)
46+
arr = copy(diff_arr)
4447

4548
for i in 2:n
4649
arr[i] = diff_arr[i] + arr[i-1]
@@ -51,7 +54,7 @@ end
5154

5255
# Add x to all elements with index from [l, r]
5356
# Parameters:
54-
# - dif_arr - a difference array of the array you want to change
57+
# - diff_arr - a difference array of the array you want to change
5558
# - l - leftmost index of the affected range
5659
# - r - rightmost index of the affected range
5760
# - x - a value to be added to all elements from a given range

src/basic/prefix_sum.jl

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,32 @@
1+
"""
2+
prefix_sum(arr::Vector{<:Number})
3+
4+
# Brief
5+
Given an input array of numbers, return an array of the sum of each "prefix" of the input array i.e.
6+
the 1st element, 1st + 2nd element, 1st + 2nd + 3rd, etc.
7+
8+
This functionality is available in base Julia as `cumsum`.
9+
10+
# Arguments
11+
- `arr`: an array of numbers
12+
13+
# Examples
14+
```julia
15+
julia> prefix_sum([1, 2, 3])
16+
3-element Vector{Int64}:
17+
1
18+
3
19+
6
20+
21+
julia> prefix_sum([0.0, 10.0, π])
22+
3-element Vector{Float64}:
23+
0.0
24+
10.0
25+
13.141592653589793
26+
```
27+
"""
128
function prefix_sum(arr::Vector{T}) where {T<:Number}
2-
pre = []
29+
pre = T[]
330
preans = zero(T)
431
for i in arr
532
preans += i

src/graph/bfs.jl

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,21 @@ TheAlgorithms.Graph.bfs(graph, 4)
2323
2424
# output
2525
26-
4 1 2 5 3 6
26+
6-element Vector{Int64}:
27+
4
28+
1
29+
2
30+
5
31+
3
32+
6
2733
```
2834
2935
Contributed by: [Yannick Brenning](https://github.com/ybrenning)
3036
"""
3137
function bfs(graph::Vector{Vector{Int}}, source::Int = 1)
3238
# Use a boolean "visited" array to avoid processing a vertex more than once
3339
visited = [false for _ in 1:length(graph)]
40+
result = Vector{Int}()
3441

3542
queue = Queue{Int}()
3643
enqueue!(queue, source)
@@ -39,7 +46,7 @@ function bfs(graph::Vector{Vector{Int}}, source::Int = 1)
3946

4047
while length(queue) > 0
4148
curr_v = dequeue!(queue)
42-
print(curr_v, " ")
49+
push!(result, curr_v)
4350

4451
# Add every unvisited target to the end of the queue
4552
for i in 1:length(graph[curr_v])
@@ -50,5 +57,5 @@ function bfs(graph::Vector{Vector{Int}}, source::Int = 1)
5057
end
5158
end
5259

53-
return print("\n")
60+
return result
5461
end

src/graph/dfs.jl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,21 @@ dfs(graph, 6)
2323
2424
# output
2525
26-
6 5 2 4 3 1
26+
6-element Vector{Int64}:
27+
6
28+
5
29+
2
30+
4
31+
3
32+
1
2733
```
2834
2935
Contributed by: [Yannick Brenning](https://github.com/ybrenning)
3036
"""
3137
function dfs(graph::Vector{Vector{Int}}, source::Int = 1)
3238
# Use a boolean "visited" array to avoid processing a vertex more than once
3339
visited = [false for _ in 1:length(graph)]
40+
result = Vector{Int}()
3441

3542
stack = Stack{Int}()
3643
push!(stack, source)
@@ -39,7 +46,7 @@ function dfs(graph::Vector{Vector{Int}}, source::Int = 1)
3946

4047
while length(stack) > 0
4148
curr_v = pop!(stack)
42-
print(curr_v, " ")
49+
push!(result, curr_v)
4350

4451
# Add every unvisited target to the top of the stack
4552
for i in 1:length(graph[curr_v])
@@ -49,6 +56,5 @@ function dfs(graph::Vector{Vector{Int}}, source::Int = 1)
4956
end
5057
end
5158
end
52-
53-
return print("\n")
59+
return result
5460
end

src/longest_increasing_subsequence/binary_search.jl

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
lis(arr::Array{Int}, ::Val{:bs})
2+
lis(arr::Array{<:Integer}, ::Val{:bs})
33
44
# Arguments:
55
- `arr`: sequence of integers
@@ -23,12 +23,12 @@ https://cp-algorithms.com/sequences/longest_increasing_subsequence.html
2323
- [Igor Malheiros](https://github.com/igormalheiros)
2424
"""
2525

26-
function lis(arr::Array{Int}, ::Val{:bs})
26+
function lis(arr::Array{T}, ::Val{:bs}) where T <: Integer
2727
len = length(arr)
28-
memo = ones(Int, len)
28+
memo = ones(T, len)
2929
p = ones(Int, len)
3030

31-
lis_arr = Int[]
31+
lis_arr = T[]
3232

3333
len == 0 && return lis_arr # if `arr` is empty
3434

@@ -48,12 +48,10 @@ function lis(arr::Array{Int}, ::Val{:bs})
4848
last_pos = lis_len
4949
for i in len:-1:1
5050
if p[i] == last_pos
51-
push!(lis_arr, arr[i])
51+
pushfirst!(lis_arr, arr[i])
5252
last_pos -= 1
5353
end
5454
end
5555

56-
reverse!(lis_arr)
57-
5856
return lis_arr
5957
end

src/longest_increasing_subsequence/dynamic_programming.jl

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
lis(arr::Array{Int}, ::Val{:dp})
2+
lis(arr::Array{<:Integer}, ::Val{:dp})
33
44
# Arguments:
55
- `arr`: sequence of integers
@@ -23,12 +23,12 @@ https://cp-algorithms.com/sequences/longest_increasing_subsequence.html
2323
- [Igor Malheiros](https://github.com/igormalheiros)
2424
"""
2525

26-
function lis(arr::Array{Int}, ::Val{:dp})
26+
function lis(arr::Array{T}, ::Val{:dp}) where T <: Integer
2727
len = length(arr)
2828
memo = ones(Int, len)
2929
p = zeros(Int, len)
3030

31-
lis_arr = Int[]
31+
lis_arr = T[]
3232

3333
len == 0 && return lis_arr # if arr is empty
3434

@@ -51,11 +51,9 @@ function lis(arr::Array{Int}, ::Val{:dp})
5151

5252
# Restoring
5353
while lis_pos != 0
54-
push!(lis_arr, arr[lis_pos])
54+
pushfirst!(lis_arr, arr[lis_pos])
5555
lis_pos = p[lis_pos]
5656
end
5757

58-
reverse!(lis_arr)
59-
6058
return lis_arr
6159
end

src/matrix/determinant.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Determinant of triangualar matrices is the product of their diagonal entries. He
1616
function determinant(mat)
1717
n, m = size(mat)
1818
if n != m
19-
DomainError(mat, "The matrix should be a square matrix.")
19+
throw(DomainError(mat, "The matrix should be a square matrix."))
2020
end
2121
L, U = lu_decompose(mat)
2222
l_prod = prod([L[i, i] for i in 1:n])

src/project_euler/ProjectEuler.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export problem_011
1919
export problem_012
2020
export problem_013
2121
export problem_014
22+
export problem_015
2223

2324
include("../math/divisors.jl")
2425
include("../math/sieve_of_eratosthenes.jl")
@@ -36,5 +37,6 @@ include("problem_011.jl")
3637
include("problem_012.jl")
3738
include("problem_013.jl")
3839
include("problem_014.jl")
40+
include("problem_015.jl")
3941

4042
end

src/project_euler/problem_010.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ Contributed by: [Praneeth Jain](https://www.github.com/PraneethJain)
2323
"""
2424
function problem_010(n::Int)
2525
n < 1 && throw(DomainError("n must be a natural number"))
26-
return reduce(+, eratosthenes(Int64(n)), init=Int64(0))
26+
return reduce(+, eratosthenes(Int64(n)), init = Int64(0))
2727
end

0 commit comments

Comments
 (0)