-
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 #4568 from sugus-labs/main
Reto #20 - Python
- Loading branch information
Showing
9 changed files
with
360 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
Retos/Reto #20 - LA TRIFUERZA [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,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) | ||
|
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 @@ | ||
fn draw_triforce(num: usize) { | ||
|
||
let num_rows: usize = num; | ||
let step = 2; | ||
let big_row = (step * num) - 1; | ||
let mut num_vec: Vec<usize> = 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); | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
Retos/Reto #21 - NÚMEROS PRIMOS GEMELOS [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,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) | ||
|
38 changes: 38 additions & 0 deletions
38
Retos/Reto #21 - NÚMEROS PRIMOS GEMELOS [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,38 @@ | ||
fn twin_primes(num_max: usize) -> Vec<(usize, usize)> { | ||
|
||
let mut is_prime: bool = true; | ||
let mut prime_vec: Vec<usize> = 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); | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
Retos/Reto #23 - LA BASE DE DATOS [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,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() | ||
|
42 changes: 42 additions & 0 deletions
42
Retos/Reto #23 - LA BASE DE DATOS [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,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(); | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
Retos/Reto #24 - CIFRADO CÉSAR [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,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) | ||
|
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,61 @@ | ||
fn caesar_cypher_decypher( | ||
mut text: String, _type: &String, movement: &String, | ||
num_positions: i16, alphabet_vec: &Vec<char>) -> String{ | ||
|
||
text = text.to_lowercase(); | ||
let mut new_alphabet_vec: Vec<char> = 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<char> = 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); | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
Retos/Reto #25 - EL CÓDIGO KONAMI [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,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) | ||
|