Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Even/Odd permutation
#1
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 :)
Reply
#2
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.
ndc85430 likes this post
Reply
#3
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? :)
Reply
#4
(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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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'.
Reply
#6
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
Reply
#7
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
Thank you !
Reply
#9
(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?
Reply
#10
Please don't reply to 2.5 year old threads.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question How to print each possible permutation in a dictionary that has arrays as values? noahverner1995 2 1,736 Dec-27-2021, 03:43 AM
Last Post: noahverner1995
  memory error using permutation list of 11 elements kikidog 1 3,887 Sep-10-2019, 08:22 PM
Last Post: ichabod801
  how to get all the possible permutation and combination of a sentence in python sodmzs 1 4,162 Jun-13-2019, 07:02 AM
Last Post: perfringo
  Calling list() on permutation trevorkavanaugh 2 2,378 Mar-01-2019, 06:00 AM
Last Post: trevorkavanaugh
  Permutation help. jarrod0987 1 2,190 Jun-28-2018, 04:44 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020