Created
May 28, 2021 18:40
-
-
Save solebox/4fdc76334fa5b73c73753f69aa42d6ec to your computer and use it in GitHub Desktop.
solving simple riddles to play with go lang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func split(sum int) (x, y int) { | |
x = sum * 4 / 9 | |
y = sum - x | |
return | |
} | |
func find_pair(sum int, nums []int) (x, y int){ | |
dict := make(map[int]int) | |
for index, num := range nums { | |
if diff_index, found := dict[num]; found { | |
x = diff_index | |
y = index | |
return | |
} else { | |
dict[sum-num] = index | |
} | |
} | |
return | |
} | |
func sum_array(nums []int) (total int){ | |
total = 0 | |
for _, num := range nums { | |
total += num | |
} | |
return | |
} | |
func main() { | |
array1 := []int{1,2,1, 4} | |
fmt.Println(find_pair(6, array1)) | |
fmt.Println(sum_array(array1)) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment