Skip to content

Commit

Permalink
Merge branch 'main' into julia1.9
Browse files Browse the repository at this point in the history
  • Loading branch information
Panquesito7 authored Jan 15, 2024
2 parents c673d1a + bfd8a43 commit 8b409aa
Show file tree
Hide file tree
Showing 37 changed files with 846 additions and 211 deletions.
11 changes: 8 additions & 3 deletions .gitpod.dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
FROM gitpod/workspace-full-vnc
FROM gitpod/workspace-base:2023-10-19-14-24-02

RUN sudo apt-get update \
&& sudo apt-get install julia -y
ENV JULIA_TMP="julia_tmp.tar.gz"

RUN test ! -e "${JULIA_TMP}" \
&& curl https://julialang-s3.julialang.org/bin/linux/x64/1.9/julia-1.9.3-linux-x86_64.tar.gz -sSf -o "${JULIA_TMP}" \
&& tar zxvf ${JULIA_TMP} \
&& rm ${JULIA_TMP} \
&& echo "export PATH=${HOME}/julia-1.9.3/bin/:\$PATH" >> ~/.bashrc
15 changes: 9 additions & 6 deletions src/basic/difference_arr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
- print array after any numbers of changes - O(N)
# Functions
- create_diff_arr(original::Array{T}) - Create difference array for array 'original'
- calculate_arr(diff_arr::Array{T}) - Create a original array from the given difference array
- add_to_arr(diff_arr::Array{T}, l::Int, r::Int, x::Number) - Add x to all elements with index from [l, r]
- create_diff_arr(original::Array{<:Number})
* Create difference array for array 'original'
- calculate_arr(diff_arr::Array{<:Number})
* Recreate the original array from the given difference array
- add_to_arr(diff_arr::Array{<:Number}, l::Int, r::Int, x::Number)
* Add x to all elements with index from [l, r]
# Contributed by: [Nikola Mircic](https://github.com/Nikola-Mircic)
Expand All @@ -25,7 +28,7 @@ module DifferenceArray
function create_diff_arr(original::Array{T}) where {T<:Number}
n = length(original)

diff_arr = Array(original)
diff_arr = copy(original)

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

arr = Array(diff_arr)
arr = copy(diff_arr)

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

# Add x to all elements with index from [l, r]
# Parameters:
# - dif_arr - a difference array of the array you want to change
# - diff_arr - a difference array of the array you want to change
# - l - leftmost index of the affected range
# - r - rightmost index of the affected range
# - x - a value to be added to all elements from a given range
Expand Down
29 changes: 28 additions & 1 deletion src/basic/prefix_sum.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
"""
prefix_sum(arr::Vector{<:Number})
# Brief
Given an input array of numbers, return an array of the sum of each "prefix" of the input array i.e.
the 1st element, 1st + 2nd element, 1st + 2nd + 3rd, etc.
This functionality is available in base Julia as `cumsum`.
# Arguments
- `arr`: an array of numbers
# Examples
```julia
julia> prefix_sum([1, 2, 3])
3-element Vector{Int64}:
1
3
6
julia> prefix_sum([0.0, 10.0, π])
3-element Vector{Float64}:
0.0
10.0
13.141592653589793
```
"""
function prefix_sum(arr::Vector{T}) where {T<:Number}
pre = []
pre = T[]
preans = zero(T)
for i in arr
preans += i
Expand Down
2 changes: 1 addition & 1 deletion src/conversions/weight_conversion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ WEIGHT_TYPE_CHART = Dict{String,Float64}(
"atomic-mass-unit" => 1.660540199e-27,
)

