-
Notifications
You must be signed in to change notification settings - Fork 0
/
points_calculator.go
82 lines (65 loc) · 1.54 KB
/
points_calculator.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
package main
import (
"fmt"
"math"
"strconv"
"strings"
"time"
"unicode"
)
func CalculatePoints(receipt *receipt) int {
points := 0
points += processRetailerName(receipt.Retailer)
points += processTotalAmount(receipt.Total)
points += processItems(receipt.Items)
points += processPurchaseDateTime(receipt.PurchaseDate, receipt.PurchaseTime)
return points
}
func processRetailerName(name string) int {
retailer_points := 0
for _, char := range name {
if unicode.IsDigit(char) || unicode.IsLetter(char) {
retailer_points += 1
}
}
return retailer_points
}
func processTotalAmount(total string) int {
total_pts := 0
convertedTotal, err := strconv.ParseFloat(total, 64)
if err != nil {
}
if math.Mod(convertedTotal, 1) == 0 {
total_pts += 50
}
if math.Mod(convertedTotal, 0.25) == 0 {
total_pts += 25
}
return total_pts
}
func processItems(items []item) int {
items_pts := (len(items) / 2) * 5
for _, item := range items {
trimmed_description := strings.TrimSpace(item.ShortDescription)
if len(trimmed_description)%3 == 0 {
price_value, err := strconv.ParseFloat(item.Price, 64)
if err != nil {
}
items_pts += int(math.Ceil(price_value * 0.2))
}
}
return items_pts
}
func processPurchaseDateTime(date string, t string) int {
date_pts := 0
parsedDate, _ := time.Parse("2006-01-02", date)
parsedTime, _ := time.Parse("15:04", t)
if parsedDate.Day()%2 != 0 {
date_pts += 6
}
if parsedTime.Hour() >= 14 && parsedTime.Hour() <= 16 {
fmt.Printf("%v", parsedTime.Hour())
date_pts += 10
}
return date_pts
}