From 12c4483c12f40d9c48b4b2f614f12a15f2632607 Mon Sep 17 00:00:00 2001 From: WitchlyBulb Date: Wed, 8 May 2024 11:17:28 -0400 Subject: [PATCH 1/4] solved problems 1-11 --- solution1.py | 14 ++++++++++++++ solution10.py | 25 +++++++++++++++++++++++++ solution11.py | 26 ++++++++++++++++++++++++++ solution2.py | 26 ++++++++++++++++++++++++++ solution3.py | 29 +++++++++++++++++++++++++++++ solution4.py | 29 +++++++++++++++++++++++++++++ solution5.py | 30 ++++++++++++++++++++++++++++++ solution6.py | 18 ++++++++++++++++++ solution7.py | 25 +++++++++++++++++++++++++ solution8.py | 29 +++++++++++++++++++++++++++++ solution9.py | 30 ++++++++++++++++++++++++++++++ 11 files changed, 281 insertions(+) create mode 100644 solution1.py create mode 100644 solution10.py create mode 100644 solution11.py create mode 100644 solution2.py create mode 100644 solution3.py create mode 100644 solution4.py create mode 100644 solution5.py create mode 100644 solution6.py create mode 100644 solution7.py create mode 100644 solution8.py create mode 100644 solution9.py diff --git a/solution1.py b/solution1.py new file mode 100644 index 0000000..462e72a --- /dev/null +++ b/solution1.py @@ -0,0 +1,14 @@ +"""ex.1 +Create two variables a and b, and initially set them each to a different number. +Write a program that swaps both values.""" + +def input_variables(a, b): + temporary_c = a #assign the value of a to the temporary variable c + a = b #assign the value of b to a, now a has b's value + b = temporary_c #now b has a's original value + return (a, b) + +x = 7 +y = 8 +result = input_variables(x, y) +print("The swapped values are", result) diff --git a/solution10.py b/solution10.py new file mode 100644 index 0000000..6b7ff6a --- /dev/null +++ b/solution10.py @@ -0,0 +1,25 @@ +"""ex.10 +Create a program that prints the last digit of a given integer + +Create a program that: +1) Reads the integer +2) Prints the last digit +Warning! Do not use the programming language MAGIC. After you complete the exercise feel free to do so. +Warning! Don't try to convert the number into string etc. +Warning! For this problem it's ok after spending some time to look for the solution.""" + + +number = int(input("enter any integer: ")) + +if number in range(0, 9): + print (number) +elif number > 9: + while number >9: + number = number - 10 + print (number) +else: + while number <-10: + number = number + 10 + number *= -1 + print (number) + diff --git a/solution11.py b/solution11.py new file mode 100644 index 0000000..02a5f9e --- /dev/null +++ b/solution11.py @@ -0,0 +1,26 @@ +""" +ex.11 +You have started working and you are wondering how many things you can buy with the money +you've earned. A PS4 costs 200$, a Samsung phone 900$, a TV 500$, a game skin 9.99$ + +Create a program: +* Notice that you can't but half TV or 1/4 of PS4. +* Reads how many hours you've worked +* Reads your hourly income +* Prints how many items you can buy +* Output: "I can buy 4 PS4, 1 Samsung, 3 TV, 80 game skin" +""" + +def calculate_income(hours_worked, hourly_pay): + money_earned = hours_worked * hourly_pay + number_PS4 = money_earned // 200 + number_phone = money_earned // 900 + number_TV = money_earned // 500 + number_gameskin = money_earned // 9.99 + return number_PS4, number_phone, number_TV, number_gameskin + +hours = 40 +pay_per_hour = 100 +ps4, ph, tv, gs = calculate_income(hours, pay_per_hour) +print(f"I can buy {ps4} PS4, {ph} Samsung phones, {tv}TVs and {gs} game skins") + diff --git a/solution2.py b/solution2.py new file mode 100644 index 0000000..d653b22 --- /dev/null +++ b/solution2.py @@ -0,0 +1,26 @@ +""" +# ex.2 +It's the end of the semester and you got your grades from three classes: Geometry, Algebra, and Physics. + +Create a program that: +1) Reads the grades of these 3 classes (Grades range from 0 - 10) +2) Calculate the average of your grades + +* Example: Geometry = 6, Algebra = 7, Physics = 8 +* Output: average_score = 7 +""" + +def subjects_dict(dict1): + sum1 = sum(dict1.values()) + average = sum1/3 + return average + +geo = 6 +alg = 7 +phy = 8 + +dict2 = {"Geometry" : geo, "Algebra" : alg, "Physics" : phy} + +result = subjects_dict(dict2) +print(f"Your scores are Geometry: {geo}, Algebra: {alg}, Physics: {phy}") +print("Your average score is", result) diff --git a/solution3.py b/solution3.py new file mode 100644 index 0000000..ac59f45 --- /dev/null +++ b/solution3.py @@ -0,0 +1,29 @@ +""" ex.3 +You've bought a Bitcoin and now it's on the rise!!! + +Create a program that: +1) Reads the value of the bitcoin at the time of purchase +2) Reads the percentage of increase (or decrease) +3) Prints the total value of your bitcoin +4) Prints the increase or decrease value + +* Example: bitcoin_value = 10000, bitcoin_increase = 10 +* Output: total_bitcoin_value = 11000, bitcoin_increase_value = 1000 +""" + +def bitcoin_value(value_of_bitcoin_at_purchase, percentage_change, type_of_change): + change_in_value = (percentage_change/100) * value_of_bitcoin_at_purchase + if type_of_change == "increase": + total_value_of_bitcoin = value_of_bitcoin_at_purchase + change_in_value + else: + total_value_of_bitcoin = value_of_bitcoin_at_purchase - change_in_value + return total_value_of_bitcoin, change_in_value, type_of_change + +bought_bitcoin_at = 20000 +percentage_diff = 10 +increase_or_decrease = "decrease" +x, y, z = bitcoin_value(bought_bitcoin_at, percentage_diff, increase_or_decrease) + +print(f"The total value of your Bitcoin is ${x}") + +print(f"The {z} in Bitcoin since last year is ${y}") diff --git a/solution4.py b/solution4.py new file mode 100644 index 0000000..85fc029 --- /dev/null +++ b/solution4.py @@ -0,0 +1,29 @@ +"""ex.4 +You now own some property and you want to calculate the total area of the property. + +Create a program that: +1) Reads the width and height +2) Prints the area + +* Example: width = 5, height = 2 +* Output: total_area = 10 """ + +def dimensions_of_property(shape, base, height): + if shape == "Triangle": + area = 0.5 * base * height + elif shape == "Rectangle": + area = base * height + elif shape == "Square": + area = base * height + elif shape == "Parallelogram": + area = base * height + else: + area = print("System cannot determine area for that shape") + return area + +property_shape = "Rectangle" +b = 7 +h = 8 + +result = dimensions_of_property(property_shape, b, h) +print(result) \ No newline at end of file diff --git a/solution5.py b/solution5.py new file mode 100644 index 0000000..410972e --- /dev/null +++ b/solution5.py @@ -0,0 +1,30 @@ +""" +# ex.5 +You are interested in buying crypto-currencies. You want to check the current amount of money you have and see how many coins you can buy in Bitcoin, Ethereum, and Litecoin. + +Create a program that: +1) Reads the total amount of money you have +2) Reads the price of Bitcoin, Ethereum, and Litecoin +3) Prints the amount of Bitcoin, Ethereum, and Litecoin you can buy + +* Example: money = 100, bitcoin_price = 50, ethereum_price = 25, litecoin_price = 10 +* Output: "With 100$ you can buy: 2 Bitcoins, 4 Ethereum, and 10 Litecoins" + +(Warning! Τhe prices are made up for exercise purposes) """ + +def stocks_you_can_buy(money, bitcoin, ethereum, litecoin): + if money != 0: + num_bitcoin = money // bitcoin + num_ethereum = money // ethereum + num_litecoin = money // litecoin + else: + None + return num_bitcoin, num_ethereum, num_litecoin + +available_money = 800 +price_bitcoin = 80 +price_ethereum = 30 +price_litecoin = 20 + +b, e, l = stocks_you_can_buy(available_money, price_bitcoin, price_ethereum, price_litecoin) +print(f"With ${available_money}, you can buy {b} Bitcoins, {e} ethereums, and {l} litecoins.") \ No newline at end of file diff --git a/solution6.py b/solution6.py new file mode 100644 index 0000000..0cd664d --- /dev/null +++ b/solution6.py @@ -0,0 +1,18 @@ +"""You are interested in buying a new laptop. You check the price and you see that the price is 300$ without the 10% tax. + +Create a program that: +1) Reads the price of the laptop +2) Reads the tax percentage +3) Prints the total amount + +* Output: "The total price of the laptop is 330$" """ + +def calculate_total(price_of_laptop, tax_percent): + tax_value = (tax_percent/100) * price_of_laptop + total_value = price_of_laptop + tax_value + return total_value + +laptop = 1000 +tax = 6.5 +total = calculate_total(laptop, tax) +print("The total price of the laptop is ",total) diff --git a/solution7.py b/solution7.py new file mode 100644 index 0000000..7ca1e60 --- /dev/null +++ b/solution7.py @@ -0,0 +1,25 @@ + +""" ex.7 +In a company the monthly salary of an employee is calculated by: the minimum wage 400$ per month, plus 20$ multiplied +by the number of years employed, plus 30$ for each child they have. + +Create a program that: +1) Reads the number of years employed +2) Reads the number of children the employee has +3) Prints the total amount of salary the employee makes + +* Output: "The total amount is 560$. 400$ minimum wage + 100$ for 5 years experience + 60$ for 2 kids" """ + +def calculate_salary(years_employed, num_children): + minimum_wage = 400 + money_for_experience = years_employed * 20 + benefits_for_children = num_children * 30 + monthly_salary = minimum_wage + money_for_experience + benefits_for_children + return monthly_salary, money_for_experience, benefits_for_children + +years = 20 +children = 3 +s, y, c = calculate_salary(years, children) +print(f"The total amount is ${s}. 400$ minimum wage + {y}$ for 20 years experience + {c}$ for 3 kids") + + diff --git a/solution8.py b/solution8.py new file mode 100644 index 0000000..32c03c0 --- /dev/null +++ b/solution8.py @@ -0,0 +1,29 @@ +"""ex.8 +The exercise is almost identical to a previous exercise with a minor change. In a company the monthly salary of an employee is calculated by minimum wage + 400$ per month, plus 20$ multiply by the employment years, plus 30$ for each employee kid, plus 100$ if the employee didn't miss 1 day of work. + +Create a program that: +* Reads the employment years +* Reads the number of each employee kids +* Prints the total amount the employee must take +* Output: "The total amount is 660$, 400$ minimum wage + 100$ for 5 years experience + 60$ for 2 kids + 100$ for not missing a day at work" +""" + +def calculate_salary(years_employed, num_children, missed_days): + minimum_wage = 400 + money_for_experience = years_employed * 20 + benefits_for_children = num_children * 30 + monthly_salary = minimum_wage + money_for_experience + benefits_for_children + bonus = 100 + if missed_days == 0: + monthly_salary += bonus + return monthly_salary, money_for_experience, benefits_for_children + +years = 20 +children = 3 +holidays = 0 +s, y, c = calculate_salary(years, children, holidays) +if holidays == 0: + print(f"The total amount is ${s}. 400$ minimum wage + {y}$ for 20 years experience + {c}$ for 3 kids + $100 for not missing a day at work") +else: + print(f"The total amount is ${s}. 400$ minimum wage + {y}$ for 20 years experience + {c}$ for 3 kids") diff --git a/solution9.py b/solution9.py new file mode 100644 index 0000000..9d95541 --- /dev/null +++ b/solution9.py @@ -0,0 +1,30 @@ +""" +ex.9 +The exercise is almost identical to a previous exercise with a minor change. It's the end of the semester and you got your marks from, Geometry, Algebra, + Physics classes. If the average score is 7 and above print "Good job!", if the average score is between 6 and 4 print "You need to work harder!", + if the average score is below 4 print "Failed, you really need to work harder!". + +Create a program that: +* Reads the values of these 3 lessons +* Calculate the average of your grades +* Example: Geometry = 6, Algebra = 7, Physics = 8 +* Output: Your average score is 7, Good job!" +""" + +def subjects_scores(geometry, algebra, physics): + sum1 = geometry + algebra + physics + average = sum1/3 + return average + +geo = 6 +alg = 2 +phy = 3 + +avg_score = subjects_scores(geo, alg, phy) + +if avg_score >= 7: + print("Good job!") +elif 6 >= avg_score >= 4: + print("You need to work harder!") +else: + print("Failed, you really need to work harder!") From 3f351d3f07a00c931ccacbd5bda683ba67f99e34 Mon Sep 17 00:00:00 2001 From: WitchlyBulb Date: Thu, 9 May 2024 17:36:25 -0400 Subject: [PATCH 2/4] Adding solutions 12-14 --- solution12.py | 36 ++++++++++++++++++++++++++++++++++++ solution13.py | 34 ++++++++++++++++++++++++++++++++++ solution14.py | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 solution12.py create mode 100644 solution13.py create mode 100644 solution14.py diff --git a/solution12.py b/solution12.py new file mode 100644 index 0000000..23dbf91 --- /dev/null +++ b/solution12.py @@ -0,0 +1,36 @@ +"""# ex.12 +You've consumed X amount of Mbps on Wikipedia and Y amount of Mbps on memes. The cost of visiting Wikipedia is 0,10$ per Mb +and the cost for watching memes is 0,05$ per Mb. If total consumption is more than 100$ print "Too much consumption". +If watching meme consumption is greater than reading wikipedia consumption print "WOW MANY MEMES", "SUCH LOL"(in new line). + +Create a program that: +* Reads X(wikipedia Mb consupmtion) and Y(watching meme Mb consumption) +* Calculates the total consumption +* If total consumption greater than 100$ print proper message +* If watching meme consumption is greater than reading wikipedia articles print proper messages + +Warning! For the greater meme consumption you will use one print statement and the output must be in seperate lines +""" + +def calculate_consumption(X_wikipedia, Y_memes, time_in_sec_wiki, time_in_sec_memes): + consumption_wiki = (X_wikipedia/0.10) * time_in_sec_wiki + consumption_memes = (Y_memes/0.05) * time_in_sec_memes + total_consumption = consumption_wiki + consumption_memes + if total_consumption > 100: + print("Too much consumption") + if consumption_memes > consumption_wiki: + print("WOW MANY MEMES\nSUCH LOL") + return consumption_wiki, consumption_memes, total_consumption + + + + +X = 400 +Y = 300 +time_wiki = 60 +time_memes = 50 +wiki, meme, con = calculate_consumption(X, Y, time_wiki, time_memes) +print(f"The cost of your wikipedia usage is {wiki} and the cost of your memes usage is {meme} your total consumption is {con}") + + + diff --git a/solution13.py b/solution13.py new file mode 100644 index 0000000..971988f --- /dev/null +++ b/solution13.py @@ -0,0 +1,34 @@ +"""# ex.13 +An internet cafe has 2 ways of charging. If the user is a member pays 2$/hour, Else the user pays 5$. Find if someone is a member or not +and calculate the price based on how many hours the user spend. If the user is a member the tax is 10% else the tax is 20%. + +Create a program that: +* Reads how many hours the user spend +* Check if is a member +* Add the proper tax fee +* Print the total amount the user has to pay +* Output: "The user is a member stayed 2 hours for 2$/hour plus the 10% the total amount is 4.4$" +""" + +def calculate_payment (hours_used, membership): + if membership == "yes": + basic_payment = hours_used * 2 #hours spent * $2 for member + total_payment = basic_payment * 1.1 #basic payment + 10% tax for member + total_payment = round(total_payment, 2) + description = "member" + rate = "$2/hour" + tax = "10%" + else: + basic_payment = hours_used * 5 #hours spent * $5 for non-member + total_payment = basic_payment * 1.2 #basic payment + 20% tax for non-member + total_payment = round(total_payment, 2) + description = "non-member" + rate = "$5/hour" + tax = "20%" + return total_payment, description, rate, tax + + +member = "no" +hours = 10 +money_owed, desc, hourly_rate, taxes = calculate_payment (hours, member) +print(f"The user is a {desc} and stayed {hours} hours for {hourly_rate} plus the {taxes} the total amount is {money_owed}") diff --git a/solution14.py b/solution14.py new file mode 100644 index 0000000..9d160ff --- /dev/null +++ b/solution14.py @@ -0,0 +1,34 @@ +"""# ex.14 +You want to buy something from Amazon. The seller charges different prices for shipping cost based on location. + For US it's 5$, for Europe it's 7$, for Canada it's 3$, for other places it's 9$ + +Create a program that: +* Reads the cost of the product +* Reads your location +* Print the amount of money you have to pay +* Ouput: "You have to pay 23$, 20$ for the product and 3$ for shipping cost" +""" + + +def total_cost(item_cost, location): + if location == "US": + shipping_fee = "$5" + total_due = item_cost + 5 + elif location == "Europe": + shipping_fee = "$7" + total_due = item_cost + 7 + elif location == "Canada": + shipping_fee = "$3" + total_due = item_cost + 3 + else: + shipping_fee = "$9" + total_due = item_cost + 9 + return total_due, shipping_fee + +item_costs = 10 +place = "US" +pay, shipping = total_cost(item_costs, place) +print(f"You have to pay ${pay}, {item_costs}$ for the product and {shipping} for shipping cost") + + + \ No newline at end of file From 511df09159eb780686fdf5bebb8fde6d3339f532 Mon Sep 17 00:00:00 2001 From: WitchlyBulb Date: Fri, 10 May 2024 08:33:37 -0400 Subject: [PATCH 3/4] solved problems 15-24 --- solution15.py | 42 ++++++++++++++++++++++++++++++++++++++++++ solution16.py | 30 ++++++++++++++++++++++++++++++ solution17.py | 15 +++++++++++++++ solution18.py | 17 +++++++++++++++++ solution19.py | 27 +++++++++++++++++++++++++++ solution20.py | 23 +++++++++++++++++++++++ solution21.py | 20 ++++++++++++++++++++ solution22.py | 25 +++++++++++++++++++++++++ solution23.py | 24 ++++++++++++++++++++++++ solution24.py | 23 +++++++++++++++++++++++ 10 files changed, 246 insertions(+) create mode 100644 solution15.py create mode 100644 solution16.py create mode 100644 solution17.py create mode 100644 solution18.py create mode 100644 solution19.py create mode 100644 solution20.py create mode 100644 solution21.py create mode 100644 solution22.py create mode 100644 solution23.py create mode 100644 solution24.py diff --git a/solution15.py b/solution15.py new file mode 100644 index 0000000..4f0c238 --- /dev/null +++ b/solution15.py @@ -0,0 +1,42 @@ +""" +# ex.15 +A cell phone company has the following billing policy + +| | Fixed cost 25$ | +|---------|--------------| +| Call duration(in seconds)| Charge($/per second)| +| 1-500 | 0,01 | +| 501-800 | 0,008 | +| 801+ | 0,005 | + +Create a program that: +* Reads how many seconds was the calls duration +* Calculates the monthly bill for the subscriber +* Prints the total amount +* Output: "total amount: 48$" + +#### Notice that that the charge for the first 500 seconds it's 0,01$ then for the next 501 to 800 seconds + it's 0,008 and then it's 0,005$ + """ + +def calculate_montly_bill (calls_duration): + fixed_cost = 25 + if calls_duration == 0: + bill = fixed_cost + elif calls_duration in range(1, 500): + bill = (calls_duration * 0.01) + fixed_cost + elif calls_duration in range(501, 800): + diff1 = calls_duration - 500 #where diff1 represents the next tier 501-800 sec + bill = (diff1 * 0.008) + (500 * 0.01) + fixed_cost + else: + diff2 = calls_duration - 800 + bill = (diff2 * 0.005) + (300 * 0.008) + (500 * 0.01) + fixed_cost + return bill + +seconds_duration = 800 +amount = calculate_montly_bill (seconds_duration) +print(f"Total amount {amount}") + + + + diff --git a/solution16.py b/solution16.py new file mode 100644 index 0000000..71295d4 --- /dev/null +++ b/solution16.py @@ -0,0 +1,30 @@ +""" +# ex.16 +A fast food chain has these meals + +| Meal | Price | +|---------|--------------| +| Burger | 5$ | +| Pizza | 3$ | +| Hot Dog | 1,5$ | + +Create a program that: +* Reads the meal the customer wants +* Prints the cost of the meal +* Input example: "Hot Dog" +* Output: "Hot Dog 1,50$" +""" + +#Solution using dictionary: + +def menu_price(menu_dict, meal_item): + if meal_item in menu_dict.keys(): + return meal_item, menu_dict[meal_item] + else: + return("This item is not in the menu") + +dict_meals = {"Burger": "5$","Pizza": "3$", "Hot Dog": "1.50$"} +food = "Caterpillar" + +cost = menu_price(dict_meals, food) +print(cost) diff --git a/solution17.py b/solution17.py new file mode 100644 index 0000000..e46a000 --- /dev/null +++ b/solution17.py @@ -0,0 +1,15 @@ +""" +# ex.17 +Create a program that: +* Calculates the sum of 1+2+3+4...+98+99 +* Prints the sum of 1+2+3+4...+98+99 +* Output: "The sum is 4950" +""" + +def sum_of_natural_numbers(last_number): + sum = last_number* (1 + last_number) / 2 + return int(sum) + +num = 99 +result = sum_of_natural_numbers(num) +print(f"The sum is {result}") diff --git a/solution18.py b/solution18.py new file mode 100644 index 0000000..5720a37 --- /dev/null +++ b/solution18.py @@ -0,0 +1,17 @@ +""" +# ex.18 +* Calculates the sum of 1+3+5+7...+99+101 +* Prints the sum of 1+3+5+7...+99+101 +* Output: "The sum is 2601" +""" + +def arithmetic_series(first_term, last_term, difference): + number_term = (last_term - first_term)/difference + 1 + sum = (number_term)/2 * (first_term + last_term) + return int(sum) + +a = 1 +an = 101 +d = 2 +result = arithmetic_series(a, an, d) +print(f"The sum is {result}") \ No newline at end of file diff --git a/solution19.py b/solution19.py new file mode 100644 index 0000000..4ff478f --- /dev/null +++ b/solution19.py @@ -0,0 +1,27 @@ +""" +# ex.19 +Create a program that reads a number that you want to get the sum until that number + +Create a program that: +* Reads the number you want to sum +* Calculates the sum of 1+2+3+4...+98+99+n +* Prints the sum of 1+2+3+4...+98+99+n +* Input example: 100 +* Output: "The sum is 5050" + +###### Try the program with a very very big number, [if it takes too long check this +out](http://mathcentral.uregina.ca/qq/database/qq.02.06/jo1.html) +""" +from timeit import default_timer as timer + +start = timer() +def sum_of_natural_numbers(last_number): + sum = last_number* (1 + last_number) / 2 + return int(sum) + +num = 2000000202020202020202020202 +result = sum_of_natural_numbers(num) +print(f"The sum is {result}") +stop = timer() +print(stop-start) + diff --git a/solution20.py b/solution20.py new file mode 100644 index 0000000..fa56e8a --- /dev/null +++ b/solution20.py @@ -0,0 +1,23 @@ +""" +# ex.20 +Create a program that reads a number that you want to get the sum until that number and +then calculate the averge of these numbers + +Create a program that: +* Reads the number you want to sum +* Calculates the sum of 1+2+3+4...+98+99+n +* Calculates the average of the sum 1+2+3+4...+98+99+n +* Input example: 100 +* Output: "The average is 50.5" +""" + + +def sum_avg_of_natural_numbers(last_number, n): + sum = last_number* (1 + last_number) / 2 + avg = (sum + n)/2 + return int(avg) + +num = 99 +x = 100 +result = sum_avg_of_natural_numbers(num, x) +print(f"The average is {result}") \ No newline at end of file diff --git a/solution21.py b/solution21.py new file mode 100644 index 0000000..964a228 --- /dev/null +++ b/solution21.py @@ -0,0 +1,20 @@ +""" +# ex.21 +Create a program that reads 5 numbers and find the average of these numbers + +Create a program that: +* Reads the 5 numbers you want +* Calculates the average of these numbers +* Input example: 4, 6, 1, 4, 9 +* Ouput: "the average is 4.8" +""" + +def list_of_numbers(list1): + sum_list1 = sum(list1) + length = len(list1) + avg = sum_list1/length + return int(avg) + +list2 = [1, 2, 3, 4, 5] +average = list_of_numbers(list2) +print("The average is", average) diff --git a/solution22.py b/solution22.py new file mode 100644 index 0000000..114e42b --- /dev/null +++ b/solution22.py @@ -0,0 +1,25 @@ +""" +# ex.22 +Create a program that reads 5 numbers and prints if the number is negative or positive + +Create a program that: +* Reads the 5 numbers you want +* Print if a number is negative or positive +* Input example: 4, 6, -11, -4, 9 +* Ouput: "Positive", "Positive", "Negative", "Negative", "Positive", +""" + +def sign_of_number(list1): + for num in list1: + if num > 0: + x = print ("Positive") + elif num < 0: + x = print ("Negative") + else: + x = print ("Zero") + + +list2 = [-2, 4, 5, 0, -5.9] +number_sign = sign_of_number(list2) + + diff --git a/solution23.py b/solution23.py new file mode 100644 index 0000000..e7b5a9b --- /dev/null +++ b/solution23.py @@ -0,0 +1,24 @@ +""" +# ex.23 +Create a program that reads numbers and sum them until the user inputs a negative value + +Create a program that: +* Reads numbers +* Sum them +* Prints the sum +* Input example: 5, 9, 3, 0, 2, 0, 4, -7 +* Output: "The sum is 23" +""" + + +def add_until_negative(list1): + sum = 0 + for num in list1: + if num < 0: + break + sum += num + return sum + +list_of_given_numbers = [5, 9, 3, 0, 2, 0, 4, -7] +result = add_until_negative(list_of_given_numbers) +print(result) diff --git a/solution24.py b/solution24.py new file mode 100644 index 0000000..8017254 --- /dev/null +++ b/solution24.py @@ -0,0 +1,23 @@ +""" +# ex.24 +Create a program that reads numbers and sum them until the user inputs a negative value or zero value + +Create a program that: +* Reads numbers +* Sum them +* Prints the sum +* Input example: 5, 9, 3, 7, 0 +* Output: "The sum is 24" +""" + +def sum_of_given_numbers(list_numbers): + sum = 0 + for num in list_numbers: + if num <= 0: + break + sum += num + return sum + +given_numbers = [1, 5, 5.8, 9, 0, -3, -5, 8] +result = sum_of_given_numbers(given_numbers) +print(result) From 5d725adfa6611def9fdbd65ca3e2a53d3b7478eb Mon Sep 17 00:00:00 2001 From: WitchlyBulb Date: Tue, 14 May 2024 06:56:37 -0400 Subject: [PATCH 4/4] solutions 25 to 28 --- solution25.py | 39 +++++++++++++++++++++++++++++++++++++ solution26.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ solution27.py | 39 +++++++++++++++++++++++++++++++++++++ solution28.py | 26 +++++++++++++++++++++++++ 4 files changed, 157 insertions(+) create mode 100644 solution25.py create mode 100644 solution26.py create mode 100644 solution27.py create mode 100644 solution28.py diff --git a/solution25.py b/solution25.py new file mode 100644 index 0000000..ecfb8b1 --- /dev/null +++ b/solution25.py @@ -0,0 +1,39 @@ +""" +# ex.25 +You start flipping a coin, count and print how many times the result was head or tails until you enter the word +"stop". Then find and print the percentage of how many head or tails was the result. + +Create a program that: +* Reads if the flipped coin was head or tails +* If the value is "stop", print proper message and quit program +* While value not "stop", count the result +* Print the proper message +* Calculates the percentage of head and tails +* Prints the proper message +* Input: "head", "tails", "tails", "tails", "head", "head", "tails", "tails", "tails", "head" +* Ouput: "Head won 4 times and tails won 6 times" +* Output: "40% Head, 60% Tails" +""" + +def read_flip(flip_result_list): + count_heads = 0 + count_tails = 0 + for item in flip_result_list: + if item == "head": + count_heads += 1 + elif item == "tails": + count_tails += 1 + else: + item == "stop" + break + total_flips = count_heads + count_tails + percentage_heads = (count_heads/total_flips) * 100 + percentage_tails = (count_tails/total_flips) * 100 + return count_heads, count_tails, percentage_heads, percentage_tails, total_flips + +list_flips = ["head", "tails", "tails", "tails", "head", "head", "tails", "tails", "tails", "head", "stop"] +heads, tails, perc_heads, perc_tails, total = read_flip(list_flips) +print(f"The coin flipping was stopped after {total} flips") +print(f"Head won {heads} times and tails won {tails} times") +print(f"{perc_heads}% Head, {perc_tails}% tails") + diff --git a/solution26.py b/solution26.py new file mode 100644 index 0000000..c6a0b02 --- /dev/null +++ b/solution26.py @@ -0,0 +1,53 @@ +""" +# ex.26 +-Create a program that read values of apartments you want to rent until the inpute value is 0 or +a negative number. +-You will calculate the average price for rent and how many apartments you've registered. Print +the proper message. +-Then compare values of apartments you want to rent with the avarage price of the apartments +you've registered until you enter 0 or a negative value. +-If the price is above average price print the proper message, else if the price is below average + print the proper message. If the input value is 0 or a negative number, print the proper message and exit. + +Create a program that: +* Reads values until user inputs 0 or a negative value +* Calculates the average price +* Counts how many apartments registered +* Prints the average price and how many apartments registered +* Reads prices and compare with the average price and print proper message +* Input: 234, 764, 123, 654 +* Output: "4 apartments have registed. The average price for rent is 443.75$" +* Input: 500, 200, 350, 450, 0, -7 +* Output: "Above average price", "Above below price", "Above below price", "Above average price", "Quit Program","Quit Program" +""" + +def apartment_values_list(list_values): + total_apartment_rent = 0 + rents_greater_than_zero = 0 + number_of_registered_apts = len(list_values) + list_info_on_rents = [] + for value in list_values: + if value != 0 and value > 0: + total_apartment_rent += value + rents_greater_than_zero += 1 + avg_rent = total_apartment_rent/rents_greater_than_zero + else: + break + for value in list_values: + if value > avg_rent: + response = "Above average price" + elif value < avg_rent and value > 0: + response = "Below average price" + else: + response = "Quit Program" + list_info_on_rents.append(response) + + return number_of_registered_apts, avg_rent, list_info_on_rents + +list_apts = [500, 200, 350, 450, 0, -7] +number, average, info = apartment_values_list(list_apts) +print(f"{number} apartments have registed. The average price for rent is {average}$") +print(info) + + + diff --git a/solution27.py b/solution27.py new file mode 100644 index 0000000..d542bc6 --- /dev/null +++ b/solution27.py @@ -0,0 +1,39 @@ +""" +# ex.27 +Create a program that register a user with a username and a password. Then the user will try to login with the login +credentials. If the user make 3 wrong attempts exit program with proper message. + +Create a program that: +* Reads the username and the password +* Then the user try to login +* If the user makes 3 wrong attempts exit with proper message +""" + +def login_info(username, password): + dict_login_info = {} + dict_login_info[username] = password + return dict_login_info + +ask_username = input("Enter your username: ") +ask_password = input("Enter your password: ") +info_client_dict = login_info(ask_username, ask_password) + +attempts = 0 +max_attempts = 3 + +while attempts < max_attempts: + ask_username_again = input("Enter your username: ") + ask_password_again = input("Enter your password: ") + + if ask_username_again in info_client_dict and info_client_dict[ask_username_again] == ask_password_again: + print("Welcome back") + break + else: + print("Try again") + attempts += 1 + + +else: + print("Sorry, your login failed") + exit() + \ No newline at end of file diff --git a/solution28.py b/solution28.py new file mode 100644 index 0000000..1f23bfa --- /dev/null +++ b/solution28.py @@ -0,0 +1,26 @@ +""" +# ex.28 +Create a program that asks the user for seconds(integers values) and then start printing the countdown, +when the countdown ends print Go!. If the user enters a non integer value, exit the program with proper message. + +Create a program that: +* Reads integers until user enters a non integer value. +* Print the countdown and at the end print Go! +* Input: 3, -7 +* Output: 3, 2, 1, Go! - Exit Program +""" + +def input_seconds(list1): + output_list = [] + for i in list1: + if type(i) == int: + output_list.append(i) + else: + output_list.append("Go") + break + return output_list + +input_list = [3, 5, 7, 0, -3, 3.4, "sally", 6, 9] +result = input_seconds(input_list) +print(result) +