Author name: Vikram Chiluka

Program to Replace Every List Element by Multiplication of Previous and Next

Python Program to Replace Every List Element by Multiplication of Previous and Next

In the previous article, we have discussed Python Program for Double Factorial
Given a list and the task is to replace each element of a list by multiplying the previous and next elements.

The first element is replaced by multiplying the first and second elements.

The last element is replaced by multiplying the last and second last elements.

Examples:

Example1:

Input:

Given list = [7, 8, 1, 2, 3, 4]

Output:

The given List after replacing each element of a list by multiplying the previous and next elements:
56 7 16 3 8 12

Explanation:

Given List = [7*8, 7*1, 8*2, 1*3, 2*4, 3*4]

Example2:

Input:

Given list =[ 10 ,1 ,20, 2 ,30, 3]

Output:

The given List after replacing each element of a list by multiplying the previous and next elements:
10 200 2 600 6 90

Explanation:

Given List = [10*1, 10*20, 1*2, 20*30, 2*3, 30*3] = [10, 200, 2, 600, 6, 90]

Program to Replace Every List Element by Multiplication of Previous and Next in Python

Below are the ways to replace each element of a list by multiplying the previous and next elements.

Method #1: Using For Loop (Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Calculate the length of the list using the len() function and store it in another variable.
  • Check if the length of the given list is less than or equal to 1 using the if conditional statement.
  • If the statement is true, do not print anything.
  • Take a variable and initialize it with the first element of the given list say “previous”.
  • Multiply the first and the next element of a list and store it in another variable.
  • Loop from 1 to length of the given list -1 using the for loop.
  • Inside the loop, assign the iterator value of the given list and store it in a
    variable say “currnt”.
  • Multiply the above obtained “previous” with a given list of iterator+1 value
    and store it in the given list of the iterator.
  • Assign the variable “currnt” to the “previus” and come out of the loop.
  • Multiply the above obtained “previous” with a given list of length-1 values and store it in the given list of length-1.
  • print the given list after replacing each element of a list by multiplying the previous and next elements.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = [7, 8, 1, 2, 3, 4]
# Calculate the length of the list using the len() function and
# store it in another variable.
lenth = len(gvn_lst)
# Check if the length of the given list is less than or equal to 1 using the
# if conditional statement.
if lenth <= 1:
  # If the statement is true, do not print anything.
    print()
 # Take a variable and initialize it with the first element of the given list
# say "previous".
previus = gvn_lst[0]
# Multiply the first and the next element of a list and store it in another variable.
gvn_lst[0] = gvn_lst[0] * gvn_lst[1]
# Loop from 1 to length of the given list -1 using the for loop.
for itr in range(1, lenth-1):
  # Inside the loop, assign the iterator value of the given list and store it in a
  # variable say "currnt"
    currnt = gvn_lst[itr]
# Multiply the above obtained "previous" with given list of iterator+1 value
# and store it in given list of iterator.
    gvn_lst[itr] = previus * gvn_lst[itr+1]
 # Assign the variable "currnt" to the "previus" and come out of the loop.
    previus = currnt
# Multiply the above obtained "previous" with given list of length-1 value
# and store it in given list of length-1.
gvn_lst[lenth-1] = previus * gvn_lst[lenth-1]
# print the given list after replacing each element of a list by
# multiplying the previous and next elements
print("The given List after replacing each element of a list by multiplying the previous and next elements:")
for itr in range(0, lenth):
    print(gvn_lst[itr], end=" ")

Output:

The given List after replacing each element of a list by multiplying the previous and next elements:
56 7 16 3 8 12

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.
  • Calculate the length of the list using the len() function and store it in another variable.
  • Check if the length of the given list is less than or equal to 1 using the if conditional statement.
  • If the statement is true, do not print anything.
  • Take a variable and initialize it with the first element of the given list say “previous”.
  • Multiply the first and the next element of a list and store it in another variable.
  • Loop from 1 to length of the given list -1 using the for loop.
  • Inside the loop, assign the iterator value of the given list and store it in a
    variable say “currnt”.
  • Multiply the above obtained “previous” with a given list of iterator+1 value
    and store it in the given list of the iterator.
  • Assign the variable “currnt” to the “previus” and come out of the loop.
  • Multiply the above obtained “previous” with a given list of length-1 values and store it in the given list of length-1.
  • print the given list after replacing each element of a list by multiplying the previous and next elements.
  • 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.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Calculate the length of the list using the len() function and
# store it in another variable.
lenth = len(gvn_lst)
# Check if the length of the given list is less than or equal to 1 using the
# if conditional statement.
if lenth <= 1:
  # If the statement is true, do not print anything.
    print()
 # Take a variable and initialize it with the first element of the given list
# say "previous".
previus = gvn_lst[0]
# Multiply the first and the next element of a list and store it in another variable.
gvn_lst[0] = gvn_lst[0] * gvn_lst[1]
# Loop from 1 to length of the given list -1 using the for loop.
for itr in range(1, lenth-1):
  # Inside the loop, assign the iterator value of the given list and store it in a
  # variable say "currnt"
    currnt = gvn_lst[itr]
