Skip to content

Vector operation koans #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions koans/VectorOperationKoans.jl
Original file line number Diff line number Diff line change
@@ -1,2 +1,90 @@
module VectorOperationKoans
using LinearAlgebra

#=
Vectors are a commmon data structure containers present in most common programming
languages. Particularly in Julia, they are represented as a 1D Array. Also, basic linear algebra
operations are useful for common needed calculations such as norms and distances.
Here is some useful documentation for arrays (and vectors) in Julia and the LinearAlgebra module:
- https://docs.julialang.org/en/v1/base/arrays/
- https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/
=#

function vector_length(vec)
#=
Return the length of the vector "vec".
Ex: vec = [ 9, 5, 6]; return 3.
=#
end

function range_1_to_n(n)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These exercises would really benefit from having a small description of the problem to solve, along with a few examples for each.

#=
Return a vector with ordered elements from 1 to n.
Ex: n = 5; return [1, 2, 3, 4, 5].
=#
end

function vector_of_zeros(n)
#=
Return a vector with n zero elements.
Ex: n = 4; return [0, 0, 0, 0].
=#
end

function vector_of_ones(n)
#=
Return a vector with n ones.
Ex: n = 3; return [1, 1, 1].
=#
end

function elementwise_sum(x, y)
#=
Return the element-wise sum of the vectors x and y.
Ex: x = [2, 4, 6]; y = [8, 0, -1]; return [10, 4, 5].
=#
end

function elementwise_substraction(x, y)
#=
Return the element-wise substraction of the vectors x and y.
Ex: x = [2, 4, 6]; y = [8, 0, -1]; return [-6, 4, 7].
=#
end

function scalar_vector_addition(vec, k)
#=
Return the outcome of adding the scalar k to the vector vec.
Ex: k = 2; vec = [3, 4, 5]; return [5, 6, 7].
=#
end

function scalar_vector_multiplication(vec, k)
#=
Return the outcome of multiplying the scalar k to the vector vec.
Ex: k = 2; vec = [3, 4, 5]; return [6, 8, 10].
=#
end

function scalar_product(x, y)
#=
Return the scalar or dot product of x and y.
Ex: x = [2, 4, 6]; y = [3, 7, 8]; return 82 (2*3 + 4*7 + 8*6).
=#
end

function norm_l2(x)
#=
Return the norm L2 of the vector x.
Ex: x = [4, -3, 7]; return ≈ 8.6 (sqrt(4*4 + -3*-3 + 7*7))
=#
end

function euclidean_distance(x, y)
#=
Return the euclidean distance between x and y.
Ex: x = [1, 2, 4]; y = [ 5, 6, 7]; return ≈ 6.4 (sqrt( (1-5)^2 + (2-6)^2 + (4-7)^2 ))
=#
end

end
101 changes: 101 additions & 0 deletions solutions/VectorOperationKoans.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
module VectorOperationKoans
using LinearAlgebra

#=
Vectors are a commmon data structure containers present in most common programming
languages. Particularly in Julia, they are represented as a 1D Array. Also, basic linear algebra
operations are useful for common needed calculations such as norms and distances.
Here is some useful documentation for arrays (and vectors) in Julia and the LinearAlgebra module:
- https://docs.julialang.org/en/v1/base/arrays/
- https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/
=#

function vector_length(vec)
#=
Return the length of the vector "vec".
Ex: vec = [ 9, 5, 6]; return 3.
=#
return length(vec)
end

function range_1_to_n(n)
#=
Return a vector with ordered elements from 1 to n.
Ex: n = 5; return [1, 2, 3, 4, 5].
=#
return collect(1:n)
end

function vector_of_zeros(n)
#=
Return a vector with n zero elements.
Ex: n = 4; return [0, 0, 0, 0].
=#
return zeros(n)
end

function vector_of_ones(n)
#=
Return a vector with n ones.
Ex: n = 3; return [1, 1, 1].
=#
return ones(n)
end

function elementwise_sum(x, y)
#=
Return the element-wise sum of the vectors x and y.
Ex: x = [2, 4, 6]; y = [8, 0, -1]; return [10, 4, 5].
=#
return x+y
end

function elementwise_substraction(x, y)
#=
Return the element-wise substraction of the vectors x and y.
Ex: x = [2, 4, 6]; y = [8, 0, -1]; return [-6, 4, 7].
=#
return x-y
end

function scalar_vector_addition(vec, k)
#=
Return the outcome of adding the scalar k to the vector vec.
Ex: k = 2; vec = [3, 4, 5]; return [5, 6, 7].
=#
return vec.+k
end

function scalar_vector_multiplication(vec, k)
#=
Return the outcome of multiplying the scalar k to the vector vec.
Ex: k = 2; vec = [3, 4, 5]; return [6, 8, 10].
=#
return k*vec
end

function scalar_product(x, y)
#=
Return the scalar or dot product of x and y.
Ex: x = [2, 4, 6]; y = [3, 7, 8]; return 82 (2*3 + 4*7 + 8*6).
=#
return dot(x, y)
end

function norm_l2(x)
#=
Return the norm L2 of the vector x.
Ex: x = [4, -3, 7]; return ≈ 8.6 (sqrt(4*4 + -3*-3 + 7*7))
=#
norm(x)
end

function euclidean_distance(x, y)
#=
Return the euclidean distance between x and y.
Ex: x = [1, 2, 4]; y = [ 5, 6, 7]; return ≈ 6.4 (sqrt( (1-5)^2 + (2-6)^2 + (4-7)^2 ))
=#
norm(x-y)
end

