Friday, December 04, 2015

Golang Exercise: Errors

package main

import (
 "fmt"
 "math"
)


type ErrNegativeSqrt float64

func (e ErrNegativeSqrt) Error() string {
 if e < 0 {
  return fmt.Sprintf("cannot Sqrt negative number: %v ", float64(e))
 }
 return ""
}


func Sqrt(x float64) (float64, error) {
 if x < 0 {
  return x, ErrNegativeSqrt(x)
 }
 return math.Sqrt(x), ErrNegativeSqrt(x)
}



func main() {
 fmt.Println(Sqrt(2))
 fmt.Println(Sqrt(-2))
}

Output:
1.4142135623730951 
-2 cannot Sqrt negative number: -2 

Program exited.

Reference:

http://tour.golang.org/methods/9

Thursday, December 03, 2015

Golang Exercise: Stringers

package main

import "fmt"

type IPAddr [4]byte

// TODO: Add a "String() string" method to IPAddr.
func (ip IPAddr) String() string {
 var ls_ip string
 for i, value := range ip {
  //ls_ip = ls_ip + string(i) + ":" + string(int(value)) + "."
  switch {
  case i == 0:
   ls_ip = fmt.Sprintf("%v", value)
  default:
   ls_ip = fmt.Sprintf("%v.%v", ls_ip, value)
  }
 }
 return ls_ip
}


func main() {
 addrs := map[string]IPAddr{
  "loopback":  {127, 0, 0, 1},
  "googleDNS": {8, 8, 8, 8},
 }
 for n, a := range addrs {
  fmt.Printf("%v: %v\n", n, a)
 }
}



Output:

loopback: 127.0.0.1
googleDNS: 8.8.8.8

Program exited.

Reference:

http://tour.golang.org/methods/7

Wednesday, December 02, 2015

Golang Exercise: Slices


Exercise: Slices

Implement Pic. It should return a slice of length dy, each element of which is a slice of dx 8-bit unsigned integers. When you run the program, it will display your picture, interpreting the integers as grayscale (well, bluescale) values.
The choice of image is up to you. Interesting functions include (x+y)/2, x*y, and x^y.
(You need to use a loop to allocate each []uint8 inside the [][]uint8.)
(Use uint8(intValue) to convert between types.)


.
Solution:

package main

import "golang.org/x/tour/pic"
//import "fmt"

func Pic(dx, dy int) [][]uint8 {
 var la_pic [][]uint8
 var la_x []uint8
 la_x = make([]uint8, dx)
 //la_pic = make([][]uint8, 0)
 for i := 0; i < dy; i++ {
  la_pic = append(la_pic, la_x)
  for j := 0; j < dx; j++ {
   la_pic[i][j] = uint8((i ^ j))
   //fmt.Println(i, j)
  }
 }
 return la_pic
}

func main() {
 pic.Show(Pic)
}


Reference:

http://tour.golang.org/moretypes/15

Tuesday, December 01, 2015

Go语言 斐波那契数列 Exercise: Fibonacci closure

My code:

package main

import "fmt"

// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
 var ln_prev, ln_current, ln_next int
 ln_current = 1
 return func() int {
  ln_next = ln_current + ln_prev
  ln_prev = ln_current
  ln_current = ln_next
  fmt.Println("log:", ln_prev, ln_current, ln_next)
  return ln_next
 }
}

func main() {
 f := fibonacci()
 for i := 0; i < 10; i++ {
  fmt.Println(i, f())
 }
}


Output:

log: 1 1 1
0 1
log: 1 2 2
1 2
log: 2 3 3
2 3
log: 3 5 5
3 5
log: 5 8 8
4 8
log: 8 13 13
5 13
log: 13 21 21
6 21
log: 21 34 34
7 34
log: 34 55 55
8 55
log: 55 89 89
9 89

Program exited.

Option 2, Learned from Python way :

package main

import "fmt"

// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
 var la_f []int
 la_f = make([]int, 2)
 la_f = []int{0, 1}
 //la_f := []int{0, 1}
 var li_idx int
 li_idx = 1
 return func() int {
  la_f = append(la_f, la_f[li_idx]+la_f[li_idx-1])
  li_idx = li_idx + 1
  fmt.Printf("log: i %d, value %d, pre element %d \n", li_idx, la_f[li_idx], la_f[li_idx-1])
  return la_f[li_idx]
 }
}

func main() {
 f := fibonacci()
 for i := 0; i < 10; i++ {
  fmt.Printf("idx: %d, value: %d \n", i, f())
 }
}


Output:

