From aa2911b5e72f7efd6fed4a4d2f1a70f4c56d72d9 Mon Sep 17 00:00:00 2001 From: BloodYce Date: Sun, 8 Sep 2024 11:12:38 +0200 Subject: [PATCH 1/6] solution-m4/001 --- projects/001-taxi-fare/python/main.py | 19 +++++++++++++++++++ .../002-shipping-calculator/python/main.py | 13 +++++++++++++ 2 files changed, 32 insertions(+) diff --git a/projects/001-taxi-fare/python/main.py b/projects/001-taxi-fare/python/main.py index e69de29..fdd3204 100644 --- a/projects/001-taxi-fare/python/main.py +++ b/projects/001-taxi-fare/python/main.py @@ -0,0 +1,19 @@ +''' +In a particular jurisdiction, taxi fares consist of: +- base fare of €4.00, +- plus €0.25 for every 140 meters travelled. + +Write a function named **taxi fare** that takes the distance travelled (in kilometers) as its only parameter +and returns the total fare as its only result. +Write a main program that demonstrates the function. +''' + +def taxi_fare(kilometers): + addedtaxes = kilometers // 140 + taxfare = 0.25 * addedtaxes + totalfare = 4.00 + taxfare + return totalfare + +kilometers = int(input("Kilometers: ")) +totalfare = taxi_fare(kilometers) +print("Total fare = ", totalfare) \ No newline at end of file diff --git a/projects/002-shipping-calculator/python/main.py b/projects/002-shipping-calculator/python/main.py index e69de29..39f4aa0 100644 --- a/projects/002-shipping-calculator/python/main.py +++ b/projects/002-shipping-calculator/python/main.py @@ -0,0 +1,13 @@ +''' +An online retailer provides express shipping for many of its items at a rate of: +- €10.99 for the first item in an order +- €2.99 for each subsequent item in the same order. + +Write a function named **shipping calculator** that takes the number of items in the order as its **only parameter**. + +Return the shipping charge for the order as the function’s result. + +Include a main program that reads the number of items purchased from the user +and displays the shipping charge. +''' + From e7aedefbf05188a31ab5034b78012380772e1463 Mon Sep 17 00:00:00 2001 From: BloodYce Date: Wed, 25 Sep 2024 21:35:18 +0200 Subject: [PATCH 2/6] solution-m4/007 --- projects/007-capitalize-it/python/main.py | 38 +++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/projects/007-capitalize-it/python/main.py b/projects/007-capitalize-it/python/main.py index e69de29..820dc8e 100644 --- a/projects/007-capitalize-it/python/main.py +++ b/projects/007-capitalize-it/python/main.py @@ -0,0 +1,38 @@ +''' +Many people do not use capital letters correctly, especially when typing on small devices like smartphones. +To help address this situation, you will create a function named capitalize it that takes a string as its only parameter and returns a new copy of the string that has been correctly capitalized. + +In particular, your function must: + +Capitalize the first non-space character in the string, +Capitalize the first non-space character after a period, exclamation mark or question mark, +Capitalize a lowercase “i” if it is preceded by a space and followed by a space, period, exclamation mark, question mark or apostrophe. +Implementing these transformations will correct most capitalization errors. + +Example: +input: +“what time do I have to be there? what’s the address? this time I’ll try to be on time!” +output: +“What time do I have to be there? What’s the address? This time I’ll try to be on time!”. + +Include a main program that reads a string from the user, capitalizes it using your function, and displays the result. +''' + +def capitalize_it(string): + array = list(string) + newarray = [] + newarray.append(array[0].upper()) + del array[0] + for letter in array: + if letter == "." "!" "?": + nextletter = + newarray.append(nextletter.upper()) + elif letter == "i": + newarray.append(letter.upper()) + else: + newarray.append(letter) + return newarray + +string = str(input("Please insert a string without capitalizing anything: ")) +capitalizedstring = capitalize_it(string) +print("The correct string is: ", capitalizedstring) \ No newline at end of file From 1d746a62d4caa7035c3b13f3319c7183cb64d1cc Mon Sep 17 00:00:00 2001 From: BloodYce Date: Wed, 25 Sep 2024 22:50:25 +0200 Subject: [PATCH 3/6] Correzione con Francesco --- projects/007-capitalize-it/python/main.py | 40 ++++++++++------- .../python/main.py | 26 +++++++++++ .../python/main.py | 45 +++++++++++++++++++ projects/012-days-in-a-month/python/main.py | 34 ++++++++++++++ projects/013-reduce-measures/python/main.py | 35 +++++++++++++++ projects/014-magic-dates/python/main.py | 28 ++++++++++++ 6 files changed, 192 insertions(+), 16 deletions(-) diff --git a/projects/007-capitalize-it/python/main.py b/projects/007-capitalize-it/python/main.py index 820dc8e..8a276ff 100644 --- a/projects/007-capitalize-it/python/main.py +++ b/projects/007-capitalize-it/python/main.py @@ -19,20 +19,28 @@ ''' def capitalize_it(string): - array = list(string) - newarray = [] - newarray.append(array[0].upper()) - del array[0] - for letter in array: - if letter == "." "!" "?": - nextletter = - newarray.append(nextletter.upper()) - elif letter == "i": - newarray.append(letter.upper()) + array = string.split() + newarray = [array[0].capitalize()] + #for index, letter in enumerate(array[1:]): + for i in range(1,len(array)): + #print(f"{array[i-1]=}") + if array[i-1][-1] in [".", "!", "?"]: + newarray.append(array[i].capitalize()) + elif array[i] == "i": + newarray.append(array[i].capitalize()) + elif array[i][0] == "i": + if array[i][1] in [".", "!", "?", "'"]: + newarray.append(array[i].capitalize()) else: - newarray.append(letter) - return newarray - -string = str(input("Please insert a string without capitalizing anything: ")) -capitalizedstring = capitalize_it(string) -print("The correct string is: ", capitalizedstring) \ No newline at end of file + newarray.append(array[i]) + return " ".join(newarray) + + +def main(): + string = "what time do i have to be there? what's the address? i'll be there i.ll in a minute" + capitalizedstring = capitalize_it(string) + print("The correct string is: ", capitalizedstring) + + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/projects/010-hexadecimal-and-decimal-digits/python/main.py b/projects/010-hexadecimal-and-decimal-digits/python/main.py index e69de29..b63e338 100644 --- a/projects/010-hexadecimal-and-decimal-digits/python/main.py +++ b/projects/010-hexadecimal-and-decimal-digits/python/main.py @@ -0,0 +1,26 @@ +''' +Write two functions, hex2int and int2hex, that convert between hexadecimal digits (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E and F) and decimal (base 10) integers. +The hex2int function is responsible for converting a string containing a single hexadecimal digit to a base 10 integer. +The int2hex function is responsible for converting an integer between 0 and 15 to a single hexadecimal digit. +Each function will take the value to convert as its only parameter and return the converted value as its only result. +Ensure that the hex2int function works correctly for both uppercase and lowercase letters. +Your functions should display a meaningful error message and end the program if the parameter’s value is outside the expected range. +''' + +def hex2int(initialvalue): + convertedvalue = int(initialvalue, 16) + return convertedvalue + +def int2hex(initialvalue): + newvalue = int(initialvalue) + convertedvalue = hex(newvalue) + return convertedvalue + +initialvalue = input("Please insert a value: ") +question = input("In which base do you want to convert the precedent string? (hex/dec)") +if question == "hex": + finalvalue = int2hex(initialvalue) + print("The initial value converted into hexadecimal is: ", finalvalue) +else: + finalvalue = hex2int(initialvalue) + print("The initial value converted into decimal is: ", finalvalue) \ No newline at end of file diff --git a/projects/011-arbitrary-base-conversions/python/main.py b/projects/011-arbitrary-base-conversions/python/main.py index e69de29..1e10a15 100644 --- a/projects/011-arbitrary-base-conversions/python/main.py +++ b/projects/011-arbitrary-base-conversions/python/main.py @@ -0,0 +1,45 @@ +''' +Write a program that allows the user to convert a number from one base to another. +Your program should support bases between 2 and 16 for both the input number and the result number. + +If the user chooses a base outside of this range then an appropriate error message should be displayed and the program should exit. + +Divide your program into several functions: + +a function named arbitrary to base 10 that converts from an arbitrary base to base 10 +a function that base 10 to arbitrary base converts from base 10 to an arbitrary base +a main program that reads the bases and input number from the user +''' + +def arbitrary(usernumber, userbase): + if userbase == "2": + convertedvalue = int(usernumber, 2) + return convertedvalue + elif userbase == "8": + convertedvalue = int(usernumber, 8) + return convertedvalue + elif userbase == "16": + convertedvalue = int(usernumber, 16) + return convertedvalue + else: + return userbase + +def arbitrary_base_converts(convertedvalue, basechoice): + if basechoice == "10": + return convertedvalue + elif basechoice == "2": + newvalue = bin(convertedvalue) + return newvalue + elif basechoice == "8": + newvalue = oct(convertedvalue) + return newvalue + elif basechoice == "16": + newvalue = hex(convertedvalue) + return newvalue + +usernumber = int(input(("Please insert number: ")) +userbase = int(input("Please insert the base of the number: ")) +convertedvalue = arbitrary(usernumber, userbase) +basechoice = int(input("Please choose a base to convert the number into from 2, 8, 10, 16: ")) +finalvalue = arbitrary_base_converts(convertedvalue, basechoice) +print("Here's the number converted: ", finalvalue) \ No newline at end of file diff --git a/projects/012-days-in-a-month/python/main.py b/projects/012-days-in-a-month/python/main.py index e69de29..7feaa65 100644 --- a/projects/012-days-in-a-month/python/main.py +++ b/projects/012-days-in-a-month/python/main.py @@ -0,0 +1,34 @@ +''' +Write a function called days in a month that determines how many days there are in a particular month. +Your function will take two parameters: + +the month as an integer between 1 and 12 +the year as a four digit integer +Ensure that your function reports the correct number of days in February for leap years. Include a main program that reads a month and year from the user and displays the number of days in that month. +''' + +def days_in_a_month(usermonth, useryear): + if useryear % 400 == 0 and useryear % 100 == 0: + totaldays = 366 + elif useryear % 100 != 0 and useryear % 4 == 0: + totaldays = 366 + else: totaldays = 365 + + if usermonth == 1 or 3 or 5 or 7 or 8 or 10 or 12: + daysinmonth = 31 + return daysinmonth + elif usermonth == 4 or 6 or 9 or 11: + daysinmonth = 30 + return daysinmonth + elif usermonth == 2: + if totaldays == 365: + daysinmonth = 28 + return daysinmonth + else: + daysinmonth = 29 + return daysinmonth + +usermonth = int(input("Please insert a month in integer: ")) +useryear = int(input("Please insert a 4-digit year: ")) +daysinmonth = days_in_a_month(usermonth, useryear) +print("The days in the month, and year, selected are: ", daysinmonth) \ No newline at end of file diff --git a/projects/013-reduce-measures/python/main.py b/projects/013-reduce-measures/python/main.py index e69de29..a3a84bc 100644 --- a/projects/013-reduce-measures/python/main.py +++ b/projects/013-reduce-measures/python/main.py @@ -0,0 +1,35 @@ +''' +Many recipe books still use cups, tablespoons and teaspoons to describe the volumes of ingredients used when cooking or baking. + +While such recipes are easy enough to follow if you have the appropriate measuring cups and spoons, they can be difficult to double, triple or quadruple when cooking Christmas dinner for the entire extended family. + +For example, a recipe that calls for 4 tablespoons of an ingredient requires 16 tablespoons when quadrupled. +However, 16 tablespoons would be better expressed (and easier to measure) as 1 cup. + +Write a function named reduce measures that expresses an imperial volume using the largest units possible. +The function will take the number of units as its first parameter, and the unit of measure (cup, tablespoon or teaspoon) as its second parameter. It will return a string representing the measure using the largest possible units as its only result. + +Example +units: 59 +unit_measure: teaspoons +output: 1 cup, 3 tablespoons, 2 teaspoons +''' + +def reduce_measures(userunits, userunitmeasure): + if userunitmeasure == "teaspoons": + teaspoons = userunits + print("Teaspoons: ", teaspoons) + elif userunitmeasure == "tablespoons": + tablespoons = userunits // 4 + teaspoons = userunits - (4 * tablespoons) + print("Tablespoons: ", tablespoons, "teaspoons: ", teaspoons) + elif userunitmeasure == "cups": + cups = userunits // 16 + newunits = userunits - (16 * cups) + tablespoons = newunits // 4 + teaspoons = newunits - (4 * tablespoons) + print("Cups: ", cups, "tablespoons: ", tablespoons, "teaspoons: ", teaspoons) + +userunits = int(input("Please input how many units you need to create: ")) +userunitmeasure = str(input("Please input the unit measure you want to use (cups, tablespoons, teaspoons): ")) +finalconversion = reduce_measures(userunits, userunitmeasure) diff --git a/projects/014-magic-dates/python/main.py b/projects/014-magic-dates/python/main.py index e69de29..eb5d903 100644 --- a/projects/014-magic-dates/python/main.py +++ b/projects/014-magic-dates/python/main.py @@ -0,0 +1,28 @@ +''' +A magic date is a date where the day multiplied by the month is equal to the two digit year. + +For example, June 10, 1960 is a magic date because June is the sixth month, and 6 times 10 is 60, which is equal to the two digit year. + +Write a function named is magic date that determines whether a date is a magic date. Your function will take two parameters: + +the day as integer +the month as an integer between 1 and 12 +the year as a four digit integer And should return True if the date is a magic date otherwise it should return False. +Use your function to create a main program that finds and displays all the magic dates in the 20th century. +''' + +def magic_date(userday, usermonth, useryear): + magicdaymonth = userday * usermonth + trueyear = useryear[-2:] + if magicdaymonth < 100: + if magicdaymonth == trueyear: + print("The date it's a magic date!") + else: + print("The date it's NOT a magic date!") + else: + print("Error: the sum of the integer inserted ") + +userday = int(input("Please insert day of the month as integer: ")) +usermonth = int(input("Please insert a month as integer: ")) +useryear = int(input("Please insert a year as a 4-digit integer: ")) +finalresult = magic_date(userday, usermonth, useryear) From 31ac897aed7f6b35e65d5b056ae107c85c8e452f Mon Sep 17 00:00:00 2001 From: BloodYce Date: Fri, 18 Oct 2024 00:06:09 +0200 Subject: [PATCH 4/6] solution-m4/015 --- projects/015-time-calculator/python/main.py | 75 +++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/projects/015-time-calculator/python/main.py b/projects/015-time-calculator/python/main.py index e69de29..e6f25af 100644 --- a/projects/015-time-calculator/python/main.py +++ b/projects/015-time-calculator/python/main.py @@ -0,0 +1,75 @@ +''' +Write a function named `add_time` that takes in two required parameters and one optional parameter: +* a start time in the 12-hour clock format (ending in AM or PM) +* a duration time that indicates the number of hours and minutes +* (optional) a starting day of the week, case insensitive + +The function should add the duration time to the start time and return the result. + +If the result will be the next day, it should show `(next day)` after the time. If the result will be more than one day later, it should show `(n days later)` after the time, where "n" is the number of days later. + +If the function is given the optional starting day of the week parameter, then the output should display the day of the week of the result. +The day of the week in the output should appear after the time and before the number of days later. + +Below are some examples of different cases the function should handle. Pay close attention to the spacing and punctuation of the results. +```py +add_time("3:00 PM", "3:10") +# Returns: 6:10 PM + +add_time("11:30 AM", "2:32", "Monday") +# Returns: 2:02 PM, Monday + +add_time("11:43 AM", "00:20") +# Returns: 12:03 PM + +add_time("10:10 PM", "3:30") +# Returns: 1:40 AM (next day) + +add_time("11:43 PM", "24:20", "tueSday") +# Returns: 12:03 AM, Thursday (2 days later) + +add_time("6:30 PM", "205:12") +# Returns: 7:42 AM (9 days later) +``` + +Do not import any Python libraries. Assume that the start times are valid times. The minutes in the duration time will be a whole number less than 60, but the hour can be any whole number.''' + +def add_time(userstarttime, userdurationtime, userstartday): + array = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"} + daycounter = 0 + hourscounter = 0 + + correctstarttime = userstarttime[:-3] + hoursstarttime = correctstarttime[:2] + minutesstarttime = correctstarttime[-2:] + hoursdurationtime = userdurationtime[:2] + minutesdurationtime = userdurationtime[-2:] + + inthoursstarttime = int(hoursstarttime) + intminutesstarttime = int(minutesstarttime) + inthoursdurationtime = int(hoursdurationtime) + intminutesdurationtime = int(minutesdurationtime) + + totalhours = inthoursstarttime + inthoursdurationtime + totalminutes = intminutesstarttime + intminutesdurationtime + + #sistemare i cicli per aggiungere maggiori giorni o ore + if totalminutes >= 60: + newtotalminutes = totalminutes % 24 + hourscounter += 1 + + + if totalhours >= 24: + newtotalhours = totalhours % 24 + daycounter += 1 + + print(newtotalhours, newtotalminutes) + +def main(): + userstarttime = input("Please input a starting time (indicate AM or PM): ") + userdurationtime = input("Please input how many hours and minutes you wanna add: ") + userstartday = input("Starting day in letters: ") + add_time(userstarttime, userdurationtime, userstartday) + +if __name__ == '__main__': + main() \ No newline at end of file From 70dc18513626518526022c3ecae525e0e282c07e Mon Sep 17 00:00:00 2001 From: BloodYce Date: Sat, 19 Oct 2024 18:59:43 +0200 Subject: [PATCH 5/6] Aggiustamenti alla solution-m4/015 --- projects/015-time-calculator/python/main.py | 25 +++++++++++++-------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/projects/015-time-calculator/python/main.py b/projects/015-time-calculator/python/main.py index e6f25af..1a63350 100644 --- a/projects/015-time-calculator/python/main.py +++ b/projects/015-time-calculator/python/main.py @@ -34,7 +34,7 @@ Do not import any Python libraries. Assume that the start times are valid times. The minutes in the duration time will be a whole number less than 60, but the hour can be any whole number.''' -def add_time(userstarttime, userdurationtime, userstartday): +def add_time(userstarttime, userdurationtime): array = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"} daycounter = 0 hourscounter = 0 @@ -53,23 +53,30 @@ def add_time(userstarttime, userdurationtime, userstartday): totalhours = inthoursstarttime + inthoursdurationtime totalminutes = intminutesstarttime + intminutesdurationtime - #sistemare i cicli per aggiungere maggiori giorni o ore +# sistemare il valore newtotalminutes ed il valore newtotalhours if totalminutes >= 60: - newtotalminutes = totalminutes % 24 - hourscounter += 1 - + newtotalminutes = totalminutes % 60 + hourscounter = newtotalminutes // 60 + return newtotalminutes if totalhours >= 24: newtotalhours = totalhours % 24 - daycounter += 1 + daycounter = newtotalminutes // 24 + return newtotalhours + + finalminutes = newtotalminutes + finalhours = newtotalhours + daycounter - print(newtotalhours, newtotalminutes) + if finalhours > 12: + PMfinalhours = finalhours - 12 + print(PMfinalhours, ":", finalminutes, " PM") + else: + print(finalhours, ":", finalminutes, " AM") def main(): userstarttime = input("Please input a starting time (indicate AM or PM): ") userdurationtime = input("Please input how many hours and minutes you wanna add: ") - userstartday = input("Starting day in letters: ") - add_time(userstarttime, userdurationtime, userstartday) + add_time(userstarttime, userdurationtime) if __name__ == '__main__': main() \ No newline at end of file From e963e2e778c939ecff48fa63eb22a5b265d449b8 Mon Sep 17 00:00:00 2001 From: BloodYce Date: Mon, 21 Oct 2024 22:18:27 +0200 Subject: [PATCH 6/6] Esercizio finito solution/m4-015 --- projects/015-time-calculator/python/main.py | 41 +++++++++------------ 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/projects/015-time-calculator/python/main.py b/projects/015-time-calculator/python/main.py index 1a63350..08698a0 100644 --- a/projects/015-time-calculator/python/main.py +++ b/projects/015-time-calculator/python/main.py @@ -34,44 +34,39 @@ Do not import any Python libraries. Assume that the start times are valid times. The minutes in the duration time will be a whole number less than 60, but the hour can be any whole number.''' +import datetime + def add_time(userstarttime, userdurationtime): array = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"} daycounter = 0 hourscounter = 0 - correctstarttime = userstarttime[:-3] - hoursstarttime = correctstarttime[:2] - minutesstarttime = correctstarttime[-2:] - hoursdurationtime = userdurationtime[:2] - minutesdurationtime = userdurationtime[-2:] - - inthoursstarttime = int(hoursstarttime) - intminutesstarttime = int(minutesstarttime) - inthoursdurationtime = int(hoursdurationtime) - intminutesdurationtime = int(minutesdurationtime) + splitteduserstarttime = userstarttime.split() + doublesplitteduserstarttime = splitteduserstarttime[0].split(":") + splitteduserdurationtime = userdurationtime.split(":") - totalhours = inthoursstarttime + inthoursdurationtime - totalminutes = intminutesstarttime + intminutesdurationtime + hoursstarttime = int(doublesplitteduserstarttime[0]) + minutesstarttime = int(doublesplitteduserstarttime[1]) + hoursdurationtime = int(splitteduserdurationtime[0]) + minutesdurationtime = int(splitteduserdurationtime[1]) -# sistemare il valore newtotalminutes ed il valore newtotalhours - if totalminutes >= 60: - newtotalminutes = totalminutes % 60 - hourscounter = newtotalminutes // 60 - return newtotalminutes + totalhours = hoursstarttime + hoursdurationtime + totalminutes = minutesstarttime + minutesdurationtime if totalhours >= 24: newtotalhours = totalhours % 24 - daycounter = newtotalminutes // 24 - return newtotalhours - - finalminutes = newtotalminutes + daycounter = totalhours // 24 + else: + newtotalhours = totalhours + daycounter = 0 + finalhours = newtotalhours + daycounter if finalhours > 12: PMfinalhours = finalhours - 12 - print(PMfinalhours, ":", finalminutes, " PM") + print(f"{PMfinalhours:03d} : {totalminutes:02d} PM") else: - print(finalhours, ":", finalminutes, " AM") + print(f"{finalhours:03d} : {totalminutes:02d} {splitteduserstarttime[1]}") def main(): userstarttime = input("Please input a starting time (indicate AM or PM): ")