-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
516 lines (480 loc) · 13.6 KB
/
main.go
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"strconv"
"time"
"github.com/jung-kurt/gofpdf"
)
// Helper functions.
func randbool() bool {
c := make(chan struct{})
close(c)
select {
case <-c:
return true
case <-c:
return false
}
}
// I just grabbed this code from
// golangcookbook.com/chapters/arrays/reverse
func reverse(numbers []string) {
for i, j := 0, len(numbers)-1; i < j; i, j = i+1, j-1 {
numbers[i], numbers[j] = numbers[j], numbers[i]
}
}
func loopT(n int) []Term {
termSlice := make([]Term, 0)
for i := 0; i < n; i++ {
termSlice = append(termSlice, GenTerm(1))
}
return termSlice
}
// Term represents an individual
// term in any algebraic pattern
type Term struct {
Coefficient int
Positive bool
Variable string
Degree int
}
// GenTerm generates a completely
// random term. This is the backbone
// of the whole program.
func GenTerm(degree int) Term {
rand.Seed(time.Now().UnixNano())
cof := rand.Intn(8) + 2
deg := degree
if degree == 1 {
if randbool() == false {
if randbool() == false {
deg = 0
} else {
deg = 1
}
}
}
// if cof == 0 {
// GenTerm(deg)
// }
vars := []string{
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",
"o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
rand.Seed(153)
varn := rand.Intn(25)
randBool := randbool()
err := errors.New("nothing happened")
if !randBool {
cof, err = strconv.Atoi(fmt.Sprintf("-%d", cof))
if err != nil {
println(err)
panic(nil)
}
}
return Term{cof, randBool, vars[varn], deg}
}
func (t Term) formatTerm() string {
superscript := []string{"²", "³", "⁴", "⁵", "⁶", "⁷", "⁸", "⁹", "¹⁰"}
answer := ""
if t.Coefficient != 0 {
answer += fmt.Sprintf("%d", t.Coefficient)
}
if t.Coefficient != 1 {
if answer != fmt.Sprintf("%d", t.Coefficient) {
answer += fmt.Sprintf("%d", t.Coefficient)
}
}
if t.Degree >= 1 {
answer += t.Variable
}
if t.Degree > 1 {
answer += superscript[t.Degree]
}
if answer == "" {
t = GenTerm(t.Degree)
return t.formatTerm()
}
return answer
}
// Polynomial represents
// a random polynomial problem
type Polynomial struct {
Terms []Term
}
// NewPolynomial generates a completely
// random Polynomial
func NewPolynomial() Polynomial {
p := Polynomial{make([]Term, 0)}
rand.Seed(time.Now().UnixNano())
numTerms := rand.Intn(3) + 2
for i := 0; i < numTerms; i++ {
rand.Seed(time.Now().UnixNano())
p.Terms = append(p.Terms, GenTerm(rand.Intn(4)+1))
}
return p
}
// ToTerms converts a Polynomial to
// a slice of Terms
func (p Polynomial) ToTerms() []Term {
return p.Terms
}
// PolynomialFromTerms creates a Polynomial
// from a slice of Terms
func PolynomialFromTerms(t []Term) Polynomial {
return Polynomial{t}
}
// Format formats the polynomial
// and pulls it together
func (p Polynomial) Format() string {
polynomial := ""
for i, v := range p.Terms {
polynomial += v.formatTerm()
if i+1 < len(p.Terms) {
if p.Terms[i+1].Positive == true {
polynomial += "+"
continue
}
if p.Terms[i+1].Positive != true {
continue
}
}
}
return polynomial
}
// AlgebraExpr represents
// a completely random algebraic
// expression
type AlgebraExpr struct {
Terms []Term
operand string
// this represents the value of
// x in any given expression
VarVal int
}
// random easter egg
var egg = []int{1, 1, 2, 3, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 6, 7, 8, 9, 10}
// NewAlgebraExpr generates a new
// algebraic expression in the form
// of the exported struct AlgebraExpr.
func NewAlgebraExpr() AlgebraExpr {
rand.Seed(time.Now().UnixNano())
// this little trick ensures a nonzero
// value for the variable
variableValue := rand.Intn(9) + 1
termSlice := loopT(2)
var operand string
if randbool() == false {
operand = "-"
} else {
operand = "+"
}
return AlgebraExpr{termSlice, operand, variableValue}
}
// FormatExpr uses the structural representation
// of AlgebraExpr and formats the information to
// a mathematical format
func (alx AlgebraExpr) FormatExpr() string {
return fmt.Sprintf("%s %s %s (x = %d)", alx.Terms[0].formatTerm(),
alx.operand, alx.Terms[1].formatTerm(), alx.VarVal)
}
// LinearEquation represents a
// random linear equation to solve
type LinearEquation struct {
// The linear equation looks
// like the following:
// N\Nx o Nx\N = N o Nx\X
// N means a constant coefficent.
// Nx means a term with coefficent and variable
// X means a constant variable.
// o means either addition or subtraction.
// A regular expression to validate a Linear Equation
// [123456789]|([123456789][abcdefghijklmnop]) [-+] = [123456789] [-+] ([123456789][abcdefghijklmnop])|[abcdefghijklmnop]
Terms []Term
operand string
}
// NewLinearEquation generates a completely random
// LinearEquation
func NewLinearEquation() LinearEquation {
t := make([]Term, 0)
var operand string
if randbool() == false {
operand = "-"
} else {
operand = "+"
}
t = append(t, GenTerm(1))
t = append(t, GenTerm(1))
t = append(t, GenTerm(0))
return LinearEquation{t, operand}
}
// the unfinished solver code
// func (l LinearEquation) Solve() {
// ls := l.FormatEquation()
// // 12x - 4 = 20
// // 12x - 4 + 4 = 20 + 4
// // 12x = 20 + 4
// // identify the left and right sides of the equation
// var leftEq, rightEq string
// eqArr := strings.FieldsFunc(ls, func(c rune) bool { return c == '=' })
// leftEq = eqArr[0]
// rightEq = eqArr[1]
// // spread over the operations
// leftEqSlice := strings.Split(leftEq, " ")
// for i, v := range leftEqSlice {
// if v == "+" || v == "-" {
// numStr := leftEqSlice[i+1]
// var strToSuffix string
// if v == "+" {
// strToSuffix = "-" + numStr
// } else if v == "-" {
// strToSuffix = "+" + numStr
// }
// leftEq += strToSuffix
// rightEq += strToSuffix
// }
// }
// }
// FormatEquation formats the linear equation
func (l LinearEquation) FormatEquation() string {
return fmt.Sprintf("%s %s %s = %s", l.Terms[0].formatTerm(), l.operand, l.Terms[1].formatTerm(), l.Terms[2].formatTerm())
}
// QuadracticEquation is mainly
// a second-degree version of
// LinearEquation
type QuadracticEquation struct {
Terms []Term
operand string
}
// NewQuadracticEquation generates a completely random
// QuadracticEquation
func NewQuadracticEquation() QuadracticEquation {
t := make([]Term, 0)
var operand string
if randbool() == false {
operand = "-"
} else {
operand = "+"
}
t = append(t, GenTerm(2))
t = append(t, GenTerm(2))
t = append(t, GenTerm(0))
return QuadracticEquation{t, operand}
}
// FormatEquation formats the quadractic equation
func (q QuadracticEquation) FormatEquation() string {
return fmt.Sprintf("%s %s %s = %s", q.Terms[0].formatTerm(), q.operand, q.Terms[1].formatTerm(), q.Terms[2].formatTerm())
}
// WordProblem represents any random word problem.
// It's a unique class of problem in that it doesn't use
// the GenTerm() function at all, since algebraic terms are
// not strictly related to word problems.
// WordProblem for the time being is merely a disguised arithmetic
// word problem. I'll add more complex ones later. TODO
type WordProblem struct {
// People represents the characters used in the word problem
// The number of people is always 2 for the time being. TODO
People []person
// e.g apples, oranges
Item string
// arithmetic is a must in word problems
Operator string
}
type person struct {
name string
val int
}
// NewWordProblem makes new Word Problems
func NewWordProblem() WordProblem {
possibleNames := []string{"Johnny", "Rohit", "Maple", "George", "Ankit", "Eugene", "Vikram"}
// Remember, we have 2 people with 2 corresponding values
// these are the names
rand.Seed(time.Now().UnixNano())
name1 := possibleNames[rand.Intn(len(possibleNames))]
rand.Seed(time.Now().UnixNano())
name2 := possibleNames[rand.Intn(len(possibleNames))]
// these are the values
// e.g the number of apples Johnny has
rand.Seed(time.Now().Unix())
value1 := rand.Intn(9) + 1
// e.g Vikram gives 5 apples
rand.Seed(time.Now().Unix())
value2 := rand.Intn(value1-1) + 1
// this is the arithmetic that unites them both
op := ""
if randbool() {
op = "+"
} else {
op = "-"
}
// item of random choice
rand.Seed(time.Now().Unix())
randItems := []string{"apples", "oranges", "mangoes", "walnuts", "peanuts"}
// put it all together
return WordProblem{[]person{person{name1, value1}, person{name2, value2}}, randItems[rand.Intn(len(randItems))], op}
}
// FormatProblem is the actual function that converts
// numbers into an actual word problem
// e.g (Johnny = 4) - (Rohit = 2) ->
// "Johnny has 4 apples, Rohit took 2 apples. How many does Johnny have?"
func (w WordProblem) FormatProblem() string {
// this is the return value
if w.Operator == "+" {
return fmt.Sprintf("%s has %d %s. %s gave %s %d %s. How many %s does %s now have?",
w.People[0].name, w.People[0].val, w.Item, w.People[1].name, w.People[0].name, w.People[1].val, w.Item, w.Item, w.People[0].name)
}
return fmt.Sprintf("%s has %d %s. %s took %d %s. How many %s does %s have?",
w.People[0].name, w.People[0].val, w.Item, w.People[1].name, w.People[1].val, w.Item, w.Item, w.People[0].name)
}
// Question represents client request for problems.
type Question struct {
// Grade int `json:"grade"`
// Syllabus string `json:"syllabus"`
// Mode string `json:"mode"`
Pattern string `json:"pattern"`
Amount int `json:"amount"`
}
// RequestReply represents a server reply made
// to a client request
type RequestReply struct {
Request Question `json:"request"`
Reply []string `json:"reply"`
}
// Reply creates problems according to question criteria
// and returns it.
func (q Question) Reply() []string {
if q.Pattern == "polynomial" {
rp := []string{}
for i := 0; i < q.Amount; i++ {
rp = append(rp, NewPolynomial().Format())
}
return rp
}
if q.Pattern == "alx" {
rp := []string{}
for i := 0; i < q.Amount; i++ {
rp = append(rp, NewAlgebraExpr().FormatExpr())
}
return rp
}
if q.Pattern == "lineq" {
rp := []string{}
for i := 0; i < q.Amount; i++ {
rp = append(rp, NewLinearEquation().FormatEquation())
}
return rp
}
if q.Pattern == "quadr" {
rp := []string{}
for i := 0; i < q.Amount; i++ {
rp = append(rp, NewQuadracticEquation().FormatEquation())
}
return rp
}
if q.Pattern == "wrdp" {
rp := []string{}
for i := 0; i < q.Amount; i++ {
rp = append(rp, NewWordProblem().FormatProblem())
}
return rp
}
return nil
}
func main() {
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("frontend"))))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, "{\"msg\": \"No such URL\", \"code\": 404}")
return
}
w.Header().Set("Content-Type", "text/html")
b, err := ioutil.ReadFile("frontend/index.html")
if err != nil {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, "{\"msg\": \"Error while trying to serve index: %v\", \"code\": 500}", err)
return
}
w.Write(b)
})
http.HandleFunc("/api/", func(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
// grade, err := strconv.Atoi(q.Get("grade"))
// if err != nil {
// fmt.Fprintf(w, "Error while processing query (the grade part): %v", err)
// return
// }
// syllabus := q.Get("syllabus")
// mode := q.Get("mode")
pattern := q.Get("pattern")
amount, err := strconv.Atoi(q.Get("amount"))
if err != nil {
fmt.Fprintf(w, "{\"msg\": \"Error while processing query (the amount part) because: %v\", \"code\": 500}", err)
return
}
// turn the request into something that can be processed
// lq := Question{grade, syllabus, mode, pattern, amount}
lq := Question{pattern, amount}
if lq.Reply() == nil {
fmt.Fprintf(w, "{\"msg\": \"URL not supported\", \"code\": 501}")
return
}
rq := &RequestReply{lq, lq.Reply()}
b, err := json.MarshalIndent(rq, "", " ")
if err != nil {
fmt.Fprintf(w, "{\"msg\": \"Can't marshal the answer because: %v\", \"code\": 500}", err)
}
w.Header().Set("Content-Type", "application/json")
w.Write(b)
})
http.HandleFunc("/api/pdf", func(w http.ResponseWriter, r *http.Request) {
pdf := gofpdf.New("P", "mm", "A4", "")
pdf.AddPage()
pdf.SetFont("Times", "", 16)
pdf.CellFormat(190, 20, "Solve the expressions using the values given.", "", 1, "TC", false, 0, "")
dataSlice := make([]AlgebraExpr, 0)
data := make([]string, 0)
for i := 0; i < 28; i++ {
dataSlice = append(dataSlice, NewAlgebraExpr())
}
for i, v := range dataSlice {
data = append(data, fmt.Sprintf("%d. %s", i+1, v.FormatExpr()))
}
reverse(data)
pdf.SetFont("Times", "", 12)
// WARNING UNREADABLE CODE AHEAD!
// READ AT YOUR OWN RISK!
for i := 1; i < 4; i++ {
for j := 1; j < 4; j++ {
var x string
x, data = data[len(data)-1], data[:len(data)-1]
pdf.CellFormat(50, 0, x, "", 0, "LT", false, 0, "")
}
var x string
x, data = data[len(data)-1], data[:len(data)-1]
pdf.CellFormat(50, 35, x, "", 1, "LT", false, 0, "")
for k := 1; k < 4; k++ {
x, data = data[len(data)-1], data[:len(data)-1]
pdf.CellFormat(50, 35, x, "", 0, "LT", false, 0, "")
}
x, data = data[len(data)-1], data[:len(data)-1]
pdf.CellFormat(50, 35, x, "", 1, "LT", false, 0, "")
}
var x string
for j := 1; j < 5; j++ {
x, data = data[len(data)-1], data[:len(data)-1]
pdf.CellFormat(50, 0, x, "", 0, "LT", false, 0, "")
}
err := pdf.Output(w)
if err != nil {
fmt.Fprintf(w, "{\"msg\":\"Can't give pdf output because: %v\", \"code\": 500}", err)
return
}
})
fmt.Println("Starting API server.")
http.ListenAndServe(":8080", nil)
}