0% found this document useful (0 votes)
137 views

Assignment UserDefinedFunctions

The document provides instructions for creating two Python files to define and test functions. File1 defines functions that perform various tasks like calculating pentagonal numbers, summing digits, checking palindromes, sorting numbers, and more. File2 contains test functions that call the functions in File1 and demonstrate their outputs. The document lists 15 application problems that each require defining a function with a specific purpose and header, and writing a test for that function.

Uploaded by

raytonli n
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
137 views

Assignment UserDefinedFunctions

The document provides instructions for creating two Python files to define and test functions. File1 defines functions that perform various tasks like calculating pentagonal numbers, summing digits, checking palindromes, sorting numbers, and more. File2 contains test functions that call the functions in File1 and demonstrate their outputs. The document lists 15 application problems that each require defining a function with a specific purpose and header, and writing a test for that function.

Uploaded by

raytonli n
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1

Writing Functions ICS3U1

Assignment- Creating a Module


Application /15 Thinking /10 Knowledge /7 Communication /7

In this assignment, you will create two python files. Name the two files as follows:
1. YourName_Functions.py
2. YourName_Test.py
File1 contains all the definition of the functions. File2 contains a test function for each function
in file1.
Here is an example.
Let’s do the first progblem from this assignment. In file1, define the function given below.

In file 2 , you will create a test function that will call the above function.

Sample Run

1. Application --------------------------------------------------------- getPentagonalNumber()


(Math: pentagonal numbers) A pentagonal number is defined as n(3n- 1)/2 for
n = 1, 2, 3, 4 and so on. So, the first few numbers are 1, 5, 12, 22, .... Write a
function with the following header that returns a pentagonal number:
def getPentagonalNumber(n):
Write a test program that uses this function to display the first 50 pentagonal numbers
with 6 numbers on each line.
2
Writing Functions ICS3U1

2. Application ------------------------------------------------------------------------ sumDigits()


(Sum the digits in an integer) Write a function that computes the sum of the digits in an
integer. Use the following function header:
def sumDigits(n):

3. Application -----------------------------------------------------------------------isPalindrome()
(Palindrome integer) Write the functions isPalindrome that
# Return the reversal of an integer, e.g. reverse(456)
returns
# 654
def reverse(number):
# Return true if number is a palindrome
def isPalindrome(number):
Use the reverse function to implement isPalindrome. A number is a palindrome if its
reversal is the same as itself. Write a test program that prompts the user to enter an
integer and reports whether the integer is a palindrome.

4. Application --------------------------------------------------------------------------- reverse()


(Display an integer reversed) Write the following function to display an integer in
reverse order:
def reverse(number):
For example, reverse(3456) displays 6543. Write a test program that prompts the
user to enter an integer and displays its reversal.

5. Application --------------------------------------------------------- displaySortedNumbers()


(Sort three numbers) Write the following function to display three numbers in increasing
order:
def displaySortedNumbers(num1, num2, num3):
Write a test program that prompts the user to enter three numbers and invokes the
function to display them in increasing order. Here are some sample runs:

6. Application ----------------------------------------------------------------------displayPattern()
(Display patterns) Write a function to display a pattern as follows:

The function’s header is:


def displayPattern(n):
3
Writing Functions ICS3U1

Write a test program that prompts the user to enter a number n and invokes
displayPattern(n) to display the pattern.

7. Application -------------------------------------------------------- futureInvestmentValue()


(Financial application: compute the future investment value) Write a function that
computes a future investment value at a given interest rate for a specified number of
years. The future investment is determined using the formula in interest formula.
Use the following function header:
def futureInvestmentValue(investmentAmount,
annualInterestRate, years):

Write a test program that prompts the user to enter the investment amount and the annual
interest rate in percent and prints a table that displays the future value for the years from
1 to 30. Here is a sample run:

8. Module ------------------------------------------------------------------------- tempConversion


(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:

9. Module --------------------------------------------------------------------------- MyTriangle


(The MyTriangle module) Create a module named MyTriangle that contains the following
two functions:
# Returns true if the sum of any two sides is
4
Writing Functions ICS3U1

# 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 by the following formula:
Area = [s(s-side1)(s- side2)(s-side3)]0.5 where s is given by the
formula: s = (side1 + side2 + side3)/2.
Here are some sample runs:

10. Application ------------------------------------------------------------------------ printMatrix()


(Display matrix of 0s and 1s) Write a function that displays an n-by-n matrix using the
following header:
def printMatrix(n):
Each element is 0 or 1, which is generated randomly. Write a test program that prompts
the user to enter n and displays an n-by-n matrix. Here is a sample run:

11. Application --------------------------------------------------------------------------- distance()


(Geometry: display angles) Implement the distance formula for computing the distance
between two points.
def distance(x1, y1, x2, y2):

12. Application ---------------------------------------------------------------------- convertMillis()


(Convert milliseconds to hours, minutes, and seconds) Write a function that converts
milliseconds to hours, minutes, and seconds using the following header:
def convertMillis(millis):
5
Writing Functions ICS3U1

The function returns a string as hours:minutes:seconds. For example, convertMillis(5500)


returns the string 0:0:5, convertMillis(100000) returns the string 0:1:40, and
convertMillis(555550000) returns the string 154:19:10.
Write a test program that prompts the user to enter a value for milliseconds and
displays a string in the format of hours:minutes:seconds.

13. Application --------------------------------------------------------------------------- addCoins()


Create an AddCoins application that prompts the user for the number of pennies, nickels,
dimes, and quarters, and then displays their total dollar amount. The AddCoins
application should include a getDollarAmount() method that has four int parameters
corresponding to the number of pennies, nickels, dimes, and quarters, and returns a String
that corresponds to the dollar value of the coins.
Note that the String returned should include the currency sign ($). Application output
should look similar to:

14. Application -------------------------------------------------------------------- PerfectIntegers()


Create a PerfectIntegers application that displays all perfect integers up to 100. A perfect
integer is a number which is equal to the sum of all its factors except itself. For example,
6 is a perfect number because 1 + 2 + 3 = 6. The application should include a boolean
function isPerfect().

15. Application -------------------------------------------------------------------- Menu()

This function will prompt the user for the function Number to run (1-14) and then it will
ask the user to enter the parameter for the function chosen if there are any.
Here is a sample Run
6
Writing Functions ICS3U1

Your program should continuously ask for the function number to run until the user
enters -1.

You might also like