Python Programs
P1) Given two sorted Lists A and B of size p and q, write a Python
program to merge elements of A with B by maintaining the sorted
order i.e. fill A with first p smallest elements and fill B with remaining
elements.
Example:
Input :
int[] A = { 1, 5, 6, 7, 8, 10 }
int[] B = { 2, 4, 9 }
Output:
Sorted Arrays:
A: [1, 2, 4, 5, 6, 7]
B: [8, 9, 10]
P2) Write a Python program to rearrange a given Set of elements such
that every second element of the array is greater than its left and right
elements. Example:
Input :
nums= { 1, 2, 4, 9, 5, 3, 8, 7, 10, 12, 14 }
Output:
Array with every second element is greater than its left and right
elements:
[1, 4, 2, 9, 3, 8, 5, 10, 7, 14, 12]
P3) Write a Python program to find all triplets equal to a given sum in a
unsorted set of elements. Example:
Input :
nums = { 1, 6, 3, 0, 8, 4, 1, 7 }
Output:
Triplets of sum 7
(0 1 6)
(0 3 4)
P4) Write a Python program to replace each substring of a given string
that matches the given regular expression with the given
replacement.
Sample string : "The quick brown fox jumps over the lazy dog."
In the above string replace all the fox with cat.
Sample Output:
Original string: The quick brown fox jumps over the lazy dog.
New String: The quick brown cat jumps over the lazy dog
P5) Write a Python program to check whether a given string is a
pangram or not.
P6) Write a Python program to check whether two strings are
Anagrams or not.
P7) Write a Python program to generate all possible permutations of
given string
P8) Write a Python program to Print all the valid parentheses
combinations for the given number. Or, generate balanced
parentheses using any programming languages like C/C++, Python,
Java…
Example 1:
Input: n = 2 (number of parenthesis)
Output:
(())
()()
Example 2:
Input: n = 3 (number of parenthesis)
Output:
((()))
(()())
(())()
()(())
()()()
P9) You have given a string. There can have multiple special
characters inside the string. Write a program to reverse the given
string. The position of the special characters should not be changed.
Example:
Input: 'abc/defgh$ij'
Output: 'jih/gfedc$ba'
P10) Problem Statement:
The characters in the string should be sorted based on the following
conditions.
Sort the characters in the string by their frequency of occurrences (the
number of times characters have occurred in the string).
If the two different characters have the same frequency, these letters
should be sorted based on their alphabetical order.
Example:
Input String:
csestack
Output String:
aektccss