function weight_conversion(from_type, to_type, value)
function weight_conversion(value, from_type, to_type)
if !haskey(KILOGRAM_CHART, to_type) || !haskey(WEIGHT_TYPE_CHART, from_type)
throw(
error(
Expand Down
13 changes: 10 additions & 3 deletions src/graph/bfs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,21 @@ TheAlgorithms.Graph.bfs(graph, 4)
# output
4 1 2 5 3 6
6-element Vector{Int64}:
4
1
2
5
3
6
```
Contributed by: [Yannick Brenning](https://github.com/ybrenning)
"""
function bfs(graph::Vector{Vector{Int}}, source::Int = 1)
# Use a boolean "visited" array to avoid processing a vertex more than once
visited = [false for _ in 1:length(graph)]
result = Vector{Int}()

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

while length(queue) > 0
curr_v = dequeue!(queue)
print(curr_v, " ")
push!(result, curr_v)

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

return print("\n")
return result
end
14 changes: 10 additions & 4 deletions src/graph/dfs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,21 @@ dfs(graph, 6)
# output
6 5 2 4 3 1
6-element Vector{Int64}:
6
5
2
4
3
1
```
Contributed by: [Yannick Brenning](https://github.com/ybrenning)
"""
function dfs(graph::Vector{Vector{Int}}, source::Int = 1)
# Use a boolean "visited" array to avoid processing a vertex more than once
visited = [false for _ in 1:length(graph)]
result = Vector{Int}()

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

while length(stack) > 0
curr_v = pop!(stack)
print(curr_v, " ")
push!(result, curr_v)

# Add every unvisited target to the top of the stack
for i in 1:length(graph[curr_v])
Expand All @@ -49,6 +56,5 @@ function dfs(graph::Vector{Vector{Int}}, source::Int = 1)
end
end
end

return print("\n")
return result
end
12 changes: 5 additions & 7 deletions src/longest_increasing_subsequence/binary_search.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
lis(arr::Array{Int}, ::Val{:bs})
lis(arr::Array{<:Integer}, ::Val{:bs})
# Arguments:
- `arr`: sequence of integers
Expand All @@ -23,12 +23,12 @@ https://cp-algorithms.com/sequences/longest_increasing_subsequence.html
- [Igor Malheiros](https://github.com/igormalheiros)
"""

function lis(arr::Array{Int}, ::Val{:bs})
function lis(arr::Array{T}, ::Val{:bs}) where T <: Integer
len = length(arr)
memo = ones(Int, len)
memo = ones(T, len)
p = ones(Int, len)

lis_arr = Int[]
lis_arr = T[]

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

Expand All @@ -48,12 +48,10 @@ function lis(arr::Array{Int}, ::Val{:bs})
last_pos = lis_len
for i in len:-1:1
if p[i] == last_pos
push!(lis_arr, arr[i])
pushfirst!(lis_arr, arr[i])
last_pos -= 1
end
end

reverse!(lis_arr)

return lis_arr
end
10 changes: 4 additions & 6 deletions src/longest_increasing_subsequence/dynamic_programming.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
lis(arr::Array{Int}, ::Val{:dp})
lis(arr::Array{<:Integer}, ::Val{:dp})
# Arguments:
- `arr`: sequence of integers
Expand All @@ -23,12 +23,12 @@ https://cp-algorithms.com/sequences/longest_increasing_subsequence.html
- [Igor Malheiros](https://github.com/igormalheiros)
"""

function lis(arr::Array{Int}, ::Val{:dp})
function lis(arr::Array{T}, ::Val{:dp}) where T <: Integer
len = length(arr)
memo = ones(Int, len)
p = zeros(Int, len)

lis_arr = Int[]
lis_arr = T[]

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

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

# Restoring
while lis_pos != 0
push!(lis_arr, arr[lis_pos])
pushfirst!(lis_arr, arr[lis_pos])
lis_pos = p[lis_pos]
end

reverse!(lis_arr)

return lis_arr
end
6 changes: 6 additions & 0 deletions src/math/Math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export area_trapezium
export area_triangle
export average_absolute_deviation
export bab_sqrt
export bin_length
export bin_length_long
export bin_length_short
export catalan
export ceil
export collatz_sequence
Expand Down Expand Up @@ -60,6 +63,7 @@ export permutation
export prime_check
export prime_factors
export riemann_integration
export runge_kutta_integration
export simpsons_integration
export sum_ap
export sum_gp
Expand Down Expand Up @@ -89,6 +93,7 @@ include("average_mean.jl")
include("average_median.jl")
include("average_mode.jl")
include("babylonian_sqrt.jl")
include("binary_length.jl")
include("catalan_number.jl")
include("ceil.jl") # needed by average_median
include("collatz_sequence.jl")
Expand All @@ -111,6 +116,7 @@ include("permutation.jl")
include("prime_check.jl")
include("prime_factors.jl")
include("riemann_integration.jl")
include("runge_kutta_integration.jl")
include("sieve_of_eratosthenes.jl")
include("simpsons_integration.jl")
include("sum_of_arithmetic_series.jl")
Expand Down
71 changes: 48 additions & 23 deletions src/strings/binary_length.jl → src/math/binary_length.jl
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
function bin_length_long(s::AbstractString)
binNum = parse(UInt, s)

finNum = 0
seq = 1

for i in 1:binNum
if (i == seq)
finNum += 1
seq *= 2
end
end

return string(finNum)
"""
bin_length(binNum)
Returns the length of binNum's binary representation.
# Input parameters:
- `binNum` : The number to find the binary length of.
# Examples/Tests:
```julia
bin_length(1) # returns 1
bin_length(2) # returns 2
bin_length(3) # returns 2
bin_length(4) # returns 3
bin_length(5) # returns 3
bin_length(12) # returns 4
bin_length(256) # returns 9
bin_length(1024) # returns 11
bin_length(-1) # throws DomainError
```
Contributed by: [Praneeth Jain](https://www.github.com/PraneethJain)
"""
function bin_length(binNum::T) where {T<:Integer}
binNum <= 0 && throw(DomainError("binNum must be a positive integer"))
return floor(log2(binNum)) + 1
end

"""
Expand Down Expand Up @@ -46,19 +57,20 @@ doubled amount.
#Contributions:
Contributed by F35H: https://github.com/F35H
"""

function bin_length_short(s::AbstractString)
binNum = parse(UInt, s)
function bin_length_long(binNum::T) where {T<:Integer}
binNum <= 0 && throw(DomainError("binNum must be a positive integer"))

finNum = 0
i = 1
seq = 1

while i <= binNum
i *= 2
finNum += 1
for i in 1:binNum
if (i == seq)
finNum += 1
seq *= 2
end
end

return string(finNum)
return finNum
end

"""
Expand All @@ -68,7 +80,7 @@ the length of any binary number and returns said length.
https://oeis.org/A070939
This function, as believed, is O(n)
This function, as believed, is O(log(n))
The idea follows that the sequence is dependent on
a repeating pattern of 2. The final number being finNum
Expand All @@ -90,3 +102,16 @@ final number that iterates on every doubling of i.
Contributors:
- [F45H](https://github.com/F35H)
"""
function bin_length_short(binNum::T) where {T<:Integer}
binNum <= 0 && throw(DomainError("binNum must be a positive integer"))

finNum = 0
i = 1

while i <= binNum
i *= 2
finNum += 1
end

return finNum
end
2 changes: 0 additions & 2 deletions src/math/fibonacci.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ function fib_iterative(n::Int)
throw(DomainError("n is negative"))
elseif n == 1
return [0]
elseif n == 2
return [0, 1]
else
result = [0, 1]
a, b = 0, 1
Expand Down
Loading

0 comments on commit 8b409aa

Please sign in to comment.