-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4590 from sugus-labs/main
Reto #28 - Rust
- Loading branch information
Showing
7 changed files
with
249 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
Retos/Reto #28 - EXPRESIÓN MATEMÁTICA [Media]/rust/sugus-labs.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use evalexpr::*; | ||
use std::any::type_name; | ||
use std::panic; | ||
|
||
fn type_of<T>(_: T) -> &'static str { | ||
type_name::<T>() | ||
} | ||
|
||
fn test_math_expr(mut text: String, symbol_vec: Vec<char>) -> bool { | ||
|
||
let mut is_math_expr: bool = false; | ||
let mut text_symbol_vec: Vec<char> = vec![]; | ||
|
||
let _eval: Result<Value, EvalexprError> = eval(&text); | ||
//println!(" - {:?}", _eval); | ||
|
||
let response: Value = match _eval { | ||
Ok(res) => res, | ||
Err(err) => return false | ||
}; | ||
|
||
for c in text.replace(" ", "").chars() { | ||
//println!("{}", c); | ||
if !symbol_vec.contains(&c) | ||
&& !c.is_digit(10) { | ||
//println!(" - {}", c); | ||
is_math_expr = false; | ||
return is_math_expr | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
fn main() { | ||
|
||
let mut symbol_vec: Vec<char> = vec![' ', '.']; | ||
let mut operation_vec: Vec<char> = vec![ | ||
'+', '-', '*', '/', '%']; | ||
symbol_vec.append(&mut operation_vec); | ||
let text: String = String::from("1 + 1 / -6.5"); | ||
let is_math_expr: bool = test_math_expr(text, symbol_vec); | ||
println!("{}", is_math_expr) | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
Retos/Reto #29 - EL CARÁCTER INFILTRADO [Fácil]/python/sugus-labs.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
def diff_chars(first_text: str, second_text: str): | ||
|
||
if not isinstance(first_text, str) or not isinstance(second_text, str): | ||
raise ValueError("We need only strings!") | ||
|
||
if len(first_text) != len(second_text): | ||
raise ValueError("The two text needs to have the same length!") | ||
|
||
error_list = [] | ||
|
||
for c1, c2 in zip(first_text, second_text): | ||
if c1 != c2: | ||
error_list.append((c1, c2)) | ||
#print(c1, c2) | ||
|
||
return error_list | ||
|
||
if __name__ == "__main__": | ||
|
||
first_text = "Me llamo.Brais Moure" | ||
second_text = "Me llamo brais moure" | ||
result_list = diff_chars(first_text, second_text) | ||
print(result_list) |
26 changes: 26 additions & 0 deletions
26
Retos/Reto #29 - EL CARÁCTER INFILTRADO [Fácil]/rust/sugus-labs.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
fn diff_chars(first_text: String, sec_text: String) -> Vec<(char, char)> { | ||
|
||
let mut error_vec: Vec<(char, char)> = vec![]; | ||
let first_text_vec: Vec<char> = first_text.chars().collect(); | ||
let sec_text_vec: Vec<char> = sec_text.chars().collect(); | ||
|
||
let iter = first_text_vec.iter().zip(sec_text_vec.iter()); | ||
|
||
for (c1, c2) in iter { | ||
//println!("{} {}", c1, c2); | ||
if c1 != c2 { | ||
error_vec.push((*c1, *c2)) | ||
} | ||
} | ||
|
||
return error_vec; | ||
} | ||
|
||
fn main() { | ||
|
||
let first_text: String = String::from("Me llamo.Brais Moure"); | ||
let sec_text: String = String::from("Me llamo brais moure"); | ||
let result_vec: Vec<(char, char)> = diff_chars(first_text, sec_text); | ||
println!("{:?}", result_vec); | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
Retos/Reto #30 - EL TECLADO T9 [Media]/python/sugus-labs.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from bs4 import BeautifulSoup | ||
import requests | ||
import re | ||
import string | ||
|
||
def download_t9_alphabet(): | ||
url = "https://es.wikipedia.org/wiki/Texto_predictivo" | ||
body = requests.get(url) | ||
body_text = body.content | ||
|
||
clean = re.compile('<.*?>') | ||
|
||
alphabet_dict = dict() | ||
|
||
soup = BeautifulSoup(body_text, 'lxml') | ||
divs = soup.find_all("div", class_="mw-parser-output") | ||
for div in divs: | ||
uls = div.find_all("ul") | ||
for ul in uls: | ||
lis = ul.find_all("li") | ||
for li in lis: | ||
try: | ||
clean_line = re.sub(clean, '', str(li)) | ||
number_char = str(clean_line[0]) | ||
corr_chars = clean_line.split("(")[1].split(")")[0] | ||
corr_char_list = list(corr_chars) | ||
alphabet_dict[number_char] = corr_char_list | ||
#print(clean_line, number_char, corr_chars) | ||
except: | ||
break | ||
if number_char == "9": | ||
break | ||
if number_char == "9": | ||
break | ||
|
||
return alphabet_dict | ||
|
||
def t9_to_text(text: str, alphabet_dict: dict = download_t9_alphabet): | ||
|
||
""" text with numbers to words function """ | ||
|
||
new_str_list = [] | ||
text_list = text.split("-") | ||
|
||
for t in text_list: | ||
pos = len(t) - 1 | ||
new_char = alphabet_dict.get(t[0])[pos] | ||
new_str_list.append(new_char) | ||
new_str = "".join(new_str_list) | ||
|
||
return new_str | ||
|
||
if __name__ == "__main__": | ||
alphabet_dict = download_t9_alphabet() | ||
new_str = t9_to_text("6-666-88-777-33-3-33-888", alphabet_dict) | ||
print(new_str) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use std::collections::HashMap; | ||
|
||
fn t9_to_text(text: String, alphabet_hash: &HashMap<char, Vec<char>>) { | ||
|
||
let mut new_str_vec: Vec<char> = vec![]; | ||
let text_parts = text.split("-"); | ||
let text_vec = text_parts.collect::<Vec<&str>>(); | ||
let mut pos: usize; | ||
let mut new_char: char; | ||
let mut ch: char; | ||
|
||
for t in text_vec { | ||
pos = t.len() - 1; | ||
ch = String::from(t).chars().next().unwrap(); | ||
new_char = alphabet_hash.get(&ch); | ||
dbg!(t, pos, new_char); | ||
} | ||
// | ||
// new_char = alphabet_dict.get(t[0])[pos] | ||
// new_str_list.append(new_char) | ||
//new_str = "".join(new_str_list) | ||
|
||
//return new_str | ||
} | ||
|
||
fn main() { | ||
|
||
let text: String = String::from("6-666-88-777-33-3-33-888"); | ||
let alphabet_hash: HashMap<char, Vec<char>> = [ | ||
('2', vec!['a', 'b', 'c']), | ||
('3', vec!['d', 'e', 'f']), | ||
('4', vec!['g', 'h', 'i']), | ||
('5', vec!['j', 'k', 'l']), | ||
('6', vec!['m', 'n', 'o']), | ||
('7', vec!['p', 'q', 'r', 's']), | ||
('8', vec!['t', 'u', 'v']), | ||
('9', vec!['w', 'x', 'y', 'z'])] | ||
.iter() | ||
.cloned() | ||
.collect(); | ||
t9_to_text(text, &alphabet_hash); | ||
//println!("{:?}", alphabet_hash); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
def read_abacus_str(abacus_list: list): | ||
|
||
abacus_number = "" | ||
for line in abacus_list: | ||
num = str(len(line.split("-")[0])) | ||
abacus_number = abacus_number + num | ||
abacus_number = int(abacus_number) | ||
|
||
return abacus_number | ||
|
||
if __name__ == "__main__": | ||
|
||
abacus_list = [ | ||
"O---OOOOOOOO", | ||
"OOO---OOOOOO", | ||
"---OOOOOOOOO", | ||
"OO---OOOOOOO", | ||
"OOOOOOO---OO", | ||
"OOOOOOOOO---", | ||
"---OOOOOOOOO"] | ||
abacus_number = read_abacus_str(abacus_list) | ||
print(abacus_number) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
fn read_abacus_str(abacus_vec: Vec<String>) -> usize { | ||
|
||
let mut abacus_num_str = String::new(); | ||
let mut chunks: Vec<&str> = vec![]; | ||
let mut num; | ||
let abacus_number: usize; | ||
|
||
for line in abacus_vec { | ||
chunks = line.split("---").collect(); | ||
num = chunks[0].len(); | ||
//println!("{:?}", num); | ||
abacus_num_str.push_str(&num.to_string()); | ||
} | ||
let abacus_number: usize = abacus_num_str.parse().unwrap(); | ||
|
||
return abacus_number; | ||
|
||
} | ||
|
||
fn main() { | ||
|
||
let abacus_vec: Vec<String> = vec![ | ||
String::from("O---OOOOOOOO"), | ||
String::from("OOO---OOOOOO"), | ||
String::from("---OOOOOOOOO"), | ||
String::from("OO---OOOOOOO"), | ||
String::from("OOOOOOO---OO"), | ||
String::from("OOOOOOOOO---"), | ||
String::from("---OOOOOOOOO")]; | ||
let abacus_number = read_abacus_str(abacus_vec); | ||
println!("{}",abacus_number); | ||
|
||
} |