Skip to content

Commit 30797f3

Browse files
Enhancements to "Basic": mainly docs (#211)
* docs(DifferenceArray): specify array argument's eltype as <:Number, fix spelling * refactor(DifferenceArray): explicitly use `copy` to make a copy * docs(Basic): add docstring for prefix_sum * fix(Basic): return array of proper type from prefix_sum instead of Any array * test(Basic): test prefix_sum on other Number types, not just integers
1 parent dec548c commit 30797f3

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

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

test/basic.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ using TheAlgorithms.Basic
44
@testset "Basic: prefix_sum" begin
55
@test Basic.prefix_sum([1, 1, 1]) == [1, 2, 3]
66
@test Basic.prefix_sum([1, 2, 3]) == [1, 3, 6]
7+
@test Basic.prefix_sum(BigInt[]) == BigInt[]
8+
@test Basic.prefix_sum([0., 0., 0.]) == [0., 0., 0.]
9+
@test Basic.prefix_sum([1 + 2im, 2 - 3im]) == [1 + 2im, 3 - 1im]
710
end
811

912
@testset "Basic: DifferenceArray" begin

0 commit comments

Comments
 (0)