1. Give a dictionary with value lists, sort the keys by summation of values in value list.
Input : test_dict = {‘Gfg’ : [6, 7, 4], ‘best’ : [7, 6, 5]}
Output : {‘Gfg’: 17, ‘best’: 18}
Explanation : Sorted by sum, and replaced.
Input : test_dict = {‘Gfg’ : [8,8], ‘best’ : [5,5]}
Output : {‘best’: 10, ‘Gfg’: 16}
Explanation : Sorted by sum, and replaced.
Sample Input:
Gfg 6 7 4
Best 7 6 5
Sample Output
Gfg 17
Best 18
def sort_dict_by_sum(test_dict):
return {k: sum(v) for k, v in sorted(test_dict.items(), key=lambda
item: sum(item[1]))}
# Sample input
test_dict = {
'Gfg': [6, 7, 4],
'Best': [7, 6, 5]
}
result = sort_dict_by_sum(test_dict)
# Display the results
for key, value in result.items():
print(f"{key} {value}")
2Two string values S1, S2 are passed as the input. The program must print first N characters
present in S1 which are also present in S2.--python code using dictionary
def scrabble_score(word):
points = {
1: "AEILNORSTU",
2: "DG",
3: "BCMP",
4: "FHVWY",
5: "K",
8: "JX",
10: "QZ"
}
score = 0
for letter in word.upper():
for point_value, letters in points.items():
if letter in letters:
score += point_value
break
return score
# Sample usage
word = "REC"
print(f"{word} is worth {scrabble_score(word)} points.")
1. Give a dictionary with value lists, sort the keys by summation of values in value list.
Input : test_dict = {‘Gfg’ : [6, 7, 4], ‘best’ : [7, 6, 5]}
Output : {‘Gfg’: 17, ‘best’: 18}
Explanation : Sorted by sum, and replaced.
Input : test_dict = {‘Gfg’ : [8,8], ‘best’ : [5,5]}
Output : {‘best’: 10, ‘Gfg’: 16}
Explanation : Sorted by sum, and replaced.
Sample Input:
Gfg 6 7 4
Best 7 6 5
Sample Output
Gfg 17
Best 18
2. Give a dictionary with value lists, sort the keys by summation of values in value list.
Input : test_dict = {‘Gfg’ : [6, 7, 4], ‘best’ : [7, 6, 5]}
Output : {‘Gfg’: 17, ‘best’: 18}
Explanation : Sorted by sum, and replaced.
Input : test_dict = {‘Gfg’ : [8,8], ‘best’ : [5,5]}
Output : {‘best’: 10, ‘Gfg’: 16}
Explanation : Sorted by sum, and replaced.
Sample Input:
Gfg 6 7 4
Best 7 6 5
Sample Output
Gfg 17
Best 18
3. A sentence is a string of single-space separated words where each word consists only of lowercase
letters.A word is uncommon if it appears exactly once in one of the sentences, and does not appear
in the other sentence.
Given two sentences s1 and s2, return a list of all the uncommon words. You may return the answer
in any order.
Example 1:
Input: s1 = "this apple is sweet", s2 = "this apple is sour"
Output: ["sweet","sour"]
Example 2:
Input: s1 = "apple apple", s2 = "banana"
Output: ["banana"]
Constraints:
1 <= s1.length, s2.length <= 200
s1 and s2 consist of lowercase English letters and spaces.
s1 and s2 do not have leading or trailing spaces.
All the words in s1 and s2 are separated by a single space.
Note:
Use dictionary to solve the problem
def uncommon_from_sentences(s1, s2):
word_count = {}
for word in s1.split():
word_count[word] = word_count.get(word, 0) + 1
for word in s2.split():
word_count[word] = word_count.get(word, 0) + 1
return [word for word in word_count if word_count[word] == 1]
# Example usage
s1 = "this apple is sweet"
s2 = "this apple is sour"
print(uncommon_from_sentences(s1, s2)) # Output: ['sweet', 'sour']
s1 = "apple apple"
s2 = "banana"
print(uncommon_from_sentences(s1, s2)) # Output: ['banana']
4. A company wants to send its quotation secretly to its client. The company decided to encrypt the
amount they are sending to their client with some special symbols so that the equation amount will
not be revealed to any external person. They used the special symbols !,@,#,$,%,^,&,*,>,< for
0,1,2,3,4,5,6,7,8,9 respectively. Write a python code to help the company to convert the amount to
special symbols.
(Value rounded off to 2 decimal points)
Input
n: Float data type which reads amount to send
Output
s: : String data type which displays symbols
Sample Testcase 1
Input
10000
Output
@!!!!.!!
Sample Testcase2
1234.56
Output
@#$%.^&
def convert_to_symbols(amount):
symbol_map = '!,@,#,$,%,^,&,*,>,<'
amount_str = f"{amount:.2f}"
return ''.join(symbol_map[int(char)] if char.isdigit() else
char for char in amount_str)
# Sample usage
print(convert_to_symbols(10000.00)) # Output: @!!!!.!!
print(convert_to_symbols(1234.56)) # Output: @#$%.^&
5. Objective:
Develop a Python program that takes an input string from the user and counts the number of
occurrences of each vowel (a, e, i, o, u) in the string. The program should be case-insensitive,
meaning it should treat uppercase and lowercase vowels as the same.
Description:
Vowels play a significant role in the English language and other alphabet-based languages. Counting
vowels in a given string is a fundamental task that can be applied in various text processing
applications, including speech recognition, linguistic research, and text analysis. The objective of this
problem is to create a Python script that accurately counts and displays the number of times each
vowel appears in a user-provided string.
Program Requirements:
Input:
First line reading String as input, The string can contain any characters, including letters, numbers,
and special characters.
Output:
Display the number of occurrences of each vowel in the string.
The output should list each vowel followed by its count.
Example:
Consider the following example for better understanding:
• Input: "Python Programming"
• Output
a=1
e=0
i=1
o=2
u=0
For example:
Input Result
Hello World a = 0
e=1
i=0
o=2
u=0
Python a=0
e=0
i=0
o=1
u=0
def count_vowels(input_string):
vowels = 'aeiou'
input_string = input_string.lower()
vowel_count = {v: 0 for v in vowels}
for char in input_string:
if char in vowels:
vowel_count[char] += 1
for vowel in vowels:
print(f"{vowel} = {vowel_count[vowel]}")
# Example usage
input_string = "Python Programming"
count_vowels(input_string)
6. Create a student dictionary for n students with the student name as key and their test mark
assignment mark and lab mark as values. Do the following computations and display the result.
1.Identify the student with the highest average score
2.Identify the student who as the highest Assignment marks
3.Identify the student with the Lowest lab marks
4.Identify the student with the lowest average score
Note:
If more than one student has the same score display all the student names
Sample input:
James 67 89 56
Lalith 89 45 45
Ram 89 89 89
Sita 70 70 70
Sample Output:
Ram
James Ram
Lalith
Lalith
def highest_average_score(students):
return [name for name, scores in students.items() if sum(scores) / 3 == max(sum(s) / 3 for s in
students.values())]
def highest_assignment_marks(students):
return [name for name, scores in students.items() if scores[1] == max(s[1] for s in
students.values())]
def lowest_lab_marks(students):
return [name for name, scores in students.items() if scores[2] == min(s[2] for s in
students.values())]
def lowest_average_score(students):
return [name for name, scores in students.items() if
sum(scores) / 3 == min(sum(s) / 3 for s in
students.values())]
# Sample input
students = {
"James": [67, 89, 56],
"Lalith": [89, 45, 45],
"Ram": [89, 89, 89],
"Sita": [70, 70, 70]
}
print(highest_average_score(students)) # Output:
['Ram']
print(highest_assignment_marks(students)) # Output:
['James', 'Ram']
print(lowest_lab_marks(students)) # Output: ['Lalith']
print(lowest_average_score(students)) # Output:
['Lalith']
7. 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:
Input : votes[] = {"john", "johnny", "jackie",
"johnny", "john", "jackie",
"jamie", "jamie", "john",
"johnny", "jamie", "johnny",
"john"};
Output : John
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. Use dictionary to solve the
above problem
Sample Input:
10
John
John
Johny
Jamie
Jamie
Johny
Jack
Johny
Johny
Jackie
Sample Output:
Johny
def find_winner(votes):
vote_count = {}
for name in votes:
if name in vote_count:
vote_count[name] += 1
else:
vote_count[name] = 1
# Find the maximum votes
max_votes = max(vote_count.values())
# Find all candidates with the maximum votes
max_candidates = [name for name, count in vote_count.items() if
count == max_votes]
# Return the lexicographically smallest candidate
return min(max_candidates)
# Sample input
votes = ["John", "John", "Johny", "Jamie", "Jamie", "Johny", "Jack",
"Johny", "Johny", "Jackie"]
print(find_winner(votes)) # Output: Johny
8. A sentence is a list of words that are separated by a single space with no leading or trailing spaces.
Each word consists of lowercase and uppercase English letters.
A sentence can be shuffled by appending the 1-indexed word position to each word then rearranging
the words in the sentence.
For example, the sentence "This is a sentence" can be shuffled as "sentence4 a3 is2 This1" or "is2
sentence4 This1 a3".
Given a shuffled sentence s containing no more than 9 words, reconstruct and return the original
sentence.
Example 1:
Input:
is2 sentence4 This1 a3
Output:
This is a sentence
Explanation: Sort the words in s to their original positions "This1 is2 a3 sentence4", then remove the
numbers.
Example 2:
Input:
Myself2 Me1 I4 and3
Output:
Me Myself and I
Explanation: Sort the words in s to their original positions "Me1 Myself2 and3 I4", then remove the
numbers.
Constraints:
2 <= s.length <= 200
s consists of lowercase and uppercase English letters, spaces, and digits from 1 to 9.
The number of words in s is between 1 and 9.
The words in s are separated by a single space.
s contains no leading or trailing spaces.
def reconstruct_sentence(s):
return ' '.join(sorted(s.split(), key=lambda x: x[-1])[:-1])
# Example usage
print(reconstruct_sentence("is2 sentence4 This1 a3")) #
Output: This is a sentence
print(reconstruct_sentence("Myself2 Me1 I4 and3")) #
Output: Me Myself and I
9. Given a number, convert it into corresponding alphabet.
Input Output
1 A
26 Z
27 AA
676 YZ
Input Format
Input is an integer
Output Format
Print the alphabets
Constraints
1 <= num <= 4294967295
Sample Input 1
26
Sample Output 1
def excelNumber(num):
result = ' '
while num > 0:
num -= 1
result = chr(num % 26 + 65) + result
num //= 26
return result
# Sample usage
print(excelNumber(26)) # Output: Z
print(excelNumber(27)) # Output: AA
print(excelNumber(676)) # Output: YZ
10. ou are given a string word. A letter is called special if it appears both in lowercase and uppercase
in word.
Your task is to return the number of special letters in word.
Constraints
• The input string word will contain only alphabetic characters (both lowercase and
uppercase).
• The solution must utilize a dictionary to determine the number of special letters.
• The function should handle various edge cases, such as strings without any special letters,
strings with only lowercase or uppercase letters, and mixed strings.
Examples
Example 1:
Input: word = "aaAbcBC"
Output: 3
Explanation:
The special characters in `word` are 'a', 'b', and 'c'.
Example 2:
Input: word = "abc"
Output: 0
Explanation:
No character in `word` appears in uppercase.
For example:
Test Result
print(count_special_letters("AaBbCcDdEe")) 5
def count_special_letters(word):
seen = {}
special_set = set()
for char in word:
lower_char = char.lower()
if lower_char in seen:
special_set.add(lower_char)
else:
seen[lower_char] = char.islower()
return len(special_set)
# Example usage
print(count_special_letters("aaAbcBC")) # Output: 3
print(count_special_letters("abc")) # Output: 0
print(count_special_letters("AaBbCcDdEe")) # Output: 5