Intersecting number wheels: Difference between revisions
Content added Content deleted
(Promote to full task status.) |
(→{{header|Go}}: Tidied up a bit.) |
||
Line 254: | Line 254: | ||
import ( |
import ( |
||
"fmt" |
"fmt" |
||
"sort" |
|||
"strconv" |
"strconv" |
||
) |
) |
||
Line 262: | Line 263: | ||
} |
} |
||
type wheelMap = map[string]wheel |
|||
fmt.Printf(" ") |
|||
func generate(wheels wheelMap, start string, maxCount int) { |
|||
count := 0 |
count := 0 |
||
w := wheels[start] |
w := wheels[start] |
||
Line 269: | Line 271: | ||
s := w.values[w.next] |
s := w.values[w.next] |
||
v, err := strconv.Atoi(s) |
v, err := strconv.Atoi(s) |
||
w.next+ |
w.next = (w.next + 1) % len(w.values) |
||
wheels[start] = w |
|||
w.next = 0 |
|||
⚫ | |||
if err == nil { |
if err == nil { |
||
fmt.Printf("%d ", v) |
fmt.Printf("%d ", v) |
||
Line 285: | Line 285: | ||
ss := s |
ss := s |
||
s = w2.values[w2.next] |
s = w2.values[w2.next] |
||
w2.next+ |
w2.next = (w2.next + 1) % len(w2.values) |
||
if w2.next == len(w2.values) { |
|||
w2.next = 0 |
|||
⚫ | |||
wheels[ss] = w2 |
wheels[ss] = w2 |
||
v, err = strconv.Atoi(s) |
v, err = strconv.Atoi(s) |
||
Line 305: | Line 302: | ||
} |
} |
||
func printWheels(wheels |
func printWheels(wheels wheelMap) { |
||
var names []string |
|||
for name := range wheels { |
|||
names = append(names, name) |
|||
} |
|||
sort.Strings(names) |
|||
fmt.Println("Intersecting Number Wheel group:") |
fmt.Println("Intersecting Number Wheel group:") |
||
for _, name := range names { |
for _, name := range names { |
||
fmt.Printf(" %s: %v\n", name, wheels[name].values) |
fmt.Printf(" %s: %v\n", name, wheels[name].values) |
||
} |
} |
||
fmt. |
fmt.Print(" Generates:\n ") |
||
} |
} |
||
⚫ | |||
wheels := make(map[string]wheel) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
wheelMaps := []wheelMap{ |
|||
{ |
|||
printWheels(wheels, "A", "B") |
|||
⚫ | |||
generate(wheels, "A", 20) |
|||
⚫ | |||
{ |
|||
⚫ | |||
⚫ | |||
delete(wheels, "B") |
|||
"B": {0, []string{"3", "4"}}, |
|||
⚫ | |||
printWheels(wheels, "A", "D") |
|||
{ |
|||
⚫ | |||
"D": {0, []string{"6", "7", "8"}}, |
|||
}, |
|||
⚫ | |||
{ |
|||
wheels["C"] = wheel{0, []string{"5", "B"}} |
|||
⚫ | |||
delete(wheels, "D") |
|||
"B": {0, []string{"3", "4"}}, |
|||
"C": {0, []string{"5", "B"}}, |
|||
}, |
|||
} |
|||
for _, wheels := range wheelMaps { |
|||
⚫ | |||
⚫ | |||
} |
|||
}</lang> |
}</lang> |
||