-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperators.go
More file actions
55 lines (53 loc) · 1.1 KB
/
Copy pathoperators.go
File metadata and controls
55 lines (53 loc) · 1.1 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
package main
type Operator struct {
symbol rune
eval func(int, int) (int, bool)
}
var (
opAdd = Operator{'+', func(fst, snd int) (int, bool) {
if fst > 1_000_000 && snd > 1_000_000 || fst < -1_000_000 && snd < -1_000_000 {
return 0, false
}
return fst + snd, true
}}
opMultiply = Operator{'*', func(fst, snd int) (int, bool) {
if fst > 1_000 && snd > 1_000 || fst < -1_000 && snd < -1_000 || fst > 1_000_000 || fst < -1_000_000 || snd > 1_000_000 || snd < -1_000_000 {
return 0, false
}
if snd < 0 {
return 0, false
}
return fst * snd, true
}}
opDivide = Operator{'/', func(fst, snd int) (int, bool) {
if snd < 0 {
return 0, false
}
if snd == 0 {
return 0, false
}
if fst % snd != 0 {
return 0, false
}
return fst / snd, true
}}
opRaise = Operator{'^', func(fst, snd int) (int, bool) {
if fst < 0 {
return 0, false
}
if snd < 0 {
return 0, false
}
if snd > 100 {
return 0, false
}
result := 1
for i := 0; i < snd; i++ {
result *= fst
if result > 1_000_000 {
return 0, false
}
}
return result, true
}}
)