0% found this document useful (0 votes)
14 views

List Based Programs PDF

Uploaded by

NC
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

List Based Programs PDF

Uploaded by

NC
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

LIST BASED PROGRAMMING QUESTIONS ¶

SET - 01

1. WAPP to sum all the items


Input:
[1,7,-10,34,2,-8]

Output:
Sum all the items = 26

2. WAPP to multiply all the items in a list


Input:
[3,4,5,4,7]

Output:
Multiply all the items in a list: 1680

3. WAPP to get the largest number from a list


Input:
[1,7,10,34,2,8]

Output:
Largest Number: 34

4. WAPP to get the smallest number from a list


Input:
[51,7,10,34,2,8]

Output:
Smallest Number: 2

5. WAPP to count the number of strings where the string length is 2 or more
and the first and last character are same from a give list of strings
Input:
['abc','xyz','aba','1221']

Output:
Find and Last Character are same: 2
6. WAPP to remove duplicates from a list
Input:
[1,2,3,7,2,1,5,6,4,8,5,4]

Output:
[1,2,3,4,5,6,7,8]

7. WAPP to check a list is empty or not


Input:
[3,4,5,4,7]

Output:
List is Not Empty

8. WAPP to clone or copy a list


Input:
[3,4,5,4,7]

Output:
Clone or Copy a List: [10,22,44,23,4]

9. WAPP to find the list of words that are longer than n from a given list of
words.
Input:
Find the List of Words that are Longer than n from a given List of Words Given value of n = 4

Output:
['Words', 'Longer', 'given', 'Words']

10. WAPP that get two lists as input and check if they have at least one
common member.
Input:
[1,2,3,4,5] [5,6,7,8,9]

Output:
Lists have atleast one common member
SOLUTION -- SET 01
In [90]: 1 # 01
2 item=[1,7,-10,34,2,-8]
3 sum = 0
4 for i in item:
5 sum += i
6 print(sum)

26

In [91]: 1 # 02
2 item=[3,4,5,4,7]
3 total = 1
4 for i in item:
5 total *= i
6 print(total)

1680

In [92]: 1 # 03
2 a=[1,7,10,34,2,8]
3 print("Largest Number :",max(a))

Largest Number : 34

In [93]: 1 # 03 <- ALTERNATIVE SOLUTION


2 a=[1,7,10,34,2,8]
3 max_v = a[0]
4 for i in a:
5 if i > max_v:
6 max_v = i
7 print("Largest Number :",max_v)

Largest Number : 34

In [94]: 1 # 04
2 a=[51,7,10,34,2,8]
3 print("Smallest Number :",min(a))

Smallest Number : 2

In [95]: 1 # 04 <-- ALTERNATIVE SOLUTION


2 a=[51,7,10,34,2,8]
3 min_num = a[0]
4 for i in a:
5 if i < min_num:
6 min_num = i
7 print("Smallest Number :",min_num)

Smallest Number : 2
In [96]: 1 # 05
2 """
3 Sample List: ['abc', 'xyz', 'aba', '1221']
4 Expected Result: 2
5 """
6 ​
7 word=["madem","3643","apple","3756"]
8 ch = 0
9 for w in word:
10 if len(w) > 1 and w[0] == w[-1]:
11 ch += 1
12 print(ch)

In [97]: 1 # 06
2 a = [1,2,3,7,2,1,5,6,4,8,5,4]
3 b=set(a)
4 print(list(b))

[1, 2, 3, 4, 5, 6, 7, 8]

In [98]: 1 # 06 <-- ALTERNATIVE SOLUTION


2 a = [1,2,3,7,2,1,5,6,4,8,5,4]
3 dup = set()
4 uniq = []
5 for x in a:
6 if x not in dup:
7 uniq.append(x)
8 dup.add(x)
9 print(list(dup))

[1, 2, 3, 4, 5, 6, 7, 8]

In [99]: 1 # 07
2 a = []
3 if not a:
4 print("List is Empty...")
5 else:
6 print("List is Not Empty...")

List is Empty...

