How to get the digits of int value in Golang - go

How can we get the digits of num := 658943 in Golang? I need to print each digit value from the given number (num) as integer instead of string.
package main
import "fmt"
func main() {
var (
num = 68932
digits []int
)
// do something with num, insert the result to digits
for _, val := range digits {
fmt.Println(val)
}
}
// expected output
// 6
// 8
// 9
// 3
// 2

You can use strconv
package main
import (
"fmt"
"strconv"
)
func main() {
var (
num = 68932
digits []int
)
s := strconv.Itoa(num)
for _, n := range s {
digits = append(digits, int(n-'0'))
}
for _, val := range digits {
fmt.Println(val)
}
}
https://go.dev/play/p/AHzwHPd7GJC

Related

Generate string number for invitation code [duplicate]

This question already has answers here:
How to generate a random string of a fixed length in Go?
(18 answers)
Closed 4 years ago.
I would like to generate number for invitation code.
I get a number of digit for this and i need to generate a string number according to this digit.
Example :
For 3 i need to generate a string number beetween 111 and 999
for 4 1111 to 9999
for 2 11 to 99
etc...
What is the best way to do that ?
I have some idea like making two empty string filling them with 1 for the first one according to x, with 9 for the second one according to X.
Then converting them to int and make a random between these two number, but i don't thik it's the optimal way to do it.
package main
import (
"fmt"
"math/rand"
"strconv"
"time"
)
func randInt(min int, max int) int {
return min + rand.Intn(max-min)
}
func main() {
x := 3
first := ""
second := ""
i := 0
for i < x {
first = first + "1"
i = i + 1
}
i = 0
for i < x {
second = second + "9"
i = i + 1
}
rand.Seed(time.Now().UTC().UnixNano())
fmt.Println(first)
fmt.Println(second)
firstInt, _ := strconv.Atoi(first)
secondInt, _ := strconv.Atoi(second)
fmt.Println(randInt(firstInt, secondInt))
}
regards
You can do something like this:
package main
import (
"fmt"
"math/rand"
"time"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
var numbers = []rune("0123456789")
func GenerateNumberString(length int) string {
b := make([]rune, length)
for i := range b {
b[i] = numbers[rand.Intn(len(numbers))]
}
return string(b)
}
func main() {
fmt.Println(GenerateNumberString(2))
fmt.Println(GenerateNumberString(3))
fmt.Println(GenerateNumberString(4))
}
Try it here in the go playground.

How to adjust the size of two-dimensional array in Go?

I have written a program that reads input lines and checks if a line begins with #.
If so, the current line and the next one will be saved in two columns in the same row in a two-dimensional array:
Go Playground
package main
import (
"bufio"
"fmt"
"strings"
)
func main() {
const input = "#FooBar1\nFooBar1\n#Foobar2\nFooBar2\n#FooBar3\nFooBar3"
var multiDimArr [3][2]string
var lineCount int
scanner := bufio.NewScanner(strings.NewReader(input))
for line := 0; scanner.Scan(); line++ {
if strings.HasPrefix(scanner.Text(), "#") {
multiDimArr[line][0] = scanner.Text()
scanner.Scan()
multiDimArr[line][1] = scanner.Text()
lineCount++
}
}
for i := 0; i < lineCount; i++ {
for j := 0; j < 2; j++ {
fmt.Printf("multiDimArr[%d][%d] = %s\n", i, j, multiDimArr[i][j])
}
}
}
Output:
multiDimArr[0][0] = #FooBar1
multiDimArr[0][1] = FooBar1
multiDimArr[1][0] = #Foobar2
multiDimArr[1][1] = FooBar2
multiDimArr[2][0] = #FooBar3
multiDimArr[2][1] = FooBar3
My problem is that I need to know the number of lines in the input when building the program, so I can adjust the size of multiDimArr.
Can I do anything to make it take any number of lines?
Try this variation:
package main
import (
"bufio"
"fmt"
"strings"
)
func main() {
const input = "#FooBar1\nFooBar1\n#Foobar2\nFooBar2\n#FooBar3\nFooBar3"
var multiDimArr [][2]string
scanner := bufio.NewScanner(strings.NewReader(input))
for scanner.Scan() {
if strings.HasPrefix(scanner.Text(), "#") {
firstLine := scanner.Text()
scanner.Scan()
secondLine := scanner.Text()
multiDimArr = append(multiDimArr, [2]string{firstLine, secondLine})
}
}
for i, row := range multiDimArr {
for j, line := range row {
fmt.Printf("multiDimArr[%d][%d] = %s\n", i, j, line)
}
}
}
Use this Slice: [][2]string{} , so you don't need to count the number of lines of input:
Try this working sample code:
package main
import "bufio"
import "fmt"
import "strings"
func main() {
const input = "#FooBar1\nFooBar1\n#Foobar2\nFooBar2\n#FooBar3\nFooBar3"
multiDimArr := [][2]string{}
for i, r := 0, bufio.NewScanner(strings.NewReader(input)); r.Scan(); i++ {
line1 := r.Text()
if !strings.HasPrefix(line1, "#") || !r.Scan() {
break
}
multiDimArr = append(multiDimArr, [2]string{line1, r.Text()})
}
fmt.Println(multiDimArr)
}
output:
[[#FooBar1 FooBar1] [#Foobar2 FooBar2] [#FooBar3 FooBar3]]
Also you may use this working sample code if you need to pre scan input to count the number of lines, using multiDimArr := make([][2]string, n) makes this slice [][2]string with length = capacity = n:
package main
import "bufio"
import "fmt"
import "strings"
func main() {
const input = "#FooBar1\nFooBar1\n#Foobar2\nFooBar2\n#FooBar3\nFooBar3"
n := 0
for r := bufio.NewScanner(strings.NewReader(input)); r.Scan() && strings.HasPrefix(r.Text(), "#") && r.Scan(); n++ {
}
multiDimArr := make([][2]string, n)
for i, r := 0, bufio.NewScanner(strings.NewReader(input)); r.Scan(); i++ {
line1 := r.Text()
if !strings.HasPrefix(line1, "#") || !r.Scan() {
break
}
multiDimArr[i] = [2]string{line1, r.Text()}
}
fmt.Println(multiDimArr)
}
Output:
[[#FooBar1 FooBar1] [#Foobar2 FooBar2] [#FooBar3 FooBar3]]
Array types:
The length is part of the array's type; it must evaluate to a
non-negative constant representable by a value of type int.
So you can't use array because its length is constant, using multiDimArr := [n][2]string{} makes compile time error: non-constant array bound n:
n := preScan(input)
//multiDimArr := [n][2]string{} // error: non-constant array bound n

Calculating large exponentiation in Golang

I've been trying to calculating 2^100 in Golang. I understand the limit of numeric type and tried using math/big package. Here's what I've tried but I can't figure out why it doesn't work.
I've used computation by powers of two method to calculate the exponentiation.
package main
import (
"fmt"
"math/big"
)
func main() {
two := big.NewInt(2)
hundred := big.NewInt(50)
fmt.Printf("2 ** 100 is %d\n", ExpByPowOfTwo(two, hundred))
}
func ExpByPowOfTwo(base, power *big.Int) *big.Int {
result := big.NewInt(1)
zero := big.NewInt(0)
for power != zero {
if modBy2(power) != zero {
multiply(result, base)
}
power = divideBy2(power)
base = multiply(base, base)
}
return result
}
func modBy2(x *big.Int) *big.Int {
return big.NewInt(0).Mod(x, big.NewInt(2))
}
func divideBy2(x *big.Int) *big.Int {
return big.NewInt(0).Div(x, big.NewInt(2))
}
func multiply(x, y *big.Int) *big.Int {
return big.NewInt(0).Mul(x, y)
}
BigInt package allows you to calculate x^y in log time (for some reason it is called exp). All you need is to pass nil as a last parameter.
package main
import (
"fmt"
"math/big"
)
func main() {
fmt.Println(new(big.Int).Exp(big.NewInt(5), big.NewInt(20), nil))
}
If you are interested how to calculate it by yourself, take a look at my implementation:
func powBig(a, n int) *big.Int{
tmp := big.NewInt(int64(a))
res := big.NewInt(1)
for n > 0 {
temp := new(big.Int)
if n % 2 == 1 {
temp.Mul(res, tmp)
res = temp
}
temp = new(big.Int)
temp.Mul(tmp, tmp)
tmp = temp
n /= 2
}
return res
}
or play with it on go playground.
For example,
package main
import (
"fmt"
"math/big"
)
func main() {
z := new(big.Int).Exp(big.NewInt(2), big.NewInt(100), nil)
fmt.Println(z)
}
Output:
1267650600228229401496703205376
Since it's a power of two, you could also do a bit shift:
package main
import (
"fmt"
"math/big"
)
func main() {
z := new(big.Int).Lsh(big.NewInt(1), 100)
fmt.Println(z)
}
Output:
1267650600228229401496703205376
You are returning immediately if power % 2 == 0. Instead, you just want to get the result of base ** (power /2). Then multiply result * result, and if power is even then multiply base to that.
To compute 2^100
package main
import (
"fmt"
"math/big"
)
func main() {
n := big.NewInt(0)
fmt.Println(n.SetBit(n, 100, 1))
}
Playground
package main
import(
"fmt"
"math/big"
)
func main() {
bigx, power10 := new(big.Int), new(big.Int)
var x int64
bigx.SetInt64(x) //set x int64 to bigx
power10.Exp(big.NewInt(10), bigx, nil) //power10 *big.Int points to solution
str10 := power10.Text(10)
fmt.Printf(str10) // print out the number and check for your self
}

Function mutates byte slice argument

I have the following code where I have a slice of bytes with the alphabet, I copy this alphabet array in a new variable (cryptkey) and I use a function to shuffle it. The result is that the alphabet and the cryptkey byte slice get shuffled. How can I prevent this from happening?
package main
import (
"fmt"
"math/rand"
)
func main() {
alphabet := []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz.")
cryptkey := alphabet
fmt.Println(string(alphabet))
cryptkey = shuffle(cryptkey)
fmt.Println(string(alphabet))
}
func shuffle(b []byte) []byte {
l := len(b)
out := b
for key := range out {
dest := rand.Intn(l)
out[key], out[dest] = out[dest], out[key]
}
return out
}
Result :
ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz.
miclOfEInzJNvZe.YuVMCdTbXyqtaLwHGjUrABhog xQPWSpKRkDsF
Playground!
Make a copy. For example,
package main
import (
"fmt"
"math/rand"
)
func main() {
alphabet := []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz.")
cryptkey := alphabet
fmt.Println(string(alphabet))
cryptkey = shuffle(cryptkey)
fmt.Println(string(alphabet))
}
func shuffle(b []byte) []byte {
l := len(b)
out := append([]byte(nil), b...)
for key := range out {
dest := rand.Intn(l)
out[key], out[dest] = out[dest], out[key]
}
return out
}
Output:
ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz.
ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz.

Generate crypto Random Integer beetwen min, max values

I trying to generate a random number beetwen a min value and a max value,
but seems I'm lost with this, what is wrong?
package main
import (
"crypto/rand"
"encoding/binary"
"fmt"
)
func genRandNum(min, max int8) int {
var num int8
binary.Read(rand.Reader, binary.LittleEndian, &num)
return int(num*(max-min)+min)
}
func main() {
// trying to get a random number beetwen -10 and 10
fmt.Println(genRandNum(-10,10))
}
How about this
func main() {
fmt.Println(genRandNum(-10, 10))
}
func genRandNum(min, max int64) int64 {
// calculate the max we will be using
bg := big.NewInt(max - min)
// get big.Int between 0 and bg
// in this case 0 to 20
n, err := rand.Int(rand.Reader, bg)
if err != nil {
panic(err)
}
// add n to min to support the passed in range
return n.Int64() + min
}
Go play

Resources