hw1

hw1 Reading Instructions Exercises Worked Example

Reading

Chapters 1 and 2.

Instructions

1. Download and open the file hw1.py. (IDLE > File Open > …navigate to and select hw1.py …) 2. For each exercise, paste your solution into the file hw1.py. (see Worked Example below) 3. 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. 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 ‘w’ 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’af://n0af://n3af://n5af://n13

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 the the ‘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 a list that contains only 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 prints 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 one line containing the correct expression into the hw1.py file. 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

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.

Eric Sedgwick – 2020-07-1

- hw1
- Reading
- Instructions
- Exercises
- Worked Example

Why Work with Us

Top Quality and Well-Researched Papers

We always make sure that writers follow all your instructions precisely. You can choose your academic level: high school, college/university or professional, and we will assign a writer who has a respective degree.

Professional and Experienced Academic Writers

We have a team of professional writers with experience in academic and business writing. Many are native speakers and able to perform any task for which you need help.

Free Unlimited Revisions

If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.

Prompt Delivery and 100% Money-Back-Guarantee

All papers are always delivered on time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.

Original & Confidential

We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.

24/7 Customer Support

Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.

Try it now!

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.

Essays

No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.

Admissions

Admission Essays & Business Writing Help

An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.

Reviews

Editing Support

Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.

Reviews

Revision Support

If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.