University Singapore University of Social Science (SUSS) Subject ICT 133: Structured Programming

# Question 1

Develop and write complete full Python programs for each part, to perform the following tasks.

• Write a Python program to perform the following
• Prompt a user for the following pieces of information:
• the initial value of an investment, investAmt
• the expected growth rate of investment, i and
• the number of years of investment, n.
• Compute the expected investment amount, expInvestAmt, at the end of n years using the formula:

• Prompt the user for the discount rate, d, The discount rate is used to adjust the value of a future amount of money to the present
• Adjust the expected investment amount, expInvestAmt at year n, by the discount rate, d to today’s value (present value), adjAmt using the formula:

• Display expInvestAmt and 𝑎𝑑𝑗𝐴𝑚𝑡, with 2 decimal digits.

An example run is shown here with user inputs shown underlined:

Enter  investment  amount:  150000

Enter the expected investment growth rate: 5 Enter the discount rate: 7.5

Enter Number of years: 5

Investment  at  end  of  year  5  is  \$191442.23  which  is  worth  \$133350.74 today

• Make a copy of your program for Q1(a). Modify the copy to add checks to ensure that the investment amount is between 1000 and 200000, both rates are not negative, and the year is

Four example runs show the result of various checks. User inputs are shown underlined:

Run 1

Enter  investment  amount:  100

Invalid investment amount. The amount should be between 1000 and 200000

Run 2

Enter  investment  amount:  1000

Enter the expected investment growth rate: -1 Enter the discount rate: 3

Rates cannot be negative

Run 3

Enter  investment  amount:  1000

Enter the expected investment growth rate: 4

Enter the discount rate: 5 Enter number of years: -2

Year  must  be  a  positive  number

Run 4

Enter  investment  amount:  1000

Enter the expected investment growth rate: 4 Enter  the  discount  rate:  5

Enter number of years: 5

Investment  at  end  of  year  5  is  \$1216.65  which  is  worth  \$953.28  today

• Make a copy of your program for Q1(a). Modify the copy to display yearly value of the expected investment amount and adjusted amount for year 0 to year 10.

An example run is shown here with user inputs shown underlined:

Enter  investment  amount:  150000

Enter the expected interest rate: 5

Enter the discount rate: 6.5

 Year Value at Year(\$) Today’s worth(\$) 0 150,000 150,000 1 157,500 147,887 2 165,375 145,804 3 173,644 143,751 4 182,326 141,726 5 191,442 139,730 6 201,014 137,762 7 211,065 135,822 8 221,618 133,909 9 232,699 132,023 10 244,334 130,163
• Define the following functions:
• The function getExpInvestAmt has 3 parameters: the initial investment amount, the growth rate and the year. The function computes and returns the expected investment
• The function getPresentValue has 3 parameters: a future amount, the discount rate and the year. The function computes and returns amount which represents the present value (today’s value).
• Make a copy of your program for Q1(a). Modify the copy to display yearly value of the expected investment amount and adjusted amount until the adjusted amount is less than 50% of the initial investment amount or until year 20. The program calls both functions defined in Q1(d).

Enter  investment  amount:  10000

Enter the expected investment growth rate: 5

Enter the discount rate: 7

 Year Value at Year(\$) Today’s worth(\$) 1 10,500 9,346 2 11,025 8,734 3 11,576 8,163 4 12,155 7,629 5 12,763 7,130 6 13,401 6,663 7 14,071 6,227 8 14,775 5,820 9 15,513 5,439 10 16,289 5,083 11 17,103 4,751 (5 marks)

Copy as text the complete Python programs/functions for each Q1(a) to (e) into the solution word document. Screenshot output obtained from the execution of each program, and paste them into the word document.

## Question 2

A single-player guessing game uses alphabets in place of digits to make a puzzle out of the addition of two single-digit numbers and the answer. The letters a to j are randomly ordered and each digit 0 to 9 are represented by corresponding letters by position. The player solves the puzzle by guessing which digits fit into the puzzle. Study the sample program output and the accompanying notes for the problem requirements. User input is shown underlined.

14

You got it in 5 tries

Express your understanding of modular program design. Develop a program that uses the only collection type str for any collection of values, and allows a player to play ONE (1) game to solve a puzzle.

You may use this function to map digits to letters:

