Skip to content

Commit

Permalink
Add comparisons between julia and python
Browse files Browse the repository at this point in the history
  • Loading branch information
lnay committed Mar 12, 2024
1 parent e9616a9 commit e9e9998
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 0 deletions.
2 changes: 2 additions & 0 deletions julia-vs-python/expressions.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x = 1
println(2^2 + 5x)
3 changes: 3 additions & 0 deletions julia-vs-python/expressions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
x = 1
print(2**2 + 5*x)
print(2^2 + 5)
11 changes: 11 additions & 0 deletions julia-vs-python/function.jl
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions julia-vs-python/function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def my_add(x, y):
return x + y

my_add2 = lambda x, y: x + y
7 changes: 7 additions & 0 deletions julia-vs-python/if.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
x = 2

if x % 2 == 0
println("x is even")
else
println("x is odd")
end
6 changes: 6 additions & 0 deletions julia-vs-python/if.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
x = 2

if x % 2 == 0:
print("x is even")
else:
print("x is odd")
16 changes: 16 additions & 0 deletions julia-vs-python/loops.jl
Original file line number Diff line number Diff line change
@@ -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)
21 changes: 21 additions & 0 deletions julia-vs-python/loops.py
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 4 additions & 0 deletions julia-vs-python/variables.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
x = 1
y::String = "hello"
x = "bye"
y = 1
3 changes: 3 additions & 0 deletions julia-vs-python/variables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
x = 1
y = "hello"
x = "bye"

0 comments on commit e9e9998

Please sign in to comment.