From fc468682b51474ea2d712daccff0cb8ea4b38159 Mon Sep 17 00:00:00 2001 From: devfzn Date: Mon, 13 Nov 2023 02:26:58 -0300 Subject: [PATCH] +: intermedios 088-095 --- common/common.py | 4 +- interm/interm.py | 59 +++++++++--------- interm/interm03.py | 150 +++++++++++++++++++++++++++++++++++++++++++++ main.py | 5 +- 4 files changed, 185 insertions(+), 33 deletions(-) create mode 100644 interm/interm03.py diff --git a/common/common.py b/common/common.py index de5ae25..533057e 100644 --- a/common/common.py +++ b/common/common.py @@ -2,7 +2,7 @@ import os from time import sleep clear = lambda: os.system('clear') if os.name == 'posix' else os.system('cls') -tab='\n'+' '*4 +tab='\n'+' '*7 def excp_handler(function): def wrapper(*args, **kwargs): @@ -31,7 +31,7 @@ def run_func(func): enter_continue() def print_run_func(func): - print(tab, func.__doc__, '\n') + print(" ", func.__doc__, '\n') cont = 0 retry = True enter_continue() diff --git a/interm/interm.py b/interm/interm.py index 63af575..30642bd 100644 --- a/interm/interm.py +++ b/interm/interm.py @@ -1,5 +1,6 @@ from . import interm01 as ex01 from . import interm02 as ex02 +from . import interm03 as ex03 from common.common import ( print_challenges, user_input, @@ -74,34 +75,36 @@ def challenges02(): case _: continue -#def challenges03(): -# select_ok = False -# while not select_ok: -# clear() -# i, ex3 = print_challenges(ex03.basics_003) -# selection = user_input(i) -# match selection: -# case 1: -# run_func(ex3.name_length) -# case 2: -# run_func(ex3.fullname_length) -# case 3: -# run_func(ex3.full_name_lower) -# case 4: -# run_func(ex3.nursery) -# case 5: -# run_func(ex3.any_mayus) -# case 6: -# run_func(ex3.mambo_name) -# case 7: -# run_func(ex3.pig_latin) -# case 'v': -# return -# case 's': -# exit(0) -# case _: -# continue -# +def challenges03(): + select_ok = False + while not select_ok: + clear() + i, ex3 = print_challenges(ex03.interm003) + selection = user_input(i) + match selection: + case 1: + run_func(ex3.int_array) + case 2: + run_func(ex3.rand_5) + case 3: + run_func(ex3.range_save) + case 4: + run_func(ex3.repeated) + case 5: + run_func(ex3.two_arrays) + case 6: + run_func(ex3.array_dels) + case 7: + run_func(ex3.sel_one) + case 8: + run_func(ex3.decim_array) + case 'v': + return + case 's': + exit(0) + case _: + continue + #def challenges04(): # select_ok = False # while not select_ok: diff --git a/interm/interm03.py b/interm/interm03.py new file mode 100644 index 0000000..8b3bb2f --- /dev/null +++ b/interm/interm03.py @@ -0,0 +1,150 @@ +class interm003: + + def int_array(self): + """Ask the user for a list of five integers. Store them in an array. + Sort the list and display it in reverse order.""" + from array import array + nums = array ('i', []) + print("Se te solicitarán 5 números") + for n in range(5): + num = int(input(f"{n+1} Ingresa un número: ")) + nums.append(num) + nums = sorted(nums) + nums.reverse() + print(nums) + + def rand_5(self): + """Create an array which will store a list of integers. Generate five + random numbers and store them in the array. Display the array (showing + each item on a separate line)""" + from array import array + from random import randint + nums = array ('i', []) + for _ in range(5): + nums.append(randint(1,100)) + for num in nums: + print(num) + + def range_save(self): + """Ask the user to enter numbers. If they enter a number between 10 and + 20, save it in the array, otherwise display the message \"Outside the + range\". Once five numbers have been successfully added, display the + message \"Thank you\" and display the array with each item shown on a + separate line.""" + from array import array + nums = array ('i', []) + while len(nums) < 5: + num = int(input("Ingresa un número (10-20): ")) + if 10 <= num <= 20: + nums.append(num) + else: + print("Fuera de rango") + print("Gracias") + for num in nums: + print(num) + + def repeated(self): + """Create an array which contains five numbers (two of which should be + repeated). Display the whole array to the user. Ask the user to enter + one of the numbers from the array and then display a message saying how + many times that number appears in the list.""" + from array import array + nums = array ('i', [1,1,2,3,3]) + print('Números en el array: ', ', '.join(map(str, nums.tolist()))) + sel = int(input("Ingresa un número del array: ")) + if sel in nums: + print(f"{sel} aparece {nums.count(sel)} veces en el array") + else: + print(f"{sel} no es parte del array") + + def two_arrays(self): + """Create two arrays (one containing three numbers that the user enters + and one containing a set of five random numbers). Join these two arrays + together into one large array. Sort this large array and display it so + that each number appears on a separate line.""" + from array import array + from random import randint + nums_1 = array ('i', []) + nums_2 = array ('i', []) + for _ in range(5): + nums_1.append(randint(1,100)) + while len(nums_2) < 3: + num = int(input("Ingresa un número: ")) + nums_2.append(num) + nums = nums_1+nums_2 + nums = sorted(nums) + for num in nums: + print(num) + + def array_dels(self): + """Ask the user to enter five numbers. Sort them into order and present + them to the user. Ask them to select one of the numbers. Remove it from + the original array and save it in a new array.""" + from array import array + nums = array ('i', []) + dels = array ('i', []) + print("Se te solicitarán 5 números") + for n in range(5): + num = int(input(f"{n+1} Ingresa un número: ")) + nums.append(num) + nums = sorted(nums) + print('Números en el array: ', ', '.join(map(str, nums))) + while True: + sel = int(input("Ingresa un número del array: ")) + if sel in nums: + nums.remove(sel) + dels.append(sel) + break + print('Array original: ', ', '.join(map(str, nums))) + print('Array números borrados: ', ', '.join(map(str, dels))) + + def sel_one(self): + """Display an array of five numbers. Ask the user to select one of the + numbers. Once they have selected a number, display the position of that + item in the array. If they enter something that is not in the array, + ask them to try again until they select a relevant item.""" + from array import array + from random import randint + nums = array ('i', []) + while len(nums) < 5: + num = randint(1,100) + if num not in nums: + nums.append(num) + print(f"Array: {', '.join(map(str, nums))}") + while True: + sel = int(input("Ingresa un número del array: ")) + if sel in nums: + print(f"{sel} esta en el índice {nums.index(sel)} del array") + break + else: + print(f"{sel} no petenece al array") + + def decim_array(self): + """Create an array of five numbers between 10 and 100 which each have + two decimal places. Ask the user to enter a whole number between 2 and + 5. If they enter something outside of that range, display a suitable + error message and ask them to try again until they enter a valid amount. + Divide each of the numbers in the array by the number the user entered + and display the answers shown to two decimal places.""" + from array import array + from random import uniform + nums = array ('f', []) + print("Array: ", end='') + while len(nums) < 5: + num = uniform(10.00, 100.00) + num = round(num, 2) + if num not in nums: + nums.append(num) + if len(nums) < 5: + print(num, end=', ') + else: + print(num) + while True: + num = int(input("Ingresa un número entre 2 y 5: ")) + if 2 <= num <=5: + for n in range(len(nums)): + div = round(nums[n]/num, 2) + print(f"{round(nums[n],2)}/{num} = {div}") + break + else: + print("Fuera de rango") diff --git a/main.py b/main.py index 0d59308..b9e8d23 100755 --- a/main.py +++ b/main.py @@ -70,15 +70,14 @@ def interm_challenges(): clear() print(content) opcs_default(1) - selection = user_input(2) + selection = user_input(3) match selection: case 1: interm.challenges01() case 2: interm.challenges02() case 3: - #interm.challenges03() - pass + interm.challenges03() case 4: #interm.challenges04() pass