Sei sulla pagina 1di 13

UNIVERSITY OF DHAKA

Management Information Systems


An assignment
On
Python exercise problems solution

Course Name: Programming Fundamentals


Course Code: MIS-210

Submitted By Submitted To
Rashedul Islam Md. Rakibul Hasan
ID:- 029-12-058 Lecturer

Batch: 12th Dept. of MIS

Dept. of MIS University of Dhaka

University of Dhaka

Submission Date: 30 October,2018


#exercise 6.8:

def main():

print(format("Celsius", "<15s"), format("Fahrenheit", "<15s"), " | ", format("Fahrenheit", "<15s"),


format("Celsius", "<15s"))

print("---------------------------------------------------------------")

celsius = 40

farenheit = 120

i=1

while i <= 10:

print(format(celsius, "<15d"), format(celsiusToFahrenheit(celsius), "<15.2f"), " | ",

format(farenheit, "<15d"), format(fahrenheitToCelsius(farenheit), "<15.2f"))

celsius -= 1

farenheit -= 10

i += 1

def celsiusToFahrenheit(celsius):

return (9.0 / 5.0) * celsius + 32

def fahrenheitToCelsius(fahrenheit):

return (5.0 / 9) * (fahrenheit - 32)


main()

#exercise 6.10:

def main():

count = 0

N = 10000

for number in range(2, N):

if isPrime(number):

count += 1

print("The number of prime number <", 10000, "is", count)

def isPrime(number):

