联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp

您当前位置:首页 >> Python编程Python编程

日期:2018-06-27 01:33


COMP 1012 – Computer Programming for Scientists and Engineers

Page 1 of 15

Assignment 2 – Due Friday June 29, 2018 at 4:00 pm

Each program must start with a multi-line comment as shown below. For

Assignment 2 question 1 replace x with 2 and y with 1. Replace each occurrence of

DentStew and Stew Dent with your name. Replace yyyy/mm/dd with the date you

completed the program.

# DentStewAxQy.py

#

# Course: COMP 1012

# Instructor: Amirhossein Hosseinmemar

# Assignement: x Question y

# Author: Stew Dent

# Version: yyyy/mm/dd

#

# Purpose: The purpose of the program.

#

Begin each program with the following statement.

from time import ctime

The name of each program should be of the form:

LastnameFirstnameAnQm

If your name is Stew Dent and the program is for assignment 2 question 1 then the

name of the program should be:

DentStewA2Q1

The corresponding python file would be named:

DentStewA2Q1.py

Submit your solution to this assignment to the corresponding dropbox on the course

website. Attach the .py file for each program, no email submission will be accepted

and zero will be recorded as your mark for this assignment. If you have not

submitted the honestly declaration form for this course along with your assignment

1, you are required to submit it with this assignment. Failure to do so will receive

zero as the mark for this assignment.

Question 0

Write a function named displayTerminationMessage that contains the following

statement.

print """\n

Programmed by Stew Dent

Date: %s

End of processing.""" % ctime()

Replace Stew Dent with your real name. Use this function in each of the programs

you write for this assignment. Do NOT hand in anything for this question.

COMP 1012 – Computer Programming for Scientists and Engineers

Page 2 of 15

Question 1

A slot machine, also known as a one armed bandit, has three wheels that rotate

independently. Each wheel contains the same set of symbols. When a lever is pulled

each wheel rotates a random amount and then stops, displaying a sequence of three

symbols.

For example suppose each wheel contains the symbols @, #, $, * . When the lever is

pulled the sequence displayed might be #@*. If the lever is pulled again *$@ might

be displayed. If three identical symbols such as $$$ are displayed you win.

The purpose of this question is to write a python program that finds and displays all

the different sequences of the symbols, counts and displays the number of winning

sequences and the total number of sequences of the symbols.

Write a function that begins with the following header:

def getSymbols():

This function creates and returns a list of symbols entered by the user. In a loop

input the number of symbols from the user. If the value entered by the user is less

than or equal to zero display the value entered followed by the message

'must be > 0'. Exit the loop when the value is greater than zero.

In a loop input a symbol from the user and add it to the list of symbols. The loop

must be executed once for each symbol. Exit the loop when the correct number of

symbols have been entered by the user.

Return the list of symbols.

Write a function that begins with the following header:

def generateSequences(symbols):

This function is given a list of symbols and creates and returns a list of the different

sequences of the symbols, one entry in the list for each sequence of symbols. You

will need to use nested for loops to create the list of sequences of symbols.

For example if the symbols are #, $, and * then the first sequence of symbols is ###,

the next sequence is ##$ and the last sequence is ***.

Return the list of sequences of symbols.

COMP 1012 – Computer Programming for Scientists and Engineers

Page 3 of 15

Write a function that begins with the following header:

def countWinners(sequences):

This function is given the list of sequences of symbols and returns the number of

winning sequences. A sequence is a winner if all of the symbols in the sequence are

the same.

Return the number of winning sequences.

Write a function that begins with the following header:

def displaySequences(sequences):

This function is given the list of sequences of symbols and displays the heading

'The sequence of symbols are:' followed by the sequences one sequence per line. The

format of the output is shown in the sample output.

Write a function that begins with the following header:

def main():

This function:

• calls getSymbols to get the list of symbols entered by the user

• calls generateSequences to get the list of sequences

• calls displaySequences to display the sequences

• calls countWinners to get the number of winners

• displays the number of winners and the total number of sequences

• calls displayTerminationMessage to display the termination message

The main program (not to be confused with function named main) should contain

the function definitions, any import statements needed by the functions and a call to

the function named main.

A sample run of the program is shown on the next page.

COMP 1012 – Computer Programming for Scientists and Engineers

Page 4 of 15

Enter the number of symbols: 0

0 must be > 0

Enter the number of symbols: -22

-22 must be > 0

Enter the number of symbols: 3

Enter a 1 character symbol: #

Enter a 1 character symbol: @

Enter a 1 character symbol: $

The sequences of symbols are:

###

##@

##$

#@#

#@@

#@$

#$#

#$@

#$$

@##

@#@

@#$

@@#

@@@

@@$

@$#

@$@

@$$

$##

$#@

$#$