from  random  import  shuffle def  getLetterMap():

stringList  =  list(‘abcdefghij’) shuffle(stringList)

return  ”.join(stringList)

Note that only this function has a list. The rest of your program should work with str and number types only. You will incur a heavy penalty if you use other collections such as list, dictionary or sets.

The required tasks for the program are as follows:

• Puzzle Logic

To generate a random one-digit number.

To use the letter map to transform the digit(s) of a number/answer to letters.

To update the letters in the puzzle with guessed digits.

• Game Logic

To get the two one-digit number random numbers and a transformation mapping.

To transform the numbers and answer for the puzzle.

To display the cheat sheet and puzzle.

To let a player guess the digits for the letters. You may assume user will enter the correct data types.

To track the number of guesses.

To display the outcome of the guess To display the updated puzzle.

To determine whether the puzzle is solved, and if so, to end the game.

Paste as text the code for Q2(a) and (b) together as a complete Python program into the word document. Screenshot output obtained from the execution of the program, and paste them into the word document.

## Question 3

Extend the single-player guessing game to a game for 1 to 4 players. In addition, the players can choose to play with addition puzzles using n-digit numbers where n is a number from 1 to 3

• Players are given points based on their guesses, and they can play repeatedly. Study the sample program output and the accompanying notes to apply for the problem requirements. User input is shown underlined.

Player Name or <ENTER> to end:

There must be at least one player!

Player Name or <ENTER> to end: anna

Player name or <ENTER> to end: anna

Anna is already  is player list

Player name or <ENTER> to end: betty

Player  Name  or  <ENTER>  to an end:  Charlie

Player  name  or  <ENTER>  to  end:  Dennis

Choose the number  of  digits  for  each  number  in  addition  (1-3):  0

Choose  number  of  digits  for  each  number  in  addition  (1-3):  5

Choose  number  of  digits  for  each  number  in  addition  (1-3):  2

4-player Game

The required tasks for the program are as follows:

• Puzzle Logic

To generate a random number of n-digit.

To replace the digits of a number with letters based on the mapping.

To update the letters in the puzzle with guessed digits.

• Game Logic

To get the required random numbers, transformation mapping, and to set up the puzzle.

To display the cheat sheet and puzzle.

To allow players to take turns in guessing the digits for the letters.

To allocate points to players based on their guesses. The points are accumulated over games.

To display the updated puzzle.

To determine whether the puzzle is solved, and if so, to end the game.

• Overall main logic

To read the names of 1 to 4 players with no duplicate.

To read n, the number of digits where n is between 1 and 3.

To repeatedly allow players to play a game until the players choose to end.

To display the players’ name and their points, in descending order of points and ascending order of player names.

Notes:

