refactor attempt 1
This commit is contained in:
parent
232a56d130
commit
fadc27b223
78
02/main.go
78
02/main.go
@ -12,6 +12,7 @@ type Report struct {
|
|||||||
levels []int
|
levels []int
|
||||||
valid bool
|
valid bool
|
||||||
direction string
|
direction string
|
||||||
|
flag bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reports stored one per line with levels in each column.
|
// Reports stored one per line with levels in each column.
|
||||||
@ -31,6 +32,7 @@ func main() {
|
|||||||
number: idx,
|
number: idx,
|
||||||
valid: true,
|
valid: true,
|
||||||
direction: "ASC",
|
direction: "ASC",
|
||||||
|
flag: false,
|
||||||
}
|
}
|
||||||
for _, field := range reportFields {
|
for _, field := range reportFields {
|
||||||
reportVal, err := strconv.Atoi(field)
|
reportVal, err := strconv.Atoi(field)
|
||||||
@ -38,8 +40,6 @@ func main() {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
report.valid = IsValidReport(&report, reportVal)
|
|
||||||
|
|
||||||
report.levels = append(report.levels, reportVal)
|
report.levels = append(report.levels, reportVal)
|
||||||
}
|
}
|
||||||
reports = append(reports, report)
|
reports = append(reports, report)
|
||||||
@ -48,7 +48,7 @@ func main() {
|
|||||||
var valid int
|
var valid int
|
||||||
for _, report := range reports {
|
for _, report := range reports {
|
||||||
fmt.Printf("%v\n", report)
|
fmt.Printf("%v\n", report)
|
||||||
if report.valid {
|
if report.IsValid() {
|
||||||
valid++
|
valid++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -56,52 +56,54 @@ func main() {
|
|||||||
fmt.Printf("Total: %v Valid %v\n", len(reports), valid)
|
fmt.Printf("Total: %v Valid %v\n", len(reports), valid)
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsValidReport(report *Report, reportVal int) bool {
|
func (report *Report) IsValid() bool {
|
||||||
if !report.valid {
|
// Determine direction
|
||||||
return false
|
|
||||||
}
|
|
||||||
valid := true
|
|
||||||
if len(report.levels) >= 1 {
|
if len(report.levels) >= 1 {
|
||||||
fmt.Printf("===reportVal===: %v\n", reportVal)
|
report.direction = DetermineDirection(report.levels[0], report.levels[1])
|
||||||
fmt.Printf("levels: %v\n", report.levels)
|
report.valid = report.ValidDirection() && report.ValidDeltas()
|
||||||
last := report.levels[len(report.levels)-1]
|
}
|
||||||
// if len(report.levels) == 1 {
|
|
||||||
// last = 0
|
|
||||||
// }
|
|
||||||
fmt.Printf("last: %v\n", last)
|
|
||||||
|
|
||||||
// Determine direction
|
// if !report.valid && !report.flag {
|
||||||
fmt.Printf("direction: %v\n", report.direction)
|
// report.flag = true
|
||||||
if len(report.levels) == 1 {
|
// return report.IsValid()
|
||||||
report.direction = DetermineDirection(report.levels[0], reportVal)
|
// }
|
||||||
|
return report.valid
|
||||||
|
}
|
||||||
|
|
||||||
|
func (report *Report) ValidDeltas() bool {
|
||||||
|
for idx, val := range report.levels {
|
||||||
|
if prev := idx - 1; prev < 0 {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
fmt.Printf("direction: %v\n", report.direction)
|
last := report.levels[idx-1]
|
||||||
|
delta := Abs(val - last)
|
||||||
|
if delta < 1 || delta > 3 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
fmt.Printf("valid: %v\n", valid)
|
func (report *Report) ValidDirection() bool {
|
||||||
|
for idx, val := range report.levels {
|
||||||
|
if prev := idx - 1; prev < 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
last := report.levels[idx-1]
|
||||||
switch report.direction {
|
switch report.direction {
|
||||||
case "ASC":
|
case "ASC":
|
||||||
if last > reportVal {
|
if last > val {
|
||||||
valid = false
|
return false
|
||||||
}
|
}
|
||||||
case "DESC":
|
case "DESC":
|
||||||
if last < reportVal {
|
if last < val {
|
||||||
valid = false
|
return false
|
||||||
}
|
}
|
||||||
|
default:
|
||||||
|
panic("aaaaaa")
|
||||||
}
|
}
|
||||||
fmt.Printf("valid: %v\n", valid)
|
|
||||||
// fmt.Printf("abs(%v-%v)=%v\n", reportVal, last, Abs(reportVal-last))
|
|
||||||
// Check if our deltas are OK
|
|
||||||
delta := Abs(reportVal - last)
|
|
||||||
|
|
||||||
fmt.Printf("delta: %v\n", delta)
|
|
||||||
// fmt.Printf("abs(%v-%v)=%v\n", reportVal, last, Abs(reportVal-last))
|
|
||||||
if delta < 1 || delta > 3 {
|
|
||||||
valid = false
|
|
||||||
}
|
|
||||||
fmt.Printf("valid: %v\n", valid)
|
|
||||||
fmt.Printf("\n\n")
|
|
||||||
}
|
}
|
||||||
return valid
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func DetermineDirection(one, two int) string {
|
func DetermineDirection(one, two int) string {
|
||||||
|
Loading…
Reference in New Issue
Block a user