From dcf62bf778384c74ec7c3e14888c4e86a4e02028 Mon Sep 17 00:00:00 2001 From: sugus-labs Date: Thu, 10 Aug 2023 10:50:52 +0200 Subject: [PATCH 1/9] Add Triforce exercises in Python --- .../python/sugus-labs.py | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Retos/Reto #20 - LA TRIFUERZA [Media]/python/sugus-labs.py diff --git a/Retos/Reto #20 - LA TRIFUERZA [Media]/python/sugus-labs.py b/Retos/Reto #20 - LA TRIFUERZA [Media]/python/sugus-labs.py new file mode 100644 index 0000000000..adefddba1d --- /dev/null +++ b/Retos/Reto #20 - LA TRIFUERZA [Media]/python/sugus-labs.py @@ -0,0 +1,33 @@ +import math + +def draw_triforce(num): + + num_triangles = 3 + num_rows = num + step = 2 + big_row = (step * num) - 1 + num_list = [n for n in range(1, big_row + step, step)] + #print(num_list) + + for iter in range(0, num_rows): + padding_num = int((num_list[-1] - num_list[iter]) / 2) + curr_num = num_list[iter] + #print("Padding: ", padding_num) + #print("Stars: ", curr_num) + padding_str = " " * (padding_num + num) + stars_str = "*" * curr_num + print(f"{padding_str}{stars_str}{padding_str}") + for iter in range(0, num_rows): + padding_num = int((num_list[-1] - num_list[iter]) / 2) + curr_num = num_list[iter] + #print("Padding: ", padding_num) + #print("Stars: ", curr_num) + padding_str = " " * padding_num + stars_str = "*" * curr_num + print(f"{padding_str}{stars_str}{padding_str} {padding_str}{stars_str}{padding_str}") + +if __name__ == "__main__": + + num = 4 + draw_triforce(num) + From 512c7256a8ba68f6c4a273a068300405cd2f0718 Mon Sep 17 00:00:00 2001 From: sugus-labs Date: Thu, 10 Aug 2023 11:12:20 +0200 Subject: [PATCH 2/9] Add Triforce exercises in Rust --- .../rust/sugus-labs.rs | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 Retos/Reto #20 - LA TRIFUERZA [Media]/rust/sugus-labs.rs diff --git a/Retos/Reto #20 - LA TRIFUERZA [Media]/rust/sugus-labs.rs b/Retos/Reto #20 - LA TRIFUERZA [Media]/rust/sugus-labs.rs new file mode 100644 index 0000000000..ddc0ba7a60 --- /dev/null +++ b/Retos/Reto #20 - LA TRIFUERZA [Media]/rust/sugus-labs.rs @@ -0,0 +1,44 @@ +fn draw_triforce(num: usize) { + + let num_rows: usize = num; + let step = 2; + let big_row = (step * num) - 1; + let mut num_vec: Vec = vec![]; + let mut num_stars: usize = 1; + for n in 1..(big_row + step) { + //println!("{}", num_stars); + if num_stars <= big_row { + num_vec.push(num_stars); + num_stars = num_stars + step; + } else { + break; + } + } + let mut padding_num: usize = 0; + let mut curr_num: usize = 0; + let mut padding_str: String = String::new(); + let mut stars_str: String = String::new(); + //println!("{:?}", num_vec); + + for iter in 0..num_rows { + padding_num = ((num_vec[num - 1] - num_vec[iter]) / 2) as usize; + curr_num = num_vec[iter]; + padding_str = " ".repeat(padding_num + num); + stars_str = "*".repeat(curr_num); + println!("{padding_str}{stars_str}{padding_str}") + } + for iter in 0..num_rows { + padding_num = ((num_vec[num - 1] - num_vec[iter]) / 2) as usize; + curr_num = num_vec[iter]; + padding_str = " ".repeat(padding_num); + stars_str = "*".repeat(curr_num); + println!("{padding_str}{stars_str}{padding_str} {padding_str}{stars_str}{padding_str}") + } +} + +fn main() { + + let num: usize = 4; + draw_triforce(num); + +} \ No newline at end of file From d56564a2f8be0903f7e9cf6871143ba2cd7daa4d Mon Sep 17 00:00:00 2001 From: sugus-labs Date: Thu, 10 Aug 2023 11:39:57 +0200 Subject: [PATCH 3/9] Add Twin primes exercises in Python --- .../python/sugus-labs.py" | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 "Retos/Reto #21 - N\303\232MEROS PRIMOS GEMELOS [Media]/python/sugus-labs.py" diff --git "a/Retos/Reto #21 - N\303\232MEROS PRIMOS GEMELOS [Media]/python/sugus-labs.py" "b/Retos/Reto #21 - N\303\232MEROS PRIMOS GEMELOS [Media]/python/sugus-labs.py" new file mode 100644 index 0000000000..f194c93317 --- /dev/null +++ "b/Retos/Reto #21 - N\303\232MEROS PRIMOS GEMELOS [Media]/python/sugus-labs.py" @@ -0,0 +1,32 @@ +import math + +def twin_primes(num_max): + + is_prime = True + prime_list = [] + + for num in range(2, num_max): + is_prime = True + for pos in range(2, num): + if num % pos == 0: + is_prime = False + break + if is_prime: + prime_list.append(num) + + twin_prime_list = [] + for iter in range(0, len(prime_list)): + try: + if prime_list[iter + 1] - prime_list[iter] == 2: + twin_prime_list.append((prime_list[iter],prime_list[iter + 1])) + except: + break + + return twin_prime_list + +if __name__ == "__main__": + + num_max = 14 + twin_prime_list = twin_primes(num_max) + print(twin_prime_list) + From 2c5a874021922e6fd3392ab03121baf91eaa06ef Mon Sep 17 00:00:00 2001 From: sugus-labs Date: Thu, 10 Aug 2023 12:19:12 +0200 Subject: [PATCH 4/9] Add Twin primes exercises in Rust --- .../rust/sugus-labs.rs" | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 "Retos/Reto #21 - N\303\232MEROS PRIMOS GEMELOS [Media]/rust/sugus-labs.rs" diff --git "a/Retos/Reto #21 - N\303\232MEROS PRIMOS GEMELOS [Media]/rust/sugus-labs.rs" "b/Retos/Reto #21 - N\303\232MEROS PRIMOS GEMELOS [Media]/rust/sugus-labs.rs" new file mode 100644 index 0000000000..d8bf7c69c8 --- /dev/null +++ "b/Retos/Reto #21 - N\303\232MEROS PRIMOS GEMELOS [Media]/rust/sugus-labs.rs" @@ -0,0 +1,38 @@ +fn twin_primes(num_max: usize) -> Vec<(usize, usize)> { + + let mut is_prime: bool = true; + let mut prime_vec: Vec = vec![]; + let mut twin_prime_vec: Vec<(usize, usize)> = vec![]; + + for num in 2..num_max { + is_prime = true; + for pos in 2..num { + if num % pos == 0 { + is_prime = false; + break; + } + } + if is_prime { + prime_vec.push(num) + } + } + //println!("{:?}", prime_vec); + + for iter in 0..prime_vec.len() { + if iter < prime_vec.len() - 1 { + if prime_vec[iter + 1] - prime_vec[iter] == 2 { + twin_prime_vec.push((prime_vec[iter], prime_vec[iter + 1])); + } + } + } + + return twin_prime_vec; +} + +fn main() { + + let num_max: usize = 14; + let twin_prime_vec = twin_primes(num_max); + println!("{:?}", twin_prime_vec); + +} \ No newline at end of file From 78c4bdd3ff141206354c33f4a11dcfd77f084639 Mon Sep 17 00:00:00 2001 From: sugus-labs Date: Thu, 10 Aug 2023 15:49:49 +0200 Subject: [PATCH 5/9] Add Databases exercises in Python --- .../python/sugus-labs.py | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Retos/Reto #23 - LA BASE DE DATOS [Media]/python/sugus-labs.py diff --git a/Retos/Reto #23 - LA BASE DE DATOS [Media]/python/sugus-labs.py b/Retos/Reto #23 - LA BASE DE DATOS [Media]/python/sugus-labs.py new file mode 100644 index 0000000000..9843a75538 --- /dev/null +++ b/Retos/Reto #23 - LA BASE DE DATOS [Media]/python/sugus-labs.py @@ -0,0 +1,23 @@ +from sqlalchemy import create_engine, text + +def query_database(): + + host = "mysql-5707.dinaserver.com" + port = 3306 + user = "mouredev_read" + passw = "mouredev_pass" + database = "moure_test" + + engine = create_engine( + f"mysql+mysqlconnector://{user}:{passw}@{host}/{database}") + conn = engine.connect() + result = conn.execute( + text("SELECT * FROM challenges")) + for row in result: + print(row) + +if __name__ == "__main__": + + side_max = 3 + query_database() + From b6faece36d0cfa408407f034cd9fce79297e75ed Mon Sep 17 00:00:00 2001 From: sugus-labs Date: Thu, 10 Aug 2023 16:14:45 +0200 Subject: [PATCH 6/9] Add Databases exercises in Rust --- .../rust/sugus-labs.rs | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Retos/Reto #23 - LA BASE DE DATOS [Media]/rust/sugus-labs.rs diff --git a/Retos/Reto #23 - LA BASE DE DATOS [Media]/rust/sugus-labs.rs b/Retos/Reto #23 - LA BASE DE DATOS [Media]/rust/sugus-labs.rs new file mode 100644 index 0000000000..312b8da455 --- /dev/null +++ b/Retos/Reto #23 - LA BASE DE DATOS [Media]/rust/sugus-labs.rs @@ -0,0 +1,42 @@ +use mysql::*; +use mysql::prelude::*; +use chrono::NaiveDate; + +fn query_database() { + + let host: &str = "mysql-5707.dinaserver.com"; + let port: &str = "3306"; + let user: &str = "mouredev_read"; + let passw: &str = "mouredev_pass"; + let database: &str = "moure_test"; + + let mut url: String = String::from("mysql://"); + url.push_str(user); + url.push_str(":"); + url.push_str(passw); + url.push_str("@"); + url.push_str(host); + url.push_str(":"); + url.push_str(port); + url.push_str("/"); + url.push_str(database); + + //println!("{}", url); + + let pool = Pool::new(url).unwrap(); + let mut conn = pool.get_conn().unwrap(); + + conn.query_iter("select id, name, difficulty, date from challenges") + .unwrap() + .for_each(|row| { + let r:(i32, String, String, NaiveDate) = from_row(row.unwrap()); + println!("{}, {}, {}, {:?}", r.0, r.1, r.2, r.3); + }); + +} + +fn main() { + + query_database(); + +} \ No newline at end of file From ccbe3cee463d870827934b208215173e89d79ff1 Mon Sep 17 00:00:00 2001 From: sugus-labs Date: Thu, 10 Aug 2023 16:53:22 +0200 Subject: [PATCH 7/9] Add Caesar cypher exercises in Python --- .../python/sugus-labs.py" | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 "Retos/Reto #24 - CIFRADO C\303\211SAR [F\303\241cil]/python/sugus-labs.py" diff --git "a/Retos/Reto #24 - CIFRADO C\303\211SAR [F\303\241cil]/python/sugus-labs.py" "b/Retos/Reto #24 - CIFRADO C\303\211SAR [F\303\241cil]/python/sugus-labs.py" new file mode 100644 index 0000000000..67af34be29 --- /dev/null +++ "b/Retos/Reto #24 - CIFRADO C\303\211SAR [F\303\241cil]/python/sugus-labs.py" @@ -0,0 +1,50 @@ +import string + +def caesar_cypher_decypher(text, _type, movement, num_positions, alphabet_list): + + text_list = text.lower().split(" ") + new_alphabet_list = [] + magic_num = 0 + + if movement == "R": + if _type == "cypher": + magic_num = num_positions + else: + magic_num = -1 * num_positions + elif movement == "L": + if _type == "cypher": + magic_num = -1 * num_positions + else: + magic_num = num_positions + + #for num, c in enumerate(alphabet): + # new_alphabet_list = alphabet[num + magic_num] + + for word in text_list: + new_word_list = [] + for elem in word: + source_idx = alphabet_list.index(elem) + dest_idx = (source_idx + magic_num) % 27 + new_word_list.append(alphabet_list[dest_idx]) + new_alphabet_list.append("".join(c for c in new_word_list)) + + edited_text = " ".join(c for c in new_alphabet_list) + return edited_text + +if __name__ == "__main__": + + alphabet = string.ascii_lowercase + alphabet_list = list(alphabet) + #print(alphabet) + text = "HELLO WORLD" + movement = "R" + num_positions = 2 + _type = "cypher" + cyphered_text = caesar_cypher_decypher( + text, _type, movement, num_positions, alphabet_list) + print(cyphered_text) + _type = "decypher" + decyphered_text = caesar_cypher_decypher( + cyphered_text, _type, movement, num_positions, alphabet_list) + print(decyphered_text) + From 6bb63b6ac3d96b5fa7df9c1e5719e3c6dc6a972c Mon Sep 17 00:00:00 2001 From: sugus-labs Date: Thu, 10 Aug 2023 17:41:30 +0200 Subject: [PATCH 8/9] Add Caesar cypher exercises in Rust --- .../rust/sugus-labs.rs" | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 "Retos/Reto #24 - CIFRADO C\303\211SAR [F\303\241cil]/rust/sugus-labs.rs" diff --git "a/Retos/Reto #24 - CIFRADO C\303\211SAR [F\303\241cil]/rust/sugus-labs.rs" "b/Retos/Reto #24 - CIFRADO C\303\211SAR [F\303\241cil]/rust/sugus-labs.rs" new file mode 100644 index 0000000000..5cd700fca6 --- /dev/null +++ "b/Retos/Reto #24 - CIFRADO C\303\211SAR [F\303\241cil]/rust/sugus-labs.rs" @@ -0,0 +1,61 @@ +fn caesar_cypher_decypher( + mut text: String, _type: &String, movement: &String, + num_positions: i16, alphabet_vec: &Vec) -> String{ + + text = text.to_lowercase(); + let mut new_alphabet_vec: Vec = vec![]; + let mut magic_num: i16 = 0; + let mut source_idx: i16; + let mut dest_idx: i16; + + if movement == &String::from("R") { + if _type == &String::from("cypher") { + magic_num = num_positions as i16; + } else { + magic_num = -1 * (num_positions as i16); + } + } else if movement == &String::from("L") { + if _type == &String::from("cypher") { + magic_num = -1 * (num_positions as i16); + } else { + magic_num = num_positions as i16; + } + } + + //println!("{}", text); + //println!("{:?}", alphabet_vec); + + for word in text.chars() { + if word != ' ' { + //println!("{}", word); + source_idx = (alphabet_vec.iter().position(|&x| x == word).unwrap() as i16); + dest_idx = (source_idx + magic_num) % 27; + new_alphabet_vec.push(alphabet_vec[(dest_idx as usize)]); + } else { + new_alphabet_vec.push(' '); + } + } + + let edited_text = new_alphabet_vec.into_iter().collect(); + return edited_text; + +} + +fn main() { + + let alphabet: String = String::from("abcdefghijklmnopqrstuvwxyz"); + let alphabet_vec: Vec = alphabet.chars().collect(); + //println!("{:?}", alphabet_vec); + let text: String = String::from("HELLO WORLD"); + let movement: String = String::from("R"); + let num_positions: i16 = 2; + let mut _type: String = String::from("cypher"); + let cyphered_text = caesar_cypher_decypher( + text, &_type, &movement, num_positions, &alphabet_vec); + println!("{}", cyphered_text); + _type = String::from("decypher"); + let decyphered_text = caesar_cypher_decypher( + cyphered_text, &_type, &movement, num_positions, &alphabet_vec); + println!("{}", decyphered_text); + +} \ No newline at end of file From 6e05fb31d83aec85446d65fd1e3912367f3cec4f Mon Sep 17 00:00:00 2001 From: sugus-labs Date: Thu, 10 Aug 2023 18:56:09 +0200 Subject: [PATCH 9/9] Add Keyboard exercises in Python+ --- .../python/sugus-labs.py" | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 "Retos/Reto #25 - EL C\303\223DIGO KONAMI [Media]/python/sugus-labs.py" diff --git "a/Retos/Reto #25 - EL C\303\223DIGO KONAMI [Media]/python/sugus-labs.py" "b/Retos/Reto #25 - EL C\303\223DIGO KONAMI [Media]/python/sugus-labs.py" new file mode 100644 index 0000000000..8b3830cc64 --- /dev/null +++ "b/Retos/Reto #25 - EL C\303\223DIGO KONAMI [Media]/python/sugus-labs.py" @@ -0,0 +1,37 @@ +from pynput import keyboard + +# KONAMI CODE +# ['UP, UP, DOWN, DOWN, LEFT, RIGHT, LEFT, RIGHT, B, A, ENTER] + +def on_press(key): + try: + #print(key.char) + event_list.append(key.char.upper()) + except AttributeError: + #print(str(key).lower().replace("key.", "")) + event_list.append(str(key).upper().replace("KEY.", "")) + if event_list[-11:] == konami_code: + print("You WIN!") + return False + +def on_release(key): + #print(f'{key} released') + if key == keyboard.Key.esc: + return False + +if __name__ == "__main__": + + event_list = [] + konami_code = [ + "UP", "UP", "DOWN", "DOWN", + "LEFT", "RIGHT", "LEFT", "RIGHT", + "B", "A", "ENTER"] + + with keyboard.Listener( + on_press = on_press, + on_release = on_release + ) as listener: + + listener.join() + #print(event_list) +