# Multiply the above obtained "previous" with given list of iterator+1 value
# and store it in given list of iterator.
    gvn_lst[itr] = previus * gvn_lst[itr+1]
 # Assign the variable "currnt" to the "previus" and come out of the loop.
    previus = currnt
# Multiply the above obtained "previous" with given list of length-1 value
# and store it in given list of length-1.
gvn_lst[lenth-1] = previus * gvn_lst[lenth-1]
# print the given list after replacing each element of a list by
# multiplying the previous and next elements
print("The given List after replacing each element of a list by multiplying the previous and next elements:")
for itr in range(0, lenth):
    print(gvn_lst[itr], end=" ")

Output:

Enter some random List Elements separated by spaces = 10 1 20 2 30 3
The given List after replacing each element of a list by multiplying the previous and next elements:
10 200 2 600 6 90

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.

Python Program to Replace Every List Element by Multiplication of Previous and Next Read More »

Program to Reorder a List According to Given Indexes

Python Program to Reorder a List According to Given Indexes

In the previous article, we have discussed Python Program to Find Volume and Surface Area of a Cone
Given two lists of equal length i.e. one is the list of elements and the other of indexes list, the task is to reorder the given list according to the given indexes.

For example:

let Given List = [1, 2, 3, 4, 5]

Given Indexes List = [2, 0, 3, 4, 1]

The given list after reordering according to the given indexes list = [2 5 1 3 4]

The Index list after reordering = [0 1 2 3 4 ]

Examples:

Example1:

Input:

Given List = [1, 2, 3, 4, 5]
Given Index List = [3, 4, 0, 2, 1]

Output:

The given list after reordering according to the given indexes list :
3 5 4 1 2 
The Index list after reordering:
0 1 2 3

Example2:

Input:

Given List =  [10, 20, 30, 40, 50, 60]
Given Index List = [4, 3, 5, 0, 1, 2]

Output:

The given list after reordering according to the given indexes list :
40 50 60 20 10 30 
The Index list after reordering:
0 1 2 3 4 5

Program to Reorder an Array/List According to Given Indexes in Python

Below are the ways to reorder the given list according to the given indexes.

Method #1: Using For Loop (Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Give the list of indexes as static input and store it in another variable.
  • Calculate the length of the given list using the len() function and store it in another variable.
  • Take a list and assign all the elements with 0 of the length of the given list and store it in another variable say “tempry”.
  • Loop from 0 to length of the given list using the for loop.
  • Assign the givenindexlist[itertor value] as index and givenlist[iterator] as value to tempry list.
  • Loop from 0 to length of the given list using the for loop.
  • Assign the given list at the iterator with the “tempry” list at the iterator value.
  • Assign the given list at the iterator index with iterator value.
  • Loop from 0 to length of the given list using the for loop.
  • Inside the loop, Print the iterator value of the given list.
  • Loop from 0 to length of the given index list using the for loop.
  • Inside the loop, Print the iterator value of the given index list.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = [9, 8, 2, 1]
# Give the list of indexes as static input and store it in another variable.
gvn_indxlst = [1, 3, 0, 2]
# Calculate the length of the given list using the len() function and
# store it in another variable.
len_gvn_lst = len(gvn_lst)
# Take a list and assign all the elements with 0 of the length of the given list and
# store it in another variable say "tempry".
tempry = [0] * len_gvn_lst
# Loop from 0 to length of the given list using the for loop.
for itror in range(0, len_gvn_lst):
 # Assign the givenindexlist[itertor value] as index and givenlist[iteratr] as value
    # to temp list
    tempry[gvn_indxlst[itror]] = gvn_lst[itror]
# Loop from 0 to length of the given list using the for loop.
for itror in range(0, len_gvn_lst):
 # Assign the given list at the iterator with the "tempry" list at iterator value
    gvn_lst[itror] = tempry[itror]
 # Assign the given list at itertor index with iterator value
    gvn_indxlst[itror] = itror
print("The given list after reordering according to the given indexes list :")
# Loop from 0 to length of the given list using the for loop.
for itror in range(0, len_gvn_lst):
  # Inside the loop, Print the iterator value of the given list.
    print(gvn_lst[itror], end=" ")
print()
print("The Index list after reordering:")
# Loop from 0 to length of the given index list using the for loop.
for itror in range(0, len_gvn_lst):
  # Inside the loop, Print the iterator value of the given index list.
    print(gvn_indxlst[itror], end=" ")

Output:

The given list after reordering according to the given indexes list :
2 9 1 8 
The Index list after reordering:
0 1 2 3

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.
  • Give the list of indexes as user input using list(),map(),input(),and split() functions.
  • Store it in another variable.
  • Calculate the length of the given list using the len() function and store it in another variable.
  • Take a list and assign all the elements with 0 of the length of the given list and store it in another variable say “tempry”.
  • Loop from 0 to length of the given list using the for loop.
  • Assign the givenindexlist[itertor value] as index and givenlist[iterator] as value to tempry list.
  • Loop from 0 to length of the given list using the for loop.
  • Assign the given list at the iterator with the “tempry” list at the iterator value.
  • Assign the given list at the iterator index with iterator value.
  • Loop from 0 to length of the given list using the for loop.
  • Inside the loop, Print the iterator value of the given list.
  • Loop from 0 to length of the given index list using the for loop.
  • Inside the loop, Print the iterator value of the given index list.
  • 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.