$@#

$@@

$@$

$$#

$$@

$$$

There are 3 winning sequences out of 27 total sequences.

Programmed by Stew Dent.

Date: Sat Jun 2 08:18:15 2018.

End of processing.

Hand in your complete program.

COMP 1012 – Computer Programming for Scientists and Engineers

Page 5 of 15

Question 2

The purpose of this question is to write a complete python program that computes

the product of two fractions represented by large integers. Each fraction must be to

the same number of decimal places. The number of decimal places must be a

positive integer. There must not be any floats in this program! Instead of

representing a fraction as x/y where x and y are integers with y <> 0, you represent

it as a single integer z, with a fixed denominator B.

You obtain B from the number of decimal places entered by the user. For example,

for 50 decimal places, B = 10**50.

Write a function that begins with the following header:

def getPositiveInt(prompt):

This function is given:

• prompt - the string input is to display to the user when requesting input.

Valid input from the user is an integer greater than zero.

Input an integer from the user. Loop as long as the integer is less than or equal to

zero. In the loop display the value of the integer followed by the text 'is not greater

than zero'. Then input another integer from the user. When the integer entered by

the user is valid return the integer.

Write a function that begins with the following header:

def getFraction(one):

This function is given:

• one - the large integer value that represents 1.0

Input integer values for the numerator and denominator of the fraction. Loop as

long as the denominator is equal to zero and display the message 'The denominator

cannot be zero!'. Return the large integer that represents the fraction.

Note: Be sure to handle the special case that occurs when the fraction is negative.

Write a function that begins with the following header:

def multiplyFractions(fraction1, fraction2, one):

This function is given:

• fraction1 - the multiplicand

• fraction2 - the multiplier

COMP 1012 – Computer Programming for Scientists and Engineers

Page 6 of 15

• one - the large integer value that represents 1.0

Compute and return the product of fraction1 and fraction2 as a large integer.

Write a function that begins with the following header:

def displayFraction(fraction, places):

This function is given:

• fraction - the large integer that represents the fraction to display

• places - the number of decimal places to which the fraction is to be displayed

Display the large integer that represents the fraction to the given number of decimal

places. If the fractional part of the fraction is zero, display only the integer part of

the fraction.

Write a function that begins with the following header:

def main():

This function:

• calls getPositiveInt to input the number of decimal places.

• computes the large integer value that represents 1.0

• calls getFraction to input the first fraction

• loops as long as the first fraction is not zero

o calls displayFraction to display the first fraction

o calls getFraction to input the second fraction

o calls displayFraction to display the second fraction

o calls multilpyFractions to get the product of the two fractions

o calls displayFraction to display the product of the two fractions

o calls getFraction to input the first fraction

• calls displayTerminationMessage to display the termination message

The main program (not to be confused with function named main) should contain

the function definitions, any import statements needed by the functions and a call to

the function named main.

A sample run of the program is shown on the next page. (Since these are floating

numbers, it is possible to output some other close numbers to the sample output.)

COMP 1012 – Computer Programming for Scientists and Engineers

Page 7 of 15

--------------------------------------------------------------

Enter the number of decimal places (>0): 50

--------------------------------------------------------------

Enter the first fraction.

Enter the numerator as an integer: -1

Enter the denominator as an integer: 0

The denominator cannot be zero!

Enter the denominator as an integer: 3

-0.33333333333333333333333333333333333333333333333333

Enter the second fraction.

Enter the numerator as an integer: 28

Enter the denominator as an integer: 4

7

The product of the fractions is:

-2.33333333333333333333333333333333333333333333333331

--------------------------------------------------------------

Enter the first fraction.

Enter the numerator as an integer: 0

Enter the denominator as an integer: 1

Programmed by Stew Dent.

Date: Wed Jun 6 19:04:42 2018

End of processing.

Hand in your complete program.

COMP 1012 – Computer Programming for Scientists and Engineers

Page 8 of 15

Question 3

The purpose of this question is to write a complete python program that computes

the volume of an ellipsoid by dividing the ellipsoid up into a large number of small

pieces, computing the volume for each piece and adding together the volumes of the

pieces using the method discussed in class. This is the approximate volume of the

ellipsoid. The actual volume of the ellipsoid is 4πabc/3, where a is the length of the

X semi-axis, b is the length of the Y semi-axis and c is the length of the Z semi-axis of

the ellipsoid, as shown in the diagram given below.

Write a function that begins with the following header.

def computeXcoordinates(xSemiAxis, intervals):

where xSemiAxis is the length of the X semi-axis of the ellipsoid and intervals is the

number of equally spaced intervals into which the X semi-axis is to be divided.

Compute the size of an interval using xSemiAxis and intervals. Create a list of equally

spaced X coordinates from 0 to xSemiAxis and return the list of equally spaced X

