Skip to content

Instantly share code, notes, and snippets.

@iambowen
Last active December 21, 2015 22:38
Show Gist options
  • Save iambowen/6376234 to your computer and use it in GitHub Desktop.
Save iambowen/6376234 to your computer and use it in GitHub Desktop.
go implementation
package main
import "fmt"

func main() {
  a := [...]int {1, 2, 3, 4, 5, 6, 7 ,8, 9, 10}
    
    for i :=range a {
        if a[i] % 2 == 0 {
            fmt.Println(a[i] / 2)
        }
    }
 }
@zhywang
Copy link

zhywang commented Aug 29, 2013

package main

import "fmt"

func main() {
    for i := 0; i < 10; i++ {
        if i%2 == 0 {
            fmt.Printf("%d\n", i/2);
        }
    }
}

@johnelf
Copy link

johnelf commented Aug 29, 2013

package main

import "fmt"

func divide2(x int) int {
  return x / 2 
}

func even(x int) int {
  return (x % 2)
}

func main() {
  fmt.Println("Now to resolve issue.............")

  for i := 1; i <= 10; i++ {
    if even(i) == 0 {
      fmt.Println(divide2(i))
    }
  }
}

@XuefengWu
Copy link

package main

import "fmt"

func filter(nums []int) []int {

    result := make([]int,5)
    i := 0  
    for _, v := range nums {
        if v % 2 == 0 {
            result[i] = v
            i = i + 1   
        }        
    }
    return result
}

func divide(nums []int) []int {
    result := make([]int,5)

    for i,v := range nums { 
        result[i] = v / 2
    }
    return result
}

func main() {
    nums := make([]int, 10)
    for i := range nums {
        nums[i] = i+1
    }

    filtered := make([]int,5)
    for i, value := range filter(nums){
        fmt.Printf("%d\n", value)
        filtered[i] = value
    }

    fmt.Printf("\n")

    for _, value := range divide(filtered) {
        fmt.Printf("%d\n", value)
    }
}

@nihaokid
Copy link

package main
import "fmt"
func main(){    
    pow := make([]int, 10)
    for i:=range pow {
        pow[i] = i+1
        fmt.Printf("%d\t",pow[i])
    }
    fmt.Printf("\n")
    result := make([]int, 5)
    index := 0
    for i:=range pow {
        if (pow[i]%2 == 0){
            result[index] = pow[i]
            fmt.Printf("%d\t",result[index])
            index++
        }
    }
    fmt.Printf("\n")
    for i:=range result {
        fmt.Printf("%d\t",result[i]/2)
    }
    fmt.Printf("\n")
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment