Computer Science Question

Question 1

Using conditionals, design and write greatest_of_four, which takes in four numbers and outputs the largest number. Your function should work for all numeric inputs. Your algorithm can use no more than three conditionals.

Save Time On Research and Writing
Hire a Pro to Write You a 100% Plagiarism-Free Paper.
Get My Paper

Question 2

In the a walkthrough video in Module 1, we created an algorithm that determined how long it takes two trains to crash. That walkthrough assumed that the trains were always headed towards each other and would crash for the sake of simplicity. Now that we know about conditionals redesign that algorithm to remove that restriction.

Your algorithm should be named determine_crash, and it must take three inputs — the two train velocities in meters per second and the starting distance between them in kilometers — and display either

“The trains are fine!”

or “Crash in TIME minutes!” depending on if the trains will crash.

To help orient yourself, consider the velocity’s sign as the direction of travel. When the velocity is positive, we are going east (or rightward); when negative, we are going west (or leftward). Assume that the first train (train A) will always start to the west (on the left side) of the second train (train B). We can reduce all of the logic in determining if a train will crash to these statements. The trains will collide only if one of following are true:

Save Time On Research and Writing
Hire a Pro to Write You a 100% Plagiarism-Free Paper.
Get My Paper
  • A and B go east and A is faster than B.
  • A and B go west and B is faster than A.
  • A and B head towards each other.

After planning, work on your black-box tests. Remember! We have to test all of the ways a condition can be true or false in order to be complete! Your algorithm must work for all numeric inputs.

The following are some tests you can use to build confidence.

Test Cases for Question 2

InputsDisplays

“The trains are fine!”

, 0.1)

“The trains are fine!”

“The trains are fine!”

(0.0, 0.0, 0.1)
(0.0,

1.0
(-1.0, 0.0, 0.1)
(1.0, -1.0, 0.1) “Crash in 0.8333333333333334 minutes!”

Question 3

Using the information in the syllabus, design two algorithms to solve the following problems.

Question 3.1

This course has many different types of assignments, all of which are weighted differently. Using the syllabus as a guide, do the following:

Design the function final_percentage, which will compute a student’s final percentage given the unweighted percentages from each category listed in the syllabus. Inputs must follow the same order as given in the syllabus and will all be decimal numbers in the range of 0.0 to 1.0. If your algorithm receives a value outside that range, it should “clamp” the number to the nearest bound. For example, clamping the values (-5, 1.1, 0.8) would produce the result (0.0, 1.0, 0.8).Hint: It might be beneficial to divide this problem and design another algorithm as a helper. Make sure to test!

After calculating the weighted percentage, you should display this value as a native percentage in the range (0.0, 100.0) with an attached percent symbol (%). For example, the value (0.5) displays as “50.0%” and not “50.0 %”. If you get stuck here, look into your notes on how we’ve shown how to display data. Your algorithm should return the calculated weighted percentage as a decimal with the same bounds as the inputs.

In addition to the tests that you write, you can use the following examples to build confidence in your code:

Test Cases for Question 3.1

InputsDisplaysOutputs

1.0

(1.0, 1.0, 1.0, 1.0, 1.0, 1.0) “100%”
(0.5, 0.5, 0.5, 0.5, 1.0, 1.0) “75.0%” 0.75
(1.0, 1.0, 1.0, 1.0, 0.5, 0.0) “62.500000000000014%” 0.6250000000000001
(0.6, 0.73, 0.2, 99.9, 0.85, 2.0) “74.33%” 0.7433

Question 3.2

Percentage scores are nice, but often we want to reduce that to a simple letter grade for readability. Design another function called letter_grade that takes a decimal number and displays the appropriate letter grade using the scale defined in the syllabus. Be careful about edge cases (e.g., 0.94 is an “A” while 0.933333 is an “A-“)! If a value is outside the valid range, display “I” to indicate that it is invalid.

For this problem, do not use logical or chained relational operators, and test all branches!

Still stressed with your coursework?
Get quality coursework help from an expert!