summaryrefslogtreecommitdiff
path: root/learn_13/src
diff options
context:
space:
mode:
authorBrad Clawsie <brad@b7j0c.org>2026-11-16 15:25:23 -0600
committerBrad Clawsie <brad@b7j0c.org>2026-11-16 15:25:23 -0600
commit8458a3cf35ca3b8ae2dd88d744767213c17dac41 (patch)
treeff058f53a8c9ea4b89d3cd2558a919fdafd2f813 /learn_13/src
parent7f10bfcb774d4f86fc8c36092889a0951255ddf4 (diff)
feat: learn_13
commit ee8ff838f1b5b798e51c14bcced40f4f8df23e1e Author: Brad Clawsie <brad@b7j0c.org> Date: Mon Nov 16 15:25:12 2026 -0600 feat: learn_13
Diffstat (limited to 'learn_13/src')
-rw-r--r--learn_13/src/main.rs136
1 files changed, 136 insertions, 0 deletions
diff --git a/learn_13/src/main.rs b/learn_13/src/main.rs
new file mode 100644
index 0000000..635aec5
--- /dev/null
+++ b/learn_13/src/main.rs
@@ -0,0 +1,136 @@
+#[derive(Debug, PartialEq)]
+enum Expr {
+ Number(i64),
+ Add(Box<Expr>, Box<Expr>),
+ Multiply(Box<Expr>, Box<Expr>),
+ Negate(Box<Expr>),
+}
+
+impl Expr {
+ fn evaluate(&self) -> i64 {
+ match self {
+ Expr::Number(i) => *i,
+ Expr::Add(lhs, rhs) => {
+ return lhs.evaluate() + rhs.evaluate();
+ },
+ Expr::Multiply(lhs, rhs) => {
+ return lhs.evaluate() * rhs.evaluate();
+ },
+ Expr::Negate(i) => {
+ return -1 as i64 * i.evaluate();
+ },
+ }
+ }
+
+ fn depth(&self) -> usize {
+ match self {
+ Expr::Number(_) => 1,
+ Expr::Add(lhs, rhs) | Expr::Multiply(lhs, rhs)=> {
+ return 1 + std::cmp::max(lhs.depth(), rhs.depth());
+ },
+ Expr::Negate(i) => {
+ return 1 + i.depth();
+ },
+ }
+ }
+
+ fn node_count(&self) -> usize {
+ match self {
+ Expr::Number(_) => 1,
+ Expr::Add(lhs, rhs) | Expr::Multiply(lhs, rhs)=> {
+ return 1 + lhs.node_count() + rhs.node_count();
+ },
+ Expr::Negate(i) => {
+ return 1 + i.depth();
+ },
+ }
+ }
+}
+
+fn main() {
+ ()
+}
+
+#[cfg(test)]
+mod tests {
+ use crate::*;
+
+ fn test_evaluate() {
+ assert_eq!(Expr::Number(1).evaluate(), 1);
+ assert_eq!(Expr::Add(
+ Box::new(Expr::Number(1)),
+ Box::new(Expr::Number(2))
+ ).evaluate(),
+ 3);
+ assert_eq!(Expr::Multiply(
+ Box::new(Expr::Number(1)),
+ Box::new(Expr::Number(2))).evaluate(),
+ 2);
+ assert_eq!(Expr::Negate(
+ Box::new(Expr::Number(1))).evaluate(),
+ -1);
+ let expr = Expr::Multiply(
+ Box::new(Expr::Add(
+ Box::new(Expr::Number(2)),
+ Box::new(Expr::Number(3)),
+ )),
+ Box::new(Expr::Negate(
+ Box::new(Expr::Number(4)),
+ )),
+ );
+ assert_eq!(expr.evaluate(), -20);
+ assert_eq!(expr.evaluate(), -20);
+ }
+
+ fn test_depth() {
+ assert_eq!(Expr::Number(1).depth(), 1);
+ assert_eq!(Expr::Add(
+ Box::new(Expr::Number(1)),
+ Box::new(Expr::Number(2))
+ ).depth(),
+ 2);
+ assert_eq!(Expr::Multiply(
+ Box::new(Expr::Number(1)),
+ Box::new(Expr::Number(2))).depth(),
+ 2);
+ assert_eq!(Expr::Negate(
+ Box::new(Expr::Number(1))).depth(),
+ 2);
+ let expr = Expr::Multiply(
+ Box::new(Expr::Add(
+ Box::new(Expr::Number(2)),
+ Box::new(Expr::Number(3)),
+ )),
+ Box::new(Expr::Negate(
+ Box::new(Expr::Number(4)),
+ )),
+ );
+ assert_eq!(expr.depth(), 3);
+ }
+
+ fn test_node_count() {
+ assert_eq!(Expr::Number(1).node_count(), 1);
+ assert_eq!(Expr::Add(
+ Box::new(Expr::Number(1)),
+ Box::new(Expr::Number(2))
+ ).node_count(),
+ 3);
+ assert_eq!(Expr::Multiply(
+ Box::new(Expr::Number(1)),
+ Box::new(Expr::Number(2))).node_count(),
+ 3);
+ assert_eq!(Expr::Negate(
+ Box::new(Expr::Number(1))).depth(),
+ 2);
+ let expr = Expr::Multiply(
+ Box::new(Expr::Add(
+ Box::new(Expr::Number(2)),
+ Box::new(Expr::Number(3)),
+ )),
+ Box::new(Expr::Negate(
+ Box::new(Expr::Number(4)),
+ )),
+ );
+ assert_eq!(expr.node_count(), 6);
+ }
+}