gvn_lst = givnlst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Give the list of indexes as user input using list(),map(),input(),and split() functions.
# Store it in another variable.
gvn_indxlst = givnlst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Calculate the length of the given list using the len() function and
# store it in another variable.
len_gvn_lst = len(gvn_lst)
# Take a list and assign all the elements with 0 of the length of the given list and
# store it in another variable say "tempry".
tempry = [0] * len_gvn_lst
# Loop from 0 to length of the given list using the for loop.
for itror in range(0, len_gvn_lst):
 # Assign the givenindexlist[itertor value] as index and givenlist[iteratr] as value
    # to temp list
    tempry[gvn_indxlst[itror]] = gvn_lst[itror]
# Loop from 0 to length of the given list using the for loop.
for itror in range(0, len_gvn_lst):
 # Assign the given list at the iterator with the "tempry" list at iterator value
    gvn_lst[itror] = tempry[itror]
 # Assign the given list at itertor index with iterator value
    gvn_indxlst[itror] = itror
print("The given list after reordering according to the given indexes list: ")
# Loop from 0 to length of the given list using the for loop.
for itror in range(0, len_gvn_lst):
  # Inside the loop, Print the iterator value of the given list.
    print(gvn_lst[itror], end=" ")
print()
print("The Index list after reordering:")
# Loop from 0 to length of the given index list using the for loop.
for itror in range(0, len_gvn_lst):
  # Inside the loop, Print the iterator value of the given index list.
    print(gvn_indxlst[itror], end=" ")

Output:

Enter some random List Elements separated by spaces = 6 4 3 2 1
Enter some random List Elements separated by spaces = 4 0 1 3 2
The given list after reordering according to the given indexes list: 
4 3 1 2 6 
The Index list after reordering:
0 1 2 3 4

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.

Python Program to Reorder a List According to Given Indexes Read More »

Program for Alternative Sorting

Python Program for Alternative Sorting

In the previous article, we have discussed Python Program to Sort a List Containing Two Types of Elements
Given a list and the task is to Print the list so that the first element is the first maximum, the second element is the first minimum, and so on.

Examples:

Example1:

Input:

Given List = [2, 3, 7, 8, 1, 3, 6]

Output:

The Alternative sorting of the above given list is :
8 1 7 2 6 3 3

Example2:

Input:

Given List = [2, 1, 7, 4, 1, 2, 5]

Output:

The Alternative sorting of the above given list is :
7 1 5 1 4 2 2

Program for Alternative Sorting in Python

Below are the ways to Print the list so that the first element is the first maximum, the second element is the first minimum, and so on.

Method #1: Using While Loop (Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Calculate the length of the given list using the len() function and store it in another variable.
  • Sort the given list using the sort() method and store it in another variable.
  • Take a variable say ‘p’ and initialize its value with zero.
  • Take another variable say ‘q’ and initialize its value with the length of the given list -1.
  • Check if the value of p is less than q using the while loop.
  • If the statement is true, then print the value of the given list of q.
  • Subtract 1 from q (q-1)and store it in the same variable ‘q’.
  • Print the value of the given list of p.
  • Increment the value of p by 1 and store it in the same variable ‘p’.
  • Check if the length of the given list is odd(len_lst % 2 != 0) by using the if conditional statement.
  • If the statement is true, print the value of the given list of p.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = [2, 3, 7, 8, 1, 3, 6]
# Calculate the length of the given list using the len() function and
# store it in another variable.
len_lst = len(gvn_lst)
# Sort the given list using the sort() method and store it in another variable.
gvn_lst.sort()
# Take a variable say 'p' and initialize its value with zero.
p = 0
# Take another variable say 'q' and initialize its value with the length of the
# given list -1.
q = len_lst-1
print("The Alternative sorting of the above given list is :")
# Check if the value of p is less than q using the while loop.
while (p < q):
    # If the statement is true, then print the value of the given list of q.
    print(gvn_lst[q], end=" ")
    # Subtract 1 from q (q-1)and store it in the same variable 'q'.
    q -= 1
    # Print the value of the given list of p.
    print(gvn_lst[p], end=" ")
    # Increment the value of p by 1 and store it in the same variable 'p'.
    p += 1
   # Check if the length of the given list is odd(len_lst % 2 != 0) by using the
   # if conditional statement.
if (len_lst % 2 != 0):
    # If the statement is true, print the value of the given list of p.
    print(gvn_lst[p])

Output:

The Alternative sorting of the above given list is :
8 1 7 2 6 3 3

Method #2: Using While loop (User Input)

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Calculate the length of the given list using the len() function and store it in another variable.
  • Sort the given list using the sort() method and store it in another variable.
  • Take a variable say ‘p’ and initialize its value with zero.
  • Take another variable say ‘q’ and initialize its value with the length of the given list -1.
  • Check if the value of p is less than q using the while loop.
  • If the statement is true, then print the value of the given list of q.
  • Subtract 1 from q (q-1)and store it in the same variable ‘q’.
  • Print the value of the given list of p.
  • Increment the value of p by 1 and store it in the same variable ‘p’.
  • Check if the length of the given list is odd(len_lst % 2 != 0) by using the if conditional statement.
  • If the statement is true, print the value of the given list of p.
  • 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.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Calculate the length of the given list using the len() function and
