Printnbrbase.go

Hi guys i’ve got some problems with my code. I would try to create a code that prints an int in a string base passed as parameters.

If the base is not valid, the function prints NV (Not Valid).

i’ve got some validity rules:

  • A base must contain at least 2 characters.
  • Each character of a base must be unique.
  • A base should not contain + or - characters.

I just try to know how it is possible to detect the base because when the base is a string and start like this “choumi” how am i to suppose to detect the base ?

HI @Seiya-Araz_seiya380 and welcome in the community.

Do you have some code to show? In general I’d go for regular expressions but it depends on your needs

I’ve just got this to start

package piscine

func PrintNbrBase(nbr int, base string) {

}

and i know that my program gonna return this with these example:

package main

import (
	"github.com/01-edu/z01"

	"piscine"
)

func main() {
	piscine.PrintNbrBase(125, "0123456789")
	z01.PrintRune('\n')
	piscine.PrintNbrBase(-125, "01")
	z01.PrintRune('\n')
	piscine.PrintNbrBase(125, "0123456789ABCDEF")
	z01.PrintRune('\n')
	piscine.PrintNbrBase(-125, "choumi")
	z01.PrintRune('\n')
	piscine.PrintNbrBase(125, "aa")
	z01.PrintRune('\n')
}

And its output :

$ go run .
125
-1111101
7D
-uoi
NV
$

I assume from your other examples that for the “choumi” case you mean to print the number as base 6 with digits represented by the characters c=0, h=1, o=2, u=3, m=4, and i=5. The validation is simple string operations. Converting to the base involves division and modulo operations by the base, You’ll index your string with the result of modulo. HTH

the problem is this is an example, sometimes i’ve got the base “hello” or the last i’ve got is “qwerty” and i don’t know how many base my exercise got. So, I really need help

hi, sorry but it’s really hard to understand what you need.

Do you have example data ?

According what i understand:

  1. Take the length of base parameter. This is the base number to encode nbr.
  2. Apply division until cocient is less that base Number and take mod of that operation. The mod are the indexes of the character in the base string.

Here’s some code…

func validateBase(base string) bool {
	//    A base must contain at least 2 characters.
	if len(base) < 2 {
		return false
	}

	// Each character of a base must be unique.
	m := make(map[rune]int)
	for _, ch := range base {
		// A base should not contain + or - characters.
		if ch == '+' || ch == '-' {
			return false
		}

		if _, ok := m[ch]; ok {
			m[ch] = m[ch] + 1
			if m[ch] > 1 {
				return false
			}
		} else {
			m[ch] = 1
		}
	}

	return true
}

func PrintNbrBase(nbr int, base string) string {
	str := ""
	baseNumber := len(base)
	isNegative := false

	if !validateBase(base) {
		return "NV"
	}

	if nbr < 0 {
		isNegative = true
		nbr = -1 * nbr
	}

	for {
		c, r := nbr/baseNumber, nbr%baseNumber
		str = string(base[r]) + str
		if c < baseNumber {
			str = string(base[c]) + str
			break
		}
		nbr = c
	}

	if isNegative {
		str = "-" + str
	}
	return str
}