CSC401 hw1

$30.00

Category: You will Instantly receive a download link for .zip solution file upon Payment || To Order Original Work Click Custom Order?

Description

5/5 - (1 vote)

Reading
Chapters 1 and 2.

Instructions

1. Download and open the file hw1.py. (IDLE > File Open > …navigate to and select hw1.py …)
2. Fill out the comment section at the top of your file (name, collaborators, and references)
3. For each exercise, paste your solution into the file hw1.py. (see Worked Example below)
4. Submit your hw1.py through d2l.

Exercises

For each exercise, write a single expression/statement that refers to the specified variables. (You
should test them by assigning actual values to those variables and then writing expressions that
refer to those variables until you get the correct answer. See the next section for an example. For
problems 3 and greater you will need to choose appropriate values for the variables).

Most
problems are adapted from Perkovic’s Introduction to Computing Using Python (2nd Edition).
1. Suppose that we have assigned s1 = ‘-‘ and s2 = ‘+’ . Write an expression that refers to
the variables s1 and s2 and that evaluates to ‘—+++++’ .
2. Suppose that we have assigned s1 = ‘-‘ and s2 = ‘+’ . Write an expression that refers to
the variables s1 and s2 and that evaluates to ‘–+++–+++–+++–+++–+++’ .

3. The variable side contains the length of a side of a cube. Write an expression that returns
the volume of the cube.
4. The variables length and width refer the dimensions of a rectangle. Write an expression
that results in a value of True if and only if the rectangle has area 100 or more.

5. The variables days holds an integer value. Write an expression that compute the number of
complete weeks spanned by that number of days. For example if days = 27 , then the
expression would evaluate to the integer 3 (not quite 4 because that would require 28 days).

6. Assume that the variable n refers to an integer. Write an expression that returns True if
and only if n is divisible by either 2 or 5.
7. Suppose that the variable s refers to a string. e.g., s=’peach’ or s=’watermelons’ . Write
an expression that revaluates to True if and only if the 1st
character of s is ‘w’

8. Suppose that the variable s refers to a string. e.g., s=’peach’ or s=’watermelons’ . Write
an expression that revaluates to True if and only if the last character of s is ‘s’
9. Suppose that the variable s refers to a string with odd length, e.g., s=’peach’ or
s=’watermelons’ . Write an expression that revaluates to True if and only if the middle
character of s is ‘a’

10. Suppose that the variables word1 and word2 refer to words (strings). Write an expression
that returns True if and only if word1 has exactly one more character than word2 . (I.e., this
would be True for word1 = ‘apple’ and word2 = ‘pear’ but False if they are switched).

11. Assume that numbers refers to a list of numbers, for example [2,3.5,1,-7,8.2] or
[3,3.0,3.00,3.000] , or [3,3.0,3.0,3.001] . Write an expression that computes to True
if and only if all the numbers in numbers have the same value. Hint: use min and max .

12. Assume that the variable fruit refers to a list containing the names (strings) of different
kinds of fruit. Write an expression that evaluates to True if and only if ‘pear’ occurs in the
list fruit .

13. Write an expression that produces a list that contains the first three items from fruit .
14. Write an expression that produces the last item from fruit .

15. Write an expression that produces a list that contains the first three items and the last item
from fruit .

16. Suppose that flips is a list containing strings, each either ‘H’ or ‘T’ , for example., flips
= [‘H’, ‘H’, ‘H’, ‘H’, ‘H’, ‘T’, ‘T’, ‘H’, ‘T’, ‘H’] . Write an expression that
computes the number of ‘H’ ‘s that occur in the list flips .

17. Write an expression that computes the percent of flips that are ‘H’ . The returned valued
should be a decimal between 0.0 (none) and 1.0 (all). For the flips above the result should
be 0.7.

18. Write an expression that evaluates the location of the first occurrence of ‘T’ in the list
flips .

19. Suppose that the string s refers to a string with length 3. Write an expression that returns a
string with the characters from s in reverse order. (So, if s=’hat’ then the expression would
evaluate to ‘tah’ )

20. Consider a dartboard (a circle) in the plane that is centered at (0,0) and that has radius 10.
The variables x and y represent a dart that is thrown and hits the location (x,y) . Write an
expression that returns True if and only if the dart hits the dartboard (or its edge). (If you
use any outside references , cite the source(s) in a comment at the top of your hw1.py file.)

Worked Example
As an example we will work the first problem. In IDLE, we define variables and develop the
expression:
Note that an error was made along the way. When you have the right expression, copy and paste
only the line containing the correct expression (line 5) into the hw1.py file. Include the >>> at the
start of the line.

Do not include any your other lines, variable assignments (lines 1 and 2),
mistakes (lines 3 and 4), or the output of any of your expressions (lines 4 and 6). Now, hw1.py will
now include:
>>> s1 = ‘-‘
>>> s2= ‘+’
>>> 2*s1 + 4*s2 # wrong answer
‘–++++’
>>> 3*s1 + 5*s2
‘—+++++’
1
2
3
4
5
6
## 1 ##
>>> 3*s1 + 5*s2
## 2 ##

1
2
3
4
For most exercises you will need to pick values for the variables. Make sure you pick values that
make sense for the problem. And, for each problem your solution should be only a single
expression (a single line) in your hw1.py.