User validation
function validate_user()
const pw = "pass"
pwguess = ""
attempts = 1
do
print("Attempt number " + attempts)
pwguess = input("Password: ")
attempts += 1
until pwguess == pw OR attempts > 3
return pwguess == pw
endfunction
if validate_user() then
print("yay")
else
print("no")
endif
const pw = "pass"
pwguess = ""
attempts = 1
do
print("Attempt number " + attempts)
pwguess = input("Password: ")
attempts += 1
until pwguess == pw OR attempts > 3
return pwguess == pw
endfunction
if validate_user() then
print("yay")
else
print("no")
endif
Calculator
a = real(input("Enter number: "))
op = ""
do
op = input("Enter operation: ")
until op == "+" OR op == "-" OR op == "*" OR op == "/" OR op == "^" OR op == "sqrt"
b=0
if op != "sqrt" then
b=real(input("Enter number: "))
endif
result = 0
switch op:
case "+":
result = a+b
case "-":
result = a-b
case "*":
result = a*b
case "/":
result = a/b
case "^":
result = a^b
case "sqrt":
result = sqrt(a)
default:
print("Operation " + op + " not implemented")
endswitch
print("Answer: " + result)
op = ""
do
op = input("Enter operation: ")
until op == "+" OR op == "-" OR op == "*" OR op == "/" OR op == "^" OR op == "sqrt"
b=0
if op != "sqrt" then
b=real(input("Enter number: "))
endif
result = 0
switch op:
case "+":
result = a+b
case "-":
result = a-b
case "*":
result = a*b
case "/":
result = a/b
case "^":
result = a^b
case "sqrt":
result = sqrt(a)
default:
print("Operation " + op + " not implemented")
endswitch
print("Answer: " + result)
Base X to decimal
function lookup(char, base)
index = -1
symbols = [
"0", "1", "2", "3",
"4", "5", "6", "7",
"8", "9", "A", "B",
"C", "D", "E", "F"
]
if base > symbols.length then
print("Sorry, only bases 1 to "+symbols.length+" are supported")
endif
for j = 0 to base - 1
if char == symbols[j] then
index = j
endif
next j
if index == -1 then
print("Invalid character "+char)
endif
return index
endfunction
function convert(hex, base)
out = 0
for i = 0 to hex.length-1
out += base ^ (hex.length-1-i) * lookup(hex[i], base)
next i
return out
endfunction
print(convert("111101", 2)) //61
index = -1
symbols = [
"0", "1", "2", "3",
"4", "5", "6", "7",
"8", "9", "A", "B",
"C", "D", "E", "F"
]
if base > symbols.length then
print("Sorry, only bases 1 to "+symbols.length+" are supported")
endif
for j = 0 to base - 1
if char == symbols[j] then
index = j
endif
next j
if index == -1 then
print("Invalid character "+char)
endif
return index
endfunction
function convert(hex, base)
out = 0
for i = 0 to hex.length-1
out += base ^ (hex.length-1-i) * lookup(hex[i], base)
next i
return out
endfunction
print(convert("111101", 2)) //61
Maths and formatting
const bookingFee = 1.50
const childPrice = 2.50
const adultPrice = 5.00
function price(adults, children)
return adults * adultPrice + childPrice * 2.5 + bookingFee
endfunction
function format(price)
s = str(price)
out = ""
for i=0 to s.length - 1
if s[i] == "." then
if i+2 == s.length then //one dp
return "£" +out + "." + s[i+1] + "0"
else //two dp
return "£" + out + "." + s[i+1] + s[i+2]
endif
endif
out += s[i]
next i
//no dot, no dp
return "£" + out + ".00"
endfunction
print(format(price(3, 5)))
const childPrice = 2.50
const adultPrice = 5.00
function price(adults, children)
return adults * adultPrice + childPrice * 2.5 + bookingFee
endfunction
function format(price)
s = str(price)
out = ""
for i=0 to s.length - 1
if s[i] == "." then
if i+2 == s.length then //one dp
return "£" +out + "." + s[i+1] + "0"
else //two dp
return "£" + out + "." + s[i+1] + s[i+2]
endif
endif
out += s[i]
next i
//no dot, no dp
return "£" + out + ".00"
endfunction
print(format(price(3, 5)))
Phonetic alphabet
alphabet = [
"Alfa", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf",
"Hotel", "India", "Juliet", "Kilo", "Lima", "Mike", "November",
"Oscar", "Papa", "Quebec", "Romeo", "Sierra", "Tango", "Uniform",
"Victor", "Whiskey", "X-ray", "Yankee", "Zulu"
]
x = input("Word:").lower
result = ""
for i=0 to x.length-1
if i != 0 then
result += " "
endif
result += alphabet[ASC(x[i])-97]
next i
print(result)
"Alfa", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf",
"Hotel", "India", "Juliet", "Kilo", "Lima", "Mike", "November",
"Oscar", "Papa", "Quebec", "Romeo", "Sierra", "Tango", "Uniform",
"Victor", "Whiskey", "X-ray", "Yankee", "Zulu"
]
x = input("Word:").lower
result = ""
for i=0 to x.length-1
if i != 0 then
result += " "
endif
result += alphabet[ASC(x[i])-97]
next i
print(result)
String reverser
function reverse(x)
out = ""
for i = x.length - 1 to 0 step -1
out += x[i]
next i
return out
endfunction
print(reverse("Hello world!"))
out = ""
for i = x.length - 1 to 0 step -1
out += x[i]
next i
return out
endfunction
print(reverse("Hello world!"))
File handling
// file creation
if NOT existsFile("stuff.txt") then
newFile("stuff.txt")
endif
// file write
f = open("stuff.txt")
for i = 1 to 5
f.writeLine(str(random(1, 100)))
next i
f.close()
// file read
f = open("stuff.txt")
while NOT f.endOfFile()
print(f.readLine())
endwhile
f.close()
// file deletion
if random(1, 2) == 1 then
print("Deleting file")
delFile("stuff.txt")
endif
if NOT existsFile("stuff.txt") then
newFile("stuff.txt")
endif
// file write
f = open("stuff.txt")
for i = 1 to 5
f.writeLine(str(random(1, 100)))
next i
f.close()
// file read
f = open("stuff.txt")
while NOT f.endOfFile()
print(f.readLine())
endwhile
f.close()
// file deletion
if random(1, 2) == 1 then
print("Deleting file")
delFile("stuff.txt")
endif
String comparison
wordA = input("wordA: ")
wordB = input("wordB: ")
switch wordA.compareAlphabetical(wordB):
case -1:
print("wordA < wordB")
case 0:
print("wordA == wordB")
case 1:
print("wordA > wordB")
endswitch
wordB = input("wordB: ")
switch wordA.compareAlphabetical(wordB):
case -1:
print("wordA < wordB")
case 0:
print("wordA == wordB")
case 1:
print("wordA > wordB")
endswitch
Classes (A-level)
class Dog
public name
private breed
public procedure new(pName, pBreed)
name = pName
breed = pBreed
endprocedure
public function getBreed()
return breed
endfunction
public procedure bark()
print("Woof!")
endprocedure
endclass
fido = new Dog("Fido", "labrador")
fido.bark()
print("Fido's name is", fido.name)
print("Fido is a", fido.getBreed())
public name
private breed
public procedure new(pName, pBreed)
name = pName
breed = pBreed
endprocedure
public function getBreed()
return breed
endfunction
public procedure bark()
print("Woof!")
endprocedure
endclass
fido = new Dog("Fido", "labrador")
fido.bark()
print("Fido's name is", fido.name)
print("Fido is a", fido.getBreed())
Inheritance (A-level)
class Animal
public name
public procedure new(pName)
name = pName
endprocedure
public procedure eat(food)
print("(" + name + " eats the " + food + ")")
endprocedure
endclass
class Cat inherits Animal
public breed
// override constructor
public procedure new(pName, pBreed)
super.new(pName)
breed = pBreed
endprocedure
public procedure speak()
print("Meow")
endprocedure
endclass
class Human inherits Animal
public procedure speak()
print(name + ": Hi! I'm " + name)
endprocedure
// override eat method
public procedure eat(food)
if food == "broccoli" then
print("Mmm, my favourite!")
endif
// call eat on base class (Animal)
super.eat(food)
endprocedure
endclass
monty = new Cat("Monty", "Siamese")
monty.speak()
monty.eat("fish")
print("Monty's breed is", monty.breed)
print()
joe = new Human("Joe")
joe.speak()
joe.eat("bread")
joe.eat("broccoli")
public name
public procedure new(pName)
name = pName
endprocedure
public procedure eat(food)
print("(" + name + " eats the " + food + ")")
endprocedure
endclass
class Cat inherits Animal
public breed
// override constructor
public procedure new(pName, pBreed)
super.new(pName)
breed = pBreed
endprocedure
public procedure speak()
print("Meow")
endprocedure
endclass
class Human inherits Animal
public procedure speak()
print(name + ": Hi! I'm " + name)
endprocedure
// override eat method
public procedure eat(food)
if food == "broccoli" then
print("Mmm, my favourite!")
endif
// call eat on base class (Animal)
super.eat(food)
endprocedure
endclass
monty = new Cat("Monty", "Siamese")
monty.speak()
monty.eat("fish")
print("Monty's breed is", monty.breed)
print()
joe = new Human("Joe")
joe.speak()
joe.eat("bread")
joe.eat("broccoli")