python에서 if else 문을 한줄에 쓰는 법
흔히 우리가 사용하는 3항 연산자
a>b ? a:b
=> condition ?
True_Code :
False_Code
python에서 3항 연산자, 혹은 one line if-else은 아래와 같다
a if a>b else b
=> True_code if
condition else
False_code
blog of SilNex
Information of Languages OR Syntax of Languages
흔히 우리가 사용하는 3항 연산자
a>b ? a:b
=> condition ?
True_Code :
False_Code
python에서 3항 연산자, 혹은 one line if-else은 아래와 같다
a if a>b else b
=> True_code if
condition else
False_code
from datetime import date
date.days : return day (int)
from datetime import date end_date = input().split() start_date = input().split() end = date(int(end_date[2]), int(end_date[1]), int(end_date[0])) start = date(int(start_date[2]), int(start_date[1]), int(start_date[0])) resl = (end - start).days
input : 1 23 2018
1 20 2018
output : 3
package main import "fmt" func fibonacci(c, quit chan int) { var x, y int = 0, 1 for { select { case c <- x: x, y = y, x+y case <-quit: fmt.Println("quit") return } } } func main() { c := make(chan int) quit := make(chan int) go func() { for i := 0; i < 10; i++ { fmt.Println(<-c) } quit <- 0 }() fibonacci(c, quit) }
package main import ( "fmt" "time" ) func main() { tick := time.Tick(1e6) boom := time.After(5e8) for { select { case <-tick: fmt.Println("tick.") case <-boom: fmt.Println("BOOM!") return default: fmt.Println(" .") time.Sleep(5e7) } } }
> https://go-tour-kr.appspot.com/#69 참고
package main import ( "code.google.com/p/go-tour/tree" "fmt" ) // Walk walks the tree t sending all values // from the tree to the channel ch. func Walk(t *tree.Tree, ch chan int) { _walk(t, ch) close(ch) } func _walk(t *tree.Tree, ch chan int) { if t != nil { _walk(t.Left, ch) ch <- t.Value _walk(t.Right, ch) } } // Same determines whether the trees // t1 and t2 contain the same values. func Same(t1, t2 *tree.Tree) bool { ch1 := make(chan int) ch2 := make(chan int) go Walk(t1, ch1) go Walk(t2, ch2) for i := range ch1 { if i != <- ch2 { return false } } return true } func main() { //tree.New(2) ch := make(chan int) go Walk(tree.New(1), ch) for v := range ch { fmt.Print(v) } fmt.Println(Same(tree.New(1), tree.New(1))) fmt.Println(Same(tree.New(1), tree.New(2))) }
package main import ( "fmt" "time" ) func say(s string) { for i := 0; i < 5; i++ { time.Sleep(1 * time.Millisecond) fmt.Println(s) } } func main() { go say("world") say("hello") } /**** Output **** hello world world hello world hello hello world hello ****************/
go f(a, b, c)
와같이 사용되며, 새로운 고루틴을 실행한다.
채널은 채널 연산자 <-
를 이용해 값을 주고 받을 수 있는, 타입이 존재하는 파이프이다.
package main import "fmt" func sum(a []int, c chan int) { sum := 0 for _, v := range a { sum += v } c <- sum // send sum to c } func main() { a := []int{7, 2, 8, -9, 4, 0} c := make(chan int) go sum(a[:len(a)/2], c) go sum(a[len(a)/2:], c) x, y := <-c, <-c // receive from c fmt.Println(x, y, x+y) // -5 17 12 }
ch <- v
: v를 ch로 보낸다.v := <-ch
: ch로부터 값을 받아서 v로 넘긴다.ch := make(chan int)
ch := make(chan int, 100)
package main import "fmt" func main() { c := make(chan int, 2) c <- 1 c <- 2 fmt.Println(<-c) fmt.Println(<-c) }
만약 버퍼를 1을 잡게 된다면 에러가 발생하게 된다.
데이터 송신측은 더 이상 보낼 값이 없음을 알리기 위해 채널을 close할 수 있다.
수신자는 아래와 같은 수신 코드의 두번째 인자를 줌으로써 채널이 닫혓는지 테스트 할 수 있다.
v, ok := <- ch
와 같이 사용하며, 만약 채널이 닫겨있다면 ok
엔 false
가 들어가게 된다.
또한 for i := range ch
와 같은 반복문은 채널이 닫힐 때 까지 계속해서 값을 받는다.
package main import ( "fmt" ) func fibonacci(n int, c chan int) { x, y := 0, 1 for i := 0; i < n; i++ { c <- x x, y = y, x+y } close(c) } func main() { c := make(chan int, 10) go fibonacci(cap(c), c) for i := range c { fmt.Println(i) } }
package main import ( "fmt" "net/http" ) type Hello struct{} func (h Hello) ServeHTTP( w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "Hello!") } func main() { var h Hello http.ListenAndServe("localhost:4000", h) }
package http type Handler interface { ServeHTTP(w ResponseWriter, r *Request) }
아래 나오는 타입을 구현하고 그 타입의 ServeHTTP 메소드를 정의하십시오. 그 메소드를 당신의 웹 서버에서 특정 경로를 처리할 수 있도록 등록하십시오.
type String string
type Struct struct {
Greeting string
Punct string
Who string
}
예컨대, 당신은 아래와 같이 핸들러를 등록할 수 있어야 합니다.
http.Handle("/string", String("I'm a frayed knot."))
http.Handle("/struct", &Struct{"Hello", ":", "Gophers!"})
package main import ( "fmt" "net/http" ) type String string func (s String) ServeHTTP(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, s) } type Struct struct { Greeting string Punct string Who string } func (s *Struct) ServeHTTP(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, s.Greeting, s.Punct, s.Who) } func main(){ http.Handle("/string", String("I'm silnex")) http.Handle("/struct", &Struct{"Hello",":","Gopher!"}) http.ListenAndServe("localhost:4000", nil) }
package main import ( "fmt" "image" ) func main() { m := image.NewRGBA(image.Rect(0, 0, 100, 100)) fmt.Println(m.Bounds()) fmt.Println(m.At(0, 0).RGBA()) }
package image type Image interface { ColorModel() color.Model Bounds() Rectangle At(x, y int) color.Color }
package main import ( "code.google.com/p/go-tour/pic" "image" "image/color" ) type Image struct{ w, h int colorA, colorB uint8 } func (i *Image) ColorModel() color.Model{ return color.RGBAModel } func (i *Image) Bounds() image.Rectangle { return image.Rect(0,0,i.w,i.h) } func (i *Image) At(x, y int) color.Color{ return color.RGBA{uint8(x), uint8(y), i.colorB, i.colorA} } func main() { m := &Image{123,123,131,141} pic.ShowImage(m) }
package main import ( "fmt" "math" ) type Vertex struct { X, Y float64 } func (v *Vertex) Abs() float64 { return math.Sqrt(v.X*v.X + v.Y*v.Y) } func main() { v := &Vertex{3, 4} fmt.Println(v.Abs()) }
func
키워드와 메소드 이름 사이에 인자로 들어간다.func 구조체이름(혹은포인터) 메소드_리시버_이름 반환타입
이고 위 코드를 통해 예시를 들면,func (v *Vertex) Abs() float64
: 구조체 Vertex
의 Abs()
란 이름으로 호출 할 수 있다.package main import ( "fmt" "math" ) type MyFloat float64 func (f MyFloat) Abs() float64 { if f < 0 { return float64(-f) } return float64(f) } func main() { f := MyFloat(-math.Sqrt2) fmt.Println(f.Abs()) }
package main import ( "fmt" "math" ) type Vertex struct { X, Y float64 } func (v *Vertex) Scale(f float64) { v.X = v.X * f v.Y = v.Y * f } func (v *Vertex) Abs() float64 { return math.Sqrt(v.X*v.X + v.Y*v.Y) } func main() { v := &Vertex{3, 4} v.Scale(5) fmt.Println(v, v.Abs()) // &{15 20} 25 }
package main import ( "fmt" "math" ) type Abser interface { Abs() float64 } func main() { var a Abser f := MyFloat(-math.Sqrt2) v := Vertex{3, 4} a = f // a MyFloat implements Abser a = &v // a *Vertex implements Abser //a = v // a Vertex, does NOT // implement Abser fmt.Println(a.Abs()) // 5 } /****** MyFloat ******/ type MyFloat float64 func (f MyFloat) Abs() float64 { if f < 0 { return float64(-f) } return float64(f) } /****** Vertex ******/ type Vertex struct { X, Y float64 } func (v *Vertex) Abs() float64 { return math.Sqrt(v.X*v.X + v.Y*v.Y) }
package main import ( "fmt" "os" ) type Reader interface { Read(b []byte) (n int, err error) } type Writer interface { Write(b []byte) (n int, err error) } type ReadWriter interface { Reader Writer } func main() { var w ReadWriter //var w Writer // os.Stdout implements Writer w = os.Stdout fmt.Fprintf(w, "hello, writer\n") }
package main import ( "fmt" "time" ) type MyError struct { When time.Time What string } func (e *MyError) Error() string { return fmt.Sprintf("at %v, %s", e.When, e.What) } func run() error { return &MyError{ time.Now(), "it didn't work", } } func main() { if err := run(); err != nil { fmt.Println(err) // at 2009-11-10 23:00:00 +0000 UTC m=+0.000000001, it didn't work } }
type error interface {
Error() string
}
Sqrt
함수는 복소수를 지원하지 않기 때문에, 음수가 주어지면nil
이 아닌 에러 값을 반환해야 합니다.
새로운 타입을 만드십시오.
type ErrNegativeSqrt float64
and make it an error
by giving it a 그리고 아래 메소드를 구현함으로써 그 타입이 error
가 되게 하십시오.
func (e ErrNegativeSqrt) Error() string
이는 ErrNegativeSqrt(-2).Error()
가 "cannot Sqrt negative number: -2"
를 반환하는 그러한 메소드입니다.
package main import ( "fmt" "math" ) type ErrNegativeSqrt float64 func (e ErrNegativeSqrt) Error() string { if e < 0 { return fmt.Sprintf("cannot Sqrt negative number: %0.1f", e) } return fmt.Sprintf("OK") } func Sqrt(f float64) (float64, error) { if f > 0 { var a, z, c float64 = 0, 1, 0 for a = 1; a < 10; a++ { if math.Abs(z - c) > 1e-10 { c = z z = z - (z * z - f)/(2*z) } else { break } } return z, ErrNegativeSqrt(f) } else { return f, ErrNegativeSqrt(f) } } func main() { fmt.Println(Sqrt(2)) fmt.Println(Sqrt(-2)) }
package main import ( "fmt" "time" ) func main() { fmt.Println("When's Saturday?") today := time.Now().Weekday() switch time.Saturday { case today + 0: fmt.Println("Today.") case today + 1: fmt.Println("Tomorrow.") case today + 2: fmt.Println("In two days.") default: fmt.Println("Too far away.") } }
time.Saturday
가 today + 0
와 일치하면 today + 1
과 today + 2
, default
문은 실행하지 않는다.package main import ( "fmt" "time" ) func main() { t := time.Now() switch { case t.Hour() < 12: fmt.Println("Good morning!") case t.Hour() < 17: fmt.Println("Good afternoon.") default: fmt.Println("Good evening.") } }
complex64
타입과complex128
타입을 통해서 Go 언어의 복소수 지원 기능을 알아봅니다.
세제곱근을 얻기 위해서는, 뉴턴의 방법 (Newton’s method)을 적용하여 다음을 반복 수행합니다:
z = z - (z * z * z - x) / (3 * z * z)
package main import "fmt" func Cbrt(x complex128) complex128 { var z complex128 = 1 for a := 1; a < 10; a++ { z = z - (z * z * z - x) / (3 * z * z) } return z } func main() { fmt.Println(Cbrt(3)) // (1.4422495703074083+0i) }
package main import "fmt" type Vertex struct { Lat, Long float64 } var m = map[string]Vertex{ "Bell Labs": Vertex{ 40.68433, -74.39967, }, "Google": { 37.42202, -122.08408, }, } func main() { fmt.Println(t, m) // map[Bell Labs:{40.68433 -74.39967} Google:{37.42202 -122.08408}] }
"Bell Labs": {40.68433, -74.39967}
와 "Bell Labs": Vertex{40.68433, -74.39967}
는 같은 뜻이다.m[key] = data
val = m[key]
val, has_key = m[key]
>> 만약 m[key]
가 존재하지 않는다면 has_key
엔 false
가, val
엔 zero value
이 할당된다.
WordCount
함수를 구현합니다. 이 함수는s
라는 문자열 내에서 각각의 “단어”의 등장 횟수를 나타내는 맵을 반환해야 합니다.wc.Test
함수는 주어진 함수를 이용하여 테스트를 실행한 뒤에 그 성공 여부를 출력해 줍니다.
package main import ( "code.google.com/p/go-tour/wc" "strings" ) func WordCount(s string) map[string]int { m := make(map[string]int) for _, key := range strings.Fields(s) { _, ok := m[key] if ok { m[key]++ } else { m[key] = 1 } } return m } func main() { wc.Test(WordCount) }
package main import "fmt" func adder() func(int) int { sum := 0 return func(x int) int { sum += x return sum } } func main() { pos, neg := adder(), adder() for i := 0; i < 10; i++ { fmt.Println( pos(i), neg(-2*i), ) } }
fibonacci
함수를 구현합니다. 이 함수는 이어지는 피보나치 수를 반환하는 함수 (클로져)를 반환해야 합니다.
package main import "fmt" // fibonacci is a function that returns // a function that returns an int. func fibonacci() func() int { x1 := 1 x2 := 2 return func() int { x3 := x1+x2 x1, x2 = x2, x3 return x3 } } func main() { f := fibonacci() for i := 0; i < 10; i++ { fmt.Println(f()) } }