coordinates.

COMP 1012 – Computer Programming for Scientists and Engineers

Page 9 of 15

Write a function that begins with the following header.

def computeAreas(xSemiAxis, ySemiAxis, zSemiAxis, xCoordinates):

where xSemiAxis is the length of the X semi-axis, ySemiAxis is the length of the Y

semi-axis, zSemiAxis is the length of the Z semi-axis of the ellipsoid and xCoordinates

is the list of equally spaced X coordinates. For each X coordinate x in xCoordinates

compute an area and add it into a list of areas. Use the following formulae for

computing the area for each X coordinate x.

ratioYX = ySemiAxis / xSemiAxis

ratioZX = zSemiAxis / xSemiAxis

semiAxis1 = ySemiAxis - ratioYX * x

semiAxis2 = zSemiAxis - ratioZX * x

area = π * semiAxis1 * semiAxis2

NOTE:

The values of ratioYX and ratioZX do not depend on the value of x and should

be calculated only once. The values of semiAxis1, semiAxis2 and area must be

calculated for each value of x.

Return the list of areas.

Write a function that begins with the following header.

def computeVolume(xSemiAxis, intervals, areas):

where xSemiAxis is the length of the X semi-axis of the ellipsoid, intervals is the

number of equally spaced intervals into which the is the length of the X semi-axis is

to be divided and areas is the list of areas to be used to compute volume of the

ellipsoid. For each pair of adjacent areas and the width of an interval compute the

volume for that interval and add together the volumes for all the intervals to get the

approximate volume of the ellipsoid. The approximate volume computed will be too

small, do you know why? Multiply the approximate volume by the appropriate

number to get the approximate volume for the entire ellipsoid. Return the

approximate volume of the ellipsoid.

COMP 1012 – Computer Programming for Scientists and Engineers

Page 10 of 15

Write a function that begins with the following header.

def main():

• Input the number of intervals as an integer from the user.

• Loop as long as the number of intervals is greater than zero.

o input the length of the X semi-axis of the ellipsoid as a float, if the

length is less than or equal to zero display an error message that

includes the value of the length as shown in the sample run of the

program, otherwise continue

o input the length of the Y semi-axis of the ellipsoid as a float, if the

length is less than or equal to zero display an error message that

includes the value of the length as shown in the sample run of the

program, otherwise continue

o input the length of the Z semi-axis of the ellipsoid as a float, if the

length is less than or equal to zero display an error message that

includes the value of the length as shown in the sample run of the

program, otherwise continue

o call computeXcoordinates to get the list of equally spaced X

coordinates

o call computeAreas to get the list of areas

o call computeVolume to get the approximate volume of the ellipsoid

o compute the actual volume of the ellipsoid using the formula given at

the beginning of this question

o compute the error in the approximate volume by taking the absolute

value of the difference of the two volumes

o display the approximate volume and actual volume to 14 decimal

places using the exponential format as shown in the sample run of the

program

o display the error in the approximate volume to 6 decimal places using

the exponential format as shown in the sample run of the program

o Input the number of intervals as an integer from the user.

• Call displayTerminataionMessage to display the termination message

The main program (not to be confused with function named main) should contain

the function definitions, any import statements needed by the functions and a call to

the function named main.

A sample run of the program is shown on the next page.

COMP 1012 – Computer Programming for Scientists and Engineers

Page 11 of 15

Enter the number of intervals (0 to quit): 1

Enter the length of the X semi-axis in cm (> 0): 0

The length of the X semi-axis, 0, must be greater than zero!

Enter the number of intervals (0 to quit): 1

Enter the length of the X semi-axis in cm (> 0): 1

Enter the length of the Y semi-axis in cm (> 0): 0

The length of the Y semi-axis, 0, must be greater than zero!

Enter the number of intervals (0 to quit): 1

Enter the length of the X semi-axis in cm (> 0): 1

Enter the length of the Y semi-axis in cm (> 0): 1

Enter the length of the Z semi-axis in cm (> 0): 0

The length of the Z semi-axis, 0, must be greater than zero!

Enter the number of intervals (0 to quit): 10000000

Enter the length of the X semi-axis in cm (> 0): 10

Enter the length of the Y semi-axis in cm (> 0): 7.5

Enter the length of the Z semi-axis in cm (> 0): 5

The approximate volume of the ellipsoid is 1.57079632679473e+03 cm^3

The actual volume of the ellipsoid is 1.57079632679490e+03 cm^3

The error in the approximate volume is 1.623448e-10 cm^3

Enter the number of intervals (0 to quit): 0

Programmed by Stew Dent.

Date: Sat Jun 2 09:22:34 2018.

End of processing.

Hand in your complete program.

COMP 1012 – Computer Programming for Scientists and Engineers

