-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathsolution.mo
135 lines (118 loc) · 3.77 KB
/
solution.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
import Char "mo:base/Char";
import Text "mo:base/Text";
import Array "mo:base/Array";
actor {
// Challenge 1
public func nat_to_nat8(n : Nat) : async Nat8 {
if(n > 255) {
return 0; // n is too big to be representend by a Nat8, returning 0 is a personal choice you could return basically anything maybe 255 makes more sense.
} else {
return Nat8.fromNat(n);
};
};
// Challenge 2
public func max_number_with_n_bits(n : Nat) : async Nat {
var count = n;
var max_number = 0;
while(count > 0){
max_number := max_number + 2**(count - 1);
count := count - 1;
};
return(max_number);
};
// Challenge 2 : solution bis
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 m = n;
var bits = "";
while(m > 0){
let remainder = m % 2;
let m = m / 2;
if(remainder == 1){
bits = "1" # bits;
} else {
bits = "0" # bits;
};
};
return(bits)
};
// Challenge 4
public func capitalize_character(char : Char) : async Char {
let unicode_value = Char.toNat32(char);
if(unicode_value >= 97 or unicode_value <= 122){
return(Char.fromNat32(unicode_value - 32))
} else {
return (Char.fromNat32(unicode_value));
};
};
// Challenge 5
func _capitalize_character(char : Char) : Char {
let unicode_value = Char.toNat32(char);
if(unicode_value >= 97 or unicode_value <= 122){
return(Char.fromNat32(unicode_value - 32))
} else {
return (Char.fromNat32(unicode_value));
};
};
public func capitalize_text(word : Text) : async Text {
var new_word : Text = "";
for(char in word.chars()){
new_word #= Char.toText(_capitalize_character(char));
};
new_word;
};
// Challenge 6
public func is_inside(t : Text, l : Text) : async Bool {
let p = #text(l);
return(Text.contains(t, p));
};
// Challenge 7
public func trim_whitespace(t : Text) : asyncText {
let pattern = #text(" ");
return(Text.trim(t, p));
};
// Challenge 8
public func duplicate_character(t : Text) : async Text {
var characters : [Char] = [];
for (character in t.vals()){
switch(Array.filter(characters, f(x) : Text -> Bool {x == character})){
case(null) {
characters := Array.append<Text>(characters, [character]);
};
case(?char){
return Char.toText(char);
};
};
};
return (t);
};
// Challenge 9
public func size_in_bytes (t : Text) : async Nat {
let utf_blob = Text.encodeUtf8(t);
let array_bytes = Blob.toArray(utf_blob);
return(array_bytes.size());
};
// Challenge 10
func swap(array : [Nat], i : Nat, j : Nat) : [Nat] {
let mutable_array = Array.thaw<Nat>(array);
let tmp = mutable_array[i];
mutable_array[i] := mutable_array[j];
mutable_array[j] := tmp;
return(Array.freeze<Nat>(mutable_array))
};
public func bubble_sort(array : [Nat]) : async [Nat] {
var sorted = array;
let size = array.size();
for(i in Iter.range(0, size - 1){
for (j in Iter.range(0, size - 1 - i)){
if(sorted[i] > sorted[i + 1]){
sorted := _swap(sorted, i , j);
};
};
};
return (sorted)
};
}