Re: Function to take the minimum of 3 numbers

2016-10-10 Thread Steve D'Aprano
On Mon, 10 Oct 2016 10:56 am, Ian Kelly wrote:

> On Oct 9, 2016 2:57 PM,  alleged:

> The Pythonic way
> 
> if b >= a <= c:
> ...

I doubt that would be considered Pythonic by many people. Chained
comparisons are Pythonic, but not legal but weird combinations like 
`a == b < c is not d >= e <= f`.


> Better:
> 
> if a <= b <= c:
> ...

That's more like it. Unfortunately it doesn't mean the same as Mark's
version:

b >= a <= c means b >= a and a <= c
which is True for a = 1, b = 3 and c = 2;


a <= b <= c means a <= b and b <= c
which is False for a = 1, b = 3 and c = 2.


> Using consistent operators is not required but is easier to read and less
> confusing.

Indeed.





-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Function to take the minimum of 3 numbers

2016-10-09 Thread Chris Angelico
On Mon, Oct 10, 2016 at 11:38 AM, Matt Wheeler  wrote:
> On Mon, 10 Oct 2016, 00:56 Ian Kelly,  wrote:
>
>> On Oct 9, 2016 2:57 PM,  wrote:
>> The Pythonic way
>>
>> if b >= a <= c:
>> ...
>>
>>
>> Better:
>>
>> if a <= b <= c:
>> ...
>>
>
> That's not equivalent. Consider `a, b, c = 1, 7, 4`
>
>
>> Using consistent operators is not required but is easier to read and less
>> confusing.
>>
>
> Unfortunately in this case it's also less correct

Proof that it's confusing: Someone tried to tidy up the code, and
unwittingly changed its semantics.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Function to take the minimum of 3 numbers

2016-10-09 Thread Matt Wheeler
On Mon, 10 Oct 2016, 00:56 Ian Kelly,  wrote:

> On Oct 9, 2016 2:57 PM,  wrote:
> The Pythonic way
>
> if b >= a <= c:
> ...
>
>
> Better:
>
> if a <= b <= c:
> ...
>

That's not equivalent. Consider `a, b, c = 1, 7, 4`


> Using consistent operators is not required but is easier to read and less
> confusing.
>

Unfortunately in this case it's also less correct

> --

--
Matt Wheeler
http://funkyh.at
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Function to take the minimum of 3 numbers

2016-10-09 Thread Ian Kelly
On Oct 9, 2016 2:57 PM,  wrote:

On Sunday, October 9, 2016 at 2:41:41 PM UTC+1, BartC wrote:
> def min3(a,b,c):
>  if a<=b and a<=c:
>  return a
>  elif b<=a and b<=c:
>  return b
>  else:
>  return c

The Pythonic way

if b >= a <= c:
...


Better:

if a <= b <= c:
...

Using consistent operators is not required but is easier to read and less
confusing.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Function to take the minimum of 3 numbers

2016-10-09 Thread Larry Hudson via Python-list

On 10/09/2016 05:01 AM, Cai Gengyang wrote:

def min3(a, b, c):

min3 = a
if b < min3:
min3 = b
if c < min3:
min3 = c
if b < c:
min3 = b
return min3


print(min3(4, 7, 5))

4



This is NOT a recommendation here, just a different way of looking at the problem (and is 
probably cheating anyway)...


def min3(a, b, c):
return sorted([a, b, c])[0]  #  Create a list, sort it and return the 1st 
element

It can be extended to take any number of parameters:

def minn(*args):
return sorted(args)[0]

The * syntax takes all the parameters and puts them into a single tuple.

This is _almost_ the same as the built-in min() function — the built-in requires a single 
(sequence) parameter, my minn() function requires multiple individual parameters.  (Both 
versions fail with zero parameters.)


--
 -=- Larry -=-
--
https://mail.python.org/mailman/listinfo/python-list


Re: Function to take the minimum of 3 numbers

