CSC 120讲解、讲解Computer Programming、Python程序调试、辅导Python室
- 首页 >> Python编程 CSC 120: Introduction to Computer Programming for Non-majors
Spring 2019
Assignment 5
Notes:
For each question create a Python script with the name Question xx
where xx is the question number.
Provide your name and what program does in each script.
Provide necessary comments.
Once you are done, create a folder with the name Assignment-03, put all
your scripts in there, zip it, and then upload the zip file in to Canvas.
Always show that your code works by using few test cases (1-3)
If you are not clear about any of these steps, just ask.
This must be your own work. I have already explained policies regrading
plagiarism and they are in your syllabus.
Question 1 (20 points) A pentagonal number is defined as n(3n–1)/2 for
n = 1, 2, . . ., and so on. Therefore, the first few numbers are 1, 5, 12, 22, . . .
Write a function that returns a pentagonal number for a given n ≥ 1.
Write a test function that uses that to display the first 100 pentagonal
numbers with 10 numbers on each line.
Question 2 (20 points) Write a function that computes the sum of the digits
in a non negative integer. For example, sumDigits(234) returns 9
(2 + 3 + 4). Write a test function with few test cases and show that your
function sumDigits works.
Question 3 (20 points) Write a function with the name reverse, that
reverses a non negative integer passed to it. As an example, it should
returns 456 when you pass 654 as the input.
Write a test function to test your reverse function.
Now, write another function with the name palindrome which takes a non
negative integer and check if it is a palindrome.
Hint: You should use the reverse method you just created.
Write a test function to test your palindrome function.
Question 4 (20 points) Write a function to determine a number is prime.
Write a test function with atleast 5 test cases to test your function.
Question 5 (20 points) π can be estimated using following series.
m(i) = 4(1 +(1)i+12i 1)
Write a method to return m(i) for a given i and write a test function to
that displays the following approximation table
Spring 2019
Assignment 5
Notes:
For each question create a Python script with the name Question xx
where xx is the question number.
Provide your name and what program does in each script.
Provide necessary comments.
Once you are done, create a folder with the name Assignment-03, put all
your scripts in there, zip it, and then upload the zip file in to Canvas.
Always show that your code works by using few test cases (1-3)
If you are not clear about any of these steps, just ask.
This must be your own work. I have already explained policies regrading
plagiarism and they are in your syllabus.
Question 1 (20 points) A pentagonal number is defined as n(3n–1)/2 for
n = 1, 2, . . ., and so on. Therefore, the first few numbers are 1, 5, 12, 22, . . .
Write a function that returns a pentagonal number for a given n ≥ 1.
Write a test function that uses that to display the first 100 pentagonal
numbers with 10 numbers on each line.
Question 2 (20 points) Write a function that computes the sum of the digits
in a non negative integer. For example, sumDigits(234) returns 9
(2 + 3 + 4). Write a test function with few test cases and show that your
function sumDigits works.
Question 3 (20 points) Write a function with the name reverse, that
reverses a non negative integer passed to it. As an example, it should
returns 456 when you pass 654 as the input.
Write a test function to test your reverse function.
Now, write another function with the name palindrome which takes a non
negative integer and check if it is a palindrome.
Hint: You should use the reverse method you just created.
Write a test function to test your palindrome function.
Question 4 (20 points) Write a function to determine a number is prime.
Write a test function with atleast 5 test cases to test your function.
Question 5 (20 points) π can be estimated using following series.
m(i) = 4(1 +(1)i+12i 1)
Write a method to return m(i) for a given i and write a test function to
that displays the following approximation table