Hints for Liang Python Revel Quizzes and Programming Projects with CodeGrade

Introduction to Python Programming and Data Structures 3E by Y. Daniel Liang

If you need a hint on an exercise that is not listed here, please contact Dr. Liang at y.daniel.liang@gmail.com. Please check this page often. We update this page frequently in response to student questions. Last update: .

NOTE: Errata can be viewed at https://liangpy.pearsoncmg.com/errata3epy.html. Please send corrections and suggestions to Dr. Liang at y.daniel.liang@gmail.com. We appreciate your help to improve the book.

Note: In the expected output pattern of a test run, [\s\S]* represents zero or more number of any characters.

Hints for End-of-Section Programming Quizzes:

Programming Quiz 5.3 Question 1

Hint:
Test if passphrase is alphanumeric. If not, passphrase = passphrase[:-1]. Repeat the process until passphrase is not alphanumeric.

Programming Quiz 5.3 Question 2

Hint:
Your code may look like this:
fibNum = 0
num = 1
while num < 1000:
    # write your code here to update fibNum and num.

Programming Quiz 5.4 Question 1

Hint:
Your code may look like this:
iterations = 0
while n != 1:
    # write your code here to update n and iterations

Programming Quiz 5.4 Question 2

Hint:
Your code may look like this:
previousChar = text[0].lower() # Store the previousChar. Use it to compare with the next character in the string
randomCaseText = previousChar # Store the resulting string
i = 1
while i < len(text):
    char = text[i]
    # write your code here to update randomCaseText and previousChar 
    i += 1

Programming Quiz 5.11 Question 2

Hint:
Your code may look like this:
c1, c2 = -1, -1
i = 0
while i < 100 and c1 == -1:
    cand1, cand2 = n + i, m + i  # Because c1 - n == c2 - m;
    # Write the code to check if cand1 and cand2 are coprime. If so, assign cand1, cand2 to c1, c2, done, else i += 1


Programming Quiz 9.2.4 Question 2

Hint:
canCross returns True if the state is not red.

Programming Quiz 9.2.4 Question 3

Hint:
Initialize state to "red" and initialize a TrafficLight instance variable syncedWith to None in the init method.
    def __init__(self):
        self.state = "red"
        self.syncedWith = None
The syncWith should be implementes as follows:
    def syncWith(self, otherLight):
        self.syncedWith = otherLight
        otherLight.syncedWith = self
        self.state = otherLight.state
The turnGreen method is implemented as follows:
    def turnGreen(self):
        if self.state == "red":
            self.state = "green"
            if self.syncedWith is not None:
                self.syncedWith.state = "green"
Similarly, you can implement turnYellow and turnRed, and the other methods in the class.

Hints for End-of-Chapter Assignments (Programming Projects):

Chapter 1: Programming Project 1: You may click Exercise01_01 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Please check your spelling of the words carefully. It is case sensitive. The words are separated by exactly one space.

Chapter 1: Programming Project 2: You may click Exercise01_05 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Write the Math expression as (9.5 * 4.5 - 2.5 * 3) / (45.5 - 3.5).

Chapter 1: Programming Project 3: You may click Exercise01_11 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Print the result of two expressions.

Chapter 1: Programming Project 4: You may click Exercise01_01Extra to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
In this case, a is 3, b is 4, and c is 5. So, the discriminant for the equation 3x^2 + 4x + 5 is 4 * 4 - 4 * 3 * 5.

Chapter 1: Programming Project 5: You may click Exercise01_02Extra to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
The values for v0, v1, and t are given. Use these values in the formula to compute the average acceleration.

Chapter 2: Programming Project 1: You may click Exercise02_05 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Step 1: Prompt the user to enter subtotal (float) and gratuity (float) rate into variables subtotal and gratuityRate. Note the gratuityRate is in percent. For example, if it is 15%, you enter 15 as the input for gratuityRate.
Step 2: Compute gratuity: gratuity = subtotal * gratuityRate / 100.
Step 3: Compute total: total = subtotal + gratuity.
Step 4: Display gratuity and total. Note you need to first display gratuity and then total. For all the programming projects, please pay attention to output order.

Chapter 2: Programming Project 2: You may click Exercise02_07 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
You must use integers in this program. Use
int(input("Enter the number of minutes: "))
to obtain the input. See LiveExample 2.4 in Section 2.8.2 for reference.

