Skip to content

Commit

Permalink
Quiz 2 + Add rustdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
chiffonng committed Jun 21, 2024
1 parent 28e7503 commit 01e191a
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 additions & 20 deletions exercises/quiz2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,8 @@
// - Modules
// - Enums
//
// Let's build a little machine in the form of a function. As input, we're going
// to give a list of strings and commands. These commands determine what action
// is going to be applied to the string. It can either be:
// - Uppercase the string
// - Trim the string
// - Append "bar" to the string a specified amount of times
// The exact form of this will be:
// - The input is going to be a Vector of a 2-length tuple,
// the first element is the string, the second one is the command.
// - The output element is going to be a Vector of strings.
//
// No hints this time!

// I AM NOT DONE

pub enum Command {
Uppercase,
Trim,
Expand All @@ -31,21 +18,46 @@ pub enum Command {
mod my_module {
use super::Command;

// TODO: Complete the function signature!
pub fn transformer(input: ???) -> ??? {
// TODO: Complete the output declaration!
let mut output: ??? = vec![];
// Define the transformer parameters and return type
pub fn transformer(input: Vec<(String, Command)>) -> Vec<String> {
/// Transforms a list of strings according to specified commands.
///
/// # Arguments
/// - `input`: Vector of tuples, each containing a string and a command to apply.
///
/// The commands can:
/// - Uppercase the string
/// - Trim the string
/// - Append "bar" to the string a specified number of times
///
/// # Returns
/// A vector of strings, each modified according to its corresponding command.
let mut output: Vec<String> = vec![];
for (string, command) in input.iter() {
// TODO: Complete the function body. You can do it!
match command {
Command::Uppercase => {
output.push(string.to_uppercase());
}
Command::Trim => {
output.push(string.trim().to_string());
}
Command::Append(n) => {
let mut appended = string.clone();
for _ in 0..*n {
appended.push_str("bar");
}
output.push(appended);
}
}
}
output
}
}

#[cfg(test)]
mod tests {
// TODO: What do we need to import to have `transformer` in scope?
use ???;
// Use 'super::*' to bring the functions from the parent module into scope.
use super::my_module::transformer;
use super::Command;

#[test]
Expand Down

0 comments on commit 01e191a

Please sign in to comment.