From e9e9998de12e25d3c47bf4738aa6b9c588d0fe06 Mon Sep 17 00:00:00 2001 From: lnay Date: Tue, 12 Mar 2024 14:49:12 +0000 Subject: [PATCH] Add comparisons between julia and python --- julia-vs-python/expressions.jl | 2 ++ julia-vs-python/expressions.py | 3 +++ julia-vs-python/function.jl | 11 +++++++++++ julia-vs-python/function.py | 4 ++++ julia-vs-python/if.jl | 7 +++++++ julia-vs-python/if.py | 6 ++++++ julia-vs-python/loops.jl | 16 ++++++++++++++++ julia-vs-python/loops.py | 21 +++++++++++++++++++++ julia-vs-python/variables.jl | 4 ++++ julia-vs-python/variables.py | 3 +++ 10 files changed, 77 insertions(+) create mode 100644 julia-vs-python/expressions.jl create mode 100644 julia-vs-python/expressions.py create mode 100644 julia-vs-python/function.jl create mode 100644 julia-vs-python/function.py create mode 100644 julia-vs-python/if.jl create mode 100644 julia-vs-python/if.py create mode 100644 julia-vs-python/loops.jl create mode 100644 julia-vs-python/loops.py create mode 100644 julia-vs-python/variables.jl create mode 100644 julia-vs-python/variables.py diff --git a/julia-vs-python/expressions.jl b/julia-vs-python/expressions.jl new file mode 100644 index 0000000..6b05c25 --- /dev/null +++ b/julia-vs-python/expressions.jl @@ -0,0 +1,2 @@ +x = 1 +println(2^2 + 5x) \ No newline at end of file diff --git a/julia-vs-python/expressions.py b/julia-vs-python/expressions.py new file mode 100644 index 0000000..464c9a3 --- /dev/null +++ b/julia-vs-python/expressions.py @@ -0,0 +1,3 @@ +x = 1 +print(2**2 + 5*x) +print(2^2 + 5) \ No newline at end of file diff --git a/julia-vs-python/function.jl b/julia-vs-python/function.jl new file mode 100644 index 0000000..17b1c59 --- /dev/null +++ b/julia-vs-python/function.jl @@ -0,0 +1,11 @@ +function my_add(x, y) + return x + y +end + +function my_add2(x, y) + x + y +end + +my_add3(x, y) = x + y + +my_add4 = (x, y) -> x + y \ No newline at end of file diff --git a/julia-vs-python/function.py b/julia-vs-python/function.py new file mode 100644 index 0000000..ec77894 --- /dev/null +++ b/julia-vs-python/function.py @@ -0,0 +1,4 @@ +def my_add(x, y): + return x + y + +my_add2 = lambda x, y: x + y \ No newline at end of file diff --git a/julia-vs-python/if.jl b/julia-vs-python/if.jl new file mode 100644 index 0000000..d4db444 --- /dev/null +++ b/julia-vs-python/if.jl @@ -0,0 +1,7 @@ +x = 2 + +if x % 2 == 0 + println("x is even") +else + println("x is odd") +end \ No newline at end of file diff --git a/julia-vs-python/if.py b/julia-vs-python/if.py new file mode 100644 index 0000000..29118dc --- /dev/null +++ b/julia-vs-python/if.py @@ -0,0 +1,6 @@ +x = 2 + +if x % 2 == 0: + print("x is even") +else: + print("x is odd") \ No newline at end of file diff --git a/julia-vs-python/loops.jl b/julia-vs-python/loops.jl new file mode 100644 index 0000000..d32fa1f --- /dev/null +++ b/julia-vs-python/loops.jl @@ -0,0 +1,16 @@ +# sum the squares of integers 0 <= x < 10 +# which are not 0 mod 4 +result_squared = 0 +for x in 0:10 + @isdefined(result) + if x % 4 != 0 + global result_squared += x^2 + end +end +result = sqrt(result_squared) + +println(result) + +alternative_result = ( 0:10 |> filter(x -> x%4 != 0) ) .^2 |> (sqrt ∘ sum) + +println(alternative_result) \ No newline at end of file diff --git a/julia-vs-python/loops.py b/julia-vs-python/loops.py new file mode 100644 index 0000000..1d9d8de --- /dev/null +++ b/julia-vs-python/loops.py @@ -0,0 +1,21 @@ +# sqrt of sum the squares of integers 0 <= x < 10 +# which are not 0 mod 4 +result_squared = 0 +for x in range(10): + if x % 4 != 0: + result_squared += x*x +result = result_squared**0.5 + +print(result) + +alternative_result = sum( + map( + lambda x: x*x, + filter( + lambda x: x % 4 != 0, + range(10) + ) + ) +) + +print(alternative_result) \ No newline at end of file diff --git a/julia-vs-python/variables.jl b/julia-vs-python/variables.jl new file mode 100644 index 0000000..4165b44 --- /dev/null +++ b/julia-vs-python/variables.jl @@ -0,0 +1,4 @@ +x = 1 +y::String = "hello" +x = "bye" +y = 1 diff --git a/julia-vs-python/variables.py b/julia-vs-python/variables.py new file mode 100644 index 0000000..a417dd6 --- /dev/null +++ b/julia-vs-python/variables.py @@ -0,0 +1,3 @@ +x = 1 +y = "hello" +x = "bye" \ No newline at end of file