Now, the key to solve this problem is to use the correct math.
Step 1: Prompt the user to enter the minutes.
Step 2: Get the totalNumberOfDays from the minutes (i.e., totalNumberOfDays = minutes // (24 * 60)).
Step 3: Get the numberOfYears from the numberOfDays (i.e., numberOfYears = numberOfDays // 365).
Step 4: Get the remainingNumberOfDays from totalNumberOfDays (i.e., remainingNumberOfDays = totalNumberOfDays % 365).

Chapter 2: Programming Project 3: You may click Exercise02_13 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Use integer in this program. The last digit of number is number % 10. Discard the last digit using number // 10.
Step 1: Get the last digit n1. n1 = number % 10.
Step 2: Get the second last digit n2. number = number // 10. n2 = number % 10.
Step 3: Get the third last digit n3. number = number // 10. n3 = number % 10.
Step 4: Get the first last digit n4. number = number // 10. n4 = number % 10.
Step 5: Now print n1, n2, n3, and n4.

Chapter 2: Programming Project 4: You may click Exercise02_04Extra to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Use float numbers in this program.

Chapter 2: Programming Project 5: You may click Exercise02_19 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Prompt the user to enter investmentAmount (float), annualInterestRate (float), and numberOfYears (int), and apply the formula to compute the accumulatedValue.
The annualInterestRate is entered in percentage. In the formula, you need to use monthlyInterestRate, which is annualInterestRate / 1200. See LiveExample 2.8 in Section 2.14 for reference.

Chapter 3: Programming Project 1: You may click Exercise03_01 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Step 1: Prompt the user to enter a, b, c. All are float numbers.
Step 2: Compute discriminant.
Step 3: if discriminant < 0, display "The equation has no real roots".
Step 4: elif discriminant == 0, compute and display one root.
Step 5: else compute and display two roots.


Common errors from this project: In Step 1, your code should read in float values. In Step 2, discriminant is discriminant = b * b - 4 * a * c. In Step 5, please note that root1 is r1 = (-b + discriminant ** 0.5) / (2 * a). Another common mistake is to compute the roots before the if statement. Please compute roots in Step 5.

Chapter 3: Programming Project 2: You may click Exercise03_03 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Step 1: Prompt the user to enter a, b, c, d, e, f. All are float numbers.
Step 2: Compute detA = a * d - b * c.
Step 3: if detA == 0, display "The equation has no solution".
Step 4: else compute x and y, and display the result.


Common errors from this project: A common mistake is to compute x and y before the if statement. Please compute x and y in Step 4.

Chapter 3: Programming Project 3: You may click Exercise03_11 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Prompt the user to enter year (int) and test if year is a leap year. To test if a year is a leap year, see Section 3.11.

Chapter 3: Programming Project 4: You may click Exercise03_21 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Prompt the user to enter year, month, and dayOfMonth. Note that all input are integers. You need to adjust year and month as follows: if month is 1, set month = 13 and year = year - 1.
if month is 2, set month = 14 and year = year - 1.
You can now use Zeller's formula to compute h.

NOTE: Revel assigned exercises are currently submitted to a Python 3.9 interpreter. The "match statement" (covered in Section 3.14) is not supported in Python 3.9. Don't use "match statement" in your Programming Projects. Replace match statements with if/elif/else statements.

Chapter 3: Programming Project 5: You may click Exercise03_23 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
All the input are float numbers.
A point (x, y) is in the rectangle centered at (0, 0) with width 10 and height 5 if |x| <= 5 and |y| <= 2.5. That is, x <= 5 and x >= -5 and y <= 2.5 and y >= -2.5.

Chapter 4: Programming Project 1: You may click Exercise04_55 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Prompt the user to enter the numberOfSides (int) and the lengthOfSide (float) of a polygon, and apply the formula to compute the area of the polygon.

Chapter 4: Programming Project 2: You may click Exercise04_23 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Prompt the user to enter a character for grade, and test if it is A/a, B/b, C/c, D/d, F/f and display its corresponding value, otherwise displays "invalid grade".

Chapter 4: Programming Project 3: You may click Exercise04_25 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Prompt the user to enter year (int) and month (str), and displays the number of days in the month. To test if a year is a leap year, see Section 3.11.

Chapter 4: Programming Project 4: You may click Exercise04_29 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Step 1: Prompt the user to enter the first 9-digit of an ISBN number as a string into s.
Step 2: Write a if/else statement. If len(s) is not 9 or s.isdigit() is False, display "Incorrect input".
Step 3: else compute the checksum as follows:
Step 3.1: Compute checksum = (d1 * 1 + d2 * 2 + d3 * 3 + d4 * 4 + d5 * 5 + d6 * 6 + d7 * 7 + d8 * 8 + d9 * 9) % 11
Note d1 = ord(s[0]) - ord('0') and d2 = ord(s[1]) - ord('0'), etc.
Step 3.2: Use an if/else statement to get the checksum character: if checksum is 10, the character is X, otherwise it is checksum itself.

Chapter 4: Programming Project 5: You may click Exercise04_31 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Read a hex character into hex. Convert it into an uppercase using hex.upper(). Test if hex is '0', …, '9', 'A', …, 'F' to display the corresponding binary number.

Chapter 5: Programming Project 1: You may click Exercise05_01 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Step 1: Initialize variables. Your program needs to count the number of positives, number of negatives, and total. Think about what initial values are appropriate for these variables. Note that the average can be computed using total / (numberOfPositives + numberOfNegatives).
Step 2: Prompt the user to read a number into variable number.
Step 3: Write a while loop to do the following:
Step 3.1: the loop continuation condition is (number =! 0)
Step 3.2: If number > 0, increase numberOfPositives by 1; if number < 0, increase numberOfNegatives by 1.
Step 3.3: Add number to total.
Step 3.4: Prompt the user to a new input and assign it to number.

Step 4: After the loop is finished, if (numberOfPositives + numberOfNegatives == 0), this means "No numbers are entered except 0". Your program should display exactly this message. Otherwise, display the number of positives, negatives, and average. To compute average, use total / (numberOfPositives + numberOfNegatives).

Chapter 5: Programming Project 2: You may click Exercise05_11 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Note that we assume that the number of students is at least 2. So, your program does not need to consider the case for one or no students.
Step 1: Prompt the user to read the number of the students into variable numberOfStudents (int).
Step 2: Prompt the user to read first student name into student1 (str).
Step 3: Prompt the user to read first student score into score1 (float).
Step 4: Prompt the user to read second student name into student2 (str).
Step 5: Prompt the user to read second student score into score2 (float).
Step 6: If score1 < score2, swap student1 with student2 and score1 with score2. Throughout the program, we will use student1 and score1 to store the student name and score with the highest score and student2 and score1 to store the student name and score with the second highest score.
Step 7: Now write a loop to read the next student name and score. Consider the following cases:
Step 7.1: if score > score1, assign score1 to score2 and score to score1, student1 to student2 and name to student.
Step 7.2: else if score1 > score2, assign score to score2 and name to student2.
Step 8: Display the top two students’ name and score.

Chapter 5: Programming Project 3: You may click Exercise05_23 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
For this program, you need to use the formula for computing monthlyPayment and totalPayment in Section 2.14.
Step 1: Prompt the user to read loanAmount (float) and numberOfYears (int).
Step 2: Print the header using the format function, "Interest Rate", "Monthly Payment", "Total Payment";
Step 3: Write a while loop as follows:
Step 3.1: Before the loop, Initialize annualInterestRate to 5.0.
Step 3.2: The loop continuation condition is <= 8.0.
Step 3.3: In the loop body, compute monthlyPayment and totalPayment using the formula. Note the monthlyInterestRate is annualInterestRate / 1200. Display annualInterestRate, monthlyPayment, and totalPayment in one line using the format function to format the output. Please note: in the first column, you need to put three digits after the decimal point. For example, 5.000% is correct, but 5% is wrong.
Step 3.4: Don’t forget: annualInterestRate += 1.0/ 8 in the loop.

Chapter 5: Programming Project 4: You may click Exercise05_59 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Step 1: Initialize variables numberOfVowels and numberOfConsonants with 0.
Step 2: Prompt the user to enter a string.
Step 3: For each character in the string, do the following:
Step 3.1: Convert the character to uppercase.
Step 3.2: If the character is 'A', 'E', 'I', 'O', or 'U', increase numberOfVowels by 1.
Step 3.3: else if the character is a letter, increase numberOfConsonants by 1.
Step 4: Display the result using the wording as shown in the sample output.

Chapter 5: Programming Project 5: You may click Exercise05_57 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Step 1: Prompt the user to enter the first 12 digits of an ISBN-13 into a string s.
Step 2: If the length of the input is not 12, exit the program using sys.exit().
Step 3: Initialize variable sum. (In Step 4, we will compute sum. sum will be d1 + 3*d2 + d3 + 3*d4 +…+d11 + 3*d12. Note d1 is s[0], d2 is s[1], etc.)
Step 4: for each i from 0 to len(s) - 1, do the following:
Step 4.1: If (i % 2 is 0), add (ord(s[i]) - ord('0')) to sum.
Step 4.2: else, add (ord(s[i]) - ord('0')) * 3 to sum.
Step 5: Obtain checksum that is 10 - sum % 10.
Step 6: Display the entire ISBN-13 string whose last digit is checksum. Note that if checksum is 10, display digit 0.

Chapter 5: Programming Project 6: You may click Exercise05_41 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Maintain two variables (int), max and count. max stores the current max number and count stores its occurrences. Initially, assign the first number to max and 1 to count. Compare each subsequent number with max. If the number is greater than max, assign it to max and reset count to 1. If the number is equal to max, increment count by 1
Step 1: Declare two integer variables max and count. count is initialized to 0.
Step 2: Read the first number and assign it to max.
Step 3: Write a loop that continues to read an integer. When the integer is 0, the loop ends.
Step 4: For each integer read, if the integer is greater than max, assign it to max and reset count to 1, else if the integer is equal to max, increase count by 1.
Step 5: After the loop is finished, display max and count as shown in the sample run.

Chapter 6: Programming Project 1: You may click Exercise06_13 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
The program has two functions: the main function and the m(i) function.
Step 1: Implement the m(i) function to return the summation as shown in the formula for m(i) given in the description.
Step 1.1: Initialize sum with 0.
Step 1.2: for each k from 1 to i, add k / (k + 1) to sum.
Step 1.3: return sum.
Step 2: Implement the main function as follows:
Step 2.1: Display the header of the table using the format function. The header is "i m(i)".
Step 2.2: Write a for loop as follows: for each i from 1 to 20, display i and the result from invoking m(i). The result of m(i) is displayed with four digits after the decimal point. So, 0.5 should be displayed 0.5000 using the format function.

Chapter 6: Programming Project 2: You may click Exercise06_07 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
The program has two functions: the main function and the futureInvestmentValue function.
Step 1: Implement the futureInvestmentValue function to compute the futureInvestment value from investmentAmount, monthlyInterestRate and years using the formula from Exercise 2.19 in Chapter 2. This function return futureInvestmentValue.
Step 2: Implement the main function as follows:
Step 2.1: Prompt the user to enter investmentAmount (float) and annualInterestRate (float).
Step 2.2: Write a for loop as follows: for each year from 1 to 30, display year and the result from invoking futureInvestmentValue(investmentAmount, annualInterestRate / 1200, year). You need to use the format function to display formatted output.

Chapter 6: Programming Project 3: You may click Exercise06_53 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
The program has two functions: the main function and the count(s, a) function.
Step 1: Implement the count(s, a) as follows:
Step 1.1: Initialize count.
Step 1.2: for each i from 1 to len(s) - 1, if s[i] == a, increase count by 1.
Step 1.3: return count.
Step 2: Implement the main function as follows:
Step 2.1: Prompt the user to enter a string s.
Step 2.2: Prompt the user to enter a char.
Step 2.3: Simply invoke count(s, ch) to return the count and display the result as shown in the sample run.

Chapter 6: Programming Project 4: You may click Exercise06_23 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
First, please read LiveExample 2.7 ShowCurrentTime.java in Section 2.13. This is very helpful for this exercise.
The program has two functions: the main function and the convertMillis(long millis) function.
Step 1: Implement the convertMillis(millis) function as follows:
Step 1.1: Obtain seconds from millis.
Step 1.2: Obtain totalMinutes from seconds.
Step 1.3: Obtain minutes from totalMinutes % 60.
Step 1.4: Obtain totalHours from totalMinutes // 60.
Step 1.5: Return a string: hours + ":" + minutes + ":" + seconds.
Step 2: Implement the main function as follows:
Step 2.1: Prompt the user to enter an integer into variable totalMillis.
Step 2.2: Invoke convertMillis(totalMillis) to return a string.
Step 2.3: Display this string.

Chapter 6: Programming Project 5: You may click Exercise06_02Extra to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

As always, there are many ways to solve a problem. An easy way might be first convert the binary to decimal and then convert the decimal to hex. Converting a decimal to hex is covered in Section 5.9.3. Converting a binary number to decimal is similar to converting a hex to decimal, which is covered in Section 6.12.

Chapter 7: Programming Project 1: You may click Exercise07_01 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Step 1: Enter the scores in one line separated by spaces.
Step 2: Split the line into a list items and get scores from the items using list comprehension. The scores are float numbers.
Step 3: Initialize variable best to keep the best score. Set the initial value to 0.
Step 4: For each score: compare it with best. If it is greater than best, assign it to best.
Step 5: For each score, compare it with best, assign the grade for the student.

Chapter 7: Programming Project 2: You may click Exercise07_03 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Step 1: Enter the numbers in one line separated by spaces.
Step 2: Split the line into a list items and get numbers from the items using list comprehension. Numbers are integers.
Step 3: Create list counts with 100 elements. The initial values in counts are 0.
Step 4: For each value in numbers, update counts.
Step 5: For each value in counts, display the value and its count. Note the 0 count is not displayed. Single count is displayed in word "time", and multiple count is displayed in word "times".

Chapter 7: Programming Project 3: You may click Exercise07_05 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Step 1: Enter the numbers in one line separated by spaces.
Step 2: Split the line into a list list1. Numbers are integers.
Step 3: Create a new empty list list2.
Step 4: For each number in list1, if it is not in list2, add to list2.
Step 5: list2 now contains the distinct numbers from list1.

Chapter 7: Programming Project 4: You may click Exercise07_09 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Step 1: Implement the mean(x) function as follows: return sum(x) / len(x)
Step 2: Implement the deviation(xe) function as follows:
Step 2.1: Initialize squareSum.
Step 2.2: Write a loop. For each value in x, add (value - mean(x)) ^ 2 to squareSum.
Step 2.3: return sqrt(squareSum / (x.length - 1))
Step 3: Implement the main function as follows:
Step 3.1: Enter the numbers in one line separated by spaces. Numbers are float numbers.
Step 3.2: Split the line into a list and get numbers using list comprehension.
Step 3.3: Invoke mean(numbers) and deviation(numbers) to obtain mean and deviation for numbers.

Chapter 7: Programming Project 5: You may click Exercise07_15 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Step 1: Implement the isSorted(numbers) function as follows:
Step 1.1: Write a for loop: for i from 0 to len(numbers) - 1, if (numbers[i] > numbers[i + 1]), return false.
Step 1.2: If nothing is return in the for loop, return true after the for loop.
Step 2: Implement the main function as follows:
Step 2.1: Enter the numbers in one line separated by spaces. Numbers are float numbers.
Step 2.2: Split the line into a list numbers.
Step 3: Invoke isSorted(numbers) to test if the elements in list are sorted.

Chapter 7: Programming Project 6: You may click Exercise07_17 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Step 1: Implement the main() function as follows:
Step 1.1: Read string s1.
Step 1.2: Read string s2.
Step 1.3: Invoke isAnagram(s1, s2) to test if s1 and s2 are anagrams.
Step 2: Implement the isAnagram(s1, s2) function as follows:
Step 2.1: If s1 and s2 have different sizes, return false.
Step 2.2: Sort s1 and s2 and return true if they are the same.
Step 3: Implement the sort(s) function to sort a string as follows:
Step 3.1: Obtain a list from s simply using list(s).
Step 3.2: Sort the list.
Step 3.3: Create a string from the list and return the string.

Chapter 8: Programming Project 1: You may click Exercise08_01 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Make sure you use the correct row and column size. The matrix has 3 rows and 4 columns.
Step 1: Implement the sumColumn(m, columnIndex)function as follows:
Step 1.1: Initialize sum.
Step 1.2: Write a for loop: for i from 0 to 3 - 1, add m[i][columnIndex] to sum. Note that the row size is 3.
Step 1.3: Return sum.
Step 2: Implement the main function as follows:
Step 2.1: Create a list m = [].
Step 2.2: Write a for loop to prompt the user to enter a row as a string. Extract the numbers (float) in the row to create a list. Add this list to m.
Step 2.3: Write a for loop. For each j from 0 to 4 - 1, invoke sumColumn(m, j) and display it. Note that the column size is 4.

Chapter 8: Programming Project 2: You may click Exercise08_05 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Step 1: Implement the addMatrix(m1, m2) function as follows:
Step 1.1: Create a two-dimensional list m3 of the same size as m1.
Step 1.2: Write a nested for loop to assign m1[i][j] + m2[i][j] to m3[i][j].
Step 1.3: Return m3.
Step 2: Implement the printResult(m1, m2, m3, op) function as follows:
Step 2.1: For each row i from 1 to len(m1), display a row in m1, m2, and m3. In the middle row, display the op between m1 and m2 and display the = symbol between m2 and m3.
Step 3: Implement the main function as follows: (numbers are float)
Step 3.1: Create list m1. Enter input for m1 in a loop one row at a time.
Step 3.2: Create list m2. Enter input for m2 in a loop one row at a time.
Step 3.3: Create list m3. Invoke m3 = addMatrix(m1, m2).
Step 3.4: Display the result by invoking printResult(m1, m2, m3)

Chapter 8: Programming Project 3: You may click Exercise08_13 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Step 1: Implement the locateLargest(a) function to return the location of the largest element in a list. The first element in the list is the row index and the second is the column index. If there are more than one largest element, return the smallest row index and then the smallest column index.
Step 2: Implement the main function.
Step 2.1: Prompt the user to enter the number of rows and columns of the 2-d list.
Step 2.2: Create a 2-d list.
Step 2.3: Prompt the user to enter the list.
Step 2.4: Invoke locateLargest(a) function to return the location of the largest element.
Step 2.5: Display the location of the largest element.

Chapter 8: Programming Project 5: You may click Exercise08_27 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Step 1: Implement the isMarkovMatrix(a) function to test if the 2-d list a is Markov matrix.
Step 2: Implement the main function.
Step 2.1: Prompt the user to enter the number of rows and columns of the 2-d list.
Step 2.2: Create a 2-d list.
Step 2.3: Prompt the user to enter the list.
Step 2.4: Invoke isMarkovMatrix(a) function to return a boolean value.
Step 2.5: Display the result.

Chapter 8: Programming Project 6: You may click Exercise08_35 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Step 1: Implement the main function.
Step 1.1: Prompt the user to enter the coordinates for the cities. cities is a two-dimensional list. Numbers are float.
Step 1.2: Use list comprehension to obtain a two-dimensional list for cities. Each list in cities is a pair of coordinates.
Step 1.3: Initialize minTotal and minIndex to store the minimum total distance and the index of the minimum total distance city. minTotal is totalDistance(cities, 0) and minIndex is 0.
Step 1.4: For every city with index i, invoke totalDistance(cities, i) to return the totalDistance. If it is < minTotal, assign totalDistance to minTotal and i to minIndex.
Step 1.5: Display the (cities[minIndex][0], cities[minIndex][1]) and minTotal for the central city.
Step 2: Implement distance(c1, c2). This function returns the distance between (c1[0], c1[1]) and (c2[0], c2[1]).
Step 3: Implement and totalDistance(cities, i). This function returns the total distance from city i to all other cities.

Chapter 9: Programming Project 1: You may click Exercise09_01 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Please note that you have to put all code in one file in order to submit to REVEL. The outline of the program may look like this:
# Exercise09_01
class Rectangle:
# Write your code

def main():
# Write your code
main()

Chapter 9: Programming Project 2: You may click Exercise09_07Extra to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Please note that you have to put all code in one file in order to submit to REVEL. The outline of the program may look like this:
Exercise09_07Extra

class Person:
    # Write your code

def main():
    # Write your code
          
main()

Chapter 9: Programming Project 3: You may click Exercise09_05 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Please note that you have to put all code in one file in order to submit to REVEL. The outline of the program may look like this:
# Exercise09_05
import math

class RegularPolygon:
# Write your code

def main():
# Write your code
          
main()

Chapter 9: Programming Project 4: You may click Exercise09_11 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Please note that you have to put all code in one file in order to submit to REVEL. The outline of the program may look like this:
# Exercise09_11
class Point:
# Write your code

def main():
# Write your code
        
main()

Chapter 9: Programming Project 5: You may click Exercise09_15 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Please note that you have to put all code in one file in order to submit to REVEL. The outline of the program may look like this:
# Exercise09_15
class Complex:
    def __init__(self, a = 0, b = 0):
        # Implement it

    def getA(self):
        # Implement it

    def getB(self):
        # Implement it

    def __add__(self, secondComplex):
        # Implement it

    def __sub__(self, secondComplex):
        # Implement it

    def __mul__(self, secondComplex):
        # Implement it

    def __truediv__(self, secondComplex):
        # Implement it
   
    def __abs__(self):
        # Implement it

    def __str__(self):
        # Implement it

    def getRealPart(self):
        # Implement it

    def getImaginaryPart(self):
        # Implement it

def main():
    a = float(input("Enter the real part of the first complex number: "))
    b = float(input("Enter the imaginary part of the first complex number: "))
    c1 = Complex(a, b)

    c = float(input("Enter the real part of the second complex number: "))
    d = float(input("Enter the imaginary part of the second complex number: "))
    c2 = Complex(c, d)

    print(c1, "+", c2, "=", c1 + c2)
    print(c1, "-", c2, "=", c1 - c2)
    print(c1, "*", c2, "=", c1 * c2)
    print(c1, "/", c2, "=", c1 / c2)
    print("|" + str(c1) + "| = " + str(abs(c1)))

main()

Chapter 12: Programming Project 1: You may click Exercise12_01 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Please note that you have to put all code in one file in order to submit to REVEL. The outline of the program may look like this:
# Exercise12_01
import math

def main():
# Write your code

class GeometricObject:
# Copy the code from the book

class Triangle(GeometricObject):
# Write your code

main()

Chapter 12: Programming Project 2: You may click Exercise12_25 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
There is an error in the template code in Revel. Please change
    print("The index of last element", element, "before index", index,
        "is", list1.lastIndexOf(element, index))
to
    print("The index of the last element", element, "before index", index,
        "is", list1.lastIndexOf(element, index))
Please note that you have to put all code in one file in order to submit to REVEL. The outline of the program may look like this:
# Exercise12_25
class MyList(list):
# Write your code
  
def main():
    list1 = MyList()
    list1.append("Chicago")
    list1.append("Detroit")
    list1.append("Denver")
    list1.append("Chicago")
    list1.append("Atlanta")
    list1.append("New York")
    list1.append("Seattle")
    list1.append("Dallas")
    list1.append("Atlanta")
    list1.append("New York")
    
    print(list1)
    index = int(input("Enter index: "))
    element = input("Enter element: ")
    print("The index of element", element, "after index", index,
        "is", list1.indexOf(element, index))
    print("The index of the last element", element, "before index", index,
        "is", list1.lastIndexOf(element, index))
  
main()

Chapter 13: Programming Project 1:

Hint:
Download these two files and save them as scores1.txt and scores2.txt in the same directory of your .py file. If your output matches the following two screen shots (character by character), your code is correct. You can then submit it to REVEL.

Chapter 13: Programming Project 2:

Download these two files and save them as string1.txt and string2.txt in the same directory of your .py file. If your output matches the following two screen shots (character by character), your code is correct. You can then submit it to REVEL.

Chapter 14: Programming Project 1: You may click Exercise14_03Extra to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Step 1: Use a set vowels to store the vowels. vowels = {'A', 'E', 'I', 'O', 'U'}.
Step 2: Read the input as a string and convert it to uppercase.
Step 3: Initialize variables countVowels and countConsonants with 0.
Step 4: For each character in the string, if the character is in the vowels set, increase the count by 1.

Chapter 14: Programming Project 3: You may click Exercise14_05Extra to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Here are Python keywords from Appendix A.
    keyWords = {"and", "del", "from", "not", "while",     
                "as", "elif", "global", "or", "with",      
                "assert", "else", "if", "pass", "yield",    
                "break", "except", "import", "print",               
                "class", "exec", "in", "raise",              
                "continue", "finally", "is", "return",              
                "def", "for", "lambda", "try"}

Chapter 15: Programming Project 1: You may click Exercise15_01 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
The program contains the main function and the sumDigits(n). The main function prompts the user for the input number n. It then invokes sumDigits(n) to return the sum of the digits in n.
sumDigits(n) returns n if n is a single digit. Otherwise, it returns sumDigits(n // 10) + sumDigits(n % 10). n % 10 is the last digit in n. n // 10 is the remaining number after removing the last digit.

Chapter 15: Programming Project 4: You may click Exercise15_13 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
The program contains the main function and two overloaded count functions. The main function prompts the user for the input string and then a character. It then invokes the count(const string& s, char a) function.
The count(const string& s, char ch) function invokes count(s, ch, s.length() - 1).
The count(s, ch, high) is a recursive helper function. The function returns 0 if high < 0. Otherwise, it returns count(s, ch, high - 1) + (s[high] == ch ? 1 : 0).

Chapter 15: Programming Project 5: You may click Exercise15_19 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
For simplicity, you can assume that the decimal integer is greater than 0. Your submission will work with this assumption.
The program contains the main function and the decimalToBinary function. The main function prompts the user to enter an integer then invokes the decimalToBinary(int) to return a binary string for the integer. It displays the binary string.
The decimalToBinary(value) function returns "" if value is 0, otherwise, it returns decimalToBinary(value // 2) + str(value % 2).
Note that decimalrBinary(value // 2) returns a string. value % 2 is 0 or 1.

Chapter 18: Programming Project 1: You may click Exercise18_01 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Use the code template from the project description to complete the exercise.

Chapter 18: Programming Project 2: You may click Exercise18_03 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
You need to submit the entire program for this exercise.

Chapter 18: Programming Project 4: You may click Exercise18_01Extra to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Use the code template from the project description to complete the exercise.
Study the code in LiveExample 18.13 in Section 18.8 using some samples, for example, 1+2, 2 * 3 - 3, etc.
Modify the example EvaluateExpression.java incrementally. Once step at a time and you will know which step you are struggling.
Step 1. The operator % can be implemented similar to the * and / operators. Add the code for processing % in lines 39-47 in LiveExample 18.13.
Step 2. The operator ^ has the highest precedence. However, note that the ^ operator is right-associative, meaning that 2 ^ 3 ^ 2 is same as 2 ^ (3 ^ 2). In lines 51-61 in LiveExample 18.13, the program processes the * and / operators, add the code for processing the ^ operator after this block.

Chapter 19: Programming Project 1: You may click Exercise19_01 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Use the code template from the project description to complete the exercise. Hint:
For the definition of the height of a binary tree, see Section 19.2. Use recursion. If the tree is empty (i.e., root is None), return -1, else return 1 + the max of the height of the left and right subtrees.

Chapter 19: Programming Project 2: You may click Exercise19_03 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Use the code template from the project description to complete the exercise. Copy the height() method from the preceding project. You can compare the tree size with 2^(height+1) - 1 to determine if the tree is perfect.

Chapter 19: Programming Project 3: You may click Exercise19_07 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Use the code template from the project description to complete the exercise.
Hint:
Use recursion. If the tree is empty (i.e., root is None), return 0, else return 1 + the number of the non-leaf nodes in the left subtree + the number of the non-leaf nodes in the right subtree.

Chapter 19: Programming Project 4: You may click Exercise19_11 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Use the code template from the project description to complete the exercise.

Chapter 19: Programming Project 5: You may click Exercise19_01Extra to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Use the code template from the project description to complete the exercise.

Chapter 20: Programming Project 1: You may click Exercise20_07 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Use the code template from the project description to complete the exercise.

Chapter 21: Programming Project 1: You may click Exercise21_01 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Use the code template from the project description to complete the exercise.

Chapter 22: Programming Project 1: You may click Exercise22_01Extra to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Use the code template from the project description to complete the exercise.

Chapter 22: Programming Project 2: You may click Exercise22_05 to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Use the code template from the project description to complete the exercise.

Chapter 23: Programming Project 1: You may click Exercise23_01Extra to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Use the code template from the project description to complete the exercise.

Chapter 23: Programming Project 2: You may click Exercise23_02Extra to use the CheckExerciseTool to check and debug your code in addition to getting feedback from Revel.

Hint:
Use the code template from the project description to complete the exercise.