# store it in another variable.
len_lst = len(gvn_lst)
# Sort the given list using the sort() method and store it in another variable.
gvn_lst.sort()
# Take a variable say 'p' and initialize its value with zero.
p = 0
# Take another variable say 'q' and initialize its value with the length of the
# given list -1.
q = len_lst-1
print("The Alternative sorting of the above given list is :")
# Check if the value of p is less than q using the while loop.
while (p < q):
    # If the statement is true, then print the value of the given list of q.
    print(gvn_lst[q], end=" ")
    # Subtract 1 from q (q-1)and store it in the same variable 'q'.
    q -= 1
    # Print the value of the given list of p.
    print(gvn_lst[p], end=" ")
    # Increment the value of p by 1 and store it in the same variable 'p'.
    p += 1
   # Check if the length of the given list is odd(len_lst % 2 != 0) by using the
   # if conditional statement.
if (len_lst % 2 != 0):
    # If the statement is true, print the value of the given list of p.
    print(gvn_lst[p])

Output:

Enter some random List Elements separated by spaces = 2 1 7 4 1 2 5
The Alternative sorting of the above given list is :
7 1 5 1 4 2 2

 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.

Python Program for Alternative Sorting Read More »

Program for Leonardo Number Series

Python Program for Leonardo Number Series

In the previous article, we have discussed Python Program to Find the Rotation Count in Rotated Sorted List
Leonardo Number:

The Leonardo numbers are a series of numbers determined by recurrence:

L(n) =  1       if n=0

       =   1       if n=1

       =   L(n-1)+L(n-2)+1   if n>1

The first Leonardo numbers are 1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361.

The Leonardo numbers are related to the Fibonacci numbers in the following way:

L(n) = 2F(n+1) – 1 if n>=1

Given a number, the task is to find the Leonardo number sequence to the given number.

Examples:

Example1:

Input:

Given Number = 10

Output:

The Leonardo Number Sequence till the given number 10  = 
1
1
3
5
9
15
25
41
67
109

Example2:

Input:

Given Number = 15

Output:

The Leonardo Sequence till the given number 15 = 
1
1
3
5
9
15
25
41
67
109
177
287
465
753
1219

Program for Leonardo Number Series in Python

Below are the ways to find the Leonardo Number Series using the recursive approach in Python:

Method #1: Using Recursion (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Pass the given number as a parameter to the Leonardo recursive function.
  • The base condition is defined as a value that is less than or equal to 1.
  • If the base condition is true, then return 1.
  • Otherwise, call the function recursively with the argument as the number minus 1 plus the function that was called recursively with the parameter as the number minus 2 and add 1 to it.
  • Use a for loop to return the Leonardo sequence and return the result and print the result.
  • The Exit of the program.

Below is the implementation :

# function which finds the Leonardo sequence recursively
def LeonardoRecursion(numb):
  # The base condition is defined as a value that is less than or equal to 1.
    if(numb <= 1):
        # If the base condition is true, then return 1.
        return 1
    else:
      # Otherwise, call the function recursively with the argument as the number minus 1 plus the function that was called
      # recursively with the parameter as the number minus 2 and add 1 to it.
        return(LeonardoRecursion(numb-1) + LeonardoRecursion(numb-2)+1)


# The user must give the number as static input and store it in a variable.
numb = 10
print("The Leonardo Number Sequence till the given number", numb, ' = ')
# Looping from 1 to given number using for loop
for n in range(numb):
  # passing the iterter value as argument to the recursive function LeonardoRecursion
    print(LeonardoRecursion(n))

Output:

The Leonardo Number Sequence till the given number 10  = 
1
1
3
5
9
15
25
41
67
109

Method #2: Using Recursion (User Input)

Approach:

  • The user must give the number as user input using the int(input()) function and store it in a variable.
  • Pass the given number as a parameter to the Leonardo recursive function.
  • The base condition is defined as a value that is less than or equal to 1.
  • If the base condition is true, then return 1.
  • Otherwise, call the function recursively with the argument as the number minus 1 plus the function that was called recursively with the parameter as the number minus 2 and add 1 to it.
  • Use a for loop to return the Leonardo sequence and return the result and print the result.
  • The exit of the program.

Below is the implementation:

# function which finds the Leonardo sequence recursively
def LeonardoRecursion(numb):
  # The base condition is defined as a value that is less than or equal to 1.
  # If the base condition is true, then return 1.
    if(numb <= 1):
        return 1
    else:
      # Otherwise, call the function recursively with the argument as the number minus 1 plus the function that was called
      # recursively with the parameter as the number minus 2 and add 1 to it.
        return(LeonardoRecursion(numb-1) + LeonardoRecursion(numb-2)+1)


# The user must give the number as user input using the
# int(input()) function and store it in a variable.
numb = int(input('Enter some random number = '))
print("The Leonardo Sequence till the given number", numb, ' = ')
# Looping from 1 to given number using for loop
for n in range(numb):
  # passing the iterter value as argument to the recursive function LeonardoRecursion
    print(LeonardoRecursion(n))

Output:

