How to remove repeated element in a slice? - go

I have made a code to generate random numbers and delete the repeated ones like below:
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
list := [7]int{}
for i := 0; i < 7; i++ {
here:
rand.Seed(time.Now().UnixNano())
s := rand.Intn(16)
fmt.Println(s)
if s != list[0] && s != list[1] && s != list[2] && s != list[3] && s != list[4] && s != list[5] && s != list[6] {
list[i] = s
} else {
goto here
}
}
fmt.Println("list:", list)
}
I noticed that there were a lot repeated code like:
s!=list[0]&&list[1]
But when I write it to:
s!=list[0:6]
It is wrong, how can I do this properly?

Store it in map.
like that
rndmap := make(map[int]bool)
for len(rndmap) < YOUR_LEN {
rndmap[rand.Intn(YOUR_MAX_RAND)] = true
}
Result map will never store repeated indexes.
You can convert it into slice like this
rndslice := make([]int,0)
for i, _ := range rndmap {
rndslice = append(rndslice, i)
}

You can use the standard library to generate the random elements without any repetition in the first place.
package main
import (
"fmt"
"math/rand"
)
func main() {
fmt.Println(rand.Perm(16)[:7])
}

If you want a way to check a slice of ints for a value, try this function (play.golang.org):
func InSlice (arr []int, val int) (bool){
for _, v := range(arr) {
if v == val { return true; }
}
return false;
}
You can use this like below, but you won't be able to run it succesfully on play.golang.org because play.golang.org has a deterministic response to math/rand (In my case, it's 0), which will keep it from giving more than one answer, forcing this code into an infinite loop.
func main() {
list := [7]int{}
for i := 0; i < 7; i++ {
here:
rand.Seed(time.Now().UnixNano())
s := rand.Intn(16)
fmt.Println(s)
if !InSlice(list[:], s) {
list[i] = s
} else {
goto here
}
}

The following program will pick the array passed in the function findDuplicates() and returns repeated / duplicate values in another array as output. Also if there are no duplicates the function will return -1.
package main
import "fmt"
func findDuplicates(arr []int) []int {
foundMap := make(map[int]bool, 0)
respArray := []int{}
for i := 0; i < len(arr); i++ {
if foundMap[arr[i]] == true {
respArray = append(respArray, arr[i])
} else {
foundMap[arr[i]] = true
}
}
if len(respArray) == 0 {
respArray = append(respArray, -1)
}
return respArray
}
func main() {
fmt.Println(findDuplicates([]int{19, 22, 22, 100, 1, 1, 1, 22, 88}))
}
// Output [22 1 1 22]

Related

Golang code to check if first word can be formed from second word

I tried below golang code to check if first string can be formed from second string. Is there any improvement that can be done on this code?
package main
import (
"fmt"
"strings"
)
func main() {
words := []string{"hello", "ellhoo"}
result := "NO"
s := words[0]
for i := 0; i < len(words[0]); i++ {
if strings.Contains(words[1], string(s[i])) == false {
result = "NO"
break
} else {
result = "YES"
words[1] = strings.Replace(words[1],string(s[i]),"",1)
}
}
fmt.Println(result)
}
Record the count of each rune in the source string in a map. For each rune in the target string, fail if count in map is zero. Decrement count.
Here's the code:
// canmake reports whether t can constructed from the runes in s.
func canmake(t, s string) bool {
m := map[rune]int{}
for _, r := range s {
m[r]++
}
for _, r := range t {
if m[r] == 0 {
return false
}
m[r]--
}
return true
}
Here's an example showing how to use it:
func main() {
fmt.Println(canmake("hello", "ellhoo"))
fmt.Println(canmake("hello", "elhoo")) // insufficent number of l
fmt.Println(canmake("hello", "elloo")) // mising h
}

implementing bubble sort with go

Please help me implement bubble sort.It works fine if I call it with a hardcoded slice from the main() but if I call it with dynamic input from Scan it breaks
here is my code so far:
package main
import "fmt"
func main() {
fmt.Println("Enter a maximum of 10 numbers: ")
var inputs int
fmt.Scanln(&inputs)
inputSlice := make([]int, inputs)
BubbleSort(inputSlice)
fmt.Println(inputSlice)
}
func BubbleSort(input []int) {
for i := 0; i < len(input)-1; i++ {
for j := 0; j < len(input)-i-1; j++ {
Swap(input, j)
}
}
}
func Swap(input []int, j int) {
if input[j] > input[j+1] {
input[j], input[j+1] = input[j+1], input[j]
}
}
terminal:
coder:~/project$ go run bubblesort.go
Enter a maximum of 10 numbers:
12 24 54 65 11
coder:~/project$ 4 54 65 11
bash: 4: command not found
Do a little debugging by adding print lines in between your codes and see what's actually happening, you were just reading input the wrong way from command line
After Taking Reference from this link as posted above in comments by Steffen Ullrich
View In Go Playground
package main
import "fmt"
func main() {
fmt.Println(`Enter the number of integers`)
var n int
if m, err := Scan(&n); m != 1 {
panic(err)
}
fmt.Println(`Enter the integers`)
inputSlice := make([]int, n)
ReadN(inputSlice, 0, n)
//Your Input Printing Out
fmt.Println(inputSlice)
//Calling Function
BubbleSort(inputSlice)
//Output
fmt.Println(inputSlice)
}
func BubbleSort(input []int) {
for i := 0; i < len(input)-1; i++ {
for j := 0; j < len(input)-i-1; j++ {
Swap(input, j)
}
}
}
func Swap(input []int, j int) {
if input[j] > input[j+1] {
input[j], input[j+1] = input[j+1], input[j]
}
}
//Additional Functions
func ReadN(all []int, i, n int) {
if n == 0 {
return
}
if m, err := Scan(&all[i]); m != 1 {
panic(err)
}
ReadN(all, i+1, n-1)
}
func Scan(a *int) (int, error) {
return fmt.Scan(a)
}

Can I use Go slice unpacking to streamline this permutation function?

I have written the following Go function that produces all permutations of a boolean list of any size. Playground here.
package main
import (
"fmt"
)
func permutations(m int) [][]bool {
if m == 0 {
panic("CRASH")
}
if m == 1 {
return [][]bool{{true}, {false}}
}
retVal := [][]bool{}
for _, x := range permutations(m - 1) {
slice1 := []bool{true}
slice2 := []bool{false}
for _, y := range x {
slice1 = append(slice1, y)
slice2 = append(slice2, y)
}
retVal = append(retVal, slice1)
retVal = append(retVal, slice2)
}
return retVal
}
func main() {
fmt.Println("Hello, playground")
m := permutations(3)
fmt.Println("m = ", m)
}
Can I streamline this function by using slice unpacking? If so, how?

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

Iterate over two strings at the same time

I am just wondering if there is any beautiful way to iterate over two strings at the same time:
var ascii_runes = []rune(string_1)
var shifted_runes = []rune(string_2)
for i := 0; i < len(string_1); i++ {
fmt.Println(string(ascii_runes[i]) + string(shifted_runes[i]))
}
Not sure IIUC, but for example:
package main
import (
"fmt"
)
var (
ascii = []rune("string1")
shifted = []rune("STRING!")
)
func main() {
for i, v := range ascii {
fmt.Printf("%c%c\n", v, shifted[i])
}
}
Also here: http://play.golang.org/p/2ruvLFg_qe
Output:
sS
tT
rR
iI
nN
gG
1!
For example,
package main
import "fmt"
func main() {
var runes_1, runes_2 = []rune("string_1"), []rune("string_2")
for i := 0; i < len(runes_1) && i < len(runes_2); i++ {
fmt.Println(string(runes_1[i]) + string(runes_2[i]))
}
}
Output:
ss
tt
rr
ii
nn
gg
__
12
Not particularly beautiful, but an efficient way is to use strings.NewReader and its ReadRune method.
func Less(s1, s2 string) bool {
rdr1 := strings.NewReader(s1)
rdr2 := strings.NewReader(s2)
for {
rune1, _, err1 := rdr1.ReadRune()
rune2, _, err2 := rdr2.ReadRune()
if err2 == io.EOF { return false }
if err1 == io.EOF { return true }
if rune1 != rune2 { return rune1 < rune2 }
}
}

Resources