more examples!
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
# == The beer song =================
|
||||
#
|
||||
# The lyrics follow this form:
|
||||
#
|
||||
# > 99 bottles of beer on the wall
|
||||
# > 99 bottles of beer
|
||||
# > Take one down, pass it around
|
||||
# > 98 bottles of beer on the wall
|
||||
# >
|
||||
# > 98 bottles of beer on the wall
|
||||
# > 98 bottles of beer
|
||||
# > Take one down, pass it around
|
||||
# > 97 bottles of beer on the wall
|
||||
# >
|
||||
# > ...
|
||||
#
|
||||
# and go on until 0.
|
||||
#
|
||||
# ==================================
|
||||
|
||||
stringFromInteger = import("../int_to_str.noja").stringFromInteger;
|
||||
cat = string.cat;
|
||||
|
||||
fun verse(n: int)
|
||||
return cat(
|
||||
stringFromInteger(n), " bottles of beer on the wall\n",
|
||||
stringFromInteger(n), " bottles of beer\n",
|
||||
"Take one down, pass it around\n",
|
||||
stringFromInteger(n-1), " bottles of beer on the wall\n"
|
||||
);
|
||||
|
||||
fun song(start: int = 99, end: int = 1) {
|
||||
|
||||
text = "";
|
||||
i = start;
|
||||
while i >= end: {
|
||||
text = cat(text, verse(i), "\n");
|
||||
i = i-1;
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
print(song());
|
||||
@@ -0,0 +1,40 @@
|
||||
Numeric = int | float;
|
||||
|
||||
fun abs(n) {
|
||||
if n < 0:
|
||||
return -n;
|
||||
return n;
|
||||
}
|
||||
|
||||
# Calculate the arithmetic-geometric mean of x,y.
|
||||
fun agm(x: Numeric, y: Numeric,
|
||||
epsilon: float = 0.00001)
|
||||
{
|
||||
sqrt = math.sqrt;
|
||||
|
||||
# Make sure they're floats by
|
||||
# multiplying by 1.0
|
||||
a = 1.0 * x;
|
||||
g = 1.0 * y;
|
||||
|
||||
while abs(a - g) > epsilon: {
|
||||
|
||||
a2 = (a + g) / 2;
|
||||
g2 = sqrt(a * g);
|
||||
|
||||
#print("a=", a, ", a2=", a2, "\n");
|
||||
#print("g=", g, ", g2=", g2, "\n");
|
||||
|
||||
# It must converge!
|
||||
assert(abs(a2 - g2) < abs(a - g));
|
||||
|
||||
a = a2;
|
||||
g = g2;
|
||||
}
|
||||
|
||||
return a, g;
|
||||
}
|
||||
|
||||
x = 24;
|
||||
y = 6;
|
||||
print(agm(x, y));
|
||||
@@ -0,0 +1,40 @@
|
||||
|
||||
# == CARTESIAN PRODUCT OF TWO OR MORE LISTS ==
|
||||
#
|
||||
# > Show one or more idiomatic ways of
|
||||
# > generating the cartesian product of
|
||||
# > two arbitrary lists in your language.
|
||||
# >
|
||||
# > Demonstrate that your function/method
|
||||
# > correctly returns:
|
||||
# >
|
||||
# > {1, 2} x {3, 4} = {(1, 3), (1, 4), (2, 3), (2, 4)}
|
||||
# >
|
||||
# > and, in contrast:
|
||||
# >
|
||||
# > {3, 4} x {1, 2} = {(3, 1), (3, 2), (4, 1), (4, 2)}
|
||||
# >
|
||||
# > Also demonstrate, ussing your function/method,
|
||||
# > that the product of an empty list
|
||||
# > with any other list is empty.
|
||||
# >
|
||||
# > {1, 2} x {} = {}
|
||||
# > {} x {1, 2} = {}
|
||||
|
||||
fun cartesianProduct(a: List, b: List)
|
||||
{
|
||||
out = [];
|
||||
i = 0;
|
||||
while i < count(a): {
|
||||
j = 0;
|
||||
while j < count(b): {
|
||||
out[count(out)] = [a[i], b[j]];
|
||||
j = j+1;
|
||||
}
|
||||
i = i+1;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
print(cartesianProduct([1, 2], [3, 4]), "\n");
|
||||
print(cartesianProduct([3, 4], [1, 2]), "\n");
|
||||
Reference in New Issue
Block a user