Enter some random number = 15
The Leonardo Sequence till the given number 15 = 
1
1
3
5
9
15
25
41
67
109
177
287
465
753
1219

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.

Python Program for Leonardo Number Series Read More »

Program to Sort a List Containing Two Types of Elements

Python Program to Sort a List Containing Two Types of Elements

In the previous article, we have discussed Python Program to Replace Every List Element by Multiplication of Previous and Next
Given a random list of 0s and 1s and the task is to separate the list into 0s on the left and 1s on the right. Only traverse the list once.

Examples:

Example1:

Input:

Given List = [0, 0, 1, 1, 0, 1, 1, 1, 1, 0]

Output:

The above given list after separation of 0s on the left and 1s on the right:
0 0 0 0 1 1 1 1 1 1

Example2:

Input:

Given List = [1 0 1 0 0 0 1 1 0 1]

Output:

The above given list after separation of 0s on the left and 1s on the right:
0 0 0 0 0 1 1 1 1 1

Program to Sort a List Containing Two Types of Elements in Python

Below are the ways to separate the given list into 0s on the left and 1s on the right.

Method #1: Using While Loop (Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Calculate the length of the given list using the len() function and store it in another variable.
  • Take a variable say ‘p’ and initialize its value with 0.
  • Take another variable ‘q’ and initialize its value with the length of the given list -1.
  • Store it in another variable.
  • Check if the value of p is less than q using the while loop.
  • If the statement is true, check again if the value of the given list of p is equal to 1 using the if conditional statement.
  • If the statement is true, then assign a given list of p with the given list of q and a given list of q with the given list of p.
  • Decrement the value of q by 1 and store it in the same variable ‘q’
  • Else increment the value of p by 1 and store it in the same variable ‘p’.
  • Exit the while Loop.
  • Loop from 0 to the length of the given list using the for loop.
  • Inside the loop, print the given list of iterator values.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
givn_lst = [0, 0, 1, 1, 0, 1, 1, 1, 1, 0]
# Calculate the length of the given list using the len() function and store it in
# another variable.
lent_lst = len(givn_lst)
# Take a variable say 'p' and initialize its value with 0.
p = 0
# Take another variable 'q' and initialize its value with the length of the given list -1.
# Store it in another variable.
q = lent_lst - 1
# Check if the value of p is less than q using the while loop.
while (p < q):
    # If the statement is true, check again if the value of the given list of p is equal
    # to 1 using the if conditional statement.
    if(givn_lst[p] == 1):
       # If the statement is true, then assign a given list of p with the given list of q and
       # a given list of q with the given list of p.
        givn_lst[p], givn_lst[q] = givn_lst[q], givn_lst[p]
 # Decrement the value of q by 1 and store it in the same variable 'q
        q -= 1
 # Else increment the value of p by 1 and store it in the same variable 'p'.
 # Exit the while Loop.
    else:
        p += 1
print("The above given list after separation of 0s on the left and 1s on the right:")
# Loop from 0 to the length of the given list using the for loop.
for itror in range(0, lent_lst):
        # Inside the loop, print the given list of iterator values.
    print(givn_lst[itror], end=" ")

Output:

The above given list after separation of 0s on the left and 1s on the right:
0 0 0 0 1 1 1 1 1 1

Method #2: Using While loop (User Input)

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Calculate the length of the given list using the len() function and store it in another variable.
  • Take a variable say ‘p’ and initialize its value with 0.
  • Take another variable ‘q’ and initialize its value with the length of the given list -1.
  • Store it in another variable.
  • Check if the value of p is less than q using the while loop.
  • If the statement is true, check again if the value of the given list of p is equal to 1 using the if conditional statement.
  • If the statement is true, then assign a given list of p with the given list of q and a given list of q with the given list of p.
  • Decrement the value of q by 1 and store it in the same variable ‘q’
  • Else increment the value of p by 1 and store it in the same variable ‘p’.
  • Exit the while Loop.
  • Loop from 0 to the length of the given list using the for loop.
  • Inside the loop, print the given list of iterator values.
  • 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.
givn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Calculate the length of the given list using the len() function and store it in
# another variable.
lent_lst = len(givn_lst)
# Take a variable say 'p' and initialize its value with 0.
p = 0
# Take another variable 'q' and initialize its value with the length of the given list -1.
# Store it in another variable.
q = lent_lst - 1
# Check if the value of p is less than q using the while loop.
while (p < q):
    # If the statement is true, check again if the value of the given list of p is equal
    # to 1 using the if conditional statement.
    if(givn_lst[p] == 1):
       # If the statement is true, then assign a given list of p with the given list of q and
       # a given list of q with the given list of p.
        givn_lst[p], givn_lst[q] = givn_lst[q], givn_lst[p]
 # Decrement the value of q by 1 and store it in the same variable 'q
        q -= 1
 # Else increment the value of p by 1 and store it in the same variable 'p'.
 # Exit the while Loop.
    else:
        p += 1
print("The above given list after separation of 0s on the left and 1s on the right:")
# Loop from 0 to the length of the given list using the for loop.
for itror in range(0, lent_lst):
        # Inside the loop, print the given list of iterator values.
    print(givn_lst[itror], end=" ")

Output:

