Python-3 : dictionary problems & Solutions
python-3 : dictionary problems & Solutions
Problem 1. Design a Switch Case using Dictionary in python for following problem:
Take OS names as inputs : RHEL, CENTOS, UBUNTU, FEDORA, AmazonLinux, KALILinux and return the Kernel Version.
(Note:
1. Use Dummy Kernel versions as 3.0, 3.1, 3.2,3.3,3.4 & 3.5 for the OS names respectively.
2. The User Input name should be handled as case insensitiveness.
3. Default Kernel Version is 3.4.)
Solutions:
def switch(os):
return {
'rhel': '3.0',
'ubuntu': '3.2',
'centos': '3.1',
'fedora': '3.3',
'amazonlinux': '3.4',
'kalilinux': '3.5'
}.get(os.lower(), '4.0')
print(switch('RHEL'))
Problem 2. Give a List of Dict : [{'name': 'Bibash','roll':100},{'name': 'Indu','roll':101},{'name': 'Sharmi','roll':101}], get the Student name having the highest Roll Number.
Solutions:
print(sorted(my_list, key= lambda x: x['roll'])[0])
Problem 3. Given three arrays sorted in non-decreasing order, print all common elements in these arrays.
Input: ar1 = [1, 5, 10, 20, 40, 80]
ar2 = [6, 7, 20, 80, 100]
ar3 = [3, 4, 15, 20, 30, 70, 80, 120]
Output: [80, 20]
Solutions:
from collections import Counter
ar1 = [1, 5, 5,7]
ar2 = [3, 4, 5, 5, 10,7]
ar3 = [5, 5, 10, 20,7]
c1 = Counter(ar1)
c2 = Counter(ar2)
c3 = Counter(ar3)
result_dict = dict(c1.items() & c2.items() & c3.items())
res = []
for key, value in result_dict.items():
res.extend([key]*value)
print(res)
Problem 4. Given a list of Strings, replace the value mapped by Kth value of the mapped list.
Input : test_list = [“Gfg”, “is”, “Best”], subs_dict = {“Gfg” : [5, 6, 7], “is” : [7, 4, 2]}, K = 0
Output : [5, 7, “Best”]
Solutions:
Using subs_dict
for i in subs_dict.keys():
test_list[ test_list.index(i) ] = subs_dict[i][k]
print(test_list)
Using List Comprehension
print( [i if i not in subs_dict else subs_dict[i][k] for i in test_list ])
Problem 5. Given a string and a number k, find the k-th non-repeating character in the string. Consider a large input string with lacs of characters and a small character set. How to find the character by only doing only one traversal of input string?
Examples:
Input :
string = ‘bibashsahamy’
k = 3
Output :
y
Solutions:
from collections import OrderedDict
string = 'geeksforgeeks'
k = 3
my_dict = OrderedDict()
for i in string:
my_dict[i] = my_dict.get(i, 0) + 1
print([key for (key, value) in my_dict.items() if value == 1][k-1])
Problem 6. Given a string and a number N, we need to mirror the characters from N-th position up to the length of the string in the alphabetical order. In mirror operation, we change ‘a’ to ‘z’, ‘b’ to ‘y’, and so on.
Examples:
Input : N = 3
engineer
Output : engrmvvi
Solutions:
my_list = list(map(chr, range(97, 123)))
my_dict = dict(zip(my_list, my_list[::-1]))
s = 'engineer'
n = 2
start = s[:3]
for i in s[3:]:
start += my_dict[i]
print(start)
Problem 7. Given a dictionary and a character array, print all valid words that are possible using characters from the array.(Repetitions of characters are not allowed)
Examples:
Input : Dict = ["go","bat","me","eat","goal","boy", "run"]
arr = ['e','o','b', 'a','m','g', 'l']
Output : go, me, goal.
Solutions:
from collections import Counter
Dict = ["go","bat","me","eat","goal","boy", "run"]
arr = ['e','o','o','b', 'a','m','g', 'l']
my_dict = [Counter(key) for key in Dict]
for i in my_dict:
if all([True if arr.count(key) >= i[key] else False for key in i]):
print(''.join(i.elements()), end=' ')
Problem 8. The input dictionary -:my_dict = {‘best’: {‘Himani’: 150, ‘Manjeet’: 10}, ‘gfg’: {‘Himani’: 210, ‘Manjeet’: 5}, ‘is’: {‘Himani’: 900, ‘Manjeet’: 8}}
Find the key where the value of the nested key ‘Himani’ is highest.
Output: is
Solutions:
nested_key = 'Himani'
print(sorted( my_dict.items(), key= lambda x: x[1][nested_key], reverse=True )[0][0])
Problem 9. Given a string, find if it is ordered or not. Print True or False.
Examples: abdey , True(because all letters in strings comes in increasing order)
Solutions
import sys
s ='abdey'
If len(s)<2:
print(True)
sys.exit()
for i in range(1,len(s)):
if ord(s[i]) < ord(s[i-1]):
print(False)
break
else:
print(True)
Comments