Skip to content

Instantly share code, notes, and snippets.

View c-yan's full-sized avatar

c-yan c-yan

View GitHub Profile
@c-yan
c-yan / exercise-slices.go
Created January 18, 2018 22:07
[A Tour of Go] Exercise: Slices
package main
import "golang.org/x/tour/pic"
func Pic(dx, dy int) [][]uint8 {
result := make([][]uint8, dy)
for i := range result {
result[i] = make([]uint8, dx)
for j := range result[i] {
result[i][j] = uint8(i)
@c-yan
c-yan / exercise-loops-and-functions.go
Created January 18, 2018 22:09
[A Tour of Go] Exercise: Loops and Functions
package main
import (
"fmt"
)
func Sqrt(x float64) float64 {
z := 1.0
for i := 0; i < 3; i++ {
z = z - (z*z-x)/(2*z)
@c-yan
c-yan / exercise-web-crawler.go
Last active January 19, 2018 06:54
[A Tour of Go] Exercise: Web Crawler
package main
import (
"fmt"
"sync"
)
type Fetcher interface {
// Fetch returns the body of URL and
// a slice of URLs found on that page.
@c-yan
c-yan / reverse.go
Last active February 6, 2018 01:22
00. 文字列の逆順 文字列"stressed"の文字を逆に(末尾から先頭に向かって)並べた文字列を得よ.
package main
import (
"fmt"
"strings"
)
func reverse(s string) string {
t := []byte(s)
l := len(t)
package main
import (
"fmt"
"sort"
"strings"
)
func reverse(s string) string {
t := sort.StringSlice(strings.Split(s, ""))
@c-yan
c-yan / wordlen-count.go
Created February 5, 2018 11:54
03. 円周率 "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."という文を単語に分解し,各単語の(アルファベットの)文字数を先頭から出現順に並べたリストを作成せよ.
package main
import (
"fmt"
"strings"
"unicode"
)
func countLetters(s string) (result int) {
for _, e := range s {
@c-yan
c-yan / taxi.go
Created February 5, 2018 14:18
01. 「パタトクカシーー」 「パタトクカシーー」という文字列の1,3,5,7文字目を取り出して連結した文字列を得よ.
package main
import (
"fmt"
)
func main() {
s := "パタトクカシーー"
t := []rune(s)
result := ""
@c-yan
c-yan / patatoxkaxi.go
Created February 5, 2018 14:31
02. 「パトカー」+「タクシー」=「パタトクカシーー」 「パトカー」+「タクシー」の文字を先頭から交互に連結して文字列「パタトクカシーー」を得よ.
package main
import (
"fmt"
)
func main() {
s1 := "パトカー"
s2 := "タクシー"
t1 := []rune(s1)
@c-yan
c-yan / element-symbol.go
Created February 5, 2018 14:51
04. 元素記号 "Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can."という文を単語に分解し,1, 5, 6, 7, 8, 9, 15, 16, 19番目の単語は先頭の1文字,それ以外の単語は先頭に2文字を取り出し,取り出した文字列から単語の位置(先頭から何番目の単語か)への連想配列(辞書型もしくはマップ型)を作成せよ.
package main
import (
"fmt"
"strings"
)
func main() {
s := "Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can."
firstOnly := make(map[int]struct{})
@c-yan
c-yan / memoized-factorial.go
Created February 14, 2018 14:14
memoize in go
package main
import (
"fmt"
"github.com/BenLubar/memoize"
)
func main() {
var factorial func(n int) int
factorial = func(n int) int {