151 lines
6.0 KiB
Python
151 lines
6.0 KiB
Python
|
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")
|