From a850b33cb8972c6a6ff3d95af90b61e67c734cac Mon Sep 17 00:00:00 2001 From: Fabricio C Zuardi Date: Mon, 21 Nov 2022 21:12:34 +0000 Subject: [PATCH] Encontro 7 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Luiz F. A. de Prá Co-authored-by: Igor Stefano Co-authored-by: joaovictornm Co-authored-by: Guilherme de S. Vieira Beira --- 2022-11-21.md | 7 +++-- 2022-12-05.md | 40 +++++++++++++++++++++++ README.md | 7 +++-- encontro7/Cargo.lock | 7 +++++ encontro7/Cargo.toml | 8 +++++ encontro7/src/lib.rs | 75 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 140 insertions(+), 4 deletions(-) create mode 100644 2022-12-05.md create mode 100644 encontro7/Cargo.lock create mode 100644 encontro7/Cargo.toml create mode 100644 encontro7/src/lib.rs diff --git a/2022-11-21.md b/2022-11-21.md index 19a1415..a627a8d 100644 --- a/2022-11-21.md +++ b/2022-11-21.md @@ -24,8 +24,11 @@ https://youtu.be/YOUTUBE_ID ### Participantes -- [@seugithubhandleaqui](https://github.com/seugithubhandleaqui) -- ... +- [@fczuardi](https://github.com/fczuardi) +- [@igstefano](https://github.com/igstefano) +- [@luizdepra](https://github.com/luizdepra) +- [@joaovictornm](https://github.com/joaovictornm) +- [@GuilhermeVBeira](https://github.com/GuilhermeVBeira) + + +## Informações +- **Data**: segunda, 05 de dezembro de 2022, 16:30 +- **Local**: Google Meet +- [Encontro Passado](2022-11-21.md) + +### Agenda +- boas vindas +- apresentações +- ack das [regras](README.md#regras) e início da gravação +- escolha do problema +- discussão, resumo dos temas cobertos, dúvidas +- registro dos produtos e agendamento do próximo + +### Participantes + +- [@seu_handle_do_github_aqui](https://github.com/seu_handle_do_github_aqui) + + + diff --git a/README.md b/README.md index 138df15..1d22a06 100644 --- a/README.md +++ b/README.md @@ -17,17 +17,20 @@ Encontro de pessoas que querem aprender [Rust][rust]. [video4]: https://youtu.be/oDDllu9ISYo [video5]: https://youtu.be/mKt0jM3xlBs [video6]: https://youtu.be/KVehwy1hCXQ + +[video7]: https://youtu.be/KVehwy1hCXQ [contact]: https://github.com/Rust-dojo/eventos/issues ## Próximo encontro: -- 21 de Novembro de 2022 +- 05 de Dezembro de 2022 - 16:30 [nesta sala de Google Meet][meetlink] - - [agenda](2022-11-21.md) + - [agenda](2022-12-05.md) - [checklist](https://github.com/Rust-dojo/eventos/issues/15) ## Edições passadas: +- [21 de Novembro de 2022](2022-11-21.md) ([vídeo][video7]) - [07 de Novembro de 2022](2022-11-07.md) ([vídeo][video6]) - [24 de Outubro de 2022](2022-10-24.md) ([vídeo][video5]) - [10 de Outubro de 2022](2022-10-10.md) ([vídeo][video4]) diff --git a/encontro7/Cargo.lock b/encontro7/Cargo.lock new file mode 100644 index 0000000..a8ed6ed --- /dev/null +++ b/encontro7/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "encontro7" +version = "0.1.0" diff --git a/encontro7/Cargo.toml b/encontro7/Cargo.toml new file mode 100644 index 0000000..579958f --- /dev/null +++ b/encontro7/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "encontro7" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/encontro7/src/lib.rs b/encontro7/src/lib.rs new file mode 100644 index 0000000..52912d4 --- /dev/null +++ b/encontro7/src/lib.rs @@ -0,0 +1,75 @@ +/* + +https://exercism.org/tracks/rust/exercises/anagram + +Instructions +An anagram is a rearrangement of letters to form a new word. Given a word and a list of + candidates, select the sublist of anagrams of the given word. + +Given "listen" and a list of candidates like "enlists" "google" "inlets" "banana" the +program should return a list containing "inlets". + +The solution is case insensitive, which means "WOrd" is the same as "word" or "woRd". +It may help to take a peek at the std library for functions that can convert between them. + +The solution cannot contain the input word. A word is always an anagram of itself, + which means it is not an interesting result. Given "hello" and the list + ["hello", "olleh"] the answer is ["olleh"]. + +You are going to have to adjust the function signature provided in the stub in order +for the lifetimes to work out properly. This is intentional: what's there demonstrates the basics of lifetime syntax, and what's missing teaches how to interpret lifetime-related compiler errors. + +Try to limit case changes. Case changes are expensive in terms of time, so it's faster to minimize them. + +If sorting, consider sort_unstable which is typically faster than stable sorting. When applicable, unstable sorting is preferred because it is generally faster than stable sorting and it doesn't allocate auxiliary memory. + +*/ + +pub fn is_anagram(word: &String, candidate: &String) -> bool { + // candidate.len() == word.len() + + let mut first_word: Vec = word.chars().collect(); + first_word.sort(); + + let first_word_str:String = first_word.into_iter().collect(); + + let mut candidate_word = candidate.chars().collect::>(); + candidate_word.sort_unstable(); + + let candidate_word_str:String = candidate_word.into_iter().collect(); + + candidate_word_str == first_word_str +} + +pub fn find_anagrams( word: &String, anagram_candidates: Vec) -> Vec{ + let mut result = vec![]; + for c in anagram_candidates.into_iter() { + if is_anagram(word, &c) { + result.push(c); + } + } + result +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn no_match() { + let word = "foo".to_string(); + let expected: Vec = vec![]; + let result = find_anagrams(&word, vec!["huebr".to_string()]); + assert_eq!(result, expected); + } + + #[test] + fn one_match() { + let word = "foo".to_string(); + let candidates = vec!["foo".to_string(), "hue".to_string()]; + let expected: Vec = vec!["foo".to_string()]; + let result = find_anagrams(&word, candidates); + assert_eq!(result, expected); + } + +}