Permalink
Switch branches/tags
Nothing to show
Commits on Mar 29, 2015
Commits on Mar 28, 2015
  1. Go back to the recursive solution again

    carols10cents committed Mar 28, 2015
    On second thought, this feels more rusty-- fewer things are mutable.
    It's also shorter, and still works.
    
    $ cargo test
       Compiling prime_factors v0.0.1
    (file:///Users/carolnichols/Rust/prime_factors)
         Running target/prime_factors-2690254f285d4686
    
    running 10 tests
    test prime_factors_of_101 ... ok
    test prime_factors_of_48 ... ok
    test prime_factors_of_eight ... ok
    test prime_factors_of_five ... ok
    test prime_factors_of_four ... ok
    test prime_factors_of_nine ... ok
    test prime_factors_of_seven ... ok
    test prime_factors_of_six ... ok
    test prime_factors_of_three ... ok
    test prime_factors_of_two ... ok
    
    test result: ok. 10 passed; 0 failed; 0 ignored; 0 measured
    
       Doc-tests prime_factors
    
    running 0 tests
    
    test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
  2. The closest thing possible to Uncle Bob's solution in rust

    carols10cents committed Mar 28, 2015
    This is much wordier since rust doesn't allow inline declaration of for
    loop variables or for loop conditions (it will only do for in a range).
    
    I'd also argue that, while Uncle Bob's solution is only 3 lines, it's
    pretty dense and possibly too clever for its own good.
    
    That seems to be the opinions of several commenters as well:
    http://www.butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata
    
    $ cargo test
         Running target/prime_factors-2690254f285d4686
    
    running 10 tests
    test prime_factors_of_101 ... ok
    test prime_factors_of_48 ... ok
    test prime_factors_of_eight ... ok
    test prime_factors_of_five ... ok
    test prime_factors_of_four ... ok
    test prime_factors_of_nine ... ok
    test prime_factors_of_seven ... ok
    test prime_factors_of_six ... ok
    test prime_factors_of_three ... ok
    test prime_factors_of_two ... ok
    
    test result: ok. 10 passed; 0 failed; 0 ignored; 0 measured
    
       Doc-tests prime_factors
    
    running 0 tests
    
    test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
  3. Refactor inner loop into a for/in instead of a while

    carols10cents committed Mar 28, 2015
    This is slightly more succinct.
    
    $ cargo test
       Compiling prime_factors v0.0.1
    (file:///Users/carolnichols/Rust/prime_factors)
         Running target/prime_factors-2690254f285d4686
    
    running 10 tests
    test prime_factors_of_101 ... ok
    test prime_factors_of_48 ... ok
    test prime_factors_of_eight ... ok
    test prime_factors_of_five ... ok
    test prime_factors_of_four ... ok
    test prime_factors_of_nine ... ok
    test prime_factors_of_seven ... ok
    test prime_factors_of_six ... ok
    test prime_factors_of_three ... ok
    test prime_factors_of_two ... ok
    
    test result: ok. 10 passed; 0 failed; 0 ignored; 0 measured
    
       Doc-tests prime_factors
    
    running 0 tests
    
    test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
  4. Add tests for a larger non-prime and larger prime to increase coverage

    carols10cents committed Mar 28, 2015
    You might argue that this is overkill. Or you might argue that this
    isn't enough!
    
    $ cargo test
         Running target/prime_factors-2690254f285d4686
    
    running 10 tests
    test prime_factors_of_101 ... ok
    test prime_factors_of_48 ... ok
    test prime_factors_of_eight ... ok
    test prime_factors_of_five ... ok
    test prime_factors_of_four ... ok
    test prime_factors_of_nine ... ok
    test prime_factors_of_seven ... ok
    test prime_factors_of_six ... ok
    test prime_factors_of_three ... ok
    test prime_factors_of_two ... ok
    
    test result: ok. 10 passed; 0 failed; 0 ignored; 0 measured
    
       Doc-tests prime_factors
    
    running 0 tests
    
    test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
  5. Change to iterative solution to get 9 to pass

    carols10cents committed Mar 28, 2015
    $ cargo test
       Compiling prime_factors v0.0.1
    (file:///Users/carolnichols/Rust/prime_factors)
         Running target/prime_factors-2690254f285d4686
    
    running 8 tests
    test prime_factors_of_eight ... ok
    test prime_factors_of_five ... ok
    test prime_factors_of_four ... ok
    test prime_factors_of_nine ... ok
    test prime_factors_of_seven ... ok
    test prime_factors_of_six ... ok
    test prime_factors_of_three ... ok
    test prime_factors_of_two ... ok
    
    test result: ok. 8 passed; 0 failed; 0 ignored; 0 measured
    
       Doc-tests prime_factors
    
    running 0 tests
    
    test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
  6. Add a failing test for 9

    carols10cents committed Mar 28, 2015
    Now we get into the good stuff :)
    
    $ cargo test
       Compiling prime_factors v0.0.1
    (file:///Users/carolnichols/Rust/prime_factors)
         Running target/prime_factors-2690254f285d4686
    
    running 8 tests
    test prime_factors_of_eight ... ok
    test prime_factors_of_five ... ok
    test prime_factors_of_four ... ok
    test prime_factors_of_nine ... FAILED
    test prime_factors_of_seven ... ok
    test prime_factors_of_six ... ok
    test prime_factors_of_three ... ok
    test prime_factors_of_two ... ok
    
    failures:
    
    ---- prime_factors_of_nine stdout ----
    	thread 'prime_factors_of_nine' panicked at 'assertion failed:
    `(left == right) && (right == left)` (left: `[9]`, right: `[3, 3]`)',
    src/lib.rs:47
    
    failures:
        prime_factors_of_nine
    
    test result: FAILED. 7 passed; 1 failed; 0 ignored; 0 measured
    
    thread '<main>' panicked at 'Some tests failed',
    /Users/carolnichols/Rust/rust/src/libtest/lib.rs:262
  7. Add tests for 7 and 8 that pass

    carols10cents committed Mar 28, 2015
    $ cargo test
       Compiling prime_factors v0.0.1
    (file:///Users/carolnichols/Rust/prime_factors)
         Running target/prime_factors-2690254f285d4686
    
    running 7 tests
    test prime_factors_of_eight ... ok
    test prime_factors_of_five ... ok
    test prime_factors_of_four ... ok
    test prime_factors_of_seven ... ok
    test prime_factors_of_six ... ok
    test prime_factors_of_three ... ok
    test prime_factors_of_two ... ok
    
    test result: ok. 7 passed; 0 failed; 0 ignored; 0 measured
    
       Doc-tests prime_factors
    
    running 0 tests
    
    test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
  8. Add a test for 6 that passes

    carols10cents committed Mar 28, 2015
    Honestly, I thought this would fail. Shows what I know!
    
    $ cargo test
       Compiling prime_factors v0.0.1
    (file:///Users/carolnichols/Rust/prime_factors)
         Running target/prime_factors-2690254f285d4686
    
    running 5 tests
    test prime_factors_of_five ... ok
    test prime_factors_of_four ... ok
    test prime_factors_of_six ... ok
    test prime_factors_of_three ... ok
    test prime_factors_of_two ... ok
    
    test result: ok. 5 passed; 0 failed; 0 ignored; 0 measured
    
       Doc-tests prime_factors
    
    running 0 tests
    
    test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
  9. Add a test for 5 that passes

    carols10cents committed Mar 28, 2015
    I thought about skipping this because i knew it would pass, but
    sometimes you don't know :)
    
     $ cargo test
       Compiling prime_factors v0.0.1
    (file:///Users/carolnichols/Rust/prime_factors)
         Running target/prime_factors-2690254f285d4686
    
    running 4 tests
    test prime_factors_of_five ... ok
    test prime_factors_of_four ... ok
    test prime_factors_of_three ... ok
    test prime_factors_of_two ... ok
    
    test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured
    
       Doc-tests prime_factors
    
    running 0 tests
    
    test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
  10. Simplest thing to make 4 pass

    carols10cents committed Mar 28, 2015
    I'm not crazy about the recursive solutions, but it's simple and gets
    this test to pass.
    
    $ cargo test
       Compiling prime_factors v0.0.1
    (file:///Users/carolnichols/Rust/prime_factors)
         Running target/prime_factors-2690254f285d4686
    
    running 3 tests
    test prime_factors_of_four ... ok
    test prime_factors_of_three ... ok
    test prime_factors_of_two ... ok
    
    test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured
    
       Doc-tests prime_factors
    
    running 0 tests
    
    test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
  11. Add a failing test for four

    carols10cents committed Mar 28, 2015
    $ cargo test
       Compiling prime_factors v0.0.1
    (file:///Users/carolnichols/Rust/prime_factors)
         Running target/prime_factors-2690254f285d4686
    
    running 3 tests
    test prime_factors_of_four ... FAILED
    test prime_factors_of_three ... ok
    test prime_factors_of_two ... ok
    
    failures:
    
    ---- prime_factors_of_four stdout ----
    	thread 'prime_factors_of_four' panicked at 'assertion failed:
    `(left == right) && (right == left)` (left: `[4]`, right: `[2, 2]`)',
    src/lib.rs:18
    
    failures:
        prime_factors_of_four
    
    test result: FAILED. 2 passed; 1 failed; 0 ignored; 0 measured
    
    thread '<main>' panicked at 'Some tests failed',
    /Users/carolnichols/Rust/rust/src/libtest/lib.rs:262
  12. Do the simplest thing to get both tests to pass

    carols10cents committed Mar 28, 2015
    $ cargo test
       Compiling prime_factors v0.0.1
    (file:///Users/carolnichols/Rust/prime_factors)
         Running target/prime_factors-2690254f285d4686
    
    running 2 tests
    test prime_factors_of_three ... ok
    test prime_factors_of_two ... ok
    
    test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured
    
       Doc-tests prime_factors
    
    running 0 tests
    
    test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
  13. Add a failing test for three

    carols10cents committed Mar 28, 2015
    $ cargo test
       Compiling prime_factors v0.0.1
    (file:///Users/carolnichols/Rust/prime_factors)
    src/lib.rs:2:18: 2:21 warning: unused variable: `num`,
     #[warn(unused_variables)] on by default
    src/lib.rs:2 fn prime_factors(num: i64) -> Vec<i64> {
                                  ^~~
    src/lib.rs:2:18: 2:21 warning: unused variable: `num`,
     #[warn(unused_variables)] on by default
    src/lib.rs:2 fn prime_factors(num: i64) -> Vec<i64> {
                                  ^~~
         Running target/prime_factors-2690254f285d4686
    
    running 2 tests
    test prime_factors_of_three ... FAILED
    test prime_factors_of_two ... ok
    
    failures:
    
    ---- prime_factors_of_three stdout ----
    	thread 'prime_factors_of_three' panicked at 'assertion failed:
    `(left == right) && (right == left)` (left: `[2]`, right: `[3]`)',
    src/lib.rs:13
    
    failures:
        prime_factors_of_three
    
    test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured
    
    thread '<main>' panicked at 'Some tests failed',
    /Users/carolnichols/Rust/rust/src/libtest/lib.rs:262est for three
  14. Simplest thing to pass - return a vector containing 2!

    carols10cents committed Mar 27, 2015
    $ cargo test
       Compiling prime_factors v0.0.1
    (file:///Users/carolnichols/Rust/prime_factors)
    src/lib.rs:2:18: 2:21 warning: unused variable: `num`,
    src/lib.rs:2 fn prime_factors(num: i64) -> Vec<i64> {
                                  ^~~
    src/lib.rs:2:18: 2:21 warning: unused variable: `num`,
    src/lib.rs:2 fn prime_factors(num: i64) -> Vec<i64> {
                                  ^~~
         Running target/prime_factors-2690254f285d4686
    
    running 1 test
    test prime_factors_of_two ... ok
    
    test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
    
       Doc-tests prime_factors
    
    running 0 tests
    
    test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
  15. Define the return type of the function

    carols10cents committed Mar 27, 2015
    $ cargo test
       Compiling prime_factors v0.0.1
    (file:///Users/carolnichols/rust/prime_factors)
    src/lib.rs:1:1: 2:2 error: not all control paths return a value [E0269]
    src/lib.rs:1 fn prime_factors(num: i64) -> Vec<i64> {
    src/lib.rs:2 }
    error: aborting due to previous error
    Build failed, waiting for other jobs to finish...
    src/lib.rs:1:1: 2:2 error: not all control paths return a value [E0269]
    src/lib.rs:1 fn prime_factors(num: i64) -> Vec<i64> {
    src/lib.rs:2 }
    error: aborting due to previous error
    Could not compile `prime_factors`.
  16. Specify parameters for the function

    carols10cents committed Mar 27, 2015
    $ cargo test
       Compiling prime_factors v0.0.1
    (file:///Users/carolnichols/Rust/prime_factors)
    src/lib.rs:2:18: 2:21 warning: unused variable: `num`,
    src/lib.rs:2 fn prime_factors(num: i64) {
                                  ^~~
    <std macros>:5:24: 5:35 error: mismatched types:
     expected `()`,
        found `[_; 1]`
    (expected (),
        found array of 1 elements) [E0308]
    <std macros>:5 if ! ( ( * left_val == * right_val ) && ( * right_val ==
    * left_val ) ) {
                                          ^~~~~~~~~~~
    <std macros>:1:1: 9:39 note: in expansion of assert_eq!
    src/lib.rs:7:5: 7:39 note: expansion site
    <std macros>:5:43: 5:68 error: the trait `core::cmp::PartialEq<()>` is
    not implemented for the type `[_; 1]` [E0277]
    <std macros>:5 if ! ( ( * left_val == * right_val ) && ( * right_val ==
    * left_val ) ) {
                                                             ^~~~~~~~~~~~~~~~~~~~~~~~~
    <std macros>:1:1: 9:39 note: in expansion of assert_eq!
    src/lib.rs:7:5: 7:39 note: expansion site
    error: aborting due to 2 previous errors
    Could not compile `prime_factors`.
  17. Define the function prime_factors

    carols10cents committed Mar 27, 2015
    Also allow the prime_factors function to be dead code so that we don't
    get a warning that no non-test code is calling it.
    
    $ cargo test
       Compiling prime_factors v0.0.1
    (file:///Users/carolnichols/Rust/prime_factors)
    src/lib.rs:7:16: 7:32 error: this function takes 0 parameters but 1
    parameter was supplied [E0061]
    src/lib.rs:7     assert_eq!(prime_factors(2), [2]);
                                ^~~~~~~~~~~~~~~~
    <std macros>:1:1: 9:39 note: in expansion of assert_eq!
    src/lib.rs:7:5: 7:39 note: expansion site
    <std macros>:5:24: 5:35 error: mismatched types:
     expected `()`,
        found `[_; 1]`
    (expected (),
        found array of 1 elements) [E0308]
    <std macros>:5 if ! ( ( * left_val == * right_val ) && ( * right_val ==
    * left_val ) ) {
                                          ^~~~~~~~~~~
    <std macros>:1:1: 9:39 note: in expansion of assert_eq!
    src/lib.rs:7:5: 7:39 note: expansion site
    <std macros>:5:43: 5:68 error: the trait `core::cmp::PartialEq<()>` is
    not implemented for the type `[_; 1]` [E0277]
    <std macros>:5 if ! ( ( * left_val == * right_val ) && ( * right_val ==
    * left_val ) ) {
                                                             ^~~~~~~~~~~~~~~~~~~~~~~~~
    <std macros>:1:1: 9:39 note: in expansion of assert_eq!
    src/lib.rs:7:5: 7:39 note: expansion site
    error: aborting due to 3 previous errors
    Could not compile `prime_factors`.
  18. Failing test for prime factors of 2

    carols10cents committed Mar 27, 2015
    1 is special, I'm ignoring it :)
    
    $ cargo test
       Compiling prime_factors v0.0.1
    (file:///Users/carolnichols/Rust/prime_factors)
    src/lib.rs:3:16: 3:29 error: unresolved name `prime_factors`
    src/lib.rs:3     assert_eq!(prime_factors(2), [2]);
                                ^~~~~~~~~~~~~
    <std macros>:1:1: 9:39 note: in expansion of assert_eq!
    src/lib.rs:3:5: 3:39 note: expansion site
    error: aborting due to previous error
    Build failed, waiting for other jobs to finish...
    Could not compile `prime_factors`.
Commits on Mar 27, 2015
  1. cargo new result

    carols10cents committed Mar 27, 2015