log: i 2, value 1, pre element 1
idx: 0, value: 1
log: i 3, value 2, pre element 1
idx: 1, value: 2
log: i 4, value 3, pre element 2
idx: 2, value: 3
log: i 5, value 5, pre element 3
idx: 3, value: 5
log: i 6, value 8, pre element 5
idx: 4, value: 8
log: i 7, value 13, pre element 8
idx: 5, value: 13
log: i 8, value 21, pre element 13
idx: 6, value: 21
log: i 9, value 34, pre element 21
idx: 7, value: 34
log: i 10, value 55, pre element 34
idx: 8, value: 55
log: i 11, value 89, pre element 55
idx: 9, value: 89


Program exited.

 
Reference:
 
 
 

Monday, November 30, 2015

Oracle native out join syntax learned

One more out join syntax (+) observed.

Setup

drop table t1 purge;
drop table t2 purge;
drop table t3 purge;

create table t1 (id number);
create table t2 (id number);
create table t3 (id number);

insert into t1(id) select rownum from dual connect by level <= 3;
insert into t2(id) select rownum from dual connect by level <= 2;
insert into t3(id) select rownum from dual connect by level <= 1;
commit;


select t1.id t1_id, t2.id t2_id
from t2,t1
where t1.id = t2.id(+)
and (t2.id= 1 or t2.id is null)
--and t2.id(+) = 1
order by t1.id;


  T1_ID T2_ID
------- ----------
  1     1
  3


Here is a new syntax I learned last week.
The output is strange.

select t1.id t1_id, t2.id t2_id
from t2,t1
where t1.id = t2.id(+)
and t2.id(+) = 1

order by t1.id;

  T1_ID T2_ID
------- ----------
  1     1
  2
  3

Exercise: Maps 第一段 Go 语言测试通过了!

打算转换职业方向,做Golang程序设计师。
第一段代码测试通过了,好激动!

My code:

package main
import (
 "golang.org/x/tour/wc"
 "strings"
)

func WordCount(s string) map[string]int {
 var la_s = strings.Split(s, " ")

 //lm_s := map[string]int{}  // simpler
 var lm_s map[string]int
 lm_s = make(map[string]int)
 for _, word := range la_s {
  lm_s[word] = lm_s[word] + 1
 }
 return lm_s
 //return map[string]int{"x": 1}
}

func main() {
 wc.Test(WordCount)
}


Output:

PASS
 f("I am learning Go!") = 
  map[string]int{"I":1, "am":1, "learning":1, "Go!":1}
PASS
 f("The quick brown fox jumped over the lazy dog.") = 
  map[string]int{"over":1, "the":1, "The":1, "quick":1, "brown":1, "jumped":1, "fox":1, "lazy":1, "dog.":1}
PASS
 f("I ate a donut. Then I ate another donut.") = 
  map[string]int{"ate":2, "a":1, "donut.":2, "Then":1, "another":1, "I":2}
PASS
 f("A man a plan a canal panama.") = 
  map[string]int{"panama.":1, "A":1, "man":1, "a":2, "plan":1, "canal":1}

Program exited.
Reference:

Tuesday, November 03, 2015

2 level left outer join

Here is Oracle outer join behavior observation.
We get same results between Oracle notation (+), and ANSI join syntax (LEFT OUTER JOIN)

Oracle 12.1.0.2.0

drop table t1 purge;
drop table t2 purge;
drop table t3 purge;


create table t1 (id number);
create table t2 (id number);
create table t3 (id number);


insert into t1(id) select rownum from dual connect by level <= 3;
insert into t2(id) select rownum from dual connect by level <= 2;
insert into t3(id) select rownum from dual connect by level <= 1;
commit;


select t1.id t1_id, t2.id t2_id, t3.id t3_id
from t3,t2,t1
where t1.id = t2.id(+)
and t2.id = t3.id(+)
order by t1.id;


  T1_ID   T2_ID    T3_ID
------- ------- --------
      1       1        1
      2       2
      3


drop table t1 purge;
drop table t2 purge;
drop table t3 purge;


create table t1 (id number);
create table t2 (id number, t1_id number);
create table t3 (id number, t2_id number);


insert into t1(id) select rownum from dual connect by level <= 3;
insert into t2(id, t1_id) select rownum, rownum from dual connect by level <= 2;
insert into t3(id, t2_id) select rownum, rownum from dual connect by level <= 1;

commit;

select t1.id, t2.id t2_id, t2.t1_id, t3.id t3_id, t3.t2_id
from t3,t2,t1
where t1.id = t2.t1_id(+)
and t2.id = t3.t2_id(+)
order by t1.id;


  ID  T2_ID  T1_ID  T3_ID  T2_ID
---- ------ ------ ------ ------
   1      1      1      1      1
   2      2      2
   3


Your homework is to write ANSI join syntax (LEFT OUTER JOIN) SQL, to get same result.   ^_^