Set A
1. Write a python script which accepts 5 integer values and prints “DUPLICATES” if any of the
values entered are duplicates otherwise it prints “ALL UNIQUE”. Example: Let 5 integers are
(32,
45, 90, 45, 6) then output “DUPLICATES” to be printed.
Solution:
print('Enter 6 numbers..')
a=list()
for i in range(6):
a.append(int(input('Enter:
')))
if len(set(a))!=len(a):
print('DUPLICATES.')
else:
print('Unique.')
2. Write a python script to count the number of characters (character frequency) in a string.
Sample
String : google.com'. Expected Result : {'o': 3, 'g': 2, '.': 1, 'e': 1, 'l': 1, 'm': 1, 'c': 1}
Sol:1
test_str = "google.com"
all_freq = {}
for i in test_str:
if i in all_freq:
all_freq[i] += 1
else:
all_freq[i] = 1
print("Count of all characters in GeeksforGeeks is :\n "
+ str(all_freq)
from collections import Counter
test_str = "google.com"
# using collections.Counter() to get
# count of each element in string
res = Counter(test_str)
# printing result
print("Count of all characters in GeeksforGeeks is :\n
"+ str(res))
3. Write a Python program to remove the characters which have odd index values of a given
string.
def odd_values_string(str):
result = ""
for i in range(len(str)):
if i % 2 == 0:
result = result + str[i]
return result
print(odd_values_string('abcdef'))
print(odd_values_string('python'))
4. Write a program to implement the concept of stack using list
stack = []
# append() function to push
# element in the stack
stack.append('a')
stack.append('b')
stack.append('c')
print('Initial stack')
print(stack)
# pop() function to pop
# element from stack in
# LIFO order
print('\nElements popped from stack:')
print(stack.pop())
print(stack.pop())
print(stack.pop())
print('\nStack after elements are
popped:')
print(stack)
5. Write a Python program to get a string from a given string where all occurrences of its first
char have been changed to '$', except the first char itself. Sample String: 'restart'
Expected Result :
'resta$t'
def change_char(str1):
char = str1[0]
str1 = str1.replace(char, '$')
str1 = char + str1[1:]
return str1
print(change_char('restart'))
Set B
1. Write a Python program to get a string made of the first 2 and the last 2 chars from a given a string.
If the string length is less than 2, return instead of the empty string.
Sample String : 'General12'
Expected Result : 'Ge12'
Sample String : 'Ka'
Expected Result : 'KaKa'
Sample String : ' K'
18
Expected Result : Empty String
def string_both_ends(str):
if len(str) < 2:
return ''
return str[0:2] + str[-2:]
print(string_both_ends('General12'))
print(string_both_ends(''ka'))
print(string_both_ends('k'))
2. Write a Python program to get a single string from two given strings, separated by a space and
swap the first two characters of each string.
Sample String : 'abc', 'xyz'
Expected Result : 'xycabz'
str1, str2 = 'abc', 'xyz'
s1 = str2[:2] + str1[2:]
s2 = str1[:2] + str2[2:]
print(s1,s2)
3. Write a Python program to count the occurrences of each word in a given sentence.
def word_count(str):
counts = dict()
words = str.split()
for word in words:
if word in counts:
counts[word] += 1
else:
counts[word] = 1
return counts
print( word_count('the quick brown fox jumps over the lazy dog.'))
4. Write a program to implement the concept of queue using list
# implementing Queue using List :
q=[]
q.append(10)
q.append(100)
q.append(1000)
q.append(10000)
print("Initial Queue is:",q)
print(q.pop(0))
print(q.pop(0))
print(q.pop(0))
print("After Removing elements:",q)
5. Write a python program to count repeated characters in a string.
Sample string: 'thequickbrownfoxjumpsoverthelazydog'
Expected output:
o4
e3
u2
h2
r2
t2
string = "python programming";
print("Duplicate characters in a given string: ");
#Counts each character present in the string
for i in range(0, len(string)):
count = 1;
for j in range(i+1, len(string)):
if(string[i] == string[j] and string[i] != ' '):
count = count + 1;
#Set string[j] to 0 to avoid printing visited character
string = string[:j] + '0' + string[j+1:];
#A character is considered as duplicate if count is greater than 1
if(count > 1 and string[i] != '0'):
print(string[i]);