Enter some random List Elements separated by spaces = 1 0 1 0 0 0 1 1 0 1
The above given list after separation of 0s on the left and 1s on the right:
0 0 0 0 0 1 1 1 1 1

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.

Python Program to Sort a List Containing Two Types of Elements Read More »

Python Program to Find the Rotation Count in Rotated Sorted List

In the previous article, we have discussed Python Program to Sort a List in Wave Form
Consider a List of distinct numbers that are sorted in ascending order. The list has been rotated k times (clockwise). The task is to determine the value of k.

In simple terms, print the minimum index value to get the rotation count.

Examples:

Example1:

Input:

Given List = [7, 9, 11, 12, 5, 6, 1, 8]

Output:

The number of rotations of a gvn_lst [7, 9, 11, 12, 5, 6, 1, 8] = 6

Example2:

Input:

Given List = [6, 3, 1, 4, 7]

Output:

The number of rotations of a gvn_lst [6, 3, 1, 4, 7] = 1

Program to Find the Rotation Count in Rotated Sorted List in Python

Below are the ways to find the rotation count in the rotated sorted list:

Method #1: Using For Loop (Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Calculate the length of the given list using the len() function and store it in another variable.
  • Take a variable say “minim_index” and initialize it with the first element of the given list.
  • Loop from 0 to the length of the given list using the for loop.
  • Check if the value of the variable “minim_index” is greater than the given list of iterator value.
  • If the statement is true then assign the given list of iterator value to the “minim_index”.
  • Store the iterator value in a variable “minim_index”.
  • Print “minim_index” to get the number of rotations of a given list.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = [1, 3, 5, 7, 9]
# Calculate the length of the given list using the len() function and
# store it in another variable.
len_lst = len(gvn_lst)
# Take a variable say "minim_index" and initialize it with the first element of
# the given list.
minim_index = gvn_lst[0]
# Loop from 0 to the length of the given list using the for loop.
for itr in range(0, len_lst):
 # Check if the value of the variable "minim_index" is greater than the given list of
 # iterator value.
    if (minim_index > gvn_lst[itr]):
        # If the statement is true then assign the given list of iterator value to the "minim_index".
        minim_index = gvn_lst[itr]
# Store the iterator value in a variable "minim_index".
        minim_index = itr
# Print "minim_index" to get the number of rotations of a given list.
print("The number of rotations of a gvn_lst", gvn_lst, "=", minim_index)

Output:

The number of rotations of a gvn_lst [1, 3, 5, 7, 9] = 1

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.
  • Calculate the length of the given list using the len() function and store it in another variable.
  • Take a variable say “minim_index” and initialize it with the first element of the given list.
  • Loop from 0 to the length of the given list using the for loop.
  • Check if the value of the variable “minim_index” is greater than the given list of iterator value.
  • If the statement is true then assign the given list of iterator value to the “minim_index”.
  • Store the iterator value in a variable “minim_index”.
  • Print “minim_index” to get the number of rotations of a given list.
  • 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.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Calculate the length of the given list using the len() function and
# store it in another variable.
len_lst = len(gvn_lst)
# Take a variable say "minim_index" and initialize it with the first element of
# the given list.
minim_index = gvn_lst[0]
# Loop from 0 to the length of the given list using the for loop.
for itr in range(0, len_lst):
 # Check if the value of the variable "minim_index" is greater than the given list of
 # iterator value.
    if (minim_index > gvn_lst[itr]):
        # If the statement is true then assign the given list of iterator value to the "minim_index".
        minim_index = gvn_lst[itr]
# Store the iterator value in a variable "minim_index".
        minim_index = itr
# Print "minim_index" to get the number of rotations of a given list.
print("The number of rotations of a gvn_lst", gvn_lst, "=", minim_index)

Output:

Enter some random List Elements separated by spaces = 6 3 1 4 7
The number of rotations of a gvn_lst [6, 3, 1, 4, 7] = 1

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.

Python Program to Find the Rotation Count in Rotated Sorted List Read More »

Python Program to Sort a List in Wave Form

In the previous article, we have discussed Python Program for Alternative Sorting.
Waveform:

Sort an unsorted list of integers into a wave-like list given an unsorted list of integers.

If list[0..n-1] >= list[1] <= list[2] >= list[3] <= list[4] >=….., a  ‘list[0..n-1]’ is sorted in wave form.

Examples:

Example1:

Input:

Given List = [1, 5, 7, 2, 4, 6, 9]

Output:

The above given list after sorted in waveform :
2 1 5 4 7 6 9

Example2:

Input:

Given List = [12, 34, 10, 8, 90, 75, 89]

Output:

The above given list after sorted in waveform :
10 8 34 12 89 75 90

Program to Sort a List in Wave Form in Python

Below are the ways to sort a given list in the form of the waveform:

Method #1: Using For Loop (Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Sort the above-given list using the sort() function.
  • Calculate the length of the given list using the len() function and store it in another variable.
  • Loop from 0 to length of the given list with a step size of 2 using the for loop.
  • Inside the loop, assign a given list of (iterator +1) values to the given list of iterator and a given list of the iterator to the given list of iterator+1.
  • Loop from 0 to length of the given list using the for loop.
  • Inside the loop, print the given list of iterator values to sort the list in a waveform.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = [1, 5, 7, 2, 4, 6, 9]