1. You may make use of un-nested lists but NOT a dictionary. Except for the last task of part i) To display the players’ name and their points, for which you may use nested list, you should NOT use nested data structure, that is, lists within a list in any other parts of your
2. You will incur a heavy penalty if you use other collections such as a dictionary or set.
3. You may copy the code in question 2 and modify them for question You must submit your [programs for questions 2 and 3 separately. Do not combine and present as 1 program as a solution to both questions 2 and 3.

Paste as text the code for Q2(a) and (b) together as a complete Python program into the word document. Screenshot output obtained from the execution of the program, and paste them into the word document.

## Question 4

A school uses a lab operator to provide lab facilities for its programming modules.

The lesson schedule for modules is shown in Table Q3 (a). Each programming module consists of two lessons and one proficiency test. For example, the lessons for module A are conducted on Monday and Tuesday, and a proficiency test is administered on Friday. A student can take any number of modules concurrently as there is no conflict in the module schedules

 Series Module Lesson Days (2 hours per lesson) Proficiency Test (2 hours) Foundational A Monday and Tuesday Friday B Wednesday and Thursday Friday Intermediate C Monday and Tuesday Friday D Wednesday and Thursday Friday

Table Q3 (a)

A purchase of one lab day entitles one student to use lab facilities for a whole day. The school intends to purchase lab days for students only for days when they are on campus for lessons or tests; students tend to use the lab for self-practice only on those days.

The enrolment data used by the application comes from a text file, studentEnrolment.txt. The current content of the file is shown here:

A,  127  130  123  210  109  128  204  206  111  129  103  116  112  209  122  202  121  101  113  104

B,  128 206 101 111 127 119 113 207 117 204 106 123 103 105 205 118

C,  230 208 204 201 220 209 229 218 224 216

D,  228 217 206 221 210 227 224 209 203 229 218

Each line in the file starts with a module code. A comma separates the module code from a sequence of student id of students enrolled for the module. The student ids for each module are sequenced according to the time of enrolment.

To accurately compute the number of lab days to purchase each week, the school sums up the number of students who have lessons or tests on each day, rather than the number of enrolment in each module to avoid double counting. For example, students identified by student ids 204 and 209 are enrolled in both modules A and C will need 3 lab days instead of 6 as both modules are scheduled on Monday, Tuesday, and Friday.

Apply modular programming and decide on the functions to implement the application.

Note: You are NOT allowed to use the set data type and its methods as well as the direct method from the key method. You will incur a heavy penalty if you use sets or the from key method.

• At the start of the application, the text file, txt is read into a dictionary. The module code acts as the key and the value is a list to record the student id of students enrolled in the module.

Just before the application ends, the data in the dictionary should be written back to the same file, in the original order that the student ids appear in the file.

The application provides the user with the following options:

1. Add to  Enrolment

2. Get Number of Days to Purchase

0. Exit

For Q3(b), the application always removes leading and trailing whitespaces in the input string for all non-numeric input.

• When the menu option Add  to   Enrolment   is chosen, the application prompts for a module code.

If the user enters a module code that is not A to D, the application displays the message: Please enter a  valid module code or  <ENTER>  to end.

The application keeps prompting the user until he enters a valid module code or an empty string.

Enter choice: 1

Enter  module  code  or  <ENTER>  to  end:  f

Please enter a valid module code or <ENTER> to end

Enter  module  code  or  <ENTER>  to  end:  w

Please  enter  a  valid  module  code  or  <ENTER>  to  end

• If the user enters an empty string, the application displays the message:

Abandoning  Add to  Enrolment operation, and the operation ends.

Enter choice: 1

Enter  module  code  or  <ENTER>  to  end:

Abandoning Add to Enrolment operation

• If the module code is valid, the application prompts for a valid student id and enrolls the student in the A valid student id is positive (> 0). The application will repeatedly prompt the user for a positive number until it is provided.

Enter  module  code  or  <ENTER>  to  end:  c Enter student id: -1

Invalid id.  Student  id  must  be  positive Enter student id: 0

Invalid id.  Student  id  must  be  positive Enter student id: 1

Add to Enrolment operation has successfully completed

However, if a student is already enrolled previously, the application will not enroll the student.

Enter choice: 1

Enter  module  code  or  <ENTER>  to  end:  c

Enter student id: 1

Add to  Enrolment operation failed. The student is already enrolled in  C.

• When the menu option Get Number of  Days to  Purchase is chosen, the application displays the latest enrolment data, the number of students for each day, and the number of lab days required.

The sample output shown below is based on the enrolment in the file, studentEnrolment.txt and student with student id 1 has been added to C. Student ids are displayed in ascending order.

Number  of  unique  students  on  Monday  and  Tuesday  =  29

Number  of unique  students  on  Wednesday  and  Thursday  =  26

Number  of  unique students  for  Friday  test  =  41

Number  of  lab  days  required  for  the  week  =  151

• Write the main function to repeatedly display the menu to allow the user to select an option to perform tasks according to the specifications. When the user enters 0 to exit, the application saves the enrolment data back to the file and Content of studentEnrolment.txt after student 1 was added o module C:

A,  127  130  123  210  109  128  204  206  111  129  103  116  112  209  122  202  121  101  113  104

B, 128 206 101 111 127 119 113 207 117 204 106 123 103 105 205 118

C,  230  208  204  201  220  209  229  218  224  216  1

D,  228  217  206  221  210  227  224  209  203  229  218

Note that student 1 is at the end of the list for module C, in the order of time of enrolment.

Paste the Python program as text into the word document. Screenshot output obtained from the execution of the program, and paste them into the word document.

Get Help By Expert

Our best expert assignment writers have good knowledge of (ICT 133) structured programming hence compose a good quality assignment for this subject. You can get all information related to Python Programming from experts at SingaporeAssignmentHelp.com.

Looking for Plagiarism free Answers for your college/ university Assignments.