Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solutions-from-ex-17-to-31 #2982

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions projects/m1/017-what-day-of-the-week/python/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#import math module
import math

# ask to user a year
year = int(input('Type a year: '))

# using the gived formula determinate wich day is the first of the year typed
first_day = (year + math.floor((year - 1) / 4) - math.floor(year - 1 / 100) + math.floor(year - 1) / 400) % 7

# if statement the convert the number gived by formula to the name of the day and print on screen
if first_day < 1:
print('Sunday')
elif first_day < 2:
print('Monday')
elif first_day < 3:
print('Tuesday')
elif first_day < 4:
print('Wednesday')
elif first_day < 5:
print('Thursday')
elif first_day < 6:
print('Friday')
elif first_day < 7:
print('Saturday')
else:
print('error')
31 changes: 31 additions & 0 deletions projects/m1/018-roulette-payouts/python/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#import random module
import random

# use random function to get a number
choice = random.choice(range(0,39))

# if statement that print on screen wich is number and the other cases that pay like a roulette
if choice % 2 == 0 and choice != 37 and choice != 0:
print(f'pay {choice}')
print('pay black')
print('pay even')
elif choice % 2 != 0 and choice != 37:
print(f'pay {choice}')
print('pay red')
print('pay odd')
elif choice == 37:
print('pay 00')
print('pay green')
elif choice == 0:
print(f'pay {choice}')
print('pay green')


if choice <= 18 and choice > 0:
print('pay 1 to 18')
elif choice > 18 and choice < 37:
print('pay 19 to 36')




9 changes: 9 additions & 0 deletions projects/m1/019-average/python/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
while True:
number = int(input('type a number or 0 to end: '))
if number == 0:
print(result)
break
else:
new_number = int(input('type a number or 0 to end: '))
result = number + new_number

6 changes: 6 additions & 0 deletions projects/m1/020-discount-table/python/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import numpy as np

for i in np.arange(4.95,29.95,5):
discount = (round(i,2) * 60) / 100
discounted_price = round((round(i,2) - (round(i,2) * 60) / 100),2)
print(f'The original price was {round(i,2)}, the discount is 60%, equal to {discount} and the new price is {discounted_price }')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend re-implementing it without using numpy.

33 changes: 33 additions & 0 deletions projects/m1/021-compute-the-perimeter-of-a-polygon/python/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import math

perimeter = 0


first_x = input('Enter the first x-coordinate: ')
first_y = input('Enter the first y-coordinate: ')

FIRST_X = float(first_x)
FIRST_Y = float(first_y)


x = FIRST_X
y = FIRST_Y


while True:
next_x = input('Enter the next x-coordinate: ')
if next_x == '':
distance = math.sqrt((x - FIRST_X)**2 + (y - FIRST_Y)**2)
perimeter = perimeter + distance
break
else:
next_y = input('Enter the next y-coordinate: ')
next_x = float(next_x)
next_y = float(next_y)
distance = math.sqrt((next_x - x)**2 + (next_y - y)**2)
perimeter = perimeter + distance
x = next_x
y = next_y

print(f'The perimeter of that polygon is {perimeter}')

33 changes: 33 additions & 0 deletions projects/m1/022-admission-price/python/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

under_3 = 0
under_13 = 0
adult = 0
senior = 0
under_13_ticket = 14
adult_ticket = 23
senior_ticket = 18

while True:
user_input = input('insert guest age or a blank line to quit: ')
if user_input == '':
break
if int(user_input) < 3:
under_3 += 1
elif 12 >= int(user_input) >= 3:
under_13 += 1
elif int(user_input) >= 65:
senior += 1
else:
adult += 1

price_under_13 = int(under_13) * under_13_ticket
price_adult = int(adult) * adult_ticket
price_senior = int(senior) * senior_ticket
total = '%.2f' % (price_under_13 + price_adult + price_senior)


