summaryrefslogtreecommitdiff
path: root/learn_13/src/main.rs
blob: 635aec5f61c4cef974106d92eb4b673f5bbced6b (plain)
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
#[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);
    }
}