Skip to content

Commit 88f2d05

Browse files
author
Lidia Popescu
committed
Julia exercises
1 parent 8721f7d commit 88f2d05

26 files changed

+857
-0
lines changed

JuliaProject/.buildpath

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<buildpath>
3+
<buildpathentry kind="src" path=""/>
4+
<buildpathentry kind="con" path="org.eclipse.dltk.launching.INTERPRETER_CONTAINER"/>
5+
</buildpath>

JuliaProject/.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>JuliaProject</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.dltk.core.scriptbuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>com.juliacomputing.jldt.eclipse.core.nature</nature>
16+
</natures>
17+
</projectDescription>

JuliaProject/Julia.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function helloworld(count::Int)
2+
for i in 1:count
3+
println("Hello world ", i)
4+
println( BigInt(typemax(Int64)) + 1)
5+
end
6+
end
7+
helloworld(3)

JuliaProject/ch1/HelloWorld.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Finished version of HelloWorld for Learning Julia course
2+
3+
# Define a function that prints out a message
4+
# This creates a function with a single "method"
5+
function helloworld()
6+
println("Hello World!")
7+
end
8+
9+
# By defining another version of the function with different
10+
# arguments, we create another "method" - Julia will choose
11+
# the right one based on the arguments. This is called
12+
# "multiple dispatch"
13+
function helloworld(count::Int)
14+
for i in 1:count
15+
println("Hello World ", i)
16+
end
17+
end
18+
19+
# call the version with no arguments
20+
helloworld()
21+
# call the version that takes a number
22+
helloworld(3)
23+
# try calling the function with a string - this will error
24+
# because we have not defined a method that takes a string
25+
#helloworld("today")

JuliaProject/ch2/Numbers.jl

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Finished example file for Julia number types
2+
3+
# Julia defines a set of specific sub-types for numbers
4+
# Signed integers: Int, Int8, Int16, Int32, Int64, BigInt
5+
6+
a = Int16(1)
7+
println(typeof(a))
8+
9+
b = Int(2000)
10+
println(typeof(b))
11+
12+
# Unsigned integers: UInt, UInt8, UInt16, UInt32, UInt64
13+
c = UInt16(1)
14+
println(typeof(c))
15+
16+
# typemax() and typemin() will provide max and min values
17+
println(typemax(Int8))
18+
println(typemax(Int32))
19+
println(typemin(Int64))
20+
21+
# Use the WORDSIZE property to see what type of system this is
22+
println(Sys.WORD_SIZE)
23+
24+
# trying to assign a number too large for the type
25+
# will fail and give an error
26+
# a = UInt8(300) # 300 is too large for an 8 bit value
27+
28+
# special values represent Infinity and not-a-number
29+
println(1.0 / 0.0)
30+
println(1.0 / Inf)
31+
println(0.0 / 0.0)
32+
33+
# zero() and one() functions produce values for a given type
34+
println(zero(Float64))
35+
println(one(UInt32))

JuliaProject/ch2/Strings.jl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Finished example file for working with chars and strings in Julia
2+
3+
# Julia has a specific character type
4+
mychar = 'x'
5+
println(Int(mychar))
6+
println(Char(120))
7+
8+
# Strings are defined using double quotes or triple quotes
9+
mystr = "This is a sample string in Julia"
10+
myotherstr = """
11+
Hello There
12+
This is a string
13+
"""
14+
15+
# Get the length of a string
16+
println(length(mystr))
17+
18+
# Access individual characters - note that they are 1-indexed
19+
println(mystr[1])
20+
println(mystr[end])
21+
println(mystr[2:end - 1])
22+
23+
# Iterate over characters
24+
for c in mystr
25+
print(c)
26+
end
27+
println()
28+
29+
# String concatenation using *
30+
w1 = "Hello"
31+
w2 = "World"
32+
println(w1 * ", " * w2 * ".")
33+
34+
# String interpolation
35+
a = 5
36+
b = 10
37+
println("The result of $a + $b is $(a + b)")
38+

JuliaProject/ch2/Variables.jl

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Declaring variables and using data types in Julia
2+
3+
# 5 basic types of data:
4+
# Integers, Floating point numbers, booleans, strings, and characters
5+
6+
# Variable names typically are lowercase. Use underscores if the name
7+
# is too long to be easily read.
8+
first = 1
9+
second = 2.0
10+
stringvar = "This is a string"
11+
initial = 'J'
12+
boolval = true
13+
14+
println(first)
15+
println(second)
16+
println(stringvar)
17+
println(initial)
18+
println(boolval)
19+
20+
# variables can be redefined and change type
21+
first = 1.0
22+
println(first)
23+
24+
# Strings have to be in double quotes. This will error:
25+
# anotherstring = 'hello world'
26+
27+
# Constant values are declared with const and are all uppercase
28+
# Note: this is only useful in the global scope
29+
const MEANING_OF_LIFE = 42
30+
# Constants of the same type *can* be reassigned, but with a warning
31+
#MEANING_OF_LIFE = 43
32+
#println(MEANING_OF_LIFE)
33+
34+
# Constants of different types cannot be reassigned, this is an error
35+
# MEANING_OF_LIFE = 43.0
36+
37+
# Type Annotation identifies a variable as a particular type
38+
function testfunc()
39+
x::String = "some text"
40+
# the next line causes an error, x is defined as an Int
41+
x = 10
42+
end
43+
#testfunc()
44+

