# 6.15  (Financial application: print a tax table)  Listing 4.7, ComputeTax.py, gives a
# program to compute tax. Write a function for computing tax using the following
# header:
# def computeTax (status, taxableIncome ):
# Use this function to write a program that prints a tax table for taxable income from
# $50,000 to $60,000 with intervals of $50 for all four statuses, as follows:
# Taxable Single Married Married Head of
# Income Joint Separate a House
# 50000 8688 6665 8688 7352
# 50050 8700 6673 8700 7365
# ...
# 59950 11175 8158 11175 9840
# 60000 11188 8165 11188 9852
from CH6Module import MyFunctions

print("Taxable\t\tSingle\t\tMarried\t\tMarried\t\tHead of")
print("Income\t\t\t\t\tJoint\t\tSeparate\ta House")

for i in range(50000, 60001, 50):
    print(i, "\t   ", MyFunctions.computeTax(0, i), "\t",
          MyFunctions.computeTax(1, i), "\t", MyFunctions.computeTax(2, i),
          "\t", MyFunctions.computeTax(3, i))
Exemple #2
0
# 6.21 (Math: approximate the square root) There are several techniques for implementing
# the sqrt function in the math module. One such technique is known as the
# Babylonian function. It approximates the square root of a number, n, by repeatedly
# performing a calculation using the following formula:
# nextGuess = (lastGuess + (n / lastGuess)) / 2
# When nextGuess and lastGuess are almost identical, nextGuess is the
# approximated square root. The initial guess can be any positive value (e.g., 1).
# This value will be the starting value for lastGuess. If the difference between
# nextGuess and lastGuess is less than a very small number, such as 0.0001,
# you can claim that nextGuess is the approximated square root of n. If not,
# nextGuess becomes lastGuess and the approximation process continues.
# Implement the following function that returns the square root of n.
from CH6Module import MyFunctions

n = eval(input("Enter a number: "))
print("The square root of", n, "is", MyFunctions.sqrt(n))
# 6.8 (Conversions between Celsius and Fahrenheit) Write a module that contains the
# following two functions:
# # Converts from Celsius to Fahrenheit
# def celsiusToFahrenheit(celsius):
# # Converts from Fahrenheit to Celsius
# def fahrenheitToCelsius(fahrenheit):
# The formulas for the conversion are:
# celsius = (5 / 9) * (fahrenheit – 32)
# fahrenheit = (9 / 5) * celsius + 32
# Write a test program that invokes these functions to display the following tables:
# Celsius Fahrenheit | Fahrenheit Celsius
# 40.0 104.0 | 120.0 48.89
# 39.0 102.2 | 110.0 43.33
# ...
# 32.0 89.6 | 40.0 4.44
# 31.0 87.8 | 30.0 -1.11
from CH6Module import MyFunctions

print("Celsius\t\tFahrenheit  |  Fahrenheit\t\tCelsius")
celsuis = 40
fahr = 120
for i in range(1, 11):
    print(format(celsuis, ".2f"),
          format(" ", "8s"),
          format(MyFunctions.celsiusToFahrenheit(celsuis), ".2f"),
          end="\t|")
    print(format(" ", "3s"), format(fahr, ".2f"), format(" ", "8s"),
          format(MyFunctions.fahrenheitToCelsius(fahr), "-.2f"))
    celsuis -= 1
    fahr -= 10
# 6.27 (Twin primes) Twin primes are a pair of prime numbers that differ by 2. For example,
# 3 and 5, 5 and 7, and 11 and 13 are twin primes. Write a program to find all
# twin primes less than 1,000. Display the output as follows:
# (3, 5)
# (5, 7)...

from CH6Module import MyFunctions

x = 3
while x < 1000:
    if MyFunctions.isPrime(x) and MyFunctions.isPrime(x + 2):
        print("(" + str(x) + ", " + str(x + 2) + ")")

    x += 1
Exemple #5
0
# 6.11 (Financial application: compute commissions) Write a function that computes
# the commission, using the scheme in Exercise 5.39. The header of the function is:
# def computeCommission(salesAmount):
# Write a test program that displays the following table:
# Sales Amount Commission
# 10000 900.0
# 15000 1500.0
# ...
# 95000 11100.0
# 100000 11700.0
from CH6Module import MyFunctions

print("Sales Amount\t\tCommission")

salesAmount = 10000
for i in range(1, 20):
    print(salesAmount, format(" ", "14s"),
          format(MyFunctions.computeCommission(salesAmount), ".1f"))
    salesAmount += 5000
