/ printnbrbase.go
printnbrbase.go
1 package piscine 2 3 import "github.com/01-edu/z01" 4 5 func PrintNbrBase(nbr int, base string) { 6 if !isValidBase(base) { 7 z01.PrintRune('N') 8 z01.PrintRune('V') 9 return 10 } 11 if nbr < 0 { 12 z01.PrintRune('-') // let's print the minus sign 13 PrintNbrBase(-nbr, base) // then recall the function with the positive value 14 return 15 } 16 17 if nbr >= len(base) { // if the number is bigger than the base length 18 PrintNbrBase(nbr/len(base), base) // call the function with the number divided by the base length 19 } 20 21 z01.PrintRune(rune(base[nbr%len(base)])) // print the rune at the index of the remainder of the number divided by the base length 22 } 23 24 func isValidBase(base string) bool { 25 if len(base) < 2 { 26 return false 27 } 28 29 checked := "" 30 31 for _, b := range base { 32 if b == '-' || b == '+' { 33 return false 34 } 35 for _, c := range checked { 36 if c == b { 37 return false 38 } 39 } 40 checked += string(b) 41 } 42 return true 43 }