if under_3 and under_13 and adult and senior == 0:
quit()
else:
print(f"Your group is composed by {under_3} children equal or under 2 years old , {under_13} equal or under 12 years old, {senior} over 65 and {adult} between 12 and 65. Total amount for tickets is {total} $")

18 changes: 18 additions & 0 deletions projects/m1/023-parity-bits/python/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

total = 0

while True:
user_bit = input("Insert a 8 bit sequence or press enter to end: \n")
if len(user_bit) == 8:
for i in user_bit:
i = int(i)
total = total + i
if total % 2 == 0:
print(f"{user_bit} for even parity, parity bit should be 0")
else:
print(f"{user_bit} for even parity, parity bit should be 1")
elif user_bit == '':
break
else:
print('This is not a 8 bit sequence!')

11 changes: 11 additions & 0 deletions projects/m1/024-fizz-buzz/python/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

for i in range(0,101):
if i % 3 == 0 and i % 5 != 0:
print(f"{i} fizz!")
elif i % 3 != 0 and i % 5 == 0:
print(f'{i} buzz')
elif i % 5 == 0 and i % 5 == 0:
print(f"{i} fizz! buzz!")
else:
print(i)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend optimizing redundant code.

14 changes: 14 additions & 0 deletions projects/m1/025-caesar-cipher/python/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
message = input('insert a message to encode or decode: ')
number = int(input('insert a positive number to encode or negative to decode: '))

ALPHABET = 'abcdefghijklmnopqrstuvwxyz'
new_message = ''


for i in message:
for e in ALPHABET:
if e == i:
index = ALPHABET.index(e)
new_message += ALPHABET[index + number]

print(new_message)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend trying to use (input) numbers greater than 60.
If there are errors, how could you fix the code?

14 changes: 14 additions & 0 deletions projects/m1/026-is-a-string-a-palindrome/python/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
USER_INPUT = input('type a word: ')

new_word = ''

for i in reversed(USER_INPUT):
new_word += i

if new_word == USER_INPUT:
print(f'{USER_INPUT} is a palyndrome')
else:
print(f'{USER_INPUT} isn\'t a palyndrome')



16 changes: 16 additions & 0 deletions projects/m1/027-the-collatz-conjecture/python/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
while True:
user_input = int(input('type a postive number or a negative number or a 0 to end: '))
if user_input <= 0:
break
else:
number = user_input
sequence = str(number)
while number != 1:
if number % 2 == 0:
number = number // 2
sequence += ' ' + str(number)
else:
number = number * 3 + 1
sequence += ' ' + str(number)
print(sequence)

17 changes: 17 additions & 0 deletions projects/m1/028-prime-factors/python/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
number = int(input('type a number: '))
factor = 2
result = ''

if number < 2:
print('number must be grater or, at least, equal to 2')
quit()


while number >= factor:
if number % factor == 0:
number = number / factor
result += ' ' + str(factor)
else:
factor += 1

print(result)
9 changes: 9 additions & 0 deletions projects/m1/029-decimal-to-binary/python/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
result = ''

q = int(input('Type a number to convert in binary: '))

while q != 0:
r = q % 2
result += str(r)
q = q // 2
print(f'The binary value of the number is : {result}')
17 changes: 17 additions & 0 deletions projects/m1/030-maximum-integer/python/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import random

high_number = random.choice(range(1,101))

update = 0

for i in range(1,100):
number = random.choice(range(1,101))
if number > high_number:
high_number = number
print(f'{number} <== update')
update += 1
else:
print(number)

print(f'The maximum value found was {high_number} and was updated {update} times ')

17 changes: 17 additions & 0 deletions projects/m1/031-coin-flip-simulation/python/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import random

sequence = ''
average = 0

for i in range(0,10):
while sequence.count('TTT') == False and sequence.count('HHH') == False:
value = random.randint(0,1)
if value == 0:
sequence += 'H'
else:
sequence += 'T'
average += len(sequence)
print(f'{sequence} ({len(sequence)} flips)')
sequence = ''

print(f'On average {average / 10} were needed.')