Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 19 additions & 0 deletions projects/001-taxi-fare/python/main.py
Original file line number Diff line number Diff line change
@@ -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)
13 changes: 13 additions & 0 deletions projects/002-shipping-calculator/python/main.py
Original file line number Diff line number Diff line change
@@ -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.
'''

46 changes: 46 additions & 0 deletions projects/007-capitalize-it/python/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'''
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 = 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(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()
26 changes: 26 additions & 0 deletions projects/010-hexadecimal-and-decimal-digits/python/main.py
Original file line number Diff line number Diff line change
@@ -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)
45 changes: 45 additions & 0 deletions projects/011-arbitrary-base-conversions/python/main.py
Original file line number Diff line number Diff line change
@@ -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)
34 changes: 34 additions & 0 deletions projects/012-days-in-a-month/python/main.py
Original file line number Diff line number Diff line change
@@ -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)
35 changes: 35 additions & 0 deletions projects/013-reduce-measures/python/main.py
Original file line number Diff line number Diff line change
@@ -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)
28 changes: 28 additions & 0 deletions projects/014-magic-dates/python/main.py
Original file line number Diff line number Diff line change
@@ -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)
77 changes: 77 additions & 0 deletions projects/015-time-calculator/python/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
'''
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.'''

import datetime

def add_time(userstarttime, userdurationtime):
array = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}
daycounter = 0
hourscounter = 0

splitteduserstarttime = userstarttime.split()
doublesplitteduserstarttime = splitteduserstarttime[0].split(":")
splitteduserdurationtime = userdurationtime.split(":")

hoursstarttime = int(doublesplitteduserstarttime[0])
minutesstarttime = int(doublesplitteduserstarttime[1])
hoursdurationtime = int(splitteduserdurationtime[0])
minutesdurationtime = int(splitteduserdurationtime[1])

totalhours = hoursstarttime + hoursdurationtime
totalminutes = minutesstarttime + minutesdurationtime

if totalhours >= 24:
newtotalhours = totalhours % 24
daycounter = totalhours // 24
else:
newtotalhours = totalhours
daycounter = 0

finalhours = newtotalhours + daycounter

if finalhours > 12:
PMfinalhours = finalhours - 12
print(f"{PMfinalhours:03d} : {totalminutes:02d} PM")
else:
print(f"{finalhours:03d} : {totalminutes:02d} {splitteduserstarttime[1]}")

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: ")
add_time(userstarttime, userdurationtime)

if __name__ == '__main__':
main()