2016-10-09 Thread breamoreboy
On Sunday, October 9, 2016 at 2:41:41 PM UTC+1, BartC wrote:
> On 09/10/2016 13:01, Cai Gengyang wrote:
> > I'm moving on to chapter 9 
> > (http://programarcadegames.com/index.php?lang=en=lab_functions) of 
> > programarcadegames for the time being and going back to chapter 8 later 
> > (its just fucking frustrating and doesn't seem to work for the time being 
> > and I have a very bad temper).
> >
> > At least for chapter 9, I got the first part correct at first try --- 
> > define a function that takes and prints the smallest of 3 numbers. I pasted 
> > it here for reference and discussion. The code can also be further modified 
> > to include a clause that says that if two numbers tie for smallest, choose 
> > any of the two numbers. (I will attempt to do it and then post it here 
> > again for future discussion with users here
> >
> >
>  def min3(a, b, c):
> > min3 = a
> > if b < min3:
> > min3 = b
> > if c < min3:
> > min3 = c
> > if b < c:
> > min3 = b
> > return min3
> >
>  print(min3(4, 7, 5))
> > 4
> >
> 
> The exercise says you must use an if/elif chain. The first thing that 
> comes to mind is:
> 
> def min3(a,b,c):
>  if a<=b and a<=c:
>  return a
>  elif b<=a and b<=c:
>  return b
>  else:
>  return c
> 
> The bit about numbers tying for smallest is not meaningful; the caller 
> can't tell if a minimum value of 42 came from a, b or c.
> 
> -- 
> Bartc

The Pythonic way

if b >= a <= c:
...

Kindest regards.

Mark Lawrence.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Function to take the minimum of 3 numbers

2016-10-09 Thread Chris Angelico
On Mon, Oct 10, 2016 at 12:41 AM, BartC  wrote:
> The exercise says you must use an if/elif chain. The first thing that comes
> to mind is:
>
> def min3(a,b,c):
> if a<=b and a<=c:
> return a
> elif b<=a and b<=c:
> return b
> else:
> return c
>
> The bit about numbers tying for smallest is not meaningful; the caller can't
> tell if a minimum value of 42 came from a, b or c.

You assume that all equal numbers are identical. (And I say "numbers"
only because the problem definition did. This code does not require
numbers necessarily.)

>>> min3(1, True, 1.0)
1
>>> min3(1.0, True, 1)
1.0
>>> min3(True, 1.0, 1)
True
>>> 1 == 1.0 == True
True

>>> x = 1.0
>>> y = 1.0
>>> z = 1.0
>>> min3(x, y, z) is x
True
>>> min3(x, y, z) is y
False
>>> min3(x, y, z) is z
False

Your function provably returns the first number if given equal
numbers. That stipulation is far from meaningless; it explicitly
permits any means of selection, as long as one of the input values is
returned. Thus, for instance, this must always be valid:

>>> assert min3(x, y, z) in (x, y, z)

So this solution would NOT be valid:

def min2(a, b):
return (a+b)/2 - abs(a-b)/2
def min3(a, b, c):
return min2(a, min2(b, c))

It's mathematically perfect but inappropriate to this challenge, as it
violates the given rule.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Function to take the minimum of 3 numbers

2016-10-09 Thread BartC

On 09/10/2016 13:01, Cai Gengyang wrote:

I'm moving on to chapter 9 
(http://programarcadegames.com/index.php?lang=en=lab_functions) of 
programarcadegames for the time being and going back to chapter 8 later (its just 
fucking frustrating and doesn't seem to work for the time being and I have a very 
bad temper).

At least for chapter 9, I got the first part correct at first try --- define a 
function that takes and prints the smallest of 3 numbers. I pasted it here for 
reference and discussion. The code can also be further modified to include a 
clause that says that if two numbers tie for smallest, choose any of the two 
numbers. (I will attempt to do it and then post it here again for future 
discussion with users here



def min3(a, b, c):

min3 = a
if b < min3:
min3 = b
if c < min3:
min3 = c
if b < c:
min3 = b
return min3


print(min3(4, 7, 5))

4



The exercise says you must use an if/elif chain. The first thing that 
comes to mind is:


def min3(a,b,c):
if a<=b and a<=c:
return a
elif b<=a and b<=c:
return b
else:
return c

The bit about numbers tying for smallest is not meaningful; the caller 
can't tell if a minimum value of 42 came from a, b or c.


--
Bartc
--
https://mail.python.org/mailman/listinfo/python-list


Re: Function to take the minimum of 3 numbers

2016-10-09 Thread Ben Bacarisse
Cai Gengyang  writes:

> I'm moving on to chapter 9
> (http://programarcadegames.com/index.php?lang=en=lab_functions)
> of programarcadegames for the time being and going back to chapter 8
> later (its just fucking frustrating and doesn't seem to work for the
> time being and I have a very bad temper).
>
> At least for chapter 9, I got the first part correct at first try ---
> define a function that takes and prints the smallest of 3 numbers. I
> pasted it here for reference and discussion.

Presumably you must do this without Python's min function?

> The code can also be
> further modified to include a clause that says that if two numbers tie
> for smallest, choose any of the two numbers.

What would be the point?  Your code already does that.

 def min3(a, b, c):
> min3 = a
> if b < min3:
> min3 = b
> if c < min3:
> min3 = c
> if b < c:
> min3 = b
> return min3

The last if is not needed.

Forced to pretend that there is no min function already, I'd be tempted
to write

  def min3(a, b, c):
  def min2(x, y):
  return x if x < y else y;
  return min2(a, min2(b, c))

-- 
Ben.
-- 
https://mail.python.org/mailman/listinfo/python-list


Function to take the minimum of 3 numbers

2016-10-09 Thread Cai Gengyang
I'm moving on to chapter 9 
(http://programarcadegames.com/index.php?lang=en=lab_functions) of 
programarcadegames for the time being and going back to chapter 8 later (its 
just fucking frustrating and doesn't seem to work for the time being and I have 
a very bad temper). 

At least for chapter 9, I got the first part correct at first try --- define a 
function that takes and prints the smallest of 3 numbers. I pasted it here for 
reference and discussion. The code can also be further modified to include a 
clause that says that if two numbers tie for smallest, choose any of the two 
numbers. (I will attempt to do it and then post it here again for future 
discussion with users here 


>>> def min3(a, b, c):
min3 = a
if b < min3:
min3 = b
if c < min3:
min3 = c
if b < c:
min3 = b
return min3

>>> print(min3(4, 7, 5))
4
-- 
https://mail.python.org/mailman/listinfo/python-list