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

solved problems 1-11 #14

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
14 changes: 14 additions & 0 deletions solution1.py
Original file line number Diff line number Diff line change
@@ -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)
25 changes: 25 additions & 0 deletions solution10.py
Original file line number Diff line number Diff line change
@@ -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)

26 changes: 26 additions & 0 deletions solution11.py
Original file line number Diff line number Diff line change
@@ -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")

36 changes: 36 additions & 0 deletions solution12.py
Original file line number Diff line number Diff line change
@@ -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}")



34 changes: 34 additions & 0 deletions solution13.py
Original file line number Diff line number Diff line change
@@ -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}")
34 changes: 34 additions & 0 deletions solution14.py
Original file line number Diff line number Diff line change
@@ -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")



42 changes: 42 additions & 0 deletions solution15.py
Original file line number Diff line number Diff line change
@@ -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}")




30 changes: 30 additions & 0 deletions solution16.py
Original file line number Diff line number Diff line change
@@ -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)
15 changes: 15 additions & 0 deletions solution17.py
Original file line number Diff line number Diff line change
@@ -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}")
17 changes: 17 additions & 0 deletions solution18.py
Original file line number Diff line number Diff line change
@@ -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}")
27 changes: 27 additions & 0 deletions solution19.py
Original file line number Diff line number Diff line change
@@ -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)

26 changes: 26 additions & 0 deletions solution2.py
Original file line number Diff line number Diff line change
@@ -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)
23 changes: 23 additions & 0 deletions solution20.py
Original file line number Diff line number Diff line change
@@ -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}")
20 changes: 20 additions & 0 deletions solution21.py
Original file line number Diff line number Diff line change
@@ -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)
Loading