diff --git a/projects/m1/017-what-day-of-the-week/python/main.py b/projects/m1/017-what-day-of-the-week/python/main.py index e69de29bb..efcb20823 100644 --- a/projects/m1/017-what-day-of-the-week/python/main.py +++ b/projects/m1/017-what-day-of-the-week/python/main.py @@ -0,0 +1,29 @@ +#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 = int((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 == 0: + result = 'Sunday' +elif first_day == 1: + result = 'Monday' +elif first_day == 2: + result = 'Tuesday' +elif first_day == 3: + result = 'Wednesday' +elif first_day == 4: + result = 'Thursday' +elif first_day == 5: + result = 'Friday' +elif first_day == 6: + result = 'Saturday' +else: + result = 'error' + +print('First day of that year is: ' + str(result)) diff --git a/projects/m1/018-roulette-payouts/python/main.py b/projects/m1/018-roulette-payouts/python/main.py index e69de29bb..ed8d6415a 100644 --- a/projects/m1/018-roulette-payouts/python/main.py +++ b/projects/m1/018-roulette-payouts/python/main.py @@ -0,0 +1,33 @@ +#import random module +import random + +# use random function to get a number +choice = random.choice(range(0,38)) + +# 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: + number = choice + color = 'pay black' + odd_even ='pay even' +if choice % 2 != 0 and choice != 37: + number = choice + color = 'red' + odd_even = 'odd' +if choice == 37: + number = '00' + color = 'green' +if choice == 0: + number = choice + color = 'green' + +if choice <= 18 and choice > 0: + range_payout = '1 to 18' +elif choice > 18 and choice < 37: + range_payout = '19 to 36' + +if number == 0 or number == '00': + print('Pay ' + str(number) + '\n' + 'Pay ' + str(color)) +else: + print('Pay ' + str(number) + '\n' + 'Pay ' + str(color) + '\n' + 'Pay ' + str(odd_even) + '\n' + 'Pay '+ str(range_payout)) + + diff --git a/projects/m1/019-average/python/main.py b/projects/m1/019-average/python/main.py index e69de29bb..aa172f54f 100644 --- a/projects/m1/019-average/python/main.py +++ b/projects/m1/019-average/python/main.py @@ -0,0 +1,29 @@ +number = ' ' +counter = 0 +result = '' + + +while number != 0: + counter = counter + 1 + number = input('Type a number or 0 to end: ') + if not number.isdigit(): + result = 'error, value is not a number' + break + number = int(number) + if number == 0 and counter == 1: + result = 'error, first value can\'t be 0' + break + elif number != 0 and counter == 1: + result = number + else: + result = result + number + +if isinstance(result, str): + print(result) +else: + counter = counter - 1 + result = result / counter + print(result) + + + diff --git a/projects/m1/020-discount-table/python/main.py b/projects/m1/020-discount-table/python/main.py index e69de29bb..6f03e1dc9 100644 --- a/projects/m1/020-discount-table/python/main.py +++ b/projects/m1/020-discount-table/python/main.py @@ -0,0 +1,7 @@ +price = 4.95 + +for i in range(1,6): + discount = round((price * 60) / 100, 2) + discounted_price = round((price - discount),2) + print(f'The original price was {price} $, the discount is 60%, equal to {discount} % and the new price is {discounted_price } $') + price = price + 5.0 \ No newline at end of file diff --git a/projects/m1/021-compute-the-perimeter-of-a-polygon/python/main.py b/projects/m1/021-compute-the-perimeter-of-a-polygon/python/main.py index e69de29bb..6b61df251 100644 --- a/projects/m1/021-compute-the-perimeter-of-a-polygon/python/main.py +++ b/projects/m1/021-compute-the-perimeter-of-a-polygon/python/main.py @@ -0,0 +1,51 @@ + +import math + +perimeter = 0 +counter = 0 + +first_x = input('Enter the first x-coordinate: ') + +for character in first_x: + if character.isdigit() == False and character != '.': + print('error, first x-coordinate invalid') + quit() + +first_y = input('Enter the first y-coordinate: ') + +for character in first_y: + if character.isdigit() == False and character != '.': + print('error, first y-coordinate invalid') + quit() + + +FIRST_X = float(first_x) +FIRST_Y = float(first_y) + +x = FIRST_X +y = FIRST_Y + +next_x = ' ' + +while next_x != '': + counter += 1 + next_x = input('Enter the next x-coordinate: ') + if next_x == '' and counter <= 2: + print('enter at least 3 pairs of value') + quit() + elif next_x == '': + continue + next_y = input('Enter the next y-coordinate: ') + if next_y == '': + print(f'invalid value for y') + quit() + 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 +distance = math.sqrt((x - FIRST_X)**2 + (y - FIRST_Y)**2) +perimeter = perimeter + distance + +print(f'The perimeter of that polygon is {perimeter}') \ No newline at end of file diff --git a/projects/m1/022-admission-price/python/main.py b/projects/m1/022-admission-price/python/main.py index e69de29bb..ab4fe725f 100644 --- a/projects/m1/022-admission-price/python/main.py +++ b/projects/m1/022-admission-price/python/main.py @@ -0,0 +1,36 @@ + +under_3 = 0 +under_13 = 0 +adult = 0 +senior = 0 +under_13_ticket = 14 +adult_ticket = 23 +senior_ticket = 18 + +user_input = ' ' + +while user_input != '': + user_input = input('insert guest age or a blank line to quit: ') + if user_input == '': + continue + else: + 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} $") + \ No newline at end of file diff --git a/projects/m1/023-parity-bits/python/main.py b/projects/m1/023-parity-bits/python/main.py index e69de29bb..ce62cf1fc 100644 --- a/projects/m1/023-parity-bits/python/main.py +++ b/projects/m1/023-parity-bits/python/main.py @@ -0,0 +1,33 @@ + +user_bit = ' ' + +total = 0 + +message = '' + +invalid_value = None + +while user_bit != '': + user_bit = input("Insert a 8 bit sequence or press enter to end: \n") + if user_bit == '': + continue + for i in user_bit: + if i != '0' and i != '1': + invalid_value = True + if invalid_value == True: + message = message + f'{user_bit} invalid value \n' + continue + if len(user_bit) != 8: + message = message + f'{user_bit} invalid value \n' + continue + else: + for i in user_bit: + i = int(i) + total = total + i + if total % 2 == 0: + message = message + f'{user_bit} for even parity, parity bit should be 0 \n' + total = 0 + else: + message = message + f"{user_bit} for even parity, parity bit should be 1 \n" + total = 0 +print(message) diff --git a/projects/m1/024-fizz-buzz/python/main.py b/projects/m1/024-fizz-buzz/python/main.py index e69de29bb..aedef6c07 100644 --- a/projects/m1/024-fizz-buzz/python/main.py +++ b/projects/m1/024-fizz-buzz/python/main.py @@ -0,0 +1,13 @@ +result = '' + +for i in range(1,101): + if i % 3 == 0 and i % 5 == 0: + result = result + f"{i} fizz! buzz!\n" + elif i % 3 == 0: + result = result + f'{i} fizz!\n' + elif i % 5 == 0: + result = result + f"{i} buzz! \n" + else: + result = result + f'{i} \n' + +print(result) diff --git a/projects/m1/025-caesar-cipher/python/main.py b/projects/m1/025-caesar-cipher/python/main.py index e69de29bb..a46e614ff 100644 --- a/projects/m1/025-caesar-cipher/python/main.py +++ b/projects/m1/025-caesar-cipher/python/main.py @@ -0,0 +1,28 @@ +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 = '' +NUMBERS = '0123456789' + +for i in message: + if i in NUMBERS or i == ' ': + new_message += i + elif i == 'à': + i = 'a' + elif i == 'è' or i == 'é': + i = 'e' + elif i == 'ì': + i = 'i' + elif i == 'ò': + i = 'o' + elif i == 'ù': + i = 'u' + for e in ALPHABET: + if e == i: + index = ALPHABET.index(e) + if index + number >= 26: + new_message += ALPHABET [(index + (number)) % 26] + else: + new_message += ALPHABET[index + (number)] +print(new_message) \ No newline at end of file diff --git a/projects/m1/026-is-a-string-a-palindrome/python/main.py b/projects/m1/026-is-a-string-a-palindrome/python/main.py index e69de29bb..390d525d0 100644 --- a/projects/m1/026-is-a-string-a-palindrome/python/main.py +++ b/projects/m1/026-is-a-string-a-palindrome/python/main.py @@ -0,0 +1,16 @@ +USER_INPUT = input('type a word: ') + +new_word = '' + +for i in reversed(USER_INPUT): + new_word += i + +if new_word == USER_INPUT: + result = f'{USER_INPUT} is a palyndrome' +else: + result = f'{USER_INPUT} is not a palyndrome' + +print(result) + + + diff --git a/projects/m1/027-the-collatz-conjecture/python/main.py b/projects/m1/027-the-collatz-conjecture/python/main.py index e69de29bb..bd9c3acb0 100644 --- a/projects/m1/027-the-collatz-conjecture/python/main.py +++ b/projects/m1/027-the-collatz-conjecture/python/main.py @@ -0,0 +1,45 @@ +# 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) + +sequence = '' +user_input = 1 +result = '' + +while user_input > 0: + user_input = input('type a postive number or a negative number or a 0 to end: ') + if user_input == '': + result += f'Error: empty field\n' + user_input = 1 + continue + elif user_input.isdigit() == False: + result += f'Error: invalid value\n' + user_input = 1 + continue + user_input = int(user_input) + number = user_input + sequence = str(number) + if user_input != 0: + while sequence[-1] != '1': + if number % 2 == 0: + number = number // 2 + sequence += f' {number}' + else: + number = number * 3 + 1 + sequence += f' {number}' + result += f'{sequence} \n' + else: + continue +print(result) diff --git a/projects/m1/028-prime-factors/python/main.py b/projects/m1/028-prime-factors/python/main.py index e69de29bb..abc759483 100644 --- a/projects/m1/028-prime-factors/python/main.py +++ b/projects/m1/028-prime-factors/python/main.py @@ -0,0 +1,18 @@ +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) + diff --git a/projects/m1/029-decimal-to-binary/python/main.py b/projects/m1/029-decimal-to-binary/python/main.py index e69de29bb..b54cd7e8f 100644 --- a/projects/m1/029-decimal-to-binary/python/main.py +++ b/projects/m1/029-decimal-to-binary/python/main.py @@ -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}') \ No newline at end of file diff --git a/projects/m1/030-maximum-integer/python/main.py b/projects/m1/030-maximum-integer/python/main.py index e69de29bb..0893ad410 100644 --- a/projects/m1/030-maximum-integer/python/main.py +++ b/projects/m1/030-maximum-integer/python/main.py @@ -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 ') + diff --git a/projects/m1/031-coin-flip-simulation/python/main.py b/projects/m1/031-coin-flip-simulation/python/main.py index e69de29bb..7d50e0702 100644 --- a/projects/m1/031-coin-flip-simulation/python/main.py +++ b/projects/m1/031-coin-flip-simulation/python/main.py @@ -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.') \ No newline at end of file