Page 12 of 15

Question 4

The purpose of this question is to write a complete python program that creates and

displays a table of polar and Cartesian coordinates for a spiral. An example of a

spiral is shown below.

Write a function that begins with the following header.

def computeCoordinates(intervals, maxAngle, scale):

where intervals is the number of equally spaced angles, maxAngle is the maximum

angle for the spiral, and scale is the scale factor for the spiral. The larger maxAngle is,

the more revolutions in the spiral. The larger scale is, the farther apart are the lines

of the spiral. Create a list of equally spaced angles from 0 to maxAngle radians.

Create a list of radii. Use the formula radius = scale × angle to compute each radius

from the corresponding angle. Add the radius to the list of radii. The lists of angles

and radii are the polar coordinates of the points in the spiral. Using the polar

coordinates, create lists of the corresponding X and Y coordinates (Cartesian

coordinates) of the points on the spiral. Use the formula x = radius × cos(angle) to

compute each X coordinate from the corresponding radius and angle. Add each X

coordinate to the list of X coordinates. Use the formula y = radius × sin(angle) to

compute each Y coordinate from the corresponding radius and angle. Add each Y

coordinate to the list of Y coordinates. Return the lists of angles, radii, X coordinates

and Y coordinates in that order.

COMP 1012 – Computer Programming for Scientists and Engineers

Page 13 of 15

Write a function that begins with the following header.

def displayCoordinates(angles, radii, xCoords, yCoords):

This function is given the lists of angles, radii, X coordinates and Y coordinates of the

points in the spiral. Display the angle, radius, X coordinate and Y coordinate of each

point in the spiral with the coordinates for one point per line of output. See the

sample run of the program for the format of the output. Display each angle and

radius to 4 decimal places in 10 character positions. Display each X and Y coordinate

to 6 decimal places in 14 character positions. Separate each value in a line of output

from the next value by one space. Display the two lines of headings so that the text is

positioned as shown in the sample run of the program.

Write a function that begins with the following header.

def main():

Input the number of intervals as an integer

Loop as long as the number of intervals is greater than zero:

• input the maximum angle as a float, if the angle is less than or equal to zero

display an error message that includes the value of the angle as shown in the

sample run of the program, otherwise continue

• input the scale factor as a float, if the scale factor is less than or equal to zero

display an error message that includes the value of the scale factor as shown

in the sample run of the program, otherwise continue

• call computeCoordinates to get the lists of angles, radii, X coordinates and

Y coordinates

• call displayCoordinates to display the lists of angles, radii, X coordinates and

Y coordinates

• input the number of intervals as an integer

Call displayTerminationMessage to display the termination message.

The main program (not to be confused with function named main) should contain

the function definitions, any import statements needed by the functions and a call to

the function named main.

A sample run of the program is shown on the next page.

COMP 1012 – Computer Programming for Scientists and Engineers

Page 14 of 15

Enter the number of intervals: 10

Enter the maximum angle in radians: 0

The maximum angle, 0, must be greater than zero.

Enter the number of intervals: 10

Enter the maximum angle in radians: 6.28

Enter the scale factor for the spiral: 0

The scale factor, 0, must be greater than zero.

Enter the number of intervals: 20

Enter the maximum angle in radians: 6.28

Enter the scale factor for the spiral: 2.5

Polar Coordinates Cartesian Coordinates

Angle Radius X coordinate Y coordinate

0.0000 0.0000 0.000000 0.000000

0.3140 0.7850 0.746618 0.242459

0.6280 1.5700 1.270451 0.922418

0.9420 2.3550 1.385144 1.904573

1.2560 3.1400 0.972216 2.985699

1.5700 3.9250 0.003126 3.924999

1.8840 4.7100 -1.451189 4.480865

2.1980 5.4950 -3.224922 4.449146

2.5120 6.2800 -5.075919 3.697762

2.8260 7.0650 -6.716078 2.192834

3.1400 7.8500 -7.849990 0.012502

3.4540 8.6350 -8.217035 -2.653970

3.7680 9.4200 -7.631508 -5.522362

4.0820 10.2050 -6.015429 -8.243581

4.3960 10.9900 -3.419394 -10.444513

4.7100 11.7750 -0.028130 -11.774966

5.0240 12.5600 3.850801 -11.955121

5.3380 13.3450 7.814734 -10.817530

5.6520 14.1300 11.407553 -8.338143

5.9660 14.9150 14.170996 -4.651892

6.2800 15.7000 15.699920 -0.050009

Enter the number of intervals: 0

Programmed by Stew Dent.

Date: Sat Jun 2 09:58:03 2018.

End of processing.

COMP 1012 – Computer Programming for Scientists and Engineers

Page 15 of 15

Hand in your complete program.


版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp