Discussion:
[Tutor] Complications Take Two (Long) Frustrations.
Joseph Gulizia
2015-08-20 03:12:56 UTC
Permalink
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
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Roel Schroeven
2015-08-21 20:58:23 UTC
Permalink
Post by Joseph Gulizia
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.
--------------------------------------
Code that gave best results but didn't work for negative numbers...
--------------------------------------
Original = abs(max (-A, -B))
print (Original)
--------------------------------------
Did not pass tests.
You change the sign of A and B before passing them to max(). To revert
that afterwards, you just need to change the sign of the result again.

abs() indeed doesn't work for that: it only changes the sign on negative
numbers, not on positive numbers.


Regards,
Roel
--
The saddest aspect of life right now is that science gathers knowledge
faster than society gathers wisdom.
-- Isaac Asimov

Roel Schroeven

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Joel Goldstick
2015-08-21 21:22:09 UTC
Permalink
Post by Joseph Gulizia
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.
--------------------------------------
Code that gave best results but didn't work for negative numbers...
--------------------------------------
Original = abs(max (-A, -B))
print (Original)
--------------------------------------
Did not pass tests.
You change the sign of A and B before passing them to max(). To revert that
afterwards, you just need to change the sign of the result again.
abs() indeed doesn't work for that: it only changes the sign on negative
numbers, not on positive numbers.
Regards,
Roel
--
The saddest aspect of life right now is that science gathers knowledge
faster than society gathers wisdom.
-- Isaac Asimov
Roel Schroeven
so:
print -max(-A, -B)
_______________________________________________
https://mail.python.org/mailman/listinfo/tutor
--
Joel Goldstick
http://joelgoldstick.com
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Roel Schroeven
2015-08-21 21:29:52 UTC
Permalink
Post by Joel Goldstick
print -max(-A, -B)
That's what I mean, yes. I haven't tried it, but I don't see why it
wouldn't work.


Regards,
Roel
--
The saddest aspect of life right now is that science gathers knowledge
faster than society gathers wisdom.
-- Isaac Asimov

Roel Schroeven

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Steven D'Aprano
2015-08-22 07:00:55 UTC
Permalink
Post by Roel Schroeven
Post by Joel Goldstick
print -max(-A, -B)
That's what I mean, yes. I haven't tried it, but I don't see why it
wouldn't work.
It won't work with anything which isn't a number:

py> min("hello", "goodbye")
'goodbye'


But the max trick fails:

py> -max(-"hello", -"goodbye")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: bad operand type for unary -: 'str'


If you want to write your own min without using the built-in, there is
only one correct way to do it that works for all objects:

def min(a, b):
if a < b: return a
return b

Well, more than one way -- you can change the "a < b" to "a <= b" if you
prefer. Or reverse the test and use >, or similar, but you know what I
mean.
--
Steve
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Laura Creighton
2015-08-22 08:10:49 UTC
Permalink
Post by Steven D'Aprano
Post by Roel Schroeven
Post by Joel Goldstick
print -max(-A, -B)
That's what I mean, yes. I haven't tried it, but I don't see why it
wouldn't work.
py> min("hello", "goodbye")
'goodbye'
py> -max(-"hello", -"goodbye")
File "<stdin>", line 1, in <module>
TypeError: bad operand type for unary -: 'str'
If you want to write your own min without using the built-in, there is
if a < b: return a
return b
Well, more than one way -- you can change the "a < b" to "a <= b" if you
prefer. Or reverse the test and use >, or similar, but you know what I
mean.
--
Steve
Yes, but I think the OP's problem is that he has a fool for a teacher,
or a course designer at any rate. For some reason the author thinks
that the fact that max(A, B) == -max(-A, -B) (for integers) is very,
very clever.

And somehow the teacher hasn't learnt that his or her job is to make
students question 'clever programming' while not distroying the
enthusiasm of any students who come up with clever solutions on their
own. Cleverness is the consolation prize in this business -- what you
want to write is code that demonstrates wisdom, not cleverness.

They are fun to write, though.

But remember:

Everyone knows that debugging is twice as hard as writing a
program in the first place. So if you're as clever as you can be
when you write it, how will you ever debug it?

— Brian Kernighan The Elements of Programming Style

So the reflex you want to develop is 'I just did something clever.
Hmmm. Maybe _too_ clever. Let's see ...' The cleverer you are as a
person, the more you have to develop this reflex, because after all,
somebody much less clever -- or experienced -- than you are may have
to fix a bug in your code some day.

so the max(A, B) == -max(-A, -B) trick has everything to do with
'Watch me pull a rabbit out of this hat' and nothing to do with
'good programming style'.

Too much education of the sort that rewards cleverness and penalises
wisdom means we end up with a lot of smart people in this world who
have managed to get the idea that 'Wisdom is something that only
stupid people need. It is optional for smart people, and I am smart
enough to do without!'

Some people _never_ unlearn this one. My family is, alas, full of them.

Laura

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
Roel Schroeven
2015-08-25 21:16:07 UTC
Permalink
Post by Roel Schroeven
Post by Joel Goldstick
print -max(-A, -B)
That's what I mean, yes. I haven't tried it, but I don't see why it
wouldn't work.
True, I hadn't thought of that.

It didn't occur to me to think about anything else than numbers, but I
went back to the original post and the instructions never mention numbers.

I need to take more care to think out of the box I'm putting myself into
sometimes.
--
The saddest aspect of life right now is that science gathers knowledge
faster than society gathers wisdom.
-- Isaac Asimov

Roel Schroeven

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