+: intermedios 096-104

This commit is contained in:
devfzn 2023-11-13 13:07:55 -03:00
parent fc468682b5
commit 5ae8c65f1a
Signed by: devfzn
GPG Key ID: E070ECF4A754FDB1
3 changed files with 195 additions and 34 deletions

View File

@ -1,6 +1,7 @@
from . import interm01 as ex01
from . import interm02 as ex02
from . import interm03 as ex03
from . import interm04 as ex04
from common.common import (
print_challenges,
user_input,
@ -105,36 +106,38 @@ def challenges03():
case _:
continue
#def challenges04():
# select_ok = False
# while not select_ok:
# clear()
# i, ex4 = print_challenges(ex04.basics_004)
# selection = user_input(i)
# match selection:
# case 1:
# run_func(ex4.decims)
# case 2:
# run_func(ex4.decims_round)
# case 3:
# run_func(ex4.square_root)
# case 4:
# run_func(ex4.pi_round)
# case 5:
# run_func(ex4.circle_area)
# case 6:
# run_func(ex4.cyl_vol)
# case 7:
# run_func(ex4.friendly_div)
# case 8:
# run_func(ex4.circ_trian)
# case 'v':
# return
# case 's':
# exit(0)
# case _:
# continue
#
def challenges04():
select_ok = False
while not select_ok:
clear()
i, ex4 = print_challenges(ex04.interm004)
selection = user_input(i)
match selection:
case 1:
run_func(ex4.two_d)
case 2:
run_func(ex4.sel_row_col)
case 3:
run_func(ex4.sel_row)
case 4:
run_func(ex4.change_val)
case 5:
run_func(ex4.bidims_dict)
case 6:
run_func(ex4.bidict)
case 7:
run_func(ex4.user_data)
case 8:
run_func(ex4.dict_show_filter)
case 9:
run_func(ex4.dict_remove)
case 'v':
return
case 's':
exit(0)
case _:
continue
#def challenges05():
# select_ok = False
# while not select_ok:

159
interm/interm04.py Normal file
View File

@ -0,0 +1,159 @@
class interm004:
def two_d(self):
"""Create the following using a simple 2D list using the standard Python
indexing
- 0 1 2
0 2 5 8
1 3 7 4
2 1 6 9
3 4 2 0
"""
bidims = [[2,5,8],[3,7,4],[1,6,9],[4,2,0]]
print(" | 0 | 1 | 2 |\n━━━━━━━━━━━━━━━")
for i in range(len(bidims)):
print(i, '|', ' | '.join(map(str, bidims[i])), end=' |\n')
def sel_row_col(self):
"""Using the 2D list from program 096, ask the user to select a row and
a column and display that value."""
bidims = [[2,5,8],[3,7,4],[1,6,9],[4,2,0]]
print(" | 0 | 1 | 2 |\n━━━━━━━━━━━━━━━")
for i in range(len(bidims)):
print(i, '|', ' | '.join(map(str, bidims[i])), end=' |\n')
sel_r = int(input("\nSelecciona una fila: "))
sel_c = int(input("Selecciona una columna: "))
if sel_r < len(bidims) and sel_c < len(bidims[0]):
print(bidims[sel_r][sel_c])
else:
print("Fuera de rango")
def sel_row(self):
"""Using the 2D list from program 096, ask the user which row they would
like displayed and display just that row. Ask them to enter a new value
and add it to the end of the row and display the row again."""
bidims = [[2,5,8],[3,7,4],[1,6,9],[4,2,0]]
print(" | 0 | 1 | 2 |\n━━━━━━━━━━━━━━━")
for i in range(len(bidims)):
print(i, '|', ' | '.join(map(str, bidims[i])), end=' |\n')
sel_r = int(input("\nSelecciona una fila: "))
if sel_r < len(bidims):
print(bidims[sel_r])
new_value = int(input("Ingresa un valor: "))
bidims[sel_r][-1] = new_value
print(bidims[sel_r])
else:
print("Fuera de rango")
def change_val(self):
"""Change your previous program to ask the user which row they want
displayed. Display that row. Ask which column in that row they want
displayed and display the value that is held there. Ask the user if
they want to change the value. If they do, ask for a new value and
change the data. Finally, display the whole row again."""
bidims = [[2,5,8],[3,7,4],[1,6,9],[4,2,0]]
print(" | 0 | 1 | 2 |\n━━━━━━━━━━━━━━━")
for i in range(len(bidims)):
print(i, '|', ' | '.join(map(str, bidims[i])), end=' |\n')
sel_r = int(input("\nSelecciona una fila: "))
if sel_r < len(bidims):
print(bidims[sel_r])
sel_c = int(input("Selecciona una columna: "))
if sel_c < len(bidims[0]):
print(bidims[sel_r][sel_c])
resp = input("¿Quieres cambiar este valor? (s|n): ").lower()
if resp == 's':
print(bidims[sel_r])
new_value = int(input("Ingresa un valor: "))
bidims[sel_r][sel_c] = new_value
else:
print("Fuera de rango")
else:
print("Fuera de rango")
print(bidims[sel_r])
def bidims_dict(self):
"""Create the following using a 2D dictionary showing the sales each
person has made in the different geographical regions:
- N S E W
John 3056 8463 8441 2694
Tom 4832 6786 4737 3612
Anne 5239 4802 5820 1859
Fiona 3904 3645 8821 2451
"""
bidict = {"John":{'N':3056, 'S':8463, 'E':8441, 'W':2694},
"Tom":{'N':4832, 'S':6786, 'E':4737, 'W':3612},
"Anne":{'N':5239, 'S':4802, 'E':5820, 'W':1859},
"Fiona":{'N':3904, 'S':3645, 'E':8821, 'W':2451}}
print(" | N | S | E | W |")
print("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
for name in bidict:
print(name.rjust(5), '|', ' | '.join(map(str, bidict[name].values())), end=' |\n')
def bidict(self):
"""Using program 100, ask the user for a name and a region. Display the
relevant data. Ask the user for the name and region of data they want
to change and allow them to make the alteration to the sales figure.
Display the sales for all regions for the name they choose."""
bidict = {"John":{'N':3056, 'S':8463, 'E':8441, 'W':2694},
"Tom":{'N':4832, 'S':6786, 'E':4737, 'W':3612},
"Anne":{'N':5239, 'S':4802, 'E':5820, 'W':1859},
"Fiona":{'N':3904, 'S':3645, 'E':8821, 'W':2451}}
self.bidims_dict()
name = input("\nIngresa un nombre del cuadro: ").title()
reg = input("Ingresa un sector: ").upper()
print(bidict[name][reg])
new_val = int(input("Ingresa el nuevo valor: "))
bidict[name][reg] = new_val
print(', '.join(map(str, bidict[name].values())))
def user_data(self):
"""Ask the user to enter the name, age and shoe size for four people.
Ask for the name of one of the people in the list and display their age
and shoe size."""
data = {}
print("Se solicitarán 4 usuarios y su respectivos datos")
for i in range(4):
name = input(f"{i+1} Ingresa un nombre: ").capitalize()
age = int(input(f"Ingresa la edad de {name}: "))
shoe = int(input(f"Ingresa cuanto calza {name}: "))
data[name]={"Edad":age, "Calzado":shoe}
name = input("Ingresa uno de los nombres agregados anteriormente: ").capitalize()
print(*[str(k) + ':' + str(v) for k,v in data[name].items()])
def dict_show_filter(self):
"""Adapt program 102 to display the names and ages of all the people
in the list but do not show their shoe size."""
data = {}
print("Se solicitarán 4 usuarios y su respectivos datos")
for i in range(4):
name = input(f"{i+1} Ingresa un nombre: ").capitalize()
age = int(input(f"Ingresa la edad de {name}: "))
shoe = int(input(f"Ingresa cuanto calza {name}: "))
data[name]={"Edad":age, "Calzado":shoe}
for name in data.keys():
print("Nombre:", name, "Edad:", data[name]["Edad"])
def dict_remove(self):
"""After gathering the four names, ages and shoe sizes, ask the user to
enter the name of the person they want to remove from the list. Delete
this row from the data and display the other rows on separate lines."""
data = {}
print("Se solicitarán 4 usuarios y su respectivos datos")
for i in range(4):
name = input(f"{i+1} Ingresa un nombre: ").capitalize()
age = int(input(f"Ingresa la edad de {name}: "))
shoe = int(input(f"Ingresa cuanto calza {name}: "))
data[name]={"Edad":age, "Calzado":shoe}
for name in data.keys():
print("Nombre:", name,
"Edad:", data[name]["Edad"],
"Talla:", data[name]["Calzado"])
name = input("Ingresa el nombre del usuario a borrar: ").capitalize()
data.pop(name)
for name in data.keys():
print("Nombre:", name,
"Edad:", data[name]["Edad"],
"Talla:", data[name]["Calzado"])

View File

@ -60,7 +60,7 @@ def interm_challenges():
1) Interm 069-079
2) Interm 080-087
3) Interm 088-095
4) Interm 096-103
4) Interm 096-104
5) Interm 105-110
6) Interm 111-117
7) Interm 118-123
@ -70,7 +70,7 @@ def interm_challenges():
clear()
print(content)
opcs_default(1)
selection = user_input(3)
selection = user_input(4)
match selection:
case 1:
interm.challenges01()
@ -79,8 +79,7 @@ def interm_challenges():
case 3:
interm.challenges03()
case 4:
#interm.challenges04()
pass
interm.challenges04()
case 5:
#interm.challenges05()
pass