for divisor in range(2, number // 2 + 1):

if number % divisor == 0: # If true, number is not prime

return False # number is not a prime

return True # number is prime

main()

#exercise 6.15:

print("Status\tTaxable Income")

def computeTax(status,taxableIncome):

statement = str(status) + "\t" + str(taxableIncome)

print(statement)

taxAmount = 50000
while taxAmount <= 60000:

computeTax("Single", taxAmount)

taxAmount += 50

marriedIncome = [8688,8700,11175, 11188]

for i in marriedIncome:

computeTax("Married", i)

marriedJointIncome = [6665, 8688, 6673, 8700, 8158, 11175, 8165, 11188]

for i in marriedIncome:

computeTax("Married Joint Separate", i)

headIncome = [ 7352 ,7365 ,9840, 9852]

for i in headIncome:

computeTax("Head of a House", i)

status = eval(input(

"(0-single filer, 1-married jointly,\n" +

"2-married separately, 3-head of household)\n" +

"Enter the filing status: "))

# Prompt the user to enter taxable income

taxableIncome = eval(input("Enter the taxable income: "))

# Compute tax

tax = 0

if status == 0: # Compute tax for single filers

if taxableIncome <= 8350:


tax = taxableIncome * 0.10

elif taxableIncome <= 33950:

tax = 8350 * 0.10 + (income - 8350) * 0.15

elif taxableIncome <= 82250:

tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + \

(taxableIncome - 33950) * 0.25

elif taxableIncome <= 171550:

tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + \

(82250 - 33950) * 0.25 + (income - 82250) * 0.28

elif taxableIncome<= 372950:

tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + \

(82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 + \

(taxableIncome - 171550) * 0.33

else:

tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + \

(82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 + \

(372950 - 171550) * 0.33 + (taxableIncome - 372950) * 0.35;

elif status == 1: # Compute tax for married file jointly

print("Left as exercise")

elif status == 2: # Compute tax for married separately

print("Left as exercise")

elif status == 3: # Compute tax for head of household

print("Left as exercise")

else:

print("Error: invalid status")


sys.exit()

# Display the result

print("Tax is", format(tax, ".2f"))

print(computeTax(status,taxableIncome))

#exercise 6.16:

def main():

for year in range(2000, 2010 + 1):

print(year, "has", numberOfDaysInAYear(year))

def numberOfDaysInAYear(year):

if isLeapYear(year):

return 366

else:

return 365

# Determine if it is a leap year *

def isLeapYear(year):

return year % 400 == 0 or (year % 4 == 0 and year % 100 != 0)

main()

#exercise 6.29:

def isValid(number):

valid = False
if prefixMatched(4 , getPrefix(number, 1)): #visa check

valid |= True # actually its an OR operator used as statement. so True | False => True, boolean OR
logic similar to you might have read V(v symbol) in discrete mathematics or related field.

if prefixMatched(5 , getPrefix(number, 1)): #master check

valid |= True

if prefixMatched(6 , getPrefix(number, 1)): #discovery check

valid |= True

if prefixMatched(37 , getPrefix(number, 2)): #american express check

valid |= True

if getSize(number) < 13 or getSize(number) > 16:

valid &= False

result = sumOfDoubleEvenPlace(number) + sumOfOddPlace(number)

if result % 10 != 0:

valid &= False

return valid

def sumOfDoubleEvenPlace(number):

summation = 0

even = 0

while number > 0:

if even == 1:

summation += getDigit(number % 10)

even ^= 1 # it's a xor operaotr so basically 1 will become 0 and 0 will become 1 what I actually doing
here is making an alternate turn so suppose number is 1213 then so i am traversing from right side (digit
3) its odd place and even = 0, then even = 1 and we are 1 then even = 0 and we are at 2 then even = 1
we are at 1. Now over. So when even = 1 then it is at even place

number //= 10

return summation

def getDigit(number):

number *= 2

return (number % 10) + (number // 10); # suppose number = 8, then number *= 2 will make number =
16, so number % 10 will give 6 and number // 10 will give 1 and we have to return the sum of digits so 1
+ 6 = 7. Now suppose number = 3, so number *= 2 will make number = 6, so number % 10 will give 6 and
number // 10 will give 0 = 0 + 6 = 6, // it is integer division in python.

def sumOfOddPlace(number):

summation = 0

odd = 1

while number > 0:

if odd == 1:

summation += number % 10

odd ^= 1

number //= 10

return summation

def prefixMatched(number, d):

return True if str(number).startswith(d) else False


# Return the number of digits in d

def getSize(d):

return len(str(d))

def getPrefix(number, k):

return str(number) if getSize(number) <= k else str(number)[0:k] # actually it works like ternary
operator so if condition is true then str(number) will execute else str(number)[0:k] will execute.
str(number) just typecasting number to string. getSize(number) returns the no of digits in number. so if
its <= k return the number otherwise return its prefix so for example number= 12345, and k = 2 it will
return 12, but if k = 5 or more it will return 12345 as string. so "12345"

print("Enter the credit card number")

number = int(input())

print ("valid" if isValid(number) else "invalid")

#Listing 6.7:

NUMBER_OF_PRIMES = 50

NUMBER_OF_PRIMES_PER_LINE = 10

count = 0

number = 2

print("The first 50 prime numbers are:")

print("------------------------------------")

while count < NUMBER_OF_PRIMES:

isPrime = True

divisor = 2
while divisor <= number / 2:

if number % divisor == 0:

isPrime = False

break

divisor += 1

if isPrime:

count += 1

print(format(number, '5d'), end = '')

if count % NUMBER_OF_PRIMES_PER_LINE == 0:

print()

number += 1

#listing 6.13:

def printMonth(year, month):

printMonthTitle(year, month)

printMonthBody(year, month)

def printMonthTitle(year, month):

print(" ", getMonthName(month), " ", year)

print("-----------------------------")

print(" Sun Mon Tue Wed Thu Fri Sat")

def printMonthBody(year, month):

startDay = getStartDay(year, month)

numberOfDaysInMonth = getNumberOfDaysInMonth(year, month)


i=0

for i in range(startDay):

print(" ", end = "")

for i in range(1, numberOfDaysInMonth + 1):

print(format(i, '4d'), end = "")

if (i + startDay) % 7 == 0:

print()

def getMonthName(month):

if month == 1:

monthName = "January"

elif month == 2:

monthName = "February"

elif month == 3:

monthName = "March"

elif month == 4:

monthName = "April"

elif month == 5:

monthName = "May"

elif month == 6:

monthName = "June"

elif month == 7:

monthName = "July"

elif month == 8:
monthName = "August"

elif month == 9:

monthName = "September"

elif month == 10:

monthName = "October"

elif month == 11:

monthName = "November"

else:

monthName = "December"

return monthName

def getStartDay(year, month):

START_DAY_FOR_JAN_1_1800 = 3

totalNumberOfDays = getTotalNumberOfDays(year, month)

return (totalNumberOfDays + START_DAY_FOR_JAN_1_1800) % 7

def getTotalNumberOfDays(year, month):

total = 0

for i in range(1800, year):

if isLeapYear(i):

total = total + 366

else:

total = total + 365

for i in range(1, month):


total = total + getNumberOfDaysInMonth(year, i)

return total

def getNumberOfDaysInMonth(year, month):

if (month == 1 or month == 3 or month == 5 or month == 7 or

month == 8 or month == 10 or month == 12):

return 31

if month == 4 or month == 6 or month == 9 or month == 11:

return 30

if month == 2:

return 29 if isLeapYear(year) else 28

return 0

def isLeapYear(year):

return year % 400 == 0 or (year % 4 == 0 and year % 100 != 0)

def main():

year = eval(input("Enter full year (e.g., 2001): "))

month = eval(input(("Enter month as number between 1 and 12: ")))

printMonth(year, month)

Potrebbero piacerti anche