Browse Source

Convert last "prog" exercises as functions exercises (intermediate commit)

content-update
Xavier Petit 4 years ago committed by xpetit
parent
commit
3821cdb631
  1. 8
      subjects/printrevcombprog.en.md
  2. 12
      subjects/reachablenumberprog.en.md
  3. 10
      tests/go/solutions/doppelgangerprog/main.go
  4. 3
      tests/go/solutions/doppelgangerprog/test_doppelgangerprog.go
  5. 15
      tests/go/solutions/findprevprime.go
  6. 27
      tests/go/solutions/findprevprimeprog/main.go
  7. 3
      tests/go/solutions/findprevprimeprog/test_findprevprimeprog.go
  8. 13
      tests/go/solutions/halfcontestprog/main.go
  9. 3
      tests/go/solutions/halfcontestprog/test_halfcontestprog.go
  10. 4
      tests/go/solutions/printrevcombprog/main.go
  11. 2
      tests/go/solutions/printrevcombprog/test_printrevcombprog.go
  12. 21
      tests/go/solutions/reachablenumberprog/main.go
  13. 3
      tests/go/solutions/reachablenumberprog/test_reachablenumberprog.go
  14. 45
      tests/go/solutions/sliceprog/main.go
  15. 3
      tests/go/solutions/sliceprog/test_sliceprog.go

8
subjects/printrevcombprog.en.md

@ -1,4 +1,4 @@
## printrevcombprog
## printrevcomb
### Instructions
@ -11,10 +11,10 @@ These combinations are separated by a comma and a space.
Here is an **incomplete** output :
```console
student@ubuntu:~/[[ROOT]]/printcombprog$ go build
student@ubuntu:~/[[ROOT]]/printcombprog$ ./printcombprog | cat -e
student@ubuntu:~/[[ROOT]]/printrevcomb$ go build
student@ubuntu:~/[[ROOT]]/printrevcomb$ ./printrevcomb | cat -e
987, 986, 985, 984, 983, 982, 981, 980, 976, ..., 310, 210$
student@ubuntu:~/[[ROOT]]/printcombprog$
student@ubuntu:~/[[ROOT]]/printrevcomb$
```
`999` or `000` are not valid combinations because the digits are not different.

12
subjects/reachablenumberprog.en.md

@ -19,12 +19,16 @@ Here is a possible program to test your function :
```go
package main
import "fmt"
import (
"fmt"
piscine ".."
)
func main() {
fmt.Println(ReachableNumber(1))
fmt.Println(ReachableNumber(10))
fmt.Println(ReachableNumber(1001))
fmt.Println(piscine.ReachableNumber(1))
fmt.Println(piscine.ReachableNumber(10))
fmt.Println(piscine.ReachableNumber(1001))
}
```

10
tests/go/solutions/doppelgangerprog/main.go

@ -1,10 +0,0 @@
package main
import "strings"
func DoppelGanger(s, substr string) int {
return strings.LastIndex(s, substr)
}
func main() {
}

3
tests/go/solutions/doppelgangerprog/test_doppelgangerprog.go

@ -4,6 +4,7 @@ import (
"github.com/01-edu/z01"
correct "./correct"
student "./student"
)
type node struct {
@ -47,6 +48,6 @@ func main() {
}
for _, arg := range table {
z01.Challenge("DoppelGangerProg", DoppelGanger, correct.DoppelGanger, arg.big, arg.little)
z01.Challenge("DoppelGangerProg", student.DoppelGanger, correct.DoppelGanger, arg.big, arg.little)
}
}

15
tests/go/solutions/findprevprime.go

