-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdice.st
142 lines (106 loc) · 2.23 KB
/
dice.st
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
Object subclass: Poly [
| degree coeffz |
Poly class [
new: cz [
|rv|
rv := super new.
rv init: (cz size) coeffz: cz.
^rv
]
mkroll: n [
|sv|
sv := Array new: n.
1 to: n do: [:i | sv at: i put: 1].
^self new: sv
]
]
init: deg coeffz: cz [
degree := deg.
coeffz := cz.
^self
]
getDegree [ ^degree ]
getCoeff: n [ ^coeffz at: n ]
show [
1 to: degree do: [:i| i display. ' ' display. (coeffz at: i) displayNl ]
]
* other [
| noob oDeg newD k |
(other isNumber)
ifTrue: [
"scalar"
noob := Array new: degree.
1 to: degree do: [:i| noob at: i put: (coeffz at: i) * other ].
^Poly new: noob.
]
ifFalse: [
"Poly"
oDeg := other getDegree.
newD := oDeg + degree.
noob := Array new: newD.
1 to: newD do: [:x| noob at: x put: 0].
1 to: degree do: [:i|
1 to: oDeg do: [:j|
k := i + j.
noob at: k
put: (noob at: k) + ((coeffz at: i) * (other getCoeff: j)).
]
].
^Poly new: noob
]
]
/ scalar [
| noob |
noob := Array new: degree.
1 to: degree do: [:i|
noob at: i
put: (self getCoeff: i) / scalar asFloat
].
^Poly new: noob
]
]
const := 0.
combos := 1.
polys := OrderedCollection new.
Smalltalk arguments do: [:arg|
(arg =~ 'd')
ifMatched: [:x|
pair := arg subStrings: $d.
num := (pair at: 1) asInteger.
die := (pair at: 2) asInteger.
combos := combos * (die raisedTo: num).
1 to: num do: [:i|
polys add: (Poly mkroll: die)
]
]
ifNotMatched: [
const := const + arg asInteger
]
].
bigpoly := 1.
polys do: [:poly|
bigpoly := poly * bigpoly.
].
bigpoly := bigpoly / combos.
floatFormatter := [:x| ((x roundTo: 0.00001) * 1e5) rounded / 1e5 ].
histogram := [:x|
n := (0.5 + (500 * x)) floor.
hg := ''.
mark := $# asString.
1 to: n do: [:i| hg := hg, mark ].
hg
].
tb := Character tab asString.
nl := Character lf asString.
tt := tb, tb.
output := nl.
1 to: (bigpoly getDegree) do: [:i|
num := const + i.
pr := (bigpoly getCoeff: i).
(pr > 0) ifTrue: [
hig := histogram value: pr.
prf := (floatFormatter value: pr) asString.
output := (output, num asString, tt, prf, tt, hig, nl).
].
].
output displayNl.