Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
team pair issue
#3
I tried below but it is not satisfying given scenarios:-
[code]
# This function updates
# indexes of elements 'a' and 'b'
def updateindex(index,a,ai,b,bi):

index[a] = ai
index[b] = bi


# This function returns minimum
# number of swaps required to arrange
# all elements of arr[i..n]
# become aranged
def minSwapsUtil(arr,pairs,index,i,n):

# If all pairs procesed so
# no swapping needed return 0
if (i > n):
return 0

# If current pair is valid so
# DO NOT DISTURB this pair
# and move ahead.
if (pairs[arr[i]] == arr[i+1]):
return minSwapsUtil(arr, pairs, index, i+2, n)

# If we reach here, then arr[i]
# and arr[i+1] don't form a pair

# Swap pair of arr[i] with
# arr[i+1] and recursively compute
# minimum swap required
# if this move is made.
one = arr[i+1]
indextwo = i+1
indexone = index[pairs[arr[i]]]
two = arr[index[pairs[arr[i]]]]
arr[i+1],arr[indexone]=arr[indexone],arr[i+1]

updateindex(index, one, indexone, two, indextwo)

a = minSwapsUtil(arr, pairs, index, i+2, n)

# Backtrack to previous configuration.
# Also restore the
# previous indices,
# of one and two
arr[i+1],arr[indexone]=arr[indexone],arr[i+1]
updateindex(index, one, indextwo, two, indexone)
one = arr[i]
indexone = index[pairs[arr[i+1]]]

# Now swap arr[i] with pair
# of arr[i+1] and recursively
# compute minimum swaps
# required for the subproblem
# after this move
two = arr[index[pairs[arr[i+1]]]]
indextwo = i
arr[i],arr[indexone]=arr[indexone],arr[i]
updateindex(index, one, indexone, two, indextwo)
b = minSwapsUtil(arr, pairs, index, i+2, n)

# Backtrack to previous
# configuration. Also restore
# 3 the previous indices,
# of one and two
arr[i],arr[indexone]=arr[indexone],arr[i]
updateindex(index, one, indextwo, two, indexone)

# Return minimum of two cases
return 1 + min(a, b)

# Returns minimum swaps required
def minSwaps(n,pairs,arr):

index=[] # To store indices of array elements
for i in range(2*n+1+1):
index.append(0)

# Store index of each
# element in array index
for i in range(1,2*n+1):
index[arr[i]] = i

# Call the recursive function
return minSwapsUtil(arr, pairs, index, 1, 2*n)

# Driver code

# For simplicity, it is
# assumed that arr[0] is
# not used. The elements
# from index 1 to n are
# only valid elements
arr = [0, 3, 5, 6, 4, 1, 2]

# if (a, b) is pair than
# we have assigned elements
# in array such that
# pairs[a] = b and pairs[b] = a
pairs= [0, 3, 6, 1, 5, 4, 2]
m = len(pairs)

n = m//2 # Number of pairs n
# is half of total elements

# If there are n
# elements in array, then
# there are n pairs
print("Min swaps required is ",minSwaps(n, pairs, arr))

[code\]
Reply


Messages In This Thread
team pair issue - by jk91 - Feb-28-2020, 07:03 AM
RE: team pair issue - by ndc85430 - Feb-28-2020, 08:19 AM
RE: team pair issue - by jk91 - Feb-28-2020, 10:24 AM
RE: team pair issue - by jefsummers - Feb-28-2020, 11:40 AM
RE: team pair issue - by jk91 - Feb-28-2020, 01:37 PM
RE: team pair issue - by jefsummers - Feb-28-2020, 03:01 PM
RE: team pair issue - by jk91 - Feb-28-2020, 03:56 PM
RE: team pair issue - by ndc85430 - Feb-28-2020, 04:21 PM
RE: team pair issue - by jk91 - Feb-28-2020, 04:59 PM
RE: team pair issue - by ndc85430 - Feb-28-2020, 05:16 PM
RE: team pair issue - by jk91 - Mar-02-2020, 02:05 PM
RE: team pair issue - by jk91 - Feb-29-2020, 05:51 AM
RE: team pair issue - by ndc85430 - Feb-29-2020, 06:00 AM
RE: team pair issue - by jk91 - Feb-29-2020, 07:38 AM
RE: team pair issue - by ndc85430 - Feb-29-2020, 07:42 AM
RE: team pair issue - by jk91 - Feb-29-2020, 10:05 AM
RE: team pair issue - by ndc85430 - Feb-29-2020, 11:02 AM
RE: team pair issue - by jk91 - Feb-29-2020, 12:22 PM
RE: team pair issue - by ndc85430 - Feb-29-2020, 12:36 PM
RE: team pair issue - by jefsummers - Feb-29-2020, 03:07 PM
RE: team pair issue - by jk91 - Mar-02-2020, 06:50 AM
RE: team pair issue - by buran - Mar-02-2020, 06:54 AM
RE: team pair issue - by jk91 - Mar-02-2020, 09:52 AM
RE: team pair issue - by jk91 - Mar-02-2020, 11:01 AM
RE: team pair issue - by jk91 - Mar-02-2020, 01:48 PM
RE: team pair issue - by DeaD_EyE - Mar-02-2020, 01:56 PM
RE: team pair issue - by ndc85430 - Mar-02-2020, 04:47 PM
RE: team pair issue - by jefsummers - Mar-02-2020, 04:48 PM
RE: team pair issue - by jk91 - Mar-03-2020, 04:10 PM
RE: team pair issue - by jefsummers - Mar-03-2020, 06:15 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Getting the maximum value:key pair from a dictionary sean1 2 1,534 Jan-17-2022, 01:04 PM
Last Post: DeaD_EyE
  How to extract specific key value pair from string? aditi06 0 2,643 Apr-15-2021, 06:26 PM
Last Post: aditi06
  Auto re-pair / re-sync Controller via Script? User3000 2 2,468 Nov-30-2020, 11:42 AM
Last Post: User3000
  Search a List of Dictionaries by Key-Value Pair; Return Dictionary/ies Containing KV dn237 19 7,088 May-29-2019, 02:27 AM
Last Post: heiner55
  Key Value Pair output UtiliseIT 9 4,982 May-03-2019, 07:30 AM
Last Post: buran
  Parsing Text file having repeated value key pair using python manussnair 3 3,414 Aug-04-2018, 11:48 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

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