[Tutor] Complicating a simple expression (Python 3)

2015-08-16 Thread Joseph Gulizia
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)?


My last code that worked somewhat
---
Original = max(A, B)
Opposite = max(-A, -B)
Grader =max(-A,- B)+max(A, B)

print (Grader)

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 62 and B equal to 36.

Program executed without crashing.
Program output:

26

Expected this correct output:

36

Result of grading: Your output is not correct.


Spreadsheet examples:
ABMax(A, B)Min(A, B)Min(A, B)+Max(A, B)Max(A,
B)+Max(A, B)Min(A, B)+Min(A, B)
105105152010
510105152010
912129212418
129129212418
22373722597444
37223722597444
4568684511313690
6845684511313690


Max(-A,- B)Max(-A,- B)+Max(A, B)Max(-A,- B)-Max(A, B)
-55-15
-55-15
-93-21
-93-21
-2215-59
-2215-59
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Complicating a simple expression (Python 3)

2015-08-16 Thread Alan Gauld

On 16/08/15 21:28, Joseph Gulizia wrote:


Assume that the grader defines two variables A and B for you. Write a
program which prints out the value
min(A, B)


So far a trivial exercise.


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.


Now its just a stupid exercise. I rally hate when teachers
do this. It shows great lack of imagination and actively
teaches bad habits... sigh!


Hint, Method 1
What is max(-A, -B)?



Hint, Method 2
What is min(A, B)+max(A, B)?


Note that these are two separate methods.
You don't need both of them. In fact you
don't really need either of them! Writing
the min functionality directly is not
exactly difficult!


My last code that worked somewhat
---
Original = max(A, B)
Opposite = max(-A, -B)
Grader =max(-A,- B)+max(A, B)

print (Grader)


OK, Can you explain how you thought that might work?


Before running your code: We defined A equal to 62 and B equal to 36.

26


Which is what doing the math suggests:

max (-62, -36) is -36
and max(62,36) is 62
so 62 + (-36) is 26


Expected this correct output:

36


Which is the correct min of 36 and 62


Result of grading: Your output is not correct.


Spreadsheet examples:
ABMax(A, B)Min(A, B)Min(A, B)+Max(A, B)Max(A,
B)+Max(A, B)Min(A, B)+Min(A, B)
105105152010
510105152010
912129212418
129129212418
22373722597444
37223722597444
4568684511313690
6845684511313690


Max(-A,- B)Max(-A,- B)+Max(A, B)Max(-A,- B)-Max(A, B)
-55-15
-55-15
-93-21
-93-21
-2215-59
-2215-59


This probably lost a little in (email) translation, but looks like
overkill to me. Choose one of the two methods above, since
that's what they ask you to do.

But it's just as easy to implement min() directly:

if A  B: min = A
else: min = B

HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor