Mastermind en Go

This commit is contained in:
2026-05-24 10:49:40 +02:00
parent 78efe71bc0
commit 7a37e28de1

57
src/Go/005.go Normal file
View File

@@ -0,0 +1,57 @@
package main
import "fmt"
func mastermind(lst_1 []int, lst_2 []int) (int, int) {
indexMatch := 0
valueMatch := 0
for idx, element := range lst_1 {
if lst_1[idx] == lst_2[idx] {
indexMatch++
continue
}
found := false
for _, value := range lst_2 {
if element == value {
found = true
break
}
}
if found {
valueMatch++
}
}
return indexMatch, valueMatch
}
func main() {
{
check_1 := []int{2, 6, 0, 7}
check_2 := []int{1, 4, 0, 6}
result_1, result_2 := mastermind(check_1, check_2)
fmt.Println(result_1, result_2) // (1, 1)
}
{
check_1 := []int{2, 6, 0, 7}
check_2 := []int{3, 5, 9, 1}
result_1, result_2 := mastermind(check_1, check_2)
fmt.Println(result_1, result_2) // (0, 0)
}
{
check_1 := []int{2, 6, 0, 7}
check_2 := []int{1, 6, 0, 4}
result_1, result_2 := mastermind(check_1, check_2)
fmt.Println(result_1, result_2) // (2, 0)
}
{
check_1 := []int{2, 6, 0, 7}
check_2 := []int{2, 6, 0, 7}
result_1, result_2 := mastermind(check_1, check_2)
fmt.Println(result_1, result_2) // (4, 0)
}
}