Browse Source

Move useful code again

pull/533/head
Xavier Petit 5 years ago committed by xpetit
parent
commit
3569fc1bf6
  1. 10
      go/lib/is/is.go
  2. 14
      go/tests/any/main.go
  3. 14
      go/tests/countif/main.go
  4. 4
      go/tests/findnextprime/main.go
  5. 4
      go/tests/findprevprime/main.go
  6. 4
      go/tests/islower/main.go
  7. 4
      go/tests/isnumeric/main.go
  8. 4
      go/tests/isprime/main.go
  9. 4
      go/tests/isupper/main.go
  10. 6
      go/tests/map/main.go

10
go/lib/is/is.go

@ -1,11 +1,11 @@
package common
package is
import (
"math/big"
"unicode"
)
func IsDigit(s string) bool {
func Digit(s string) bool {
for _, r := range s {
if !unicode.IsDigit(r) {
return false
@ -14,7 +14,7 @@ func IsDigit(s string) bool {
return true
}
func IsLower(s string) bool {
func Lower(s string) bool {
for _, r := range s {
if !unicode.IsLower(r) {
return false
@ -23,7 +23,7 @@ func IsLower(s string) bool {
return true
}
func IsUpper(s string) bool {
func Upper(s string) bool {
for _, r := range s {
if !(r >= 'A' && r <= 'Z') {
return false
@ -32,6 +32,6 @@ func IsUpper(s string) bool {
return true
}
func IsPrime(i int) bool {
func Prime(i int) bool {
return big.NewInt(int64(i)).ProbablyPrime(0)
}

14
go/tests/any/main.go

@ -1,9 +1,9 @@
package main
import (
"../common"
"./student"
"github.com/01-edu/public/go/lib"
"github.com/01-edu/public/go/lib/is"
)
func any(f func(string) bool, arr []string) bool {
@ -17,7 +17,7 @@ func any(f func(string) bool, arr []string) bool {
}
func main() {
functions := []func(string) bool{common.IsDigit, common.IsLower, common.IsUpper}
functions := []func(string) bool{is.Digit, is.Lower, is.Upper}
type node struct {
f func(string) bool
@ -35,31 +35,31 @@ func main() {
}
for i := 0; i < 5; i++ {
table = append(table, node{
f: common.IsDigit,
f: is.Digit,
a: lib.MultRandDigit(),
})
}
for i := 0; i < 5; i++ {
table = append(table, node{
f: common.IsLower,
f: is.Lower,
a: lib.MultRandLower(),
})
}
for i := 0; i < 5; i++ {
table = append(table, node{
f: common.IsUpper,
f: is.Upper,
a: lib.MultRandUpper(),
})
}
table = append(table,
node{
f: common.IsDigit,
f: is.Digit,
a: []string{"Hello", "how", "are", "you"},
},
node{
f: common.IsDigit,
f: is.Digit,
a: []string{"This", "is", "4", "you"},
},
)

14
go/tests/countif/main.go

@ -1,9 +1,9 @@
package main
import (
"../common"
"./student"
"github.com/01-edu/public/go/lib"
"github.com/01-edu/public/go/lib/is"
)
func countIf(f func(string) bool, arr []string) int {
@ -18,7 +18,7 @@ func countIf(f func(string) bool, arr []string) int {
}
func main() {
functions := []func(string) bool{common.IsDigit, common.IsLower, common.IsUpper}
functions := []func(string) bool{is.Digit, is.Lower, is.Upper}
type node struct {
f func(string) bool
@ -37,7 +37,7 @@ func main() {
}
for i := 0; i < 5; i++ {
val := node{
f: common.IsDigit,
f: is.Digit,
arr: lib.MultRandDigit(),
}
table = append(table, val)
@ -45,14 +45,14 @@ func main() {
for i := 0; i < 5; i++ {
val := node{
f: common.IsLower,
f: is.Lower,
arr: lib.MultRandLower(),
}
table = append(table, val)
}
for i := 0; i < 5; i++ {
val := node{
f: common.IsUpper,
f: is.Upper,
arr: lib.MultRandUpper(),
}
table = append(table, val)
@ -60,11 +60,11 @@ func main() {
table = append(table,
node{
f: common.IsDigit,
f: is.Digit,
arr: []string{"Hello", "how", "are", "you"},
},
node{
f: common.IsDigit,
f: is.Digit,
arr: []string{"This", "is", "4", "you"},
},
)

4
go/tests/findnextprime/main.go

@ -1,13 +1,13 @@
package main
import (
"../common"
"./student"
"github.com/01-edu/public/go/lib"
"github.com/01-edu/public/go/lib/is"
)
func findNextPrime(nb int) int {
if common.IsPrime(nb) {
if is.Prime(nb) {
return nb
}
return findNextPrime(nb + 1)

4
go/tests/findprevprime/main.go

@ -1,16 +1,16 @@
package main
import (
"../common"
"./student"
"github.com/01-edu/public/go/lib"
"github.com/01-edu/public/go/lib/is"
)
func findPrevPrime(nb int) int {
if nb < 2 {
return 0
}
if common.IsPrime(nb) {
if is.Prime(nb) {
return nb
}
return findPrevPrime(nb - 1)

4
go/tests/islower/main.go

@ -1,9 +1,9 @@
package main
import (
"../common"
"./student"
"github.com/01-edu/public/go/lib"
"github.com/01-edu/public/go/lib/is"
)
func main() {
@ -39,6 +39,6 @@ func main() {
"hello!",
)
for _, arg := range table {
lib.Challenge("IsLower", student.IsLower, common.IsLower, arg)
lib.Challenge("IsLower", student.IsLower, is.Lower, arg)
}
}

4
go/tests/isnumeric/main.go

@ -3,9 +3,9 @@ package main
import (
"strconv"
"../common"
"./student"
"github.com/01-edu/public/go/lib"
"github.com/01-edu/public/go/lib/is"
)
func main() {
@ -33,6 +33,6 @@ func main() {
"01,02,03",
)
for _, arg := range table {
lib.Challenge("IsNumeric", student.IsNumeric, common.IsDigit, arg)
lib.Challenge("IsNumeric", student.IsNumeric, is.Digit, arg)
}
}

4
go/tests/isprime/main.go

@ -1,9 +1,9 @@
package main
import (
"../common"
"./student"
"github.com/01-edu/public/go/lib"
"github.com/01-edu/public/go/lib/is"
)
func main() {
@ -27,6 +27,6 @@ func main() {
1000000087,
)
for _, arg := range table {
lib.Challenge("IsPrime", student.IsPrime, common.IsPrime, arg)
lib.Challenge("IsPrime", student.IsPrime, is.Prime, arg)
}
}

4
go/tests/isupper/main.go

@ -1,9 +1,9 @@
package main
import (
"../common"
"./student"
"github.com/01-edu/public/go/lib"
"github.com/01-edu/public/go/lib/is"
)
func main() {
@ -41,6 +41,6 @@ func main() {
"HELLO!",
)
for _, arg := range table {
lib.Challenge("IsUpper", student.IsUpper, common.IsUpper, arg)
lib.Challenge("IsUpper", student.IsUpper, is.Upper, arg)
}
}

6
go/tests/map/main.go

@ -1,9 +1,9 @@
package main
import (
"../common"
"./student"
"github.com/01-edu/public/go/lib"
"github.com/01-edu/public/go/lib/is"
)
func isPositive(i int) bool {
@ -25,7 +25,7 @@ func _map(f func(int) bool, a []int) []bool {
}
func main() {
functions := []func(int) bool{isPositive, isNegative0, common.IsPrime}
functions := []func(int) bool{isPositive, isNegative0, is.Prime}
type node struct {
f func(int) bool
@ -44,7 +44,7 @@ func main() {
}
table = append(table, node{
f: common.IsPrime,
f: is.Prime,
arr: []int{1, 2, 3, 4, 5, 6},
})

Loading…
Cancel
Save