end
31 changes: 28 additions & 3 deletions tests/solutions/tests_solutions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,34 @@ end
# @testset "Arrays" begin
# end

# using VectorOperationKoans
# @testset "Vector Operators" begin
# end

using VectorOperationKoans
@testset "Vector Operation" begin
@test VectorOperationKoans.vector_length([1,2,3]) == 3
@test VectorOperationKoans.vector_length([1]) == 1
@test VectorOperationKoans.range_1_to_n(3) == [1, 2, 3]
@test VectorOperationKoans.range_1_to_n(10) == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
@test VectorOperationKoans.vector_of_zeros(3) == [0, 0, 0]
@test VectorOperationKoans.vector_of_zeros(9) == [0, 0, 0, 0, 0, 0, 0, 0, 0]
@test VectorOperationKoans.vector_of_ones(3) == [1, 1, 1]
@test VectorOperationKoans.vector_of_ones(9) == [1, 1, 1, 1, 1, 1, 1, 1, 1]
@test VectorOperationKoans.elementwise_sum([2, 3], [3, 5]) == [5, 8]
@test VectorOperationKoans.elementwise_sum([-2, 3], [3, -5]) == [1, -2]
@test VectorOperationKoans.elementwise_sum([0, 3], [3, -5]) == [3, -2]
@test VectorOperationKoans.elementwise_substraction([0, 3],[3, -5]) == [-3, 8]
@test VectorOperationKoans.elementwise_substraction([1, 10],[40, -95]) == [-39, 105]
@test VectorOperationKoans.scalar_vector_addition([1, 2, 3], 4) == [5, 6, 7]
@test VectorOperationKoans.scalar_vector_addition([-10, 30], -40) == [-50, -10]
@test VectorOperationKoans.scalar_vector_addition([-10, 30], -40) == [-50, -10]
@test VectorOperationKoans.scalar_vector_multiplication([1, 5, 10], 4) == [4, 20, 40]
@test VectorOperationKoans.scalar_vector_multiplication([0, -3, 5, 6], 3) == [0, -9, 15, 18]
@test VectorOperationKoans.scalar_product([1, 2], [3, 4]) == 11
@test VectorOperationKoans.scalar_product([-4, 6], [6, 8]) == 24
@test VectorOperationKoans.norm_l2([1, 1]) == sqrt(2)
@test VectorOperationKoans.norm_l2([4, -10]) == sqrt(116)
@test VectorOperationKoans.euclidean_distance([1, 3], [3, 5]) == sqrt(8)
@test VectorOperationKoans.euclidean_distance([3, 5, 8], [3, -5, 0]) == sqrt(164)
end

# using MultiDimensionalArrayKoans
# @testset "Multi-dimensional Arrays" begin
Expand Down
29 changes: 29 additions & 0 deletions tests/tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,32 @@ using PythonInterop
@test PythonInterop.jlfib(5, pyfib) == 5
@test PythonInterop.jlfib(7, pyfib) == 13
end

using VectorOperationKoans
@testset "Vector Operation" begin
@test VectorOperationKoans.vector_length([1,2,3]) == 3
@test VectorOperationKoans.vector_length([1]) == 1
@test VectorOperationKoans.range_1_to_n(3) == [1, 2, 3]
@test VectorOperationKoans.range_1_to_n(10) == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
@test VectorOperationKoans.vector_of_zeros(3) == [0, 0, 0]
@test VectorOperationKoans.vector_of_zeros(9) == [0, 0, 0, 0, 0, 0, 0, 0, 0]
@test VectorOperationKoans.vector_of_ones(3) == [1, 1, 1]
@test VectorOperationKoans.vector_of_ones(9) == [1, 1, 1, 1, 1, 1, 1, 1, 1]
@test VectorOperationKoans.elementwise_sum([2, 3], [3, 5]) == [5, 8]
@test VectorOperationKoans.elementwise_sum([-2, 3], [3, -5]) == [1, -2]
@test VectorOperationKoans.elementwise_sum([0, 3], [3, -5]) == [3, -2]
@test VectorOperationKoans.elementwise_substraction([0, 3],[3, -5]) == [-3, 8]
@test VectorOperationKoans.elementwise_substraction([1, 10],[40, -95]) == [-39, 105]
@test VectorOperationKoans.scalar_vector_addition([1, 2, 3], 4) == [5, 6, 7]
@test VectorOperationKoans.scalar_vector_addition([-10, 30], -40) == [-50, -10]
@test VectorOperationKoans.scalar_vector_addition([-10, 30], -40) == [-50, -10]
@test VectorOperationKoans.scalar_vector_multiplication([1, 5, 10], 4) == [4, 20, 40]
@test VectorOperationKoans.scalar_vector_multiplication([0, -3, 5, 6], 3) == [0, -9, 15, 18]
@test VectorOperationKoans.scalar_product([1, 2], [3, 4]) == 11
@test VectorOperationKoans.scalar_product([-4, 6], [6, 8]) == 24
@test VectorOperationKoans.norm_l2([1, 1]) == sqrt(2)
@test VectorOperationKoans.norm_l2([4, -10]) == sqrt(116)
@test VectorOperationKoans.euclidean_distance([1, 3], [3, 5]) == sqrt(8)
@test VectorOperationKoans.euclidean_distance([3, 5, 8], [3, -5, 0]) == sqrt(164)
end