In [100]: 1 # 08
2 old_list = [10, 22, 44, 23, 4]
3 new_list = list(old_list)
4 ​
5 print("Old List : ",old_list)
6 print("New List : ",new_list)

Old List : [10, 22, 44, 23, 4]


New List : [10, 22, 44, 23, 4]
In [101]: 1 # 09
2 n=4
3 str1 ="Find the List of Words that are Longer than n from a given List of Words"
4 new_list = []
5 ​
6 text = str1.split(" ")
7 ​
8 for x in text:
9 if len(x) > n:
10 new_list.append(x)
11 print(new_list)

['Words', 'Longer', 'given', 'Words']

In [102]: 1 # 10
2 list1=[1,2,3,4,5]
3 list2=[5,6,7,8,9]
4 ​
5 result = False
6 for x in list1:
7 for y in list2:
8 if x == y:
9 result = True
10 print(result)
11 if result:
12 print("Lists have at least one common member")
13 else:
14 print("Lists do not have any common member")

True
Lists have at least one common member

SET - 02

11. WAPP to print a specified list after removing the 0th, 4th and 5th elements.
(enumerate)
Input:
["Cat", "Dog", "Elephant", "Fox", "Tiger", "Lion", "Ponda"]

Output:
['Dog', 'Elephant', 'Fox', 'Ponda']

12. WAPP to print the numbers of a specified list after removing even numbers
from it
Input:
[7,32,81,20,25,14,23,27]

Output:
[7, 81, 25, 23, 27]
13. WAPP to shuffle and print a specified list (shuffle)
Input:
["Cat", "Dog", "Elephant", "Fox", "Tiger", "Lion", "Ponda"]

Output:
['Fox', 'Cat', 'Tiger', 'Lion', 'Dog', 'Ponda', 'Elephant']

