EXP 6 (Programs)
EXP 6 (Programs)
AIM:
AIM-6A:
To write a program to check if a given value is present in a set or not, if two given sets
have no elements in common, if a given set is a superset of itself and a superset of another
given set, to find elements in a given set that are not in another set.
ALGORITHM:
PROGRAM:
if common_elements:
else:
if set1.issuperset(set2):
if set2.issuperset(set1):
else:
To write a program to find all the unique words and count the frequency of occurrence
from a given list of strings using python set data type.
ALGORITHM:
➢ Take a comma-separated list of words as input and split the input into a list.
➢ Remove duplicates by converting the list to a set.
➢ Convert the set back to a list.
➢ For each word in the list count its occurrences in the original list and print the word
and its count.
PROGRAM:
unique_words = set(words_list)
print(f"{word.strip()}: {words_list.count(word)}")
AIM-6C:
To write a program to separate out the names into two sets, one containing names
beginning with A and another containing names beginning with B.
ALGORITHM:
PROGRAM:
set1 = set(names_list)
lst = list(set1)
lstA = []
lstB = []
name = name.strip()
if name.startswith(('A', 'a')):
lstA.append(name)
lstB.append(name)
setA = set(lstA)
setB = set(lstB)
To write a function to combine two dictionaries by adding values for common keys. If a
key exists in both dictionaries, the values should be added together.
ALGORITHM:
PROGRAM:
merged_dict = dict1.copy()
if key in merged_dict:
merged_dict[key] += value
else:
merged_dict[key] = value
return merged_dict
print(result)
AIM-6E:
ALGORITHM:
def get_dictionary():
user_dict = {}
for _ in range(n):
user_dict[key] = value
return user_dict
def sort_dictionary_by_key(input_dict):
sorted_dict = dict(sorted(input_dict.items()))
return sorted_dict
user_dict = get_dictionary()
sorted_result = sort_dictionary_by_key(user_dict)
To implement a function that removes a specific key from a dictionary and returns the
modified dictionary
ALGORITHM:
➢ Accept or define the dictionary and the key you want to remove.
➢ Check if the Key Exists:
If the key exists in the dictionary, remove it using del or pop().
If the key does not exist, print a message saying the key is not found.
➢ Return the Modified Dictionary.
PROGRAM:
if key_to_remove in input_dict:
del input_dict[key_to_remove]
else:
return input_dict
user_dict = {}
for _ in range(n):
user_dict[key] = value