In the previous article, we have discussed Python Program for Maximum Distance between Two Occurrences of Same Element in Array/List
Given a list and the task is to check to see if the given list can be sorted with a single swap.
copy() method:
The list’s shallow copy is returned by the copy() method.
There are no parameters to the copy() method.
It creates a new list. It makes no changes to the original list.
Examples:
Example1:
Input:
Given List = [7, 8, 9]
Output:
The given list [7, 8, 9] can be sorted with a single swap
Explanation:
When we swap 7 and 9 we get [9, 8, 7] which is a sorted list.
Example2:
Input:
Given List = [6, 3, 2, 1]
Output:
The given list [6, 3, 2, 1] cannot be sorted with a single swap
Program to Check if Array can be Sorted with One Swap in Python:
Below are the ways to check to see if the given list can be sorted with a single swap.
Method #1: Using For Loop (Static Input)
Approach:
- Give the list as static input and store it in a variable.
- Copy the given list in a variable say “new_lst” using the copy() function.
- Take a variable say ‘c’ and initialize its value with 0.
- Sort the above obtained “new_lst”.
- Loop until the length of the “new_lst” using the for loop.
- Check if the given list element is not equal to the new_lst element using the if conditional statement.
- If the statement is true then increment the count value of ‘c’ by 1 and store it in the same variable ‘c‘.
- Check if the value of ‘c’ is equal to 0 or true using the if conditional statement and ‘or ‘ keyword.
- If the statement is true, print “The given list can be sorted with a single swap”.
- If it is false, then print “The given list cannot be sorted with a single swap”.
- The Exit of the Program.
Below is the implementation:
# Give the list as static input and store it in a variable. gven_lst = [7, 8, 9] # Copy the given list in a variable say "new_lst" using the copy() function. new_lst = gven_lst.copy() # Take a variable say 'c' and initialize its value with 0. c = 0 # Sort the above obtained "new_lst". new_lst.sort() # Loop until the length of the "new_lst" using the for loop. for i in range(len(new_lst)): # Check if the given list element is not equal to the new_lst element using the # if conditional statement. if(gven_lst[i] != new_lst[i]): # If the statement is true then increment the count value of 'c' by 1 and # store it in the same variable 'c'. c += 1 # Check if the value of 'c' is equal to 0 or true using the if conditional statement # and 'or ' keyword. if(c == 0 or c == 2): # If the statement is true, print "The given list can be sorted with a single swap". print("The given list", gven_lst, "can be sorted with a single swap") # If it is false, then print "The given list cannot be sorted with a single swap". else: print("The given list", gven_lst, "cannot be sorted with a single swap")
Output:
The given list [7, 8, 9] can be sorted with a single swap
Method #2: Using For loop (User Input)
Approach:
- Give the list as user input using list(),map(),input(),and split() functions.
- Store it in a variable.
- Copy the given list in a variable say “new_lst” using the copy() function.
- Take a variable say ‘c’ and initialize its value with 0.
- Sort the above obtained “new_lst”.
- Loop until the length of the “new_lst” using the for loop.
- Check if the given list element is not equal to the new_lst element using the if conditional statement.
- If the statement is true then increment the count value of ‘c’ by 1 and store it in the same variable ‘c‘.
- Check if the value of ‘c’ is equal to 0 or true using the if conditional statement and ‘or ‘ keyword.
- If the statement is true, print “The given list can be sorted with a single swap”.
- If it is false, then print “The given list cannot be sorted with a single swap”.
- The Exit of the Program.
Below is the implementation:
# Give the list as user input using list(),map(),input(),and split() functions. #Store it in a variable. gven_lst = list(map(int, input( 'Enter some random List Elements separated by spaces = ').split())) # Copy the given list in a variable say "new_lst" using the copy() function. new_lst = gven_lst.copy() # Take a variable say 'c' and initialize its value with 0. c = 0 # Sort the above obtained "new_lst". new_lst.sort() # Loop until the length of the "new_lst" using the for loop. for i in range(len(new_lst)): # Check if the given list element is not equal to the new_lst element using the # if conditional statement. if(gven_lst[i] != new_lst[i]): # If the statement is true then increment the count value of 'c' by 1 and # store it in the same variable 'c'. c += 1 # Check if the value of 'c' is equal to 0 or true using the if conditional statement # and 'or ' keyword. if(c == 0 or c == 2): # If the statement is true, print "The given list can be sorted with a single swap". print("The given list", gven_lst, "can be sorted with a single swap") # If it is false, then print "The given list cannot be sorted with a single swap". else: print("The given list", gven_lst, "cannot be sorted with a single swap")
Output:
Enter some random List Elements separated by spaces = 5 6 7 12 15 The given list [5, 6, 7, 12, 15] can be sorted with a single swap
Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.