computing and programming (coding)

profileis300
A2.docx

Fundamental Concepts of Computing and Programming in Civil and Environmental Engineering

Summer 2021

Assignment 2

Question 1 (10 points) (Smith text, Ex 2.1.)

A design task involves finding values for three variables. Each variable can take two values. How many solutions are possible? If each solution requires 1 second to be evaluated on a computer, how long does it take to evaluate all solutions? If the number of variables increases to 30, how long does it take to evaluate all solutions? Show your work.

Question 2 (20 points)

In sorting, stability is defined as the ability of a sorting algorithm to preserve the relative order of equal keys in a file. See illustration below.

stability-sorting

Comment on the stability of insertion sort and selection sort. Explain why they are stable or non-stable.

Question 3 (10 points)

Explain the difference between a compiler and an interpreter with your own words.

Question 4 (15 points)

Complete the following Python exercises and include screenshots of your code and execution.

· Include a screenshot of the execution of a simple line of code (e.g., print(“Hello World”)) on a Python installation on your machine.

· Write a program to prompt the user for hours and rate per hour to compute gross pay.

· Rewrite your pay computation to give the employee 1.5 times the hourly rate for hours worked above 40 hours.

Question 5 (15 points):

Write a program to prompt for a score between 0.0 and 1.0. If the score is out of range, print an error message. If the score is between 0.0 and 1.0, print a grade using the following table:

Score Grade

>= 0.9 A

>= 0.8 B

>= 0.7 C

>= 0.6 D

< 0.6 F

~~~

Question 6 (15 points):

Write a program which repeatedly reads numbers until the user enters "done". Once "done" is entered, print out the total, count, and average of the numbers. If the user enters anything other than a number, detect their mistake using try and except and print an error message and skip to the next number.

Enter a number: 4

Enter a number: 5

Enter a number: bad data

Invalid input

Enter a number: 7

Enter a number: done

16 3 5.333333333333333

Question 7 (15 points):

Take the following Python code that stores a string:`

str = 'X-DSPAM-Confidence:0.8475'

Use find and string slicing to extract the portion of the string after the colon character and then use the float function to convert the extracted string into a floating point number.