0% found this document useful (0 votes)
5 views3 pages

Python Programming (Week-6)

The document provides various Python programming exercises focused on logic building, including sorting dictionaries by keys, merging dictionaries, finding common elements in arrays, and determining election winners using dictionaries. It also covers string manipulation techniques such as mirroring characters, counting distinct substrings, and comparing strings for similar characters. Additionally, it includes problems related to substring lengths and string rotations.

Uploaded by

aayushia971
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)
5 views3 pages

Python Programming (Week-6)

The document provides various Python programming exercises focused on logic building, including sorting dictionaries by keys, merging dictionaries, finding common elements in arrays, and determining election winners using dictionaries. It also covers string manipulation techniques such as mirroring characters, counting distinct substrings, and comparing strings for similar characters. Additionally, it includes problems related to substring lengths and string rotations.

Uploaded by

aayushia971
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

Galgotias College of Engineering & Technology, Greater Noida

CSE - Training & Development


(Week-6)
Python Programming
Logic Building Questions
Sorting Dictionary By Key Using sort()
There are two elements in a Python dictionary-keys and values. You can sort
the dictionary by keys, values, or both.
Example:-
Input : {'ravi': '10', 'rajnish': '9', 'abc': '15'} L1
1 Output : OrderedDict([('abc', '15'), ('rajnish', '9'), ('ravi', '10')])
Merging or Concatenating two Dictionaries in Python
2 Example:-
Input : d1 = {'x': 10, 'y': 8} L1
d2 = {'a': 6, 'b': 4}
Output : {'x': 10, 'y': 8, 'a': 6, 'b': 4}
Find common elements in three sorted arrays by dictionary
intersection
One way to efficiently find shared items in three sorted arrays is by using
dictionary intersection. However, it’s important to note that dictionaries are
commonly used for unique keys, so if there are duplicate elements in your
arrays, some adjustments may be needed to make this approach work. Given
three arrays sorted in non-decreasing order, print all common elements in
these arrays in Python.
3
Examples:
Input : ar1 = [1, 5, 10, 20, 40, 80]
ar2 = [6, 7, 20, 80, 100] L2
ar3 = [3, 4, 15, 20, 30, 70, 80, 120]
Output : [80, 20]

Input : ar1 = [1, 5, 5]


ar2 = [3, 4, 5, 5, 10]
ar3 = [5, 5, 10, 20]
Output : [5, 5]
Dictionary and counter in Python to find winner of election

Given an array of names of candidates in an election. A candidate name in the


array represents a vote cast to the candidate. Print the name of candidates
received Max vote. If there is tie, print a lexicographically smaller name.

Examples:
4 Input : votes[] = {"john", "johnny", "jackie",

"johnny", "john", "jackie",

"jamie", "jamie", "john",

"johnny", "jamie", "johnny",


L2
"john"};
Galgotias College of Engineering & Technology, Greater Noida

CSE - Training & Development


Output : John

Explanation : We have four Candidates with name as 'John', 'Johnny',


'jamie', 'jackie'. The candidates John and Johny get maximum votes.
Since John is alphabetically smaller, we print it.
Python Dictionary to find mirror characters in a string

Given a string and a number N, we need to mirror the characters from the N-th
position up to the length of the string in alphabetical order. In mirror operation,
we change ‘a’ to ‘z’, ‘b’ to ‘y’, and so on.

Examples:
5 Input : N = 3
L2
paradox
Output : paizwlc
Explanation : We mirror characters from position 3 to end.

Input : N = 6

pneumonia
Output : pneumlmrz

Count distinct substrings of a string using Rabin Karp algorithm

Given a string, return the number of distinct substrings using Rabin Karp
Algorithm.

Examples:

Input : str = “aba”


Output : 5
Explanation :
Total number of distinct substring are 5 - "a", "ab", "aba", "b" ,"ba" L2
6
Input : str = “abcd”
Output : 10
Explanation :
Total number of distinct substring are 10 - "a", "ab", "abc", "abcd",
"b", "bc", "bcd", "c", "cd", "d"

Print anagrams together in Python using List and Dictionary

Input: arr = [‘cat’, ‘dog’, ‘tac’, ‘god’, ‘act’]


7 Output: ‘cat tac act dog god’
L2
Galgotias College of Engineering & Technology, Greater Noida

CSE - Training & Development


Python – Similar characters Strings comparison

Given two Strings, separated by delim, check if both contain same characters.
Example:

Input : test_str1 = 'e!e!k!s!g', test_str2 = 'g!e!e!k!s', delim = '!'

8 Output : True
L2
Explanation : Same characters, just diff. positions.

Input : test_str1 = 'e!e!k!s', test_str2 = 'g!e!e!k!s', delim = '!'

Output : False

Explanation : g missing in 1st String.


Python – Longest Substring Length of K

Given a String and a character K, find longest substring length of K.


Examples:
Input : test_str = ‘abcaaaacbbaa’, K = b
9 Output : 2 L2
Explanation : b occurs twice, 2 > 1.
Input : test_str = ‘abcaacccbbaa’, K = c
Output : 3
Explanation : Maximum times c occurs is 3.
Python Program to find minimum number of rotations to obtain actual
string

Given two strings s1 and s2. The task is to find out the minimum number of
string rotations for the given string s1 to obtain the actual string s2.
10
Input : eeksg, geeks
Output: 1 L2
Explanation: g is rotated left to obtain geeks.

Input : eksge, geeks


Output: 2
Explanation : e and g are left rotated to obtain geeks.

You might also like