-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package day50 | ||
|
||
// Operator represents the arithmetic operations. | ||
type Operator int | ||
|
||
const ( | ||
// NON is a zero value of this constant. | ||
NON Operator = iota | ||
// ADD is addition | ||
ADD | ||
// SUB is subtraction | ||
SUB | ||
// MUL is multiplication | ||
MUL | ||
// DIV is division | ||
DIV | ||
) | ||
|
||
// ArithmeticTree represents either a node operator or a leaf value. | ||
type ArithmeticTree struct { | ||
val int | ||
op Operator | ||
left, right *ArithmeticTree | ||
} | ||
|
||
// Calculate takes an ArithmeticTree and returns its calculated value. | ||
// Runs in O(N) in that it only views the tree nodes once. | ||
func Calculate(tree *ArithmeticTree) int { | ||
if tree.op == NON { | ||
return tree.val | ||
} | ||
left := Calculate(tree.left) | ||
right := Calculate(tree.right) | ||
var result int | ||
switch tree.op { | ||
case ADD: | ||
result = left + right | ||
case SUB: | ||
result = left - right | ||
case MUL: | ||
result = left * right | ||
case DIV: | ||
result = left / right | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package day50 | ||
|
||
import "testing" | ||
|
||
var testcases = []struct { | ||
tree *ArithmeticTree | ||
expected int | ||
}{ | ||
{&ArithmeticTree{op: MUL, | ||
left: &ArithmeticTree{op: ADD, left: &ArithmeticTree{val: 3}, right: &ArithmeticTree{val: 2}}, | ||
right: &ArithmeticTree{op: ADD, left: &ArithmeticTree{val: 4}, right: &ArithmeticTree{val: 5}}}, | ||
45}, | ||
{&ArithmeticTree{op: SUB, | ||
left: &ArithmeticTree{op: DIV, left: &ArithmeticTree{val: 100}, right: &ArithmeticTree{val: 2}}, | ||
right: &ArithmeticTree{op: DIV, left: &ArithmeticTree{val: 100}, right: &ArithmeticTree{val: 5}}}, | ||
30}, | ||
} | ||
|
||
func TestCalculate(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range testcases { | ||
if result := Calculate(tc.tree); result != tc.expected { | ||
t.Errorf("Expected %d got %d", tc.expected, result) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkCalculate(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
Calculate(tc.tree) | ||
} | ||
} | ||
} |