# Sort the above-given list using the sort() function.
gvn_lst.sort()
# Calculate the length of the given list using the len() function and
# store it in another variable.
len_lst = len(gvn_lst)
# Loop from 0 to length of the given list with a step size of 2 using the for loop.
for itr in range(0, len_lst-1, 2):
    # Assign a given list of (iterator +1) values to the given list of iterator and
    # a given list of the iterator to the given list of iterator+1.
    gvn_lst[itr], gvn_lst[itr+1] = gvn_lst[itr+1], gvn_lst[itr]
print("The above given list after sorted in waveform :")
# Loop from 0 to length of the given list using the for loop.
for itr in range(0, len(gvn_lst)):
  # Inside the loop, print the given list of iterator values to sort the list in a waveform.
    print(gvn_lst[itr], end=' ')

Output:

The above given list after sorted in waveform :
2 1 5 4 7 6 9

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.
  • Sort the above-given list using the sort() function.
  • Calculate the length of the given list using the len() function and store it in another variable.
  • Loop from 0 to length of the given list with a step size of 2 using the for loop.
  • Assign a given list of (iterator +1) values to the given list of iterator and a given list of the iterator to the given list of iterator+1.
  • Loop from 0 to length of the given list using the for loop.
  • Inside the loop, print the given list of iterator values to sort the list in a waveform.
  • 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.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Sort the above-given list using the sort() function.
gvn_lst.sort()
# Calculate the length of the given list using the len() function and
# store it in another variable.
len_lst = len(gvn_lst)
# Loop from 0 to length of the given list with a step size of 2 using the for loop.
for itr in range(0, len_lst-1, 2):
    # Assign a given list of (iterator +1) values to the given list of iterator and
    # a given list of the iterator to the given list of iterator+1.
    gvn_lst[itr], gvn_lst[itr+1] = gvn_lst[itr+1], gvn_lst[itr]
print("The above given list after sorted in waveform :")
# Loop from 0 to length of the given list using the for loop.
for itr in range(0, len(gvn_lst)):
  # Inside the loop, print the given list of iterator values to sort the list in a waveform.
    print(gvn_lst[itr], end=' ')

Output:

Enter some random List Elements separated by spaces = 15 16 17 1 2 3
The above given list after sorted in waveform :
2 1 15 3 17 16

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.

Python Program to Sort a List in Wave Form Read More »

Program to Find sum of Even Factors of a Number

Python Program to Find sum of Even Factors of a Number

In the previous article, we have discussed Python Program to Print Number in Ascending Order which contains 1, 2 and 3 in their Digits.
Given a number, and the task is to get the sum of even factors of a given number.

Factors are numbers or algebraic expressions that divide another number by themselves and leave no remainder.

Example: let the given number = 24

# The factors of 24 are : 1, 2, 3, 4, 6, 8, 12, 24

The sum of even factors of 24 = 2+4+6+ 8+12+24 = 56

Examples:

Example1:

Input:

Given Number = 60

Output:

The Sum of all even factors of { 60 }  =  144

Example2:

Input:

Given Number = 24

Output:

The Sum of all even factors of { 24 }  =  56

Program to Find the Sum of Even Factors of a Number in Python

Below are the ways to the sum of even factors of a given number.

Method #1: Using For Loop (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Take an empty list and store it in another variable.
  • Loop from ‘1’  to the above-given number range using For loop.
  • Check whether the given number modulus iterator value is equal to ‘0’ or not using if conditional statement.
  • If the statement is True, Check if the iterator modulus 2 is equal to 0 using the if conditional statement.
  • If the statement is True, append the iterator value to the above-declared list.
  • Get the sum of all the even factors of the above-given list using the built-in sum() function and store it in another variable.
  • Print the sum of all the even factors of a given number.
  • The Exit of the program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_numb = 24
# Take an empty list and store it in another variable.
all_factors = []
# Loop from '1' to above given number range using For loop.
for itr in range(1, gvn_numb+1):
    # Check whether the given number modulus iterator value is equal to '0' or not
    # using if conditional statement.
    if gvn_numb % itr == 0:
# If the statement is True, Check if the iterator modulus 2 is equal to 0 using the
# if conditional statement.
        if itr % 2 == 0:
            # If the statement is True ,append the iterator value to the above declared list .
            all_factors.append(itr)
      # Get the sum of all the even factors of above got list using built-in sum() function
      # and store it in another variable.
reslt = sum(all_factors)
# Print the sum of all even factors of a given number.
print("The Sum of all even factors of {", gvn_numb, "}  = ", reslt)

Output:

The Sum of all even factors of { 24 }  =  56

Method #2: Using For loop (User Input)

Approach:

  • Give the number as user input using int(input()) and store it in a variable.
  • Take an empty list and store it in another variable.
  • Loop from ‘1’  to the above-given number range using For loop.
  • Check whether the given number modulus iterator value is equal to ‘0’ or not using if conditional statement.
  • If the statement is True, Check if the iterator modulus 2 is equal to 0 using the if conditional statement.
  • If the statement is True, append the iterator value to the above-declared list.
  • Get the sum of all the even factors of the above-given list using the built-in sum() function and store it in another variable.
  • Print the sum of all the even factors of a given number.
  • The Exit of the program.

