COSC python - chapter 2
Algorithms
can help the programmer plan a program before writing in a programming language.
Pseudocode
describes algorithms using natural language mixed with code.
IPO
essences of system analysis and design
Assume there is a variable, h already assigned a positive integer value. Write the code necessary to assign its square to the variable g . For example, if h had the value 8 then g would get the value 64.
g=h**2
Given two variables matric_age and grad_age, write a statement that makes the associated value of grad_age 4 more than that of matric_age.
grad_age=matric_age+4
In mathematics, the Nth harmonic number is defined to be 1 + 1/2 + 1/3 + 1/4 + ... + 1/N. So, the first harmonic number is 1, the second is 1.5, the third is 1.83333... and so on. Assume that n is an integer variable whose value is some positive integer N. Assume also that hn is a variable whose value is the Nth harmonic number. Write an expression whose value is the (N+1)th harmonic number.
hn + 1.0 / (n+1)
Given two already defined variables, i and j, write a statement that swaps their associated values.
i,j=j,i
Variables i and j each have associated values. Swap them, so that i becomes associated with j's original value, and j becomes associated with is original value. You can use two more variables itemp and jtemp.
i,j=j,i
Define two variables, one named length making it refer to 3.5 and the other named width making it refer to 1.55.
length=3.5 width=1.55
Write a program that prompts the user to enter the minutes (e.g., 1 billion), and displays the number of years and days for the minutes. For simplicity, assume a year has 365 days. Here is a sample run: Enter the number of minutes: 1000000000 1000000000 minutes is approximately 1902 years and 214 days
minutes = int(input("Enter an integer for minutes: ")) days = minutes // 1440 years = days // 365 remainingDays = days % 365 print(years, "years", remainingDays, "days")
Define a variable precise and make it refer to 1.09388641.
precise=1.09388641
Assume that price is an integer variable whose value is the price (in US currency) in cents of an item. Assuming the item is paid for with a minimum amount of change and just single dollars, write an expression for the amount of change (in cents) that would have to be paid.
price%100
(Matching) Assume the following: val = 99 val += 1 Match the code with its output:
print('The value is', 'val') : The value is val print('The value is', val) : The value is 100 print("The value is:", 'val') : The value is: val
Given a variable profits , write a statement that increases its value by a factor of 10.
profits *= 10
(Matching) After the execution of the following statements, the variable result will hold/store what kind of data type?
result = 123.456 float result = int(123.456) integer result = "123.456" string result = '123' string result = 123 integer result = 123.0 float
Assign 7 to a variable named seven.
seven=7
(Select all that apply) Check all the illegal variable names:
sixth#place 1stplace
System Analysis
stage to identify the systems input and output
System Design
stage where programmers develop a process for obtaining the output from the input
Write a program that reads the subtotal and the gratuity rate and computes the gratuity and total. For example, if the user enters 10 for the subtotal and 15% for the gratuity rate, the program displays 1.5 as the gratuity and 11.5 as the total. Here is another sample run: Enter the subtotal: 15.69 Enter the gratuity rate: 15 The gratuity is 2.35 and the total is 18.04
subtotal = float(input("Enter the subtotal: ")) rate = float(input("Enter the gratuity rate: ")) gratuity = subtotal * rate / 100 total = subtotal + gratuity print("The gratuity is", int(gratuity * 100) / 100, "and total is", int(total * 100) / 100)
Write a statement that increments total by the value associated with amount . That is, add the value associated with amount to that associated with total and assign the result to total .
total += amount
You are given two variables, both already defined, one associated with a float and named total_weight , containing the weight of a shipment, the other associated with an int and named quantity , containing the number of items in the shipment. Write an expression that calculates the weight of one item.
total_weight/quantity
Suppose that you want to read a float value into x, which of the following statement correctly reads the input and stores the float value into x. Assume that input value is 32.6:
x = float(input('Enter a value: '))
Assume that a variable x has been assigned a integer value. Write an expression whose value is the last (rightmost) digit of x.
x%10
Given the variables cost_of_bus_rental and max_bus_riders , write an expression corresponding to the cost per rider (assuming the bus is full).
x=cost_of_bus_rental/max_bus_riders
Given the variables full_admission_price and discount_amount (already defined), write an expression corresponding to the price of a discount admission.
x=full_admission_price-discount_amount
You are given two variables, both already defined. One is named price and is associated with a float and is the price of an order. The other is total_number and is associated with an int and is the number of orders. Write an expression that calculates the total price for all orders.
x=price*total_number
Given the variable price_per_case , write an expression corresponding to the price of a dozen cases.
x=price_per_case*12
Write an expression that computes the remainder of the variable principal when divided by the variable divisor . (Assume that each is associated with an int.)
x=principal%divisor
Given the variables taxable_purchases and tax_free_purchases (which already have been defined), write an expression corresponding to the total amount purchased.
x=taxable_purchases+tax_free_purchases
The dimensions (width and length) of room1 have been read into two variables: width1 and length1 . The dimensions of room2 have been read into two other variables: width2 and length2 . Write a single expression whose value is the total area of the two rooms.
(width1 * length1) + (width2 * length2)
What is the output of the following statement: print ( 2 + 3 * 2 + 7//2 - 10)
1
In mathematics, the Nth harmonic number is defined to be 1 + 1/2 + 1/3 + 1/4 + ... + 1/N. So, the first harmonic number is 1, the second is 1.5, the third is 1.83333... and so on. Write an expression whose value is the 8th harmonic number.
1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 + 1/7 + 1/8
Which of the following statements will cause an error? x = 17 17 = x
17 = x
What is the output of the following command, given that value1 = 2.0 and value2 = 12.2? print(round(value1 * value2))
24
What will be displayed by the following code? a, b = 5, 6 a, b = b, a print (a, b)
6 5
After the execution of the following statement, the variable price will reference the value ________. price = int(68.549)
68
(Matching) Match the expression to its result:
9 // 2 = 4 9/2 = 4.5 9%2 = 1 9**2 = 81
The Software Development Life Cycle (SDLC) is a multistage process that includes requirements spec., system analysis, system design, implementation, testing, deployment, and maintenance
True
Given a variable bridge_players , write a statement that increases its value by 4.
bridge_players += 4
What is the output of the following code. Assume that the user enters a 10 for the length and a 2 for the width: len = input("Enter a length of a rectangle: ") wid = input("Enter a width of a rectangle: ") len *= widprint(len)
ERROR
(T or F) The following is a valid statement: value = $3,450
False
In the expression 12+7:
The 12 and 7 are the: operands The + is the: operator
Which of the following are valid camel case identifier naming styles (Choose all that apply)?
amtDollar dollarAmt amountDollar dollarAmount
import
an illegal identifier because it is a keyword or reserved word.
Which Python statement assigns the sum of 1 and 2 to the variable answer.
answer = 1 + 2
Write an expression that computes the average of the variables exam1 and exam2 (both already assigned values).
avg = (exam1 + exam2) / 2
Write an expression that computes the average of the values 12 and 40, and assign it to the variable avg , which has already been defined.
avg=(12+40)/2
