Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • shchen/cs320
  • raveendr/cs320
  • mwojnaro/cs320
3 results
Show changes
Showing
with 311 additions and 0 deletions
test finished
\ No newline at end of file
42
42
2
3
prim
prim
not prim
not prim
test finished
\ No newline at end of file
true
false
true
false
true
false
false
true
false
false
true
true
5! = 120
10! = 3628800
3
8
2
4
1
2
42
\ No newline at end of file
This side effect should only happen once!
yes
42
42
\ No newline at end of file
1
2
1
2
1
2
1
2
1
2
1
2
1
2
Left
Right
SLeft
SRight
CLeft
CRight
\ No newline at end of file
Hello World!
Hello World again!
\ No newline at end of file
9
List(-5, -1, 0, 5, 10)
object Arithmetic
def pow(b: Int(32), e: Int(32)): Int(32) = {
if (e == 0) { 1 }
else {
if (e % 2 == 0) {
val rec: Int(32) = pow(b, e/2);
rec * rec
} else {
b * pow(b, e - 1)
}
}
}
def gcd(a: Int(32), b: Int(32)): Int(32) = {
if (a == 0 || b == 0) {
a + b
} else {
if (a < b) {
gcd(a, b % a)
} else {
gcd(a % b, b)
}
}
}
Std.printInt(pow(0, 10));
Std.printInt(pow(1, 5));
Std.printInt(pow(2, 10));
Std.printInt(pow(3, 3));
Std.printInt(gcd(0, 10));
Std.printInt(gcd(17, 99)); // 1
Std.printInt(gcd(16, 46)); // 2
Std.printInt(gcd(222, 888)) // 222
end Arithmetic
\ No newline at end of file
object BasicArithmetic
def plus(a: Int(32), b: Int(32)): Int(32) = {
a + b
}
def minus(a: Int(32), b: Int(32)): Int(32) = {
a - b
}
def mul(a: Int(32), b: Int(32)): Int(32) = {
a * b
}
def mod(a: Int(32), b: Int(32)): Int(32) = {
a % b
}
def div(a: Int(32), b: Int(32)): Int(32) = {
a / b
}
val a: Int(32) = 1;
val b: Int(32) = 2;
Std.printString(Std.intToString(plus(a, b)));
Std.printString(Std.intToString(mul(plus(a, b), b)));
Std.printString(Std.intToString(minus(mul(plus(a, b), b), b)));
Std.printString(Std.intToString(mul(minus(mul(plus(a, b), b), b), b)));
Std.printString(Std.intToString(div(4, 2)));
Std.printString(Std.intToString(div(3, 2)));
Std.printString(Std.intToString(div(1, 2)));
Std.printString(Std.intToString(div(-1, 2)));
Std.printString(Std.intToString(div(-2, 2)));
Std.printString(Std.intToString(div(-3, 2)));
Std.printString(Std.intToString(mod(5, 2)));
Std.printString(Std.intToString(mod(-5, 2)));
Std.printString("test finished")
end BasicArithmetic
object BasicBranching
def less(a: Int(32), b: Int(32)): Boolean = {
a < b
}
def lessOrEqual(a: Int(32), b: Int(32)): Boolean = {
a <= b
}
def test1(): Unit = {
val a: Int(32) = 1;
val b: Int(32) = a + 1;
if(less(a, b)) {
Std.printString("correct")
} else {
Std.printString("not correct")
}
}
def test2(): Unit = {
val a: Int(32) = 1;
val b: Int(32) = a + 1;
if(less(a, 1)) {
Std.printString("correct")
} else {
Std.printString("not correct")
}
}
def test3(): Unit = {
val a: Int(32) = 1;
val b: Int(32) = a + 1;
if(less(b, a)) {
Std.printString("correct")
} else {
Std.printString("not correct")
}
}
def test4(): Unit = {
val a: Int(32) = 1;
val b: Int(32) = a + 1;
if(lessOrEqual(a, a)) {
Std.printString("correct")
} else {
Std.printString("not correct")
}
}
def test(): Unit = {
val a: Int(32) = 1;
val b: Int(32) = a + 1;
if(less(a, b)) {
Std.printString("correct")
} else {
Std.printString("not correct")
};
if(less(a, 1)) {
Std.printString("correct")
} else {
Std.printString("not correct")
};
if(less(b, a)) {
Std.printString("correct")
} else {
Std.printString("not correct")
};
if(lessOrEqual(a, a)) {
Std.printString("correct")
} else {
Std.printString("not correct")
}
}
test();
Std.printString("test finished")
end BasicBranching
object BasicConditions
def neverTrigger(): Boolean = {
Std.printString("should not happen");
true
}
val c1: Boolean = true || neverTrigger();
val c2: Boolean = false && neverTrigger();
Std.printString("test finished")
end BasicConditions
\ No newline at end of file