-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexamples.txt
81 lines (77 loc) · 1.91 KB
/
examples.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//recursive fibonacci
def fib(n: int): int {
if(n <= 1) {return n;};
return (fib((n-1)) + fib((n-2)));
}
//dynamic array implementation
object Dynamic {
size: int;
allocated: int;
arr: array[int];
def Dynamic(self: Dynamic) {
self.arr = array[int][2];
self.allocated = 2;
self.size = 0;
}
def push(self: Dynamic, value: int) {
self.arr[self.size] = value;
self.size += 1;
if(self.allocated == self.size) {
val newsize = (self.allocated * 2);
val old = self.arr;
self.arr = array[int][newsize];
for(val i = 0; i < self.size; i+= 1;) {
self.arr[i] = old[i];
};
self.allocated = newsize;
};
}
def get(self: Dynamic, index: int): int {
if(index >= self.size) {
print("can not do that");
throw exception;
};
return self.arr[index];
}
def print_arr(self: Dynamic) {
for(val i = 0; i < self.size; i+= 1;) {
print(self.arr[i]);
};
}
def compare(self: Dynamic, other: Dynamic): bool {
val same: bool = true;
if(self.size != other.size) {return false;};
for(val i = 0; i < self.size; i+= 1;) {
if(self.get(i) != other.get(i)) {same = false;};
};
return same;
}
}
//example with templates
/*
def main(): int {
val a = array(1,2,3);
val b = array(4,5,6);
val c = concat(a,b);
print_arr(c);
}
*/
def concat(a: array[T1], b: array[T1]): array[T1] {
val combined = (a.size + b.size);
val ret = array[T1][combined];
for(val i = 0; i < a.size; i += 1;) {
ret[i] = a[i];
};
for(val i = 0; i < b.size; i += 1;) {
ret[(i + a.size)] = b[i];
};
return ret;
}
def print_arr(a: array[int]) {
val b = 0;
while(b < a.size) {
print(a[b]);
b += 1;
};
return;
}