-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_1.mo
107 lines (94 loc) · 2.36 KB
/
day_1.mo
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import Array "mo:base/Array";
import Iter "mo:base/Iter";
import Nat "mo:base/Nat";
actor {
var counter:Nat = 0;
public func greet(name : Text) : async Text {
return "Hello, " # name # "!";
};
public func add(n:Nat, m:Nat):async Nat{
// Challenge 1
return n+m;
};
public func square(n:Nat):async Nat{
// Challenge 2
return n*n;
};
public func days_to_second(n:Nat):async Nat{
// Challenge 3
return n*24*60*60;
};
public func increment_counter(n:Nat):async Nat{
// Challenge 4
counter:= (counter+n);
return counter;
};
public func clear_counter():async Nat{
// Challenge 4
counter:=0;
return counter;
};
public func divide(n:Nat,m:Nat):async Bool{
// Challenge 5
if(m%n==0){
return true;
}
else{
return false;
}
};
public func is_even(n:Nat):async Bool{
// Challenge 6
if(n%2==0){
return true;
}
else{
return false;
};
};
public func sum_of_array(array:[Nat]):async Nat{
// Challenge 7
var sum:Nat=0;
for(val in array.vals()){
sum:=(sum+val);
};
return sum;
};
public func maximum(array:[Nat]):async Nat{
// Challenge 8
var max:Nat=0;
for(val in array.vals()){
if(max < val){
max:=val;
}
};
return max;
};
public func remove_from_array(array:[Nat],n:Nat):async [Nat]{
// Challenge 9
return Array.filter<Nat>(array,func(val:Nat):Bool{
if(val==n){
return false;
};
return true;
})
};
public func selection_sort(array:[Nat]):async [Nat]{
// Challenge 10
var array1:[var Nat] = Array.thaw(array);
for (i in Iter.range(0,array1.size()-1)) {
var min:Nat=i;
for(j in Iter.range(i+1,array1.size()-1)){
if(array1[j] < array1[min]){
min:=j;
}
};
if(min!=i){
let temp = array1[min];
array1[min]:=array1[i];
array1[i]:=temp;
}
};
return Array.freeze<Nat>(array1);
};
};