Skip to main content

Posts

Showing posts with the label rustlang

Reverse a number without converting to a string.

Sometimes I like to work on coding problems just for fun. Things like Project Euler, Code Eval Hackerrank etc. Depending on what language you decide to do things in it might be easier and more efficient to think a little different. Some languages like Ruby, for example, converting a number to a string with to_str() and then reversing is rather trivial. But what if I want to solve it by keeping the number a number? Currently, I am playing around with Rust so my example will be in that language however you can use this same formula in whatever language you like. pub fn num_reverse(number: i32) -> i32 { let mut num = number; let mut rev = 0; while num > 0 { rev = rev * 10 + (num % 10); num = num / 10; } return rev; } Now that we have the code lets map out a little bit of how it works. Let us take the number 1234. num = 1234; rev = 0; Grab the first digit. rev = rev * 10 + (num % 10) Which can be written as. rev = 0 * 10 ...

Get a little Rusty

Been playing around with Rust lang and I have to say I am starting to really enjoy it as a low level programming language.  Its kinda cool to have some modern functionality from the beginning.  Now as a software developer I can get excited but what about being a test developer.  One should probably think those are the same thing.  Well I wont start up that argument but I tend to believe they are one in the same. Lets do some basic coding. We can create us a new library called test_example. $ cargo new test_example This will create you a basic structure for a project. Included will be a src directory. Underneath that you will see lib.rs. You can open this up in your favorite text editor and see something that looks like. #[cfg(test)] mod test { #[test] fn it_works() { } } So you could run this right now and get some passing tests. $ cargo test Lets simplify what we are looking a little more. Change your lib.rs to look like. pub fn truthy(...