# 6.9 (Conversions between feet and meters) Write a module that contains the following
# two functions:
# # Converts from feet to meters
# def footToMeter(foot):
# # Converts from meters to feet
# def meterToFoot(meter):
# The formulas for the conversion are:
# foot = meter / 0.305
# meter = 0.305 * foot
# Write a test program that invokes these functions to display the following tables:
# Feet Meters | Meters Feet
# 1.0 0.305 | 20.0 66.574
# 2.0 0.610 | 26.0 81.967
# ...
# 9.0 2.745 | 60.0 196.721
# 10.0 3.050 | 66.0 213.115
from CH6Module import MyFunctions

print("Feet\t\tMeters  |  Meters\t\tFeet")
feet = 1
meter = 20
for i in range(1, 11):
    print(format(feet, ".1f"), format(" ", "8s"), format(MyFunctions.footToMeter(feet), ".3f"), end="\t|")
    print(format(" ", "2s"), format(meter, ".1f"), format(" ", "4s"),
          format(MyFunctions.meterToFoot(meter), ".3f"))
    feet += 1
    meter += 6
# 6.10 (Use the isPrime Function) Listing 6.7, PrimeNumberFunction.py, provides the
# isPrime(number) function for testing whether a number is prime. Use this
# function to find the number of prime numbers less than 10,000.
from CH6Module import MyFunctions

count = 0
for i in range(1, 10001):
    if MyFunctions.isPrime(i):
        count += 1

print("The number of primes less than 10,000 is", count)
Exemple #8
0
# 6.24 (Palindromic prime) A palindromic prime is a prime number that is also palindromic.
# For example, 131 is a prime and also a palindromic prime, as are 313 and
# 757. Write a program that displays the first 100 palindromic prime numbers. Display
# 10 numbers per line and align the numbers properly,

from CH6Module import MyFunctions

counter = 1
x = 2
while counter < 101:
    if MyFunctions.isPrime(x) and MyFunctions.isPalindrome(x):
        if counter % 10 != 0:
            print(x, end='  ')
        else:
            print(x)
        counter += 1
    x += 1

# 6.25 (Emirp) An emirp ( prime spelled backward) is a nonpalindromic prime number
# whose reversal is also a prime. For example, both 17 and 71 are prime numbers, so
# 17 and 71 are emirps. Write a program that displays the first 100 emirps. Display
# 10 numbers per line and align the numbers properly,

from CH6Module import MyFunctions

counter = 1
x = 2
while counter < 101:
    if MyFunctions.isPrime(x) and MyFunctions.isPrime(MyFunctions.reverse(x)):
        if counter % 10 != 0:
            print(x, end='  ')
        else:
            print(x)
        counter += 1
    x += 1
# 6.19 (Geometry: point position) Exercise 4.31 shows how to test whether a point is on
# the left side of a directed line, on the right, or on the same line. Write the following
# functions:
# # Return true if point (x2, y2) is on the left side of the
# # directed line from (x0, y0) to (x1, y1)
# def leftOfTheLine(x0, y0, x1, y1, x2, y2):
# # Return true if point (x2, y2) is on the same
# # line from (x0, y0) to (x1, y1)
# def onTheSameLine(x0, y0, x1, y1, x2, y2):
# # Return true if point (x2, y2) is on the
# # line segment from (x0, y0) to (x1, y1)
# def onTheLineSegment(x0, y0, x1, y1, x2, y2):
# Write a program that prompts the user to enter the three points for p0, p1, and p2
# and displays whether p2 is on the left of the line from p0 to p1, on the right, on the
# same line, or on the line segment.
from CH6Module import MyFunctions

x0, y0, x1, y1, x2, y2 = eval(
    input("Enter coordinates for the three points p0, p1, and p2: "))
if MyFunctions.leftOfTheLine(x0, y0, x1, y1, x2, y2):
    print("p2 is on the left side of the line from p0 to p1")

elif MyFunctions.onTheSameLine(x0, y0, x1, y1, x2, y2):
    print("p2 is on the same line from p0 to p1")

elif MyFunctions.onTheLineSegment(x0, y0, x1, y1, x2, y2):
    print("p2 is on the line segment from p0 to p1")
# *6.17 (The MyTriangle module) Create a module named MyTriangle that contains
# the following two functions:
# # Returns true if the sum of any two sides is
# # greater than the third side.
# def isValid(side1, side2, side3):
# # Returns the area of the triangle.
# def area(side1, side2, side3):
# Write a test program that reads three sides for a triangle and computes the area if the
# input is valid. Otherwise, it displays that the input is invalid. The formula for computing
# the area of a triangle is given in Exercise 2.14.
from CH6Module import MyFunctions

s1, s2, s3 = eval(input("Enter three sides in double: "))

if MyFunctions.isValid(s1, s2, s3):
    print("The area of the triangle is", MyFunctions.area(s1, s2, s3))
else:
    print("Input is invalid")
def main():
    print("i\t\t\t  m(i)")
    for i in range(1, 1001, 100):
        print(format(i, ".0f"), "\t\t",
              format(MyFunctions.computePi(i), "-3.4f"))