@ -1,10 +1,23 @@
package correct
func isPrime(nb int) bool {
if nb <= 0 || nb == 1 {
return false
}
for i := 2; i <= nb/2; i++ {
if nb%i == 0 {
return false
}
}
return true
}
func FindPrevPrime(nbr int) int {
if nbr < 2 {
return 0
}
if IsPrime(nbr) {
if isPrime(nbr) {
return nbr
}
return FindPrevPrime(nbr - 1)

27
tests/go/solutions/findprevprimeprog/main.go

@ -1,27 +0,0 @@
package main
func main() {
}
func FindPrevPrime(nbr int) int {
if nbr < 2 {
return 0
}
if IsPrime(nbr) {
return nbr
}
return FindPrevPrime(nbr - 1)
}
func IsPrime(nb int) bool {
if nb <= 0 || nb == 1 {
return false
}
for i := 2; i <= nb/2; i++ {
if nb%i == 0 {
return false
}
}
return true
}

3
tests/go/solutions/findprevprimeprog/test_findprevprimeprog.go

@ -4,11 +4,12 @@ import (
"github.com/01-edu/z01"
correct "./correct"
student "./student"
)
func main() {
a := append(z01.MultRandIntBetween(0, 99999), 5, 4, 1)
for _, elem := range a {
z01.Challenge("FindPrevPrimeProg", FindPrevPrime, correct.FindPrevPrime, elem)
z01.Challenge("FindPrevPrimeProg", student.FindPrevPrime, correct.FindPrevPrime, elem)
}
}

13
tests/go/solutions/halfcontestprog/main.go

@ -1,13 +0,0 @@
package main
func Halfcontest(h1, m1, h2, m2 int) int {
t1 := h1*60 + m1
t2 := h2*60 + m2
t2 = (t2 + t1) / 2
h2 = t2 / 60
m2 = t2 % 60
return h2*100 + m2
}
func main() {
}

3
tests/go/solutions/halfcontestprog/test_halfcontestprog.go

@ -4,6 +4,7 @@ import (
"github.com/01-edu/z01"
correct "./correct"
student "./student"
)
func main() {
@ -28,6 +29,6 @@ func main() {
}
for _, arg := range table {
z01.Challenge("HalfContestProg", Halfcontest, correct.Halfcontest, arg.h1, arg.m1, arg.h2, arg.m2)
z01.Challenge("HalfContestProg", student.Halfcontest, correct.Halfcontest, arg.h1, arg.m1, arg.h2, arg.m2)
}
}

4
tests/go/solutions/printrevcombprog/main.go

@ -6,10 +6,10 @@ func main() {
for a := 9; a >= 2; {
for b := a - 1; b >= 1; {
for c := b - 1; c >= 0; {
if a > b && b > c && (a+b+c) != 3 {
if a > b && b > c && a+b+c != 3 {
fmt.Printf("%d%d%d, ", a, b, c)
}
if (a + b + c) == 3 {
if a+b+c == 3 {
fmt.Printf("%d%d%d\n", a, b, c)
}
c--

2
tests/go/solutions/printrevcombprog/test_printrevcombprog.go

@ -3,5 +3,5 @@ package main
import "github.com/01-edu/z01"
func main() {
z01.ChallengeMain("printrevcombprog")
z01.ChallengeMain("printrevcomb")
}

21
tests/go/solutions/reachablenumberprog/main.go

@ -1,21 +0,0 @@
package main
func ReachableNumber(n int) int {
cnt := 0
for n > 0 {
cnt++
if n < 10 {
cnt += 8
break
} else {
n++
}
for n%10 == 0 {
n /= 10
}
}
return cnt
}
func main() {
}

3
tests/go/solutions/reachablenumberprog/test_reachablenumberprog.go

@ -4,6 +4,7 @@ import (
"github.com/01-edu/z01"
correct "./correct"
student "./student"
)
func main() {
@ -17,6 +18,6 @@ func main() {
table = append(table, z01.MultRandIntBetween(1, 877))
}
for _, arg := range table {
z01.Challenge("ReachableNumber", ReachableNumber, correct.ReachableNumber, arg)
z01.Challenge("ReachableNumber", student.ReachableNumber, correct.ReachableNumber, arg)
}
}

45
tests/go/solutions/sliceprog/main.go

@ -1,45 +0,0 @@
package main
func ifNegative(a []string, n int) int {
if n < 0 {
n = len(a) + n
}
if n < 0 {
n = 0
} else if n > len(a) {
n = len(a)
}
return n
}
func Slice(a []string, nbr ...int) []string {
if len(nbr) == 0 {
return a
}
first := nbr[0]
if len(nbr) == 1 {
if first < 0 {
first = len(a) + first
if first < 0 {
return a
}
}
return a[first:]
}
second := nbr[1]
first = ifNegative(a, first)
second = ifNegative(a, second)
if first > second {
return nil
}
return a[first:second]
}
func main() {
}

3
tests/go/solutions/sliceprog/test_sliceprog.go

@ -4,6 +4,7 @@ import (
"github.com/01-edu/z01"
correct "./correct"
student "./student"
)
func main() {
@ -40,6 +41,6 @@ func main() {
}
for _, a := range arr {
z01.Challenge("SliceProg", Slice, correct.Slice, a...)
z01.Challenge("SliceProg", student.Slice, correct.Slice, a...)
}
}

Loading…
Cancel
Save