Created
April 12, 2014 04:52
-
-
Save pyk/10519339 to your computer and use it in GitHub Desktop.
find the biggest and smallest number in the given slice or array using go lang
This file contains 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 main() { | |
var n, smallest, biggest int | |
x := []int{ | |
48,96,86,68, | |
57,82,63,70, | |
37,34,83,27, | |
19,97, 9,17, | |
} | |
for _,v:=range x { | |
if v>n { | |
fmt.Println(v,">",n) | |
n = v | |
biggest = n | |
} else { | |
fmt.Println(v,"<",n) | |
} | |
} | |
fmt.Println("The biggest number is ", biggest) | |
for _,v:=range x { | |
if v>n { | |
fmt.Println(v,">",n) | |
} else { | |
fmt.Println(v,"<",n) | |
n = v | |
smallest = n | |
} | |
} | |
fmt.Println("The smallest number is ", smallest) | |
} |
This file contains 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
48 > 0 | |
96 > 48 | |
86 < 96 | |
68 < 96 | |
57 < 96 | |
82 < 96 | |
63 < 96 | |
70 < 96 | |
37 < 96 | |
34 < 96 | |
83 < 96 | |
27 < 96 | |
19 < 96 | |
97 > 96 | |
9 < 97 | |
17 < 97 | |
The biggest number is 97 | |
48 < 97 | |
96 > 48 | |
86 > 48 | |
68 > 48 | |
57 > 48 | |
82 > 48 | |
63 > 48 | |
70 > 48 | |
37 < 48 | |
34 < 37 | |
83 > 34 | |
27 < 34 | |
19 < 27 | |
97 > 19 | |
9 < 19 | |
17 > 9 | |
The smallest number is 9 |
@muslih cepet mas dari pada ruby, terus bahasanya simpel banget jadi mudah kalo langsung belajar dari source code
A solution from me
package main
import (
"fmt"
)
func main() {
x := []int{
48, 96, 86, 68,
57, 82, 63, 70,
37, 34, 83, 27,
19, 97, 9, 17,
}
slice := []bool{}
var smallest int
for i := 0; i < len(x); {
for _, value := range x {
if x[i] == value {
continue
}
switch true {
case x[i] < value:
slice = append(slice, true)
default:
continue
}
}
if len(slice) == (len(x) - 1) {
smallest = x[i]
break
}
slice = []bool{}
i++
}
fmt.Println("The smallest number is ", smallest) // output: The smallest number is 9
}
package main
import "fmt"
import "sort"
func main() {
x := []int{
48,96,86,68,
57,82,63,70,
37,34,83,27,
19,97, 9,17,
}
sort.Ints(x)
lowest := x[0]
fmt.Println(lowest)
}
package main
import (
"fmt"
)
func main() {
x := []int{
48, 96, 86, 68,
57, 82, 63, 70,
37, 34, 83, 27,
19, 97, 9, 17,
}
max := x[0]
min := x[0]
for _, v := range x {
if max < v {
max = v
}
if min > v {
min = v
}
}
fmt.Println(min, max)
}
`package main
import "fmt"
func main() {
x := []int{
48, 96, 86, 68,
57, 82, 63, 70,
37, 34, 83, 27,
19, 97, 9, 17,
}
y := x[0]
for _, v := range x {
if v < y {
y = v
}
}
fmt.Println("The smallest value is : ", y)
}`
Pass in the Slice and it will return the biggest number in the slice.
func Biggest(seq []int) int {
var cache int
for i := 0; i < len(seq)-1; i++ {
if seq[i] > seq[i+1] {
if seq[i] > cache {
cache = seq[i]
}
}
}
return cache
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
enaknya bahasa go apa bang?