decimal a2

advertisement
The Hong Kong Polytechnic University
COMP1001 – Problem solving in IT
Due Date: 5:00pm, Friday, 25 September 2015
Assignment 2 – for estimating  and a rock-moving game (30 marks)
1. [20 marks] A famous formula for estimating  is given below:
Implement it in Python and print out both the estimated  and the actual value of  in 30 decimal places. The correct outputs are:
You can use print("The estimated pi is: {0:0.30f}.".format(pi)) to print pi in 30 decimal places.
2. [10 marks] (A rock-moving game) Recall that
 A straight road is divided into many equal sections. The distance between every two adjacent sections is 1.
 N piles of rocks are placed in N different sections which may not be consecutive. You are asked to move all the piles to the same section.
 Find the section which will require the smallest total moving distance.
Please implement a function that will accept user’s input on the number of piles of rocks and their positions (i.e., 1, 2, …) and return the
number of the section where all the piles should be moved to. Some codes have already been written for you:
The Hong Kong Polytechnic University
def main():
n = eval(input("Please enter the number of piles of rocks >> "))
rock = [] # create an empty list
# get the positions of the n piles
for i in range(n):
x = eval(input("Enter the section number (>0) for the "+str(i)+"th pile >>"))
rock.append(x) # append a new number to the list
#
# Find and print out the section number here
#
main()
Hint: The built-in functions len() and sorted() are useful.
What to submit?
Python files with the name of COMP1001_A2_Qn_YourStudentID.py, where n is the question number of your programs
How to submit?
 Submit your assignment to blackboard. Other submission methods will not be accepted.
 For late assignments, your marks will be deducted as follows:
o 5 minutes or less (less 20%)
o 6 ~ 60 minutes (less 50%)
o More than 60 minutes (less 100%)
What are the consequences of plagiarism?
Generally if two submissions are similar in 80% or more of their content, they will be treated as plagiarism cases. All the students involved
not only will receive 0 marks for this assignment, they will also lose additional, same number of marks from their total assignment marks.
Moreover, we may report the serious cases to the Departmental Learning and Teaching Committee for further disciplinary actions.
~ End of Assignment 2 ~
Download