JuliaProject/ch3/BuildIn.jl

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Starting example for built in functions in Learning Julia
2+
3+
# numeric functions
4+
# TODO: round, floor, ceil, abs
5+
x = round(30.5)
6+
#println(x)
7+
8+
# text i/o
9+
# TODO: print, println
10+
x= round(30.5, RoundUp)
11+
#println(x)
12+
13+
14+
y = 29.95
15+
#println(floor(y))
16+
#println(ceil(y))
17+
18+
z= -15
19+
#println(abs(z))
20+
21+
#print("Hello")
22+
#print("there")
23+
#println("world!")
24+
25+
# TODO: printstyled
26+
thestr = "This is some text"
27+
printstyled(thestr)
28+
println()
29+
printstyled(thestr, bold=true)
30+
println()
31+
printstyled(thestr, bold=true, color=:red)
32+
println()
33+
34+
# read standard input
35+
# TODO: readline
36+
print("What is your name: ")
37+
str = readline()
38+
println(str)
39+
40+
# TODO: "is" functions
41+
println(isascii("abc"))
42+
println(isascii("αβγ"))
43+
44+
println(isdigit('9'))
45+
println(isdigit('a'))
46+
47+
println(isspace(' '))
48+
println(isspace('\r'))
49+
println(isspace('\n'))
50+
println(isspace('A'))

JuliaProject/ch3/Convert.jl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Starting point example for converting data types in Learning Julia
2+
3+
# declare some basic data types
4+
x = 10
5+
y = 20.0
6+
z = "40"
7+
f = "30.0"
8+
9+
# TODO: Convert an integer to a character and vice versa
10+
c = Char(74)
11+
println(c)
12+
13+
i = Int('J')
14+
println(i)
15+
16+
# TODO: convert between types
17+
flt1 = Float32(x)
18+
int1 = Int16(y)
19+
println(flt1, ", ", int1)
20+
21+
# TODO: parse values from a string
22+
a= parse(Int, z)
23+
b= parse(Float64, f)
24+
c= string(y)
25+
println(a)
26+
println(b)
27+
println(c)
28+
29+
# TODO: attempting to convert a data type that won't fit is an error
30+
x= Char(281)
31+
println(x)
32+
33+
a= UInt8(x)
34+
# println(a) ERROR: LoadError: InexactError: trunc(UInt8, 281)

JuliaProject/ch3/Vector.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Starting point for dot operator sample for Learning Julia
2+
3+
# TODO: The dot operator applies an operation to a collection
4+
arr1 = [2,4,6,8,10]
5+
println(arr1)
6+
7+
result = arr1 .*2
8+
println(result)
9+
10+
# TODO: functions can also be applied to collections this way
11+
result = sqrt.(arr1)
12+
println(result)
13+
14+
# TODO: custom functions can also be used this way
15+
f(x) = 3x
16+
result = f.(arr1)
17+
println(result)

JuliaProject/ch4/Conditionals.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Starting point for conditional example for Learning Julia
2+
3+
# TODO: Create an if-else conditional
4+
x = 5
5+
6+
if x < 10
7+
println("x is small")
8+
else
9+
println("x is big")
10+
end
11+
12+
# TODO: multiple conditions can be specified with if-elseif-else
13+
if x < 17
14+
println("x is small")
15+
elseif x >= 10 && x < 25
16+
println("x is medium")
17+
else
18+
println("x is big")
19+
end
20+
21+
22+
# TODO: The ternary operator can condense a comparison
23+
println(x < 10 ? "x is less than 10" : "x is 10 or greater")

JuliaProject/ch4/Exceptions.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Starting example file demonstrating exceptions for Learning Julia
2+
3+
# Exceptions provide a way for a program to handle one or more
4+
# unexpected conditions
5+
6+
arg = -9
7+
8+
# TODO: the try / catch / finally construct is used to work with exceptions
9+
try
10+
x = sqrt(arg)
11+
println(x)
12+
catch ex
13+
println(ex)
14+
finally
15+
println("This section of code always works")
16+
end
17+
18+

JuliaProject/ch4/Functions.jl

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Starting functions example for Learning Julia
2+
3+
# TODO: Functions are defined with the function keyword and are usually
4+
# lowercase names, optionally with underscores if they are hard to read
5+
function myfunc(a,b)
6+
println("This is function")
7+
a+b
8+
end
9+
10+
result = myfunc(10,15)
11+
println(result)
12+
13+
# TODO: function arguments can have default values
14+
function foo(a, b, z =10)
15+
return (a+b) * z
16+
end
17+
println(foo(2,3))
18+
println(foo(2,3,5))
19+
20+
# TODO: you can also use keyword arguments - define them after a semicolon
21+
function bar(a, b ; multiplier =10)
22+
return (a+b) * multiplier
23+
end
24+
println(bar(4,5))
25+
println(bar(multiplier = 5, 4, 5))
26+
27+
28+
# TODO: The Julia shorthand way of defining a function
29+
myfunc2(x,y) = (a = x-1 ; 2a+y)
30+
result = myfunc2(3,4)
31+
println(result)
32+
33+
# TODO: use the ... notation for variable arguments
34+
function summit(args...)
35+
sum = 0
36+
# TODO: process each argument
37+
for a in args
38+
sum += a
39+
end
40+
return sum
41+
end
42+
println(summit(1, 5, 10))
43+
println(summit(2, 4, 6, 8))
44+

0 commit comments

Comments
 (0)