-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprimitive_types_2.cairo
More file actions
77 lines (72 loc) · 2.12 KB
/
primitive_types_2.cairo
File metadata and controls
77 lines (72 loc) · 2.12 KB
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
fn main() {
// A short string is a string whose length is at most 31 characters, and therefore can fit into a single field element.
// Short strings are actually felts, they are not a real string.
// Note the _single_ quotes that are used with short strings.
let mut my_first_initial = 'C';
if is_alphabetic(
ref my_first_initial
) {
println!(" Alphabetical !");
} else if is_numeric(
ref my_first_initial
) {
println!(" Numerical !");
} else {
println!(" Neither alphabetic nor numeric!");
}
let mut your_character = 'Z'; // Finish this line like the example! What's your favorite short string?
// Try a letter, try a number, try a special character, try a short string!
if is_alphabetic(
ref your_character
) {
println!(" Alphabetical !");
} else if is_numeric(
ref your_character
) {
println!(" Numerical!");
} else {
println!(" Neither alphabetic nor numeric!");
}
}
fn is_alphabetic(ref char: felt252) -> bool {
if char >= 'a' {
if char <= 'z' {
return true;
}
}
if char >= 'A' {
if char <= 'Z' {
return true;
}
}
false
}
fn is_numeric(ref char: felt252) -> bool {
if char >= '0' {
if char <= '9' {
return true;
}
}
false
}
// Note: the following code is not part of the challenge, it's just here to make the code above work.
// Direct felt252 comparisons have been removed from the core library, so we need to implement them ourselves.
// There will probably be a string / short string type in the future
impl PartialOrdFelt of PartialOrd<felt252> {
#[inline(always)]
fn le(lhs: felt252, rhs: felt252) -> bool {
!(rhs < lhs)
}
#[inline(always)]
fn ge(lhs: felt252, rhs: felt252) -> bool {
!(lhs < rhs)
}
#[inline(always)]
fn lt(lhs: felt252, rhs: felt252) -> bool {
integer::u256_from_felt252(lhs) < integer::u256_from_felt252(rhs)
}
#[inline(always)]
fn gt(lhs: felt252, rhs: felt252) -> bool {
rhs < lhs
}
}