-
Notifications
You must be signed in to change notification settings - Fork 0
/
number_filtering.go
101 lines (82 loc) · 1.56 KB
/
number_filtering.go
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
package exercises
import (
"math"
)
type Cond = func(int) bool
func Filter_nums(input []int, cond Cond) []int {
output := make([]int, len(input))
i := 0
for _, v := range input {
if cond(v) {
output[i] = v
i++
}
}
return output
}
func IsEven(num int) bool {
return num%2 == 0
}
func IsOdd(num int) bool {
return !IsEven(num)
}
func IsPrime(num int) bool {
n := float64((fact(num-1) + 1) / num)
return int(math.Mod(n*1000.0, 1000.0)) == 0
}
func IsOddPrime(num int) bool {
return IsPrime(num) && IsOdd(num)
}
func IsMultipleOf(num int) func(int) bool {
return func(i int) bool {
return i%num == 0
}
}
func FilterByConds(input []int, cond func(int, ...Cond) bool, functions ...func(int) bool) []int {
output := make([]int, len(input))
i := 0
for _, v := range input {
if cond(v, functions...) {
output[i] = v
i++
}
}
return output
}
func All(num int, fns ...Cond) bool {
for _, fn := range fns {
if fn(num) == false {
return false
}
}
return true
}
func Any(num int, fns ...Cond) bool {
for _, fn := range fns {
if fn(num) == true {
return true
}
}
return false
}
func IsGreater(num int) func(int) bool {
return func(i int) bool {
return i > i
}
}
func IsOddGreaterAndMultipleOf(g int, m int) func(int) bool {
return func(i int) bool {
return IsOdd(i) && IsGreater(g)(i) && IsMultipleOf(m)(i)
}
}
func IsEvenMultipleOf(mul int) func(int) bool {
return func(i int) bool {
return IsEven(i) && IsMultipleOf(mul)(i)
}
}
func fact(num int) int {
if num == 0 || num == 1 {
return 1
}
return num * fact(num-1)
}