[Tutor] Complications Take Two (Long) Frustrations.
Joseph Gulizia
joseph.gulizia at gmail.com
Thu Aug 20 05:12:56 CEST 2015
Complicating a simple expression
Coding Exercise: Complication
Assume that the grader defines two variables A and B for you. Write a
program which prints out the value
min(A, B)
However, there is a catch: your program is not allowed to use the min
function. Instead, use max in a clever way to simulate min.
Hint, Method 1
What is max(-A, -B)?
Hint, Method 2
What is min(A, B)+max(A, B)?
--------------------------------------
Code that gave best results but didn't work for negative numbers...
--------------------------------------
Original = abs(max (-A, -B))
print (Original)
--------------------------------------
Did not pass tests. Please check details below and try again.
Results for test case 1 out of 5
Before running your code: We defined A equal to 35 and B equal to 45.
Program executed without crashing.
Program gave the following correct output:
35
Results for test case 2 out of 5
Before running your code: We defined A equal to 65 and B equal to 20.
Program executed without crashing.
Program gave the following correct output:
20
Results for test case 3 out of 5
Before running your code: We defined A equal to 48 and B equal to 63.
Program executed without crashing.
Program gave the following correct output:
48
Results for test case 4 out of 5
Before running your code: We defined A equal to 0 and B equal to 70.
Program executed without crashing.
Program gave the following correct output:
0
Results for test case 5 out of 5
Before running your code: We defined A equal to -64 and B equal to 0.
Program executed without crashing.
Program output:
64
Expected this correct output:
-64
Result of grading: Your output is not correct.
Spreadsheet examples:
A B Min(A, B) Max(-A,- B)
10 5 5 - 5
5 10 5 - 5
9 12 9 - 9
12 9 9 - 9
22 37 22 - 22
37 22 22 - 22
45 68 45 - 45
68 45 45 - 45
- 6 15 - 6 6
-15 6 - 15 15
-80 - 65 - 80 80
-65 - 80 - 80 80
44 -102 -102 102
-44 102 - 44 44
CS Assistant2 stated:
Using the absolute value of the numbers will cause problems with this
solution because sometimes the answer should be a negative number. However,
when you calculate the absolute value of a number, that result will always
be larger than any negative number.
I would suggest you go back to your original table, but include some values
for A and B that are negative numbers (A is negative, B is negative, A and
B are both negative). See what numbers you get for min(A, B) and max(-A,
-B) in those cases.
Think about ways, other than absolute value, that will allow you to convert
a negative number to a positive number and vice versa.
I hope this helps.
Sandy
CS Assistant1 stated:
Hi,
Gathering this much data is a very good start! The two hints give two
different approaches. So let me highlight the 4 most relevant columns:
A B Min(A, B) Max(-A,- B)
10 5 5 -5
5 10 5 -5
9 12 9 -9
12 9 9 -9
22 37 22 -22
37 22 22 -22
45 68 45 -45
68 45 45 -45
What's the relationship between min(a, b), which you want but can't
directly call, and max(-a, -b), which you can compute? Feel free to ask if
another hint would help.
Best,
- Dave
More information about the Tutor
mailing list