-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_2.mo
143 lines (127 loc) · 3.64 KB
/
day_2.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import Array "mo:base/Array";
import Char "mo:base/Char";
import Iter "mo:base/Iter";
import Nat "mo:base/Nat";
import Nat8 "mo:base/Nat8";
import Text "mo:base/Text";
actor {
// Challenge 1
public func nat_to_nat8(n : Nat) : async Nat8 {
if (n > 255) {
return 0;
};
return Nat8.fromNat(n);
};
// Challenge 2
public func max_number_with_n_bits(n : Nat) : async Nat {
return (2 ** n) - 1;
};
// Challenge 3
public func decimals_to_bits(n : Nat) : async Text {
var bits : Text = "";
var i : Nat = n;
while (i > 0) {
if (i % 2 == 1) {
bits := "1" # bits;
} else {
bits := "0" # bits;
};
i := i / 2;
};
return bits;
};
// Challenge 4
public func capitalize_character(c : Char) : async Char {
if (Char.isLowercase(c)) {
return Char.fromNat32(Char.toNat32(c) - 32);
};
return c;
};
// Challenge 5
public func capitalize_text(t : Text) : async Text {
var text : Text = "";
for (character in t.chars()) {
let capitalize : Char = await capitalize_character(character);
text := text # Char.toText(capitalize);
};
return text;
};
// Challenge 6
public func is_inside(t : Text, c : Char) : async Bool {
return Text.contains(t, #char(c));
};
// Challenge 7
public func trim_whitespace(t : Text) : async Text {
return Text.trim(t, #text(" "));
};
// Challenge 8
public func duplicated_character(t : Text) : async Text {
let characters : [Char] = Iter.toArray(t.chars());
for (i in Iter.range(0, t.size() - 2)) {
if (characters[i] == characters[i + 1]) {
return Text.fromChar(characters[i]);
}
};
return t;
};
// Challenge 9
public func size_in_bytes(t : Text) : async Nat {
return Text.encodeUtf8(t).size();
};
// Challenge 10
public func bubble_sort(array : [Nat]) : async [Nat] {
var sorted : [var Nat] = Array.thaw<Nat>(array);
let size : Nat = sorted.size();
for (i in Iter.range(0, size - 1)) {
for (j in Iter.range(0, size - 1 - i - 1)) {
if (sorted[j] > sorted[j + 1]) {
let tmp : Nat = sorted[j];
sorted[j] := sorted[j + 1];
sorted[j + 1] := tmp;
}
};
};
return Array.freeze<Nat>(sorted);
};
// Challenge 11
public func nat_opt_to_nat(n : ?Nat, m : Nat) : async Nat {
switch n {
case null return m;
case (?n) return n;
};
};
// Challenge 12
public func day_of_the_week(n : Nat) : async ?Text {
switch n {
case 1 return ?"Monday";
case 2 return ?"Tuesday";
case 3 return ?"Wednesday";
case 4 return ?"Thurday";
case 5 return ?"Friday";
case 6 return ?"Saturday";
case 7 return ?"Sunday";
case _ return null;
};
};
// Challenge 13
public func populate_array(array : [?Nat]) : async [Nat] {
return Array.map<?Nat, Nat>(array, func(n : ?Nat) : Nat {
switch n {
case null return 0;
case (?n) return n;
};
});
};
// Challenge 14
public func sum_of_array(array : [Nat]) : async Nat {
return Array.foldLeft<Nat, Nat>(array, 0, func(x : Nat, y : Nat) : Nat { x + y });
};
// Challenge 15
public func squared_array(array : [Nat]) : async [Nat] {
return Array.map<Nat, Nat>(array, func(x : Nat) : Nat { x * x });
};
// Challenge 16
public func increase_by_index(array : [Nat]) : async [Nat] {
return Array.mapEntries<Nat, Nat>(array, func(a : Nat, index : Nat) : Nat { a + index });
};
}