l8kyu/
fundamentals.rs

1//!
2//! Modules categorized by Codewars labels - 8kyu *** Fundamentals ***
3//!
4
5use shared::kata::*;
6
7/// Complete the function which converts hex number (given as a string)
8/// to a decimal number.
9/// # Example
10/// ``` a ``` <br>
11/// returns 10
12pub fn hex_to_dec(hex_string: &str) -> u32 {
13    let kata = Kata {
14        level: Level::L8kyu,
15        tags: vec![Tag::Fundamentals],
16        description: String::from("Hex to Decimal"),
17    };
18
19    println!(
20        "Level: {:?}, Tags: {:?}, Description: {}",
21        kata.level, kata.tags, kata.description
22    );
23
24    u32::from_str_radix(hex_string, 16).expect("Not a Hex string")
25}
26
27/// This kata is about multiplying a given number by eight if it is
28/// an even number and by nine otherwise.
29/// # Example
30/// ``` 4 ``` <br>
31/// returns 32
32/// # Example
33/// ``` 5 ``` <br>
34/// returns 45
35pub fn simple_multiplication(number: u8) -> u8 {
36    let kata = Kata {
37        level: Level::L8kyu,
38        tags: vec![Tag::Fundamentals],
39        description: String::from("Simple multiplication"),
40    };
41
42    println!(
43        "Level: {:?}, Tags: {:?}, Description: {}",
44        kata.level, kata.tags, kata.description
45    );
46
47    if number % 2 == 0 {
48        number * 8
49    } else {
50        number * 9
51    }
52}
53
54/// Write a function that removes the spaces from the string,
55/// then return the resultant string.
56/// # Example
57/// ``` "8 j 8   mBliB8g  imjB8B8  jl  B" ``` <br>
58/// returns "8j8mBliB8gimjB8B8jlB"
59pub fn no_space(x: String) -> String {
60    let kata = Kata {
61        level: Level::L8kyu,
62        tags: vec![Tag::Fundamentals, Tag::Strings],
63        description: String::from("Remove String Spaces"),
64    };
65
66    println!(
67        "Level: {:?}, Tags: {:?}, Description: {}",
68        kata.level, kata.tags, kata.description
69    );
70
71    // let no_spaces: String = x.chars().filter(|c| *c != ' ').collect();
72    // no_spaces
73    // Found better solution below.
74    x.replace(" ", "")
75}
76
77/// Define String.prototype.toAlternatingCase (or a similar function/method
78/// such as to_alternating_case/toAlternatingCase/ToAlternatingCase in your
79/// selected language; see the initial solution for details) such that each
80/// lowercase letter becomes uppercase and each uppercase letter becomes lowercase.
81/// # Example
82/// ``` "hello world".toAlternatingCase() ``` <br>
83/// returns "HELLO WORLD"
84pub fn to_alternating_case(s: &str) -> String {
85    let kata = Kata {
86        level: Level::L8kyu,
87        tags: vec![Tag::Fundamentals],
88        description: String::from("altERnaTIng cAsE <=> ALTerNAtiNG CaSe"),
89    };
90
91    println!(
92        "Level: {:?}, Tags: {:?}, Description: {}",
93        kata.level, kata.tags, kata.description
94    );
95
96    s.chars()
97        .map(|c| {
98            if c.is_uppercase() {
99                c.to_lowercase().to_string()
100            } else if c.is_lowercase() {
101                c.to_uppercase().to_string()
102            } else {
103                c.to_string()
104            }
105        })
106        .collect()
107}
108
109/// This function takes two numbers as parameters, the first number
110/// being the coefficient, and the second number being the exponent.
111/// The exponent will never be 1, and neither number will ever be 0.
112/// # Example
113/// ``` derive(7, 8) ``` <br>
114/// returns "56x^7"
115pub fn derive(coefficient: u32, exponent: u32) -> String {
116    let kata = Kata {
117        level: Level::L8kyu,
118        tags: vec![Tag::Fundamentals],
119        description: String::from("Take the Derivative"),
120    };
121
122    println!(
123        "Level: {:?}, Tags: {:?}, Description: {}",
124        kata.level, kata.tags, kata.description
125    );
126
127    format!("{}x^{}", coefficient * exponent, exponent - 1)
128}
129
130/// Your goal is to return multiplication table for number that is always
131/// an integer from 1 to 10.
132/// # Example
133/// ``` multi_table(5) ``` <br>
134/// returns "1 * 5 = 5\n2 * 5 = 10\n3 * 5 = 15\n4 * 5 = 20\n5 * 5 = 25\n
135/// 6 * 5 = 30\n7 * 5 = 35\n8 * 5 = 40\n9 * 5 = 45\n10 * 5 = 50"
136pub fn multi_table(n: u64) -> String {
137    let kata = Kata {
138        level: Level::L8kyu,
139        tags: vec![Tag::Fundamentals, Tag::Strings],
140        description: String::from("Multiplication table for number"),
141    };
142
143    println!(
144        "Level: {:?}, Tags: {:?}, Description: {}",
145        kata.level, kata.tags, kata.description
146    );
147
148    let mut mult_t = String::from("");
149    for i in 1..10 {
150        let line = format!("{} * {} = {}\n", i, n, i * n);
151        mult_t.push_str(&line);
152    }
153    let last_line = format!("{} * {} = {}", 10, n, 10 * n);
154    mult_t.push_str(&last_line);
155    mult_t
156}
157
158/// Timmy & Sarah think they are in love, but around where they live, they will only
159/// know once they pick a flower each. If one of the flowers has an even number of
160/// petals and the other has an odd number of petals it means they are in love.
161/// Write a function that will take the number of petals of each flower and
162/// return true if they are in love and false if they aren't.
163/// # Example
164/// ``` is_love(5, 7); ``` <br>
165/// returns false
166pub fn is_love(flower1: u16, flower2: u16) -> bool {
167    let kata = Kata {
168        level: Level::L8kyu,
169        tags: vec![Tag::Fundamentals],
170        description: String::from("Opposites Attract"),
171    };
172
173    println!(
174        "Level: {:?}, Tags: {:?}, Description: {}",
175        kata.level, kata.tags, kata.description
176    );
177
178    if (flower1 + flower2) % 2 != 0 {
179        return true;
180    } else {
181        return false;
182    }
183}