ps6: init

This commit is contained in:
2026-04-28 15:18:50 +02:00
parent 52aa6d7431
commit 66dd17c9cd
81 changed files with 4806 additions and 0 deletions
@@ -0,0 +1,59 @@
func main() {
println("try:")
println(try(-44))
println(try(-20))
println(try(45))
println(try(40))
println(try(5))
println(try(7))
println(try(10))
println("try2:")
println(try2(-44))
println(try2(-20))
println(try2(45))
println(try2(40))
println(try2(5))
println(try2(7))
println(try2(10))
}
func try(a) {
if (a < -20) return 0
if (a <= -20) return 1
if (a > 40) return 2
if (a >= 40) return 3
if (a == 5) return 4
if (a != 10) return 5
return 6
}
// This function should be identical to try()
func try2(a) {
if (a < -20) return 0
else if (a <= -20) return 1
else if (a > 40) return 2
else if (a >= 40) return 3
else if (a == 5) return 4
else if (a != 10) return 5
else return 6
}
//TESTCASE:
//try:
//0
//1
//2
//3
//4
//5
//6
//try2:
//0
//1
//2
//3
//4
//5
//6
+48
View File
@@ -0,0 +1,48 @@
func main(a) {
var x = a < 1
or (inc() and (a < 2
or (inc() and (a < 3
or (inc() and (a < 4
or (inc() and (a < 5
or (inc() and (a < 6
or (inc() and (a < 7
or (inc() and (a < 8
or (inc() and (a < 9
or (inc() and (a < 10
or tooMuch()))))))))))))))))))
println("a: ", a)
println("counter: ", counter)
}
var counter
func inc() {
counter = counter + 1
return 1
}
func tooMuch() {
println("Too high!")
}
//TESTCASE: 0
//a: 0
//counter: 0
//TESTCASE: 1
//a: 1
//counter: 1
//TESTCASE: 4
//a: 4
//counter: 4
//TESTCASE: 9
//a: 9
//counter: 9
//TESTCASE: 10
//Too high!
//a: 10
//counter: 9
+41
View File
@@ -0,0 +1,41 @@
func main() {
var x
while (1) {
var y = x
while (1) {
println(y, " <= ", x)
y = y - 1
if (y < 0)
break
}
println("====")
x = x + 1
if (x == 5)
break
}
}
//TESTCASE:
//0 <= 0
//====
//1 <= 1
//0 <= 1
//====
//2 <= 2
//1 <= 2
//0 <= 2
//====
//3 <= 3
//2 <= 3
//1 <= 3
//0 <= 3
//====
//4 <= 4
//3 <= 4
//2 <= 4
//1 <= 4
//0 <= 4
//====
+62
View File
@@ -0,0 +1,62 @@
var pos
var array[100]
func main(value) {
initialize()
find(value)
}
// Fills the array with initial values in a pseudo-random way
// Works by incrementing array elements until one of the elements
func initialize() {
var i = 0, j = 0
var index
println("Filling up array with \"random\" values")
while (1) {
i = i * 21 + i / 31 + 7
j = j * 16 + j / 13 + 18
index = i + j
index = index < 0 ? 1 - index : index
// Turn the numbers into an index in the range [0,99]
index = index - index / 100 * 100
// Increment the array at the given index, stop once a 10 is written
array[index] = array[index] + 1
if (array[index] >= 10)
break
}
}
// Starts looking for an array element with the given value, starting at "pos"
// If a match is found, 1 is returned, and pos is the index of the match
func nextPos(value) {
while (pos < 100) {
if (array[pos] == value)
return 1
pos = pos + 1
}
return 0
}
func find(value) {
println("Looking for all occurances of ", value)
pos = 0
print("Found at:")
while (nextPos(value)) {
print(" ", pos)
pos = pos + 1
}
println("")
println("Done!")
}
//TESTCASE: 3
//Filling up array with "random" values
//Looking for all occurances of 3
//Found at: 0 2 9 23 26 36 42 50 64 71 72 78 80 87 95
//Done!
+73
View File
@@ -0,0 +1,73 @@
func main(a) {
if (a < 10)
println("a is less than 10")
if (a > -5)
println("a is more than -5")
else
println("a is less than or equal to -5")
if (a == 5)
println("a is equal to 5")
if (a != 0 and a != 4)
println("a is neither 0 nor 4")
if (a == 0 or a == 4)
println("a is 0 or 4")
if (a != 0) {
println("a is not equal to 0")
if (a == 6)
println("a is however 6")
else if (a == 7)
println("a is however 7")
else if (a == 10)
println("a is however 10")
else
println("a is neither 0, 6, 7 or 10, but ", a)
if (a == 10) {
println("10 is my favorite")
} else {
println("I'm not happy about it either")
}
print("FIN? ")
}
println("FIN.")
}
//TESTCASE: 4
//a is less than 10
//a is more than -5
//a is 0 or 4
//a is not equal to 0
//a is neither 0, 6, 7 or 10, but 4
//I'm not happy about it either
//FIN? FIN.
//TESTCASE: 0
//a is less than 10
//a is more than -5
//a is 0 or 4
//FIN.
//TESTCASE: 10
//a is more than -5
//a is neither 0 nor 4
//a is not equal to 0
//a is however 10
//10 is my favorite
//FIN? FIN.
//TESTCASE: -20
//a is less than 10
//a is less than or equal to -5
//a is neither 0 nor 4
//a is not equal to 0
//a is neither 0, 6, 7 or 10, but -20
//I'm not happy about it either
//FIN? FIN.
+55
View File
@@ -0,0 +1,55 @@
var notPrime[500]
func main(max) {
println("Now printing every prime between 2 and ", max, ":")
if (max > 499) {
println("Sorry, this program can only print primes up to 499")
return 0
}
// Special cases for 0 and 1
notPrime[0] = 1
notPrime[1] = 1
var i = 2
while (i < max/2+1) {
// Only do sieve if i is a prime
if (!notPrime[i])
doSieve(i, max)
i = i + 1
}
printPrimes(max)
}
// Marks every multiple of factor, from 2*factor and above, as not being prime
func doSieve(factor, max) {
var counter = factor*2
while (counter < max + 1) {
notPrime[counter] = 1
counter = counter + factor
}
}
func printPrimes(max) {
var i = 0
while (1) {
if (!notPrime[i])
print(" ", i)
if (i == max)
break
else
i = i + 1
}
println("")
}
//TESTCASE: 600
//Now printing every prime between 2 and 600:
//Sorry, this program can only print primes up to 499
//TESTCASE: 100
//Now printing every prime between 2 and 100:
// 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
@@ -0,0 +1,33 @@
func main(a) {
if (a and true())
println("A")
if (a or false() or true())
println("B")
}
func true() {
println("Called true")
return 1
}
func false() {
println("Called false")
return 0
}
//TESTCASE: 1
//Called true
//A
//B
//TESTCASE: 100
//Called true
//A
//B
//TESTCASE: 0
//Called false
//Called true
//B
@@ -0,0 +1,19 @@
func main(a)
{
while (1)
{
println("Hello")
if (a)
break
return 0
}
println("Bye!")
}
//TESTCASE: 0
//Hello
//TESTCASE: 1
//Hello
//Bye!
@@ -0,0 +1,20 @@
func main(a) {
if (a > 5)
println("Input is above 5")
else
println("Input is not above 5")
println("Bye!")
}
//TESTCASE: 7
//Input is above 5
//Bye!
//TESTCASE: 5
//Input is not above 5
//Bye!
//TESTCASE: -2
//Input is not above 5
//Bye!
@@ -0,0 +1,13 @@
func main(a)
{
println("abs: ", a < 0 ? -a : a)
}
//TESTCASE: 6
//abs: 6
//TESTCASE: -7
//abs: 7
//TESTCASE: 0
//abs: 0
@@ -0,0 +1,16 @@
func main(count)
{
while (count > 0)
{
println("count is now ", count)
count = count - 1
}
}
//TESTCASE: 6
//count is now 6
//count is now 5
//count is now 4
//count is now 3
//count is now 2
//count is now 1
+119
View File
@@ -0,0 +1,119 @@
func main(a) {
// Nudge away from 50
a = a > 50 ? b(a, 4) : c(a, 4)
print("a: ")
// Cap to range [0, 100]
a = a > 100
? i(100)
: (a < 0
? i(0)
: i(a))
// Round to nearest 10
print("b: ")
var b = a >= 55
? (a >= 75
? (a >= 95
? i(100)
: (a >= 85
? i(90)
: i(80)))
: (a >= 65
? i(70)
: i(60)))
: (a >= 25
? (a >= 45
? i(50)
: (a >= 35
? i(40)
: i(30)))
: (a >= 15
? i(20)
: (a >= 5
? i(10)
: i(0))))
println("b: ", b)
}
func b(x, y) {
println("Inside b(", x, ", ", y, ")")
return x + y
}
func c(x, y) {
println("Inside c(", x, ", ", y, ")")
return x - y
}
// Identity function
func i(value) {
println(value)
return value
}
//TESTCASE: 97
//Inside b(97, 4)
//a: 100
//b: 100
//b: 100
//TESTCASE: 82
//Inside b(82, 4)
//a: 86
//b: 90
//b: 90
//TESTCASE: 80
//Inside b(80, 4)
//a: 84
//b: 80
//b: 80
//TESTCASE: 68
//Inside b(68, 4)
//a: 72
//b: 70
//b: 70
//TESTCASE: 51
//Inside b(51, 4)
//a: 55
//b: 60
//b: 60
//TESTCASE: 50
//Inside c(50, 4)
//a: 46
//b: 50
//b: 50
//TESTCASE: 44
//Inside c(44, 4)
//a: 40
//b: 40
//b: 40
//TESTCASE: 32
//Inside c(32, 4)
//a: 28
//b: 30
//b: 30
//TESTCASE: 28
//Inside c(28, 4)
//a: 24
//b: 20
//b: 20
//TESTCASE: 9
//Inside c(9, 4)
//a: 5
//b: 10
//b: 10
//TESTCASE: 2
//Inside c(2, 4)
//a: 0
//b: 0
//b: 0
+48
View File
@@ -0,0 +1,48 @@
func main() {
println("First printing every power of two less than 1000")
var i = 1
while (i < 1000) {
println("i is now ", i)
i = i*2
}
println("")
println("Now printing every product with factors less than 6")
i = 1
while (i < 6) {
var j
j = 1
while (j < i) {
println(j, " * ", i, " = ", i*j)
j = j+1
}
i = i+1
}
}
//TESTCASE:
//First printing every power of two less than 1000
//i is now 1
//i is now 2
//i is now 4
//i is now 8
//i is now 16
//i is now 32
//i is now 64
//i is now 128
//i is now 256
//i is now 512
//
//Now printing every product with factors less than 6
//1 * 2 = 2
//1 * 3 = 3
//2 * 3 = 6
//1 * 4 = 4
//2 * 4 = 8
//3 * 4 = 12
//1 * 5 = 5
//2 * 5 = 10
//3 * 5 = 15
//4 * 5 = 20