+: intermedios 088-095

This commit is contained in:
devfzn 2023-11-13 02:26:58 -03:00
parent 81ec6a7f58
commit fc468682b5
Signed by: devfzn
GPG Key ID: E070ECF4A754FDB1
4 changed files with 185 additions and 33 deletions

View File

@ -2,7 +2,7 @@ import os
from time import sleep from time import sleep
clear = lambda: os.system('clear') if os.name == 'posix' else os.system('cls') clear = lambda: os.system('clear') if os.name == 'posix' else os.system('cls')
tab='\n'+' '*4 tab='\n'+' '*7
def excp_handler(function): def excp_handler(function):
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
@ -31,7 +31,7 @@ def run_func(func):
enter_continue() enter_continue()
def print_run_func(func): def print_run_func(func):
print(tab, func.__doc__, '\n') print(" ", func.__doc__, '\n')
cont = 0 cont = 0
retry = True retry = True
enter_continue() enter_continue()

View File

@ -1,5 +1,6 @@
from . import interm01 as ex01 from . import interm01 as ex01
from . import interm02 as ex02 from . import interm02 as ex02
from . import interm03 as ex03
from common.common import ( from common.common import (
print_challenges, print_challenges,
user_input, user_input,
@ -74,34 +75,36 @@ def challenges02():
case _: case _:
continue continue
#def challenges03(): def challenges03():
# select_ok = False select_ok = False
# while not select_ok: while not select_ok:
# clear() clear()
# i, ex3 = print_challenges(ex03.basics_003) i, ex3 = print_challenges(ex03.interm003)
# selection = user_input(i) selection = user_input(i)
# match selection: match selection:
# case 1: case 1:
# run_func(ex3.name_length) run_func(ex3.int_array)
# case 2: case 2:
# run_func(ex3.fullname_length) run_func(ex3.rand_5)
# case 3: case 3:
# run_func(ex3.full_name_lower) run_func(ex3.range_save)
# case 4: case 4:
# run_func(ex3.nursery) run_func(ex3.repeated)
# case 5: case 5:
# run_func(ex3.any_mayus) run_func(ex3.two_arrays)
# case 6: case 6:
# run_func(ex3.mambo_name) run_func(ex3.array_dels)
# case 7: case 7:
# run_func(ex3.pig_latin) run_func(ex3.sel_one)
# case 'v': case 8:
# return run_func(ex3.decim_array)
# case 's': case 'v':
# exit(0) return
# case _: case 's':
# continue exit(0)
# case _:
continue
#def challenges04(): #def challenges04():
# select_ok = False # select_ok = False
# while not select_ok: # while not select_ok:

150
interm/interm03.py Normal file
View File

@ -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")

View File

@ -70,15 +70,14 @@ def interm_challenges():
clear() clear()
print(content) print(content)
opcs_default(1) opcs_default(1)
selection = user_input(2) selection = user_input(3)
match selection: match selection:
case 1: case 1:
interm.challenges01() interm.challenges01()
case 2: case 2:
interm.challenges02() interm.challenges02()
case 3: case 3:
#interm.challenges03() interm.challenges03()
pass
case 4: case 4:
#interm.challenges04() #interm.challenges04()
pass pass