確認方法が組み合と同等のパリティ?
-
19-09-2019 - |
質問
いなければならないというニーズの確認2 組み (代表されるリスト)の 同 パリティ.なおつもりはありませんければ も または 奇数 パリティを進めていきます。
私は新しいPythonやマナーを解を以下に示します。まPython達を見せてくれましたなるヨーロッパをテーマにしたさらにお得な価格での達成の少ない、より優雅なPythonコードです。
解決
軽微な変異体の前に回答-コピー perm1、保存配列をルックアップ
def arePermsEqualParity(perm0, perm1):
"""Check if 2 permutations are of equal parity.
Assume that both permutation lists are of equal length
and have the same elements. No need to check for these
conditions.
"""
perm1 = perm1[:] ## copy this list so we don't mutate the original
transCount = 0
for loc in range(len(perm0) - 1): # Do (len - 1) transpositions
p0 = perm0[loc]
p1 = perm1[loc]
if p0 != p1:
sloc = perm1[loc:].index(p0)+loc # Find position in perm1
perm1[loc], perm1[sloc] = p0, p1 # Swap in perm1
transCount += 1
# Even number of transpositions means equal parity
if (transCount % 2) == 0:
return True
else:
return False
他のヒント
こちらは自逆的なヘルプオーサリング-ツール
- 利用list()にコピー perm1ることで渡すことができますタプルなどの機能を持っていないため、でがますます重要になっていく。
- Enumerate()のforループの代わりにlen(..)および配列をルックアップのためのneaterコード
- 作perm1_mapかって頑張っていただける日を停止しくO(N)の検索をperm1、高価なスライスリスト
- を返しますbooleanではなく、直接の合---return TrueかFalseを返す
こちらで
def arePermsEqualParity(perm0, perm1):
"""Check if 2 permutations are of equal parity.
Assume that both permutation lists are of equal length
and have the same elements. No need to check for these
conditions.
"""
perm1 = list(perm1) ## copy this into a list so we don't mutate the original
perm1_map = dict((v, i) for i,v in enumerate(perm1))
transCount = 0
for loc, p0 in enumerate(perm0):
p1 = perm1[loc]
if p0 != p1:
sloc = perm1_map[p0] # Find position in perm1
perm1[loc], perm1[sloc] = p0, p1 # Swap in perm1
perm1_map[p0], perm1_map[p1] = sloc, loc # Swap the map
transCount += 1
# Even number of transpositions means equal parity
return (transCount % 2) == 0
した場合の両方を組み合わせ組みの結果でパリティそれぞれの順列と同じパリティ、奇数パリティれば、異なるパリティ.ない場合の解決 パリティ問題 この些細な比較の異なる二つの組み合わ.
パリティ で次のようにして決定される:選任意の要素の位置を完動するまで繰り返しが返されますが、開始しましたよ。おめでとうございますが、サイクル:の換回転するこれらすべての要素が丸による位置を調整することができます。ている必要がありスワップ以下の要素の数のサイクルへの取り消します。現在選別の要素んに扱われまで繰り返しんにまで変えようとしている。以下の事項を遵守し合いつつのスワップ当たりマイナス要素を一つのスワップ当たります。
時間計算量がO(N)のサイズ..ご注意としておりますがループ内には、ループ内回でも繰り返し処理を実行しまの任意の要素の順列.
def parity(permutation):
permutation = list(permutation)
length = len(permutation)
elements_seen = [False] * length
cycles = 0
for index, already_seen in enumerate(elements_seen):
if already_seen:
continue
cycles += 1
current = index
while not elements_seen[current]:
elements_seen[current] = True
current = permutation[current]
return (length-cycles) % 2 == 0
def arePermsEqualParity(perm0, perm1):
perm0 = list(perm0)
return parity([perm0[i] for i in perm1])
また、楽しい、少なく効率が大幅に短縮の実施にパリティ機能に基づく定義Wikipedia(Trueを返すためにも、Falseが奇数):
def parity(p):
return sum(
1 for (x,px) in enumerate(p)
for (y,py) in enumerate(p)
if x<y and px>py
)%2==0
私の素朴解
def arePermsEqualParity(perm0, perm1):
"""Check if 2 permutations are of equal parity.
Assume that both permutation lists are of equal length
and have the same elements. No need to check for these
conditions.
"""
transCount = 0
for loc in range(len(perm0) - 1): # Do (len - 1) transpositions
if perm0[loc] != perm1[loc]:
sloc = perm1.index(perm0[loc]) # Find position in perm1
perm1[loc], perm1[sloc] = perm1[sloc], perm1[loc] # Swap in perm1
transCount += 1
# Even number of transpositions means equal parity
if (transCount % 2) == 0:
return True
else:
return False
こちらは若干整理 Weebleの回答:
def arePermsEqualParity(perm0, perm1):
"""Whether permutations are of equal parity."""
return parity(combine(perm0, perm1))
def combine(perm0, perm1):
"""Combine two permutations into one."""
return map(perm0.__getitem__, perm1)
def parity(permutation):
"""Return even parity for the `permutation`."""
return (len(permutation) - ncycles(permutation)) % 2 == 0
def ncycles(permutation):
"""Return number of cycles in the `permutation`."""
ncycles = 0
seen = [False] * len(permutation)
for i, already_seen in enumerate(seen):
if not already_seen:
ncycles += 1
# mark indices that belongs to the cycle
j = i
while not seen[j]:
seen[j] = True
j = permutation[j]
return ncycles
のソリューションの辞書はbugged.このデバッグバージョン:
def arePermsEqualParity(perm0, perm1):
"""Check if 2 permutations are of equal parity.
Assume that both permutation lists are of equal length
and have the same elements. No need to check for these
conditions.
"""
perm1 = list(perm1) ## copy this into a list so we don't mutate the original
perm1_map = dict((v, i) for i,v in enumerate(perm1))
transCount = 0
for loc, p0 in enumerate(perm0):
p1 = perm1[loc]
if p0 != p1:
sloc = perm1_map[p0] # Find position in perm1
perm1[loc], perm1[sloc] = p0, p1 # Swap in perm1
perm1_map[p0], perm1_map[p1] = loc, sloc # Swap the map
transCount += 1
# Even number of transpositions means equal parity
return (transCount % 2) == 0
唯一の違いは、スワップの辞書を作ったのを修正
私の直感は、計数の差異の組み合わだつ以上のスワップくらいます。このパリティ.
こんなにスワップコードです。
例えば:
ABCD, BDCA.
あの違いが二つのスワップ取引に必要なpermute一のときでもパリティ.
別:
ABCD, CDBA.
あの違いる三つのスワップが奇数パリティ.
def equalparity(p,q):
return sum([p[q[i]] > p[q[j]] for i in range(len(p)) for j in range(i)]) % 2 == 0