開発環境
- macOS Mojave - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Go (プログラミング言語)
プログラミング言語Go (ADDISON-WESLEY PROFESSIONAL COMPUTING SERIES) (Alan A.A. Donovan(著)、Brian W. Kernighan(著)、柴田 芳樹(翻訳)、丸善出版)の第4章(コンポジット型)、4.2(スライス)、4.2.2(スライス内での技法)、練習問題4.5の解答を求めてみる。
コード
package main import "fmt" func main() { s := [][]int{ {}, {0}, {0, 0}, {0, 1}, {0, 0, 1}, {0, 1, 1}, {0, 1, 1, 2}, {0, 1, 1, 2, 2, 3}, {0, 1, 1, 2, 3, 3, 4}, {0, 1, 1, 1, 2}, {0, 0, 0, 2, 2, 2, 3}, {0, 1, 1, 1, 2, 2, 2}, {0, 1, 1, 1, 2, 2, 2, 3}, } for _, t := range s { fmt.Println(t) fmt.Println(remove(t)) fmt.Println() } } func remove(s []int) []int { n := len(s) if n <= 1 { return s } out := s[:0] for i := 1; i < n; i++ { if s[i-1] != s[i] { out = append(out, s[i-1]) } } out = append(out, s[n-1]) return out }
入出力結果(cmd(コマンドプロンプト)、Terminal)
C:\Users\...>go run sample5.go [] [] [0] [0] [0 0] [0] [0 1] [0 1] [0 0 1] [0 1] [0 1 1] [0 1] [0 1 1 2] [0 1 2] [0 1 1 2 2 3] [0 1 2 3] [0 1 1 2 3 3 4] [0 1 2 3 4] [0 1 1 1 2] [0 1 2] [0 0 0 2 2 2 3] [0 2 3] [0 1 1 1 2 2 2] [0 1 2] [0 1 1 1 2 2 2 3] [0 1 2 3] C:\Users\...>
0 コメント:
コメントを投稿