Below is the implementation:

# Give the number as user input using int(input()) and store it in a variable.
gvn_numb = int(input("Enter some random Number = "))
# Take an empty list and store it in another variable.
all_factors = []
# Loop from '1' to above given number range using For loop.
for itr in range(1, gvn_numb+1):
    # Check whether the given number modulus iterator value is equal to '0' or not
    # using if conditional statement.
    if gvn_numb % itr == 0:
# If the statement is True, Check if the iterator modulus 2 is equal to 0 using the
# if conditional statement.
        if itr % 2 == 0:
            # If the statement is True ,append the iterator value to the above declared list .
            all_factors.append(itr)
# Get the sum of all the even factors of above got list using built-in sum() function
# and store it in another variable.
reslt = sum(all_factors)
# Print the sum of all even factors of a given number.
print("The Sum of all even factors of {", gvn_numb, "}  = ", reslt)

Output:

Enter some random Number = 32
The Sum of all even factors of { 32 } = 62

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.

Python Program to Find sum of Even Factors of a Number Read More »

Program to Find Sum of Odd Factors of a Number

Python Program to Find Sum of Odd Factors of a Number

In the previous article, we have discussed Python Program to Find sum of Even Factors of a Number
Given a number, and the task is to get the sum of odd factors of a given number.

Factors are numbers or algebraic expressions that divide another number by themselves and leave no remainder.

Example: let the given number = 24

# The factors of 24 are : 1, 2, 3, 4, 6, 8, 12, 24

The sum of odd factors of 24 = 1+3= 4

Examples:

Example1:

Input:

Given Number = 24

Output:

The Sum of all odd factors of { 24 }  =  4

Example2:

Input:

Given Number = 72

Output:

The Sum of all odd factors of { 72 }  =  13

Program to Find the Sum of Odd Factors of a Number in Python

Below are the ways to the sum of odd factors of a given number.

Method #1: Using For Loop (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Take an empty list and store it in another variable.
  • Loop from ‘1’  to the above-given number range using For loop.
  • Check whether the given number modulus iterator value is equal to ‘0’ or not using the if conditional statement.
  • If the statement is True, Check if the iterator modulus 2 is not equal to 0 using the if conditional statement.
  • If the statement is True, append the iterator value to the above-declared list.
  • Get the sum of all odd factors of the above-given list using the built-in sum() function and store it in another variable.
  • Print the sum of all the odd factors of a given number.
  • The Exit of the program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_numb = 24
# Take an empty list and store it in another variable.
all_factors = []
# Loop from '1' to above given number range using For loop.
for itr in range(1, gvn_numb+1):
    # Check whether the given number modulus iterator value is equal to '0' or not
    # using if conditional statement.
    if gvn_numb % itr == 0:
        # If the statement is True, Check if the iterator modulus 2 is not equal to 0 using the
        # if conditional statement.
        if itr % 2 != 0:
            # If the statement is True ,append the iterator value to the above declared list .
            all_factors.append(itr)
      # Get the sum of all the odd factors of above got list using built-in sum() function
      # and store it in another variable.
reslt = sum(all_factors)
# Print the sum of all odd factors of a given number.
print("The Sum of all odd factors of {", gvn_numb, "}  = ", reslt)

Output:

The Sum of all odd factors of { 24 }  =  4

Method #2: Using For loop (User Input)

Approach:

  • Give the number as user input using int(input()) and store it in a variable.
  • Take an empty list and store it in another variable.
  • Loop from ‘1’  to the above-given number range using For loop.
  • Check whether the given number modulus iterator value is equal to ‘0’ or not using the if conditional statement.
  • If the statement is True, Check if the iterator modulus 2 is not equal to 0 using the if conditional statement.
  • If the statement is True, append the iterator value to the above-declared list.
  • Get the sum of all odd factors of the above-given list using the built-in sum() function and store it in another variable.
  • Print the sum of all the odd factors of a given number.
  • The Exit of the program.

Below is the implementation:

# Give the number as user input using int(input()) and store it in a variable.
gvn_numb = int(input("Enter some random Number = "))
# Take an empty list and store it in another variable.
all_factors = []
# Loop from '1' to above given number range using For loop.
for itr in range(1, gvn_numb+1):
    # Check whether the given number modulus iterator value is equal to '0' or not
    # using if conditional statement.
    if gvn_numb % itr == 0:
# If the statement is True, Check if the iterator modulus 2 is not equal to 0 using the
# if conditional statement.
        if itr % 2 != 0:
            # If the statement is True ,append the iterator value to the above declared list .
            all_factors.append(itr)
# Get the sum of all the odd factors of above got list using built-in sum() function
# and store it in another variable.
reslt = sum(all_factors)
# Print the sum of all odd factors of a given number.
print("The Sum of all odd factors of {", gvn_numb, "}  = ", reslt)

Output:

Enter some random Number = 56
The Sum of all odd factors of { 56 } = 8

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.

Python Program to Find Sum of Odd Factors of a Number Read More »

Program to Check if Array can be Sorted with One Swap

Python Program to Check if Array can be Sorted with One Swap

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.

Python Program to Check if Array can be Sorted with One Swap Read More »