aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2015-04-07 23:34:44 +0200
committerDimitri Sokolyuk <demon@dim13.org>2015-04-07 23:34:44 +0200
commit9f7de981ae3570a5672984bff977a09073f95053 (patch)
treef17a543a458111e5d21d8cb86c8ac44c3a54ef65
parent0c7cd0d54e72fde045441991119e8d459efaafb3 (diff)
User real ordered set (linked list)
-rw-r--r--main.go148
1 files changed, 69 insertions, 79 deletions
diff --git a/main.go b/main.go
index 62ea34b..6acc013 100644
--- a/main.go
+++ b/main.go
@@ -1,91 +1,37 @@
package main
import (
+ "container/list"
"fmt"
"log"
+ "math/rand"
+ "time"
)
-type testCase struct {
- First, Second, Expect string
-}
-
var (
- alphabet = []string{
- "zero",
- "one",
- "two",
- "tree",
- "four",
- "five",
- "six",
- "seven",
- "eight",
- "nine",
- "ten",
- "eleven",
- "twelve",
- "thirteen",
- "fourteen",
- "fifteen",
- "sixteen",
- "seventeen",
- "eighteen",
- "nineteen",
- "twenty",
- }
- zero = alphabet[0]
- one = alphabet[1]
- casesAdd = []testCase{
- testCase{"two", "two", "four"},
- testCase{"two", "five", "seven"},
- testCase{"two", "zero", "two"},
- }
- casesTimes = []testCase{
- testCase{"two", "two", "four"},
- testCase{"two", "tree", "six"},
- }
- casesPot = []testCase{
- testCase{"one", "two", "one"},
- testCase{"two", "two", "four"},
- testCase{"two", "tree", "eight"},
- }
- casesSub = []testCase{
- testCase{"seven", "five", "two"},
- testCase{"two", "zero", "two"},
- testCase{"four", "two", "two"},
- }
+ alphabet *list.List
+ zero, one *list.Element
)
// next returns next char in alphabet
-func next(n string) string {
- for i, v := range alphabet {
- if i == len(alphabet)-1 {
- log.Fatal("out of range")
- }
- if v == n {
- return alphabet[i+1]
- }
+func next(n *list.Element) *list.Element {
+ if nxt := n.Next(); nxt != nil {
+ return nxt
}
- log.Fatal("not found")
- return zero
+ log.Fatal("out of range", n.Value)
+ return nil
}
// prev returns previous char in alphabet
-func prev(n string) string {
- if n == zero {
- return zero
+func prev(n *list.Element) *list.Element {
+ if prv := n.Prev(); prv != nil {
+ return prv
}
- for i, v := range alphabet {
- if v == n {
- return alphabet[i-1]
- }
- }
- log.Fatal("not found")
return zero
}
// add defines addition
-func add(m, n string) string {
+func add(m, n *list.Element) *list.Element {
if m == zero {
return n
}
@@ -93,7 +39,7 @@ func add(m, n string) string {
}
// times defines mutiplication
-func times(m, n string) string {
+func times(m, n *list.Element) *list.Element {
if m == zero {
return zero
}
@@ -101,7 +47,7 @@ func times(m, n string) string {
}
// pot defines power function
-func pot(m, n string) string {
+func pot(m, n *list.Element) *list.Element {
if n == zero {
return one
}
@@ -109,24 +55,68 @@ func pot(m, n string) string {
}
// sub defines substraction
-func sub(m, n string) string {
+func sub(m, n *list.Element) *list.Element {
if n == zero {
return m
}
return prev(sub(m, prev(n)))
}
+type function func(*list.Element, *list.Element) *list.Element
+
// test permoms tests
-func test(f func(string, string) string, op string, cases []testCase) {
- for _, c := range cases {
- r := f(c.First, c.Second)
- fmt.Println(r == c.Expect, c.First, op, c.Second, "=", r)
+func test(op string, f function) {
+ var mm, nn int
+ switch op {
+ case "+":
+ mm = rand.Intn(100)
+ nn = rand.Intn(100)
+ case "-":
+ mm = rand.Intn(100)
+ nn = rand.Intn(100)
+ if nn > mm {
+ mm, nn = nn, mm
+ }
+ case "*":
+ mm = rand.Intn(10)
+ nn = rand.Intn(10)
+ case "^":
+ mm = rand.Intn(10)
+ nn = rand.Intn(3)
+ }
+ m := scan(mm)
+ n := scan(nn)
+ r := f(m, n)
+ fmt.Println(m.Value, op, n.Value, "=", r.Value)
+}
+
+func scan(n int) *list.Element {
+ if n > alphabet.Len() {
+ log.Fatal("out of range")
+ return nil
+ }
+ e := alphabet.Front()
+ for i := 0; i <= n; i++ {
+ e = e.Next()
+ }
+ return e
+}
+
+func init() {
+ alphabet = list.New()
+ for i := 0; i <= 1000; i++ {
+ alphabet.PushBack(fmt.Sprint(i))
}
+ zero = alphabet.Front()
+ one = zero.Next()
+ rand.Seed(time.Now().Unix())
}
func main() {
- test(add, "+", casesAdd)
- test(times, "*", casesTimes)
- test(pot, "^", casesPot)
- test(sub, "-", casesSub)
+ for i := 0; i < 3; i++ {
+ test("+", add)
+ test("-", sub)
+ test("*", times)
+ test("^", pot)
+ }
}