Python Forum
Even/Odd permutation - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Even/Odd permutation (/thread-11603.html)



Even/Odd permutation - braankoo - Jul-17-2018

Hi,

can somebody please help me how to write function for checking is permutation odd or even.
Here is example of even permutation: [0,3,2,4,5,6,7,1,9,8]

I don't do python at all, but i need this thing...
Thank you.

P.S.
I guess this is 3 sec for somebody who knows what he does :)


RE: Even/Odd permutation - Larz60+ - Jul-17-2018

We don't write code for you, but we are glad to help with code that you have written.
Make an attempt and show what you've got.


RE: Even/Odd permutation - braankoo - Jul-17-2018

Sorry, I didn't want you to think that way.

I guess I can write it in PHP, but can you please translate it to python after? :)


RE: Even/Odd permutation - buran - Jul-17-2018

(Jul-17-2018, 06:08 PM)braankoo Wrote: I guess I can write it in PHP, but can you please translate it to python after?
well, no...
post your python code in python tags, any traceback in error tags and ask specific questions.


RE: Even/Odd permutation - braankoo - Jul-18-2018

def is_even(p):
  i = 0
  count = 0
  while(i < len(p)):
    k = i + 1
    while(k < len(p)):
      if(p[i] > p[k]):
        count+=1
      k+=1
    i+=1
  if(count == 0):
    return True
  if(count % 2 == 0):
    return True
  else:
    return False
Here is some solution. Could it be done better? Yes.
:)

This is my first code in python ever, so I was kind of 'scared'.


RE: Even/Odd permutation - Larz60+ - Jul-18-2018

Good work.
Just one note, indentation should be 4 spaces. This is a coding style described in PEP8: https://www.python.org/dev/peps/pep-0008/?#indentation


RE: Even/Odd permutation - buran - Jul-18-2018

(Jul-18-2018, 12:11 AM)braankoo Wrote: Could it be done better?
yes. it works, but is heavily influenced by your experience with other languages.
for example we don't use indexes to iterate over elements in a sequence. We reference the elements. And use enumerate if the index is needed too. So something more pythonic would be

def is_even(sequence):
    my_count = 0
    for i, num in enumerate(sequence, start=1):
        my_count += sum(num>num2 for num2 in sequence[i:]) 
    return not my_count % 2



RE: Even/Odd permutation - braankoo - Jul-18-2018

Thank you !


RE: Even/Odd permutation - kingjacob1515 - Jan-09-2021

(Jul-18-2018, 06:44 AM)buran Wrote:
(Jul-18-2018, 12:11 AM)braankoo Wrote: Could it be done better?
yes. it works, but is heavily influenced by your experience with other languages.
for example we don't use indexes to iterate over elements in a sequence. We reference the elements. And use enumerate if the index is needed too. So something more pythonic would be

def is_even(sequence):
    my_count = 0
    for i, num in enumerate(sequence, start=1):
        my_count += sum(num>num2 for num2 in sequence[i:]) 
    return not my_count % 2

This was interesting, understanding the enumerate was very helpful, can you please walkthrough was the += sum(num>num2 for num2 line is doing?


RE: Even/Odd permutation - Larz60+ - Jan-10-2021

Please don't reply to 2.5 year old threads.