14. WAPP to generate and print a list of first and last 5 elements where the
values are square of numbers between 1 and 30
Output:
First 5 elements : [1, 4, 9, 16, 25]
Last 5 elements : [625, 676, 729, 784, 841]`

15. WAPP to generate all permutations of a list in Python. (itertools)


Input:
[1,2,3]

Output:
[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]

16. WAPP to convert a list of characters into a string


Input:
['D','P','S',' ', 'R','u','b','y']

Output:
DPS Ruby

17. WAPP to find the index of an item in a specified list


Input:
[20, 70, 30, 90, 10, 30, 90, 10, 80]

Output:
Item to find the index of 30

Index Number of Item = 2

18. WAPP to flatten a shallow list


Input:
[[20,30,70],[30,90,10], [30,20], [70,90,10,80]]

Output:
[20, 30, 70, 30, 90, 10, 30, 20, 70, 90, 10, 80]
19. WAPP to add a list to the second list
Input:
[10, 20, 30, 40]

["Cat", "Dog", "Lion", "Ponda"]

Output:
[10, 20, 30, 40, 'Cat', 'Dog', 'Lion', 'Ponda']

20. WAPP to select an item randomly from a list Using random.choice()


Input:
["Cat", "Dog", "Elephant", "Fox", "Tiger", "Lion", "Ponda"]

Output:
Item randomly from a list : Fox

SOLUTION -- SET 02
In [103]: 1 # 11
2 animal = ["Cat", "Dog", "Elephant", "Fox", "Tiger", "Lion", "Ponda"]
3 a = []
4 for i, x in enumerate(animal):
5 if i not in (0,4,5):
6 a.append(x)
7 print(a)

['Dog', 'Elephant', 'Fox', 'Ponda']

In [104]: 1 # 11 <-- ALTERNATE SOLUTION USING LIST COMPREHENSION


2 animal = ["Cat", "Dog", "Elephant", "Fox", "Tiger", "Lion", "Ponda"]
3 animal = [x for (i,x) in enumerate(animal) if i not in (0,4,5)]
4 print(animal)

['Dog', 'Elephant', 'Fox', 'Ponda']

In [105]: 1 # 12
2 n = [7,32,81,20,25,14,23,27]
3 n1 = []
4 for x in n:
5 if x%2 != 0:
6 n1.append(x)
7 print(n1)

[7, 81, 25, 23, 27]

In [106]: 1 # 12 <- ALTERNATE SOLUTION


2 n = [7,32,81,20,25,14,23,27]
3 n = [x for x in n if x%2!=0]
4 print(n)

[7, 81, 25, 23, 27]


In [107]: 1 # 13
2 from random import shuffle
3 animal = ["Cat", "Dog", "Elephant", "Fox", "Tiger", "Lion", "Ponda"]
4 shuffle(animal)
5 print(animal)

['Elephant', 'Tiger', 'Lion', 'Cat', 'Fox', 'Dog', 'Ponda']

In [108]: 1 # 14
2 l = list()
3 for i in range(1,25):
4 l.append(i**2)
5 print(l[:5])
6 print(l[-5:])

[1, 4, 9, 16, 25]


[400, 441, 484, 529, 576]

In [109]: 1 # 15
2 import itertools
3 print(list(itertools.permutations([1,2,3])))

[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]

In [110]: 1 # 16
2 s = ['D','P','S',' ', 'R','u','b','y']
3 s1 = ''.join(s)
4 print(s1)

DPS Ruby

In [111]: 1 # 17
2 num = [20, 70, 30, 90, 10, 30, 90, 10, 80]
3 print(num.index(30))

In [112]: 1 # 18
2 ori_list = [[20,30,70],[30,90,10], [30,20], [70,90,10,80]]
3 flatten = []
4 for i in ori_list:
5 for j in i:
6 flatten.append(j)
7 print(flatten)

[20, 30, 70, 30, 90, 10, 30, 20, 70, 90, 10, 80]

In [113]: 1 # 18 <-- ALTERNATE SOLUTION


2 import itertools
3 ​
4 ori_list = [[20,30,70],[30,90,10], [30,20], [70,90,10,80]]
5 merged_list = list(itertools.chain(*ori_list))
6 print(merged_list)

[20, 30, 70, 30, 90, 10, 30, 20, 70, 90, 10, 80]
In [114]: 1 # 19
2 Number = [10, 20, 30, 40]
3 animal = ["Cat", "Dog", "Lion", "Ponda"]
4 final_list = Number + animal
5 print(final_list)

[10, 20, 30, 40, 'Cat', 'Dog', 'Lion', 'Ponda']

In [115]: 1 # 20
2 import random
3 animal = ["Cat", "Dog", "Elephant", "Fox", "Tiger", "Lion", "Panda"]
4 print(random.choice(animal))

Elephant

SET - 03

21. WAPP to check whether two lists are circularly identical


Input:
[8, 8, 12, 12, 8]

[8, 8, 8, 12, 12]

[1, 8, 8, 12, 12]

Output:
Compare List1 and List2 : True

Compare List1 and List3 : False

22. WAPP to find the second smallest number in a list


Input:
[2,4,56,78,4,34,5,8,9]

Output:
Second Smallest Number : 4

23. WAPP to find the second largest number in a list


Input:
[82,4,56,78,4,34,5,100,9]

Output:
Second Largest Number : 82

24. WAPP to get unique values from a list


Input:
[82, 4, 10, 56, 78, 4, 34, 5, 10, 9]
Output:

25. WAPP to get the frequency of the elements in a list.


Input:
[10, 30, 50, 10, 20, 60, 20, 60, 40, 40, 50, 50, 30]

Output:
{50: 3, 10: 2, 30: 2, 20: 2, 60: 2, 40: 2}

26. Create a list by concatenating a given list which range goes from 1 to n
Input:
['T', 'J']

N = 10

Output:
['T1', 'J1', 'T2', 'J2', 'T3', 'J3', 'T4', 'J4', 'T5', 'J5', 'T6', 'J6', 'T7', 'J7', 'T8', 'J8', 'T9', 'J9', 'T10', 'J10']

27. WAPP to get variable unique identification number or string


Input:
x = 30

s = "DPS Ruby"

Output:
Unique Identification Number : 7005f980

Unique Identification String : c24bb0

28. WAPP to find common items from two lists


Input:
[23,45,67,78,89,34]

[34,89,55,56,39,67]

Output:
Common items from two lists : {89, 34, 67}

29. WAPP to split a list based on first character of word


Input:
["cat", "dog", "cow", "tiger", "lion", "Fox", "Shark", "Snake", "turtle", "mouse", "monkey", "bear"]

Output:
F
Fox
S
Shark
Snake
b
bear
c
cat
cow
d
dog
l
lion
m
monkey
mouse
t
tiger
turtle

30. WAPP to select the odd number of a list


Input:
[1,2,4,3,6,7,5,8,9,7,8,9,10]

Output:
[1, 3, 7, 5, 9, 7, 9]

SOLUTION -- SET 03
In [116]: 1 # 21
2 list1 = [8, 8, 12, 12, 8]
3 list2 = [8, 8, 8, 12, 12]
4 list3 = [1, 8, 8, 12, 12]
5 ​
6 print("Compare List1 and List2 : ",' '.join(map(str, list2)) in ' '.join(map(str, li
7 print("Compare List1 and List3 : ",' '.join(map(str, list3)) in ' '.join(map(str, li

Compare List1 and List2 : True


Compare List1 and List3 : False
In [117]: 1 # 22
2 num = [2,4,56,78,4,34,5,8,9]
3 ​
4 if len(num)<2:
5 print(num)
6
7 if (len(num)==2) and (num[0] == num[1]):
8 print(num)
9
10 dup_items = set()
11 uniq_items = []
12 for x in num:
13 if x not in dup_items:
14 uniq_items.append(x)
15 dup_items.add(x)
16
17 uniq_items.sort()
18 print(uniq_items[1])

In [118]: 1 # 23
2 num = [ 82,4,56,78,4,34,5,100,9]
3 ​
4 if (len(num)<2):
5 print(num)
6
7 if ((len(num)==2) and (num[0] == num[1]) ):
8 print(num)
9
10 dup_items = set()
11 uniq_items = []
12 for x in num:
13 if x not in dup_items:
14 uniq_items.append(x)
15 dup_items.add(x)
16 uniq_items.sort()
17 print(uniq_items[-2])

82

In [119]: 1 # 24
2 l = [ 82,4,10,56,78,4,34,5,10,9]
3 ​
4 print("Original List : ",l)
5 ​
6 s = set(l)
7 new_list = list(s)
8 ​
9 print("Unique Numbers : ",new_list)

Original List : [82, 4, 10, 56, 78, 4, 34, 5, 10, 9]


Unique Numbers : [34, 4, 5, 9, 10, 78, 82, 56]
In [120]: 1 # 25
2 import collections
3 l = [10,30,50,10,20,60,20,60,40,40,50,50,30]
4 ​
5 print("Original List : ",l)
6 ​
7 f = collections.Counter(l)
8 ​
9 print("Frequency of the Elements: ",f)

Original List : [10, 30, 50, 10, 20, 60, 20, 60, 40, 40, 50, 50, 30]
Frequency of the Elements: Counter({50: 3, 10: 2, 30: 2, 20: 2, 60: 2, 40: 2})

In [121]: 1 # 26
2 ch = ['T', 'J']
3 n = 10
4 new_list = ['{}{}'.format(a, b) for b in range(1, n+1) for a in ch]
5 print(new_list)

['T1', 'J1', 'T2', 'J2', 'T3', 'J3', 'T4', 'J4', 'T5', 'J5', 'T6', 'J6', 'T7', 'J7', 'T
8', 'J8', 'T9', 'J9', 'T10', 'J10']

In [122]: 1 # 27
2 x = 30
3 print(format(id(x), 'x'))
4 s = "DPS Ruby"
5 print(format(id(s), 'x'))

7ffcb4a0d6c8
2136278fdb0

In [123]: 1 # 28
2 num1 = [23,45,67,78,89,34]
3 num2 = [34,89,55,56,39,67]
4 print(set(num1) & set(num2))

{89, 34, 67}


In [124]: 1 # 29
2 from itertools import groupby
3 from operator import itemgetter
4
5 a = ["cat","dog","cow","tiger","lion","Fox","Shark","Snake","turtle","mouse","monkey
6
7 for ltr, wds in groupby(sorted(a), key=itemgetter(0)):
8 print(ltr)
9 for w in wds:
10 print(" ", w)
11 print("")

F
Fox

S
Shark

Snake

b
bear

c
cat

cow

d
dog

l
lion

m
monkey

mouse

t
tiger

turtle

In [125]: 1 # 30
2 a=[1,2,4,3,6,7,5,8,9,7,8,9,10]
3 odd_num=[]
4 ​
5 for i in a:
6 if(i%2==1):
7 #print(i)
8 odd_num.append(i)
9
10 print(odd_num)

[1, 3, 7, 5, 9, 7, 9]

SET - 04
31. WAPP to count unique values inside a list
Input:
[10, 20, 30, 50, 80, 70, 70, 80, 10]

Output:
No of Unique Items in List : 6

32. WAPP to List product excluding duplicates


Input:
[2, 1, 2, 4, 6, 4, 3, 2, 1]

Output:
Duplication removal list product : 144

33. WAPP to Extract elements with Frequency greater than K


Input:
[4, 6, 4, 3, 3, 4, 3, 7, 8, 8]

Output:

34. WAPP to Test if List contains elements in Range


Input:
[4, 5, 6, 7, 3, 9]

Output:
Does list contain all elements in range : True

35. WAPP to check if the list contains three consecutive common numbers in
Python
Input:
[18, 18, 18, 6, 3, 4, 9, 9, 9]

Output:
Three Consecutive common numbers = 18, 9

36. WAPP to find the Strongest Neighbour


Input:
[10,20,30,20,30,400]

Output:
20 30 30 30 400
37. WAPP to print all Possible Combinations from the three Digits
Input:
[1, 2, 3]

Output:
123

132

213

231

312

321

38. WAPP to find all the Combinations in the list with the given condition
Input:
['DPS Ruby', ['Software', 'Computer'], ['Solution', 'Education']]

Output:
[ ['DPS Ruby', 'Software', 'Solution'], ['DPS Ruby', 'Computer', 'Education'] ]

39. WAPP to get all unique combinations of two Lists


Input:
['A','B','C']

[1,2,3]

Output:
[ [('A', 1), ('B', 2), ('C', 3)], [('A', 1), ('C', 2), ('B', 3)], [('B', 1), ('A', 2), ('C', 3)], [('B', 1), ('C', 2), ('A', 3)], [('C', 1),
('A', 2), ('B', 3)], [('C', 1), ('B', 2), ('A', 3)] ]

40. WAPP to remove all the occurrences of an element from a list


Input:
[1, 3, 4, 6, 5, 1]

Output:
[3, 4, 6, 5]

SOLUTION -- SET 04
In [126]: 1 # 31
2 #First Method
3
4 a = [10, 20, 30, 50, 80, 70, 70, 80, 10]
5 s = set(a)
6 print("No of Unique Items in List :", len(s))

No of Unique Items in List : 6

In [127]: 1 # 31
2 # Second Method
3 a = [10, 20, 30, 50, 80, 70, 70, 80, 10]
4 l = []
5 count = 0
6 for i in a:
7 if i not in l:
8 count += 1
9 l.append(i)
10 ​
11 print("No of Unique Items in List :", count)

No of Unique Items in List : 6

In [128]: 1 # 32
2 a = [2,1,2,4,6,4,3,2,1]
3 print ("Original list : " + str(a))
4
5 b=list(set(a))
6 p=1
7 for i in b:
8 p*=i
9 ​
10 print("Duplication removal list product : " + str(p))

Original list : [2, 1, 2, 4, 6, 4, 3, 2, 1]


Duplication removal list product : 144

In [129]: 1 # 33
2 a = [4, 6, 4, 3, 3, 4, 3, 7, 8, 8]
3 print("Original list : " + str(a))
4 K = 2
5
6 res = []
7 for i in a:
8 freq = a.count(i)
9 if freq > K and i not in res:
10 res.append(i)
11 ​
12 print("The Required Elements : " + str(res))

Original list : [4, 6, 4, 3, 3, 4, 3, 7, 8, 8]


The Required Elements : [4, 3]
In [130]: 1 # 34
2 a = [4, 5, 6, 7, 3, 9]
3
4 print("Original list is : " + str(a))
5
6 i, j = 3, 10
7
8 res = True
9 for e in a:
10 if e < i or e >= j :
11 res = False
12 break
13 ​
14 print ("Does list contain all elements in range : " + str(res))

Original list is : [4, 5, 6, 7, 3, 9]


Does list contain all elements in range : True

In [131]: 1 # 35
2 # creating the aay
3 a = [18, 18, 18, 6, 3, 4, 9, 9, 9]
4 l = len(a)
5 for i in range(l - 2):
6 if a[i] == a[i + 1] and a[i + 1] == a[i + 2]:
7 print(a[i])

18
9

In [132]: 1 # 36
2 n = 6
3 a1 = [10,20,30,20,30,400]
4 a2 = []
5 for i in range(1, n):
6 r = max(a1[i], a1[i-1])
7 a2.append(r)
8 for i in a2 :
9 print(i,end=" ")

20 30 30 30 400

In [133]: 1 # 37
2 a = [1, 2, 3]
3 for i in range(3):
4 for j in range(3):
5 for k in range(3):
6 if (i!=j and j!=k and i!=k):
7 print(a[i], a[j], a[k])

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
In [134]: 1 # 38
2 val = ["DPS Ruby",["Software","Computer"], ["Solution", "Education"]]
3 print("Original List : " + str(val))
4 a = 2
5 l = []
6 c = 0
7 ​
8 while c <= a - 1:
9 t = []
10 for i in val:
11 if not isinstance(i, list):
12 t.append(i)
13 else:
14 t.append(i[c])
15 c += 1
16 l.append(t)
17 ​
18 print("\nIndex Combinations : " + str(l))

Original List : ['DPS Ruby', ['Software', 'Computer'], ['Solution', 'Education']]

Index Combinations : [['DPS Ruby', 'Software', 'Solution'], ['DPS Ruby', 'Computer', 'E
ducation']]

In [135]: 1 # 39
2 import itertools
3 from itertools import permutations
4 l1 = ['A','B','C']
5 l2 = [1,2,3]
6 unique = []
7 permut = itertools.permutations(l1, len(l2))
8 for comb in permut:
9 zipped = zip(comb, l2)
10 unique.append(list(zipped))
11 ​
12 print(unique)

[[('A', 1), ('B', 2), ('C', 3)], [('A', 1), ('C', 2), ('B', 3)], [('B', 1), ('A', 2),
('C', 3)], [('B', 1), ('C', 2), ('A', 3)], [('C', 1), ('A', 2), ('B', 3)], [('C', 1),
('B', 2), ('A', 3)]]

In [136]: 1 # 40
2 val = [1, 3, 4, 6, 5, 1]
3 a = 1
4 print ("Original list :" ,val)
5 ​
6 c = val.count(a)
7 for i in range(c):
8 val.remove(a)
9
10 print ("Remove operation :" , val)

Original list : [1, 3, 4, 6, 5, 1]


Remove operation : [3, 4, 6, 5]

SET - 05
41. WAPP to Remove Consecutive K element records
Input:
[ ('A', 'B', 'C', 'D'), ('B', 'C', 'C', 'I'), ('H', 'D', 'B', 'C'), ('C', 'C', 'G', 'F') ]

Output:
[ ('A', 'B', 'C', 'D'), ('H', 'D', 'B', 'C') ]

42. WAPP to Replace index elements with elements in Other List


Input:
[['DPS Ruby', 'Computer', 'Education']

[2, 1, 0, 1, 0, 2, 2, 0, 1, 0, 1, 2]

Output:
['Education', 'Computer', 'DPS Ruby', 'Computer', 'DPS Ruby', 'Education', 'Education', 'DPS Ruby',
'Computer', 'DPS Ruby', 'Computer', 'Education']

43. WAPP to Retain records with N occurrences of K


Input:
[ (4, 5, 6, 5, 4), (4, 5, 3), (5, 5, 2), (3, 4, 9) ]

K=5

N=2

Output:
[ (4, 5, 6, 5, 4), (5, 5, 2) ]

44. WAPP to Swap elements in String list


Input:
['DPS', 'Ruby', 'Computer', 'Education']

Output:
['DPS', "Ruby", 'Software', 'Solutions']

45. WAPP to reverse All Strings in String List


Input:
Original list : ['DPS', 'ruby', 'Computer', 'Education']

Output:
Reversed list : ['SPD', 'ybur', 'retupmoC', 'noitacudE']

46. WAPP to find the character position of Kth word from a list of strings
Input:
['DPS', 'ruby', 'Computer', 'Education']

K = 20
Output:
Index of character at Kth position word : 5

47. WAPP to Prefix frequency in string List


Input:
['TjC', 'TjCpp', 'TjPython', 'Java']

Prefix = 'Tj'

Output:
Strings count with matching frequency : 3

48. WAPP to Split Strings on Prefix Occurrence


Input:
['TjC', 'TjCpp', 'TjPython', 'Java', 'tj']

Prefix = 'Tj'

Output:
[ ['TjC'], ['TjCpp'], ['TjPython', 'Java', 'tj'] ]

49. WAPP to Replace all Characters of a List Except the given character
Input:
['P', 'Y', 'T', 'H', 'O', 'N']

Output:
['@', '@', 'T', '@', '@', '@']

50. WAPP to Add Space between Potential Words


Input:
['DPSRuby', 'ComputerEducations']

Output:
[' D P S Ruby', ' Computer Educations']

SOLUTION -- SET 05
In [137]: 1 # 41
2 val = [('A', 'B', 'C', 'D'), ('B', 'C', 'C', 'I'), ('H', 'D', 'B', 'C'), ('C', 'C',
3 print("Original List : " + str(val))
4 K = 'C'
5 res = [i for i in val if not any(i[j] == K and i[j + 1] == K for j in range(len(i) -
6
7 print("After Removal : " , res)

Original List : [('A', 'B', 'C', 'D'), ('B', 'C', 'C', 'I'), ('H', 'D', 'B', 'C'),
('C', 'C', 'G', 'F')]
After Removal : [('A', 'B', 'C', 'D'), ('H', 'D', 'B', 'C')]
In [138]: 1 # 42
2 a = ['DPS', 'Computer', 'Education']
3 b = [2 ,1 ,0 ,1 ,0 ,2 ,2 ,0 ,1 ,0 ,1 ,2]
4 print("List 1 : " , a)
5 print("List 2 : " , b)
6 res = [a[i] for i in b]
7
8 print ("After Index Elements Replacements is : " ,res)

List 1 : ['DPS', 'Computer', 'Education']


List 2 : [2, 1, 0, 1, 0, 2, 2, 0, 1, 0, 1, 2]
After Index Elements Replacements is : ['Education', 'Computer', 'DPS', 'Computer', 'D
PS', 'Education', 'Education', 'DPS', 'Computer', 'DPS', 'Computer', 'Education']

In [139]: 1 # 43
2 val = [(4, 5, 6, 5, 4), (4, 5, 3), (5, 5, 2), (3, 4, 9)]
3 print(val)
4 K = 5
5 N = 2
6 res = [e for e in val if e.count(K) == N]
7
8 print(res)

[(4, 5, 6, 5, 4), (4, 5, 3), (5, 5, 2), (3, 4, 9)]


[(4, 5, 6, 5, 4), (5, 5, 2)]

In [140]: 1 # 44
2 s = ["DPS","ruby","Computer","Education"]
3
4 print("Before Swap :",s)
5
6 res = [sub.replace("ruby","Ruby").replace("Computer", "Software").replace("Education
7
8 print ("After Swap : ",res)

Before Swap : ['DPS', 'ruby', 'Computer', 'Education']


After Swap : ['DPS', 'Ruby', 'Software', 'Solutions']

In [141]: 1 # 44 <-- Alternate Solution


2 s = ["DPS","ruby","Computer","Education"]
3
4 print("Before Swap :",s)
5 ​
6 res = []
7 for i in s:
8 res.append(i.replace("ruby","Ruby").replace("Computer", "Software").replace("Edu
9 ​
10 print ("After Swap : ",res)

Before Swap : ['DPS', 'ruby', 'Computer', 'Education']


After Swap : ['DPS', 'Ruby', 'Software', 'Solutions']
In [142]: 1 # 45
2 val = ["DPS","ruby","Computer","Education"]
3 print ("Original list : ", val)
4
5 #First Methods
6
7 res = [i[::-1] for i in val]
8 print ("Reversed list : " , res)

Original list : ['DPS', 'ruby', 'Computer', 'Education']


Reversed list : ['SPD', 'ybur', 'retupmoC', 'noitacudE']

In [143]: 1 # 45 <- ALTERNATE SOLUTION


2 # Second Methods
3 val = ["DPS","ruby","Computer","Education"]
4 print ("Original list : ", val)
5 print("Reversed list : " ,val[::-1])

Original list : ['DPS', 'ruby', 'Computer', 'Education']


Reversed list : ['Education', 'Computer', 'ruby', 'DPS']

In [144]: 1 # 46
2 val = ["DPS","ruby","Computer","Education"]
3 print("The original list is : " ,val)
4 K = 20
5 res = [i[0] for sub in enumerate(val) for i in enumerate(sub[1])]
6 res = res[K]
7 print("Index of character at Kth position word : " + str(res))

The original list is : ['DPS', 'ruby', 'Computer', 'Education']


Index of character at Kth position word : 5

In [145]: 1 # 47
2 val = ["TjC","TjCpp","TjPython","Java"]
3 print("Original List : ",val)
4 sub = 'Tj'
5 res = 0
6 for e in val:
7 if e.startswith(sub):
8 res = res + 1
9 print ("Strings count with matching frequency : ",res)

Original List : ['TjC', 'TjCpp', 'TjPython', 'Java']


Strings count with matching frequency : 3
In [146]: 1 # 49
2 l = ["TjC","TjCpp","TjPython","Java","tj"]
3 print("The original list is : ",l)
4 pref = "Tj"
5
6 res = []
7 for val in l:
8 if val.startswith(pref):
9 res.append([val])
10 else:
11 res[-1].append(val)
12 print("Prefix Split List : " + str(res))

The original list is : ['TjC', 'TjCpp', 'TjPython', 'Java', 'tj']


Prefix Split List : [['TjC'], ['TjCpp'], ['TjPython', 'Java', 'tj']]

In [147]: 1 # 49
2 val = ['P', 'Y', 'T', 'H', 'O', 'N']
3 print("The original list : " + str(val))
4
5 res = [i if i == 'T' else '@' for i in val]
6 print("List after replacement : " + str(res))

The original list : ['P', 'Y', 'T', 'H', 'O', 'N']


List after replacement : ['@', '@', 'T', '@', '@', '@']

In [148]: 1 # 50
2 val = ["DPSRuby", "ComputerEducations"]
3 print("Original list : " ,val)
4
5 res = []
6 for i in val:
7 t = [[]]
8 for ch in i:
9 if ch.isupper():
10 t.append([])
11 t[-1].append(ch)
12 res.append(' '.join(''.join(i) for i in t))
13 ​
14 print("The space added list of strings : " , res)

Original list : ['DPSRuby', 'ComputerEducations']


The space added list of strings : [' D P S Ruby', ' Computer Educations']

THANK YOU FOR SOLVING 50 QUESTIONS 😁

You might also like