1
def sorting(a, b, c):
    array = [a, b, c]
    newarray = array.sort()
    newarray2 = array.reverse()
    if newarray == array:
        print ('The fish is rising')
    elif newarray2 == array:
        print ('The fish is decesnding')
    else:
        print ('No fish')


def main():
    sorting((int(input('1st'))), (int(input('2nd'))), (int(input('3rd'))))

main()

Basically, it the numbers that are inputted are increasing then the fish is rising and if the numbers are decreasing the fish is decreasing. Finally if the numbers inputted are not consecutively increasing or decreasing i.e. 1, 3, 2 then it should output no fish. Even if there is another more efficient way of doing this which there probably is, can someone please explain how to compare two identical lists to see if they are in the same order?

oz123
  • 27,559
  • 27
  • 125
  • 187
user1814771
  • 89
  • 2
  • 8
  • given two lists...to see if they are in the same order, just iterate through both lists at the same time, and check to see if the elements are different at the same index for all the indexes. If there is ever a difference, even one, then the lists are in a different order – Josh Engelsma Feb 17 '14 at 06:05

4 Answers4

4

When you use .sort and .reverse functions, they sort/reverse the data in-place and return None. So, you are assigning None to both newarray and newarray2. Then in the conditions, you are checking whether None == None which is True always. That is why your code fails to work properly.

The straightforward and the generic way I could think of, is this

def sorting(*args):
    sorted_list, args = sorted(args), list(args)
    if sorted_list == args:
        print "Rising"
    elif sorted_list[::-1] == args:
        print "Descending"
    else:
        print "No Fish"

sorting(1, 2, 3, 4)        # Rising
sorting(5, 4, 3, 2, 1)     # Descending
sorting(2, 1, 3, 6, 4, 5)  # No Fish
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
  • I understand your code and it obviously works, but other than using sorted instead of sort which I am led to understand accomplish the same goal, why did mine fail to work? – user1814771 Feb 17 '14 at 06:12
  • 2
    @user1814771 `list.sort` and `list.reverse` change the list in-place and return `None`, so you're comparing `None == None`. – Ashwini Chaudhary Feb 17 '14 at 06:14
  • @AshwiniChaudhary Thanks for explaining it to the OP :) I included the same explanation in my answer as well. – thefourtheye Feb 17 '14 at 06:24
  • Yup you both have been a great help! I changed them to sort and ::-1 which I'm starting to realize has something to do with splicing, so I'm going to have to learn that tomorrow. Thanks for the help! – user1814771 Feb 17 '14 at 06:31
1

Why your snippet is not working:

**list.sort:**

>>> help(list.sort)
Help on method_descriptor:

sort(...)
   L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
   cmp(x, y) -> -1, 0, 1
>>>

After sorting it returns nothing i.e so your newarray or newarray2 doesn't ve anything

example:

>>> nar = [3,2,1]
>>> array = nar.sort()
>>> array
>>> nar
[1, 2, 3]
>>>

So, please use sorted() builtin function.

James Sapam
  • 16,036
  • 12
  • 50
  • 73
0

Its easier to do this using numpy arrays ...

import numpy as np
def abc(*args):
    xx = np.array(args)
    if    all(xx[1:] > xx[:-1]): print 'Increasing order'
    elif  all(xx[1:] < xx[:-1]): print 'Decreasing order'
    else:                        print 'Rando order'
user2357112
  • 260,549
  • 28
  • 431
  • 505
ssm
  • 5,277
  • 1
  • 24
  • 42
  • Doesn't work - try `abc((1, 2), (2, 1), (3, 0))`. Also, introducing a numpy dependency for this is overkill. – user2357112 Feb 17 '14 at 06:44
  • In [23]: def abc(*args): ....: xx = array(args) ....: print xx[1:] > xx[:-1] ....: In [24]: abc(1,2,3,4,5) [ True True True True] – ssm Feb 17 '14 at 07:21
  • It works fine on my computer. I thought the idea was to use it as: abc(1,2,3,4,5), as in the question posted: def sorting(a, b, c): – ssm Feb 17 '14 at 07:23
  • `(1, 2)` is less than `(2, 1)`, which is less than `(3, 0)`, but your code produces a `ValueError` for that case. – user2357112 Feb 17 '14 at 07:23
  • You are not supposed to compare tuples in the original question. The original question is, given 3 numbers in a function `def sorting(a,b,d)` how will we check if the inputs are `a>b>c` or `a – ssm Feb 17 '14 at 07:30
  • Here let me quote the supposed input ... " ... it the _numbers_ that are inputted are increasing then the fish is rising ... " The function is called as follows `sorting((int(input('1st'))), (int(input('2nd'))), (int(input('3rd'))))`. So the _numbers_ are integers. – ssm Feb 17 '14 at 07:32
  • Hmm. True. It's still a completely unnecessary dependency when you could have just used a genexp, but not worth a downvote. – user2357112 Feb 17 '14 at 08:09
-1

If you don't want do sort the lists, you can use the following snippet:

for i,j in zip(*(a,b)):
    if i == j:
        continue
    else:
        print 'lists are not equal'
        break

This is basically implementing what Josh proposed.

A quick demo:

In [7]: for i,j in zip(*(a,b)):
   ...:     print i,j
   ...:     if i==j:
   ...:         continue
   ...:     else:
   ...:         print 'lists not equal'
   ...:         break
   ...:     
a a
b b
c c

In [8]: a='abd'

In [9]: for i,j in zip(*(a,b)):
    print i,j
    if i==j:
        continue
    else:
        print 'lists not equal'
        break
   ...:     
a a
b b
d c
lists not equal
oz123
  • 27,559
  • 27
  • 125
  • 187