0% found this document useful (0 votes)
7 views1 page

Python Tuple and Set Programming Tasks

The document outlines a Python lab assignment consisting of ten programming tasks. These tasks involve using tuples, sets, and matrices to perform various operations such as counting occurrences, finding maximum and minimum values, identifying identical items, and checking for pangrams. Each task includes specific requirements and examples to guide the implementation.
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)
7 views1 page

Python Tuple and Set Programming Tasks

The document outlines a Python lab assignment consisting of ten programming tasks. These tasks involve using tuples, sets, and matrices to perform various operations such as counting occurrences, finding maximum and minimum values, identifying identical items, and checking for pangrams. Each task includes specific requirements and examples to guide the implementation.
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

Python Lab Assignment-9

1. Write a program using tuples to count the number of occurrences of days in a


week from a given string.
Note: Read the input as single line(string) without any separators and parse it.
Ex: input = MonTueTueWedThuFriFriSatMonFriWed
Output: Mon – 2 , Tue – 2 , Wed – 2 ,Thu – 1, Fri – 3, Sat – 1, Sun – 0

2. Write a program to accept n integers from the user, store them in a tuple and find
the second maximum and second minimum element. Note: read values into a list
and convert it into tuple.

3. Write a program to create two sets from a list of items and find out the identical
items.
Note: Read n values from user and add them to a list. Split the list into two sets
of size n/2 or n/2 and n/2+1.

4. Write a program using sets to solve the following problem. Assume there are two
subjects with m & n number of registered students respectively. Write a program
to read the names of those students and find the following
a. find the list of students who registered for only one subject
b. find the list of students who registered for both the subjects
c. find the list of students only registered for first subject
d. find the list of students only registered for second subject

5. Write a program to read two strings and print a new string which contains all
uncommon characters from both the strings.
Ex: String 1: HellO
String 2: Hihello
Output: Oiho
6. Write a program to find difference of the elements in two matrices of size MxN.
7. Write a program to find the given matrix of size NxN is symmetric or not.
8. Write a program to sort the elements in all columns of a given MxN matrix.
9. Given a text, write a program to find the number of occurrences of a given word
in the text.
Ex: Text: Hello, welcome to python programming lab! Happy Programming :)
Word: Programming
Output: Programming occurred 2 times.
10. Write a program to check whether a given string is pangram or not. Note: A string
is a pangram if it contains all 26 English alphabets(case insensitive).

Common questions

Powered by AI

To extract and combine uncommon characters between two strings, first convert the strings into sets to handle unique characters. Use the symmetric difference operation on these sets to find characters that are present in either of the strings but not in both. Convert this resulting set of uncommon characters back to a string and print or return it. Given string 1 as 'HellO' and string 2 as 'Hihello', the operation will yield uncommon characters 'Oiho' .

To use tuples for counting occurrences of days of the week from a concatenated string without separators, one approach is to first define a tuple containing all days of the week. Next, iterate through the concatenated input string from the beginning to the end in slices matching the length of the day names (e.g., 3 for 'Mon', 'Tue', etc.). Convert these slices into actual days if they exist in the tuple. Use a dictionary to count each occurrence by incrementing the count for each recognized day. For example, if you have the input string 'MonTueTueWedThuFriFriSatMonFriWed', slice the string every three characters, check against the tuple of days, and update the count dictionary appropriately. The resulting counts will be Mon: 2, Tue: 2, Wed: 2, Thu: 1, Fri: 3, Sat: 1, Sun: 0, assuming no 'Sun' in the string .

To identify identical items in a list when creating two subsets using sets, first divide the list into two parts. If the number of elements is even, each subset will have n/2 elements; if odd, one subset will have n/2 elements and the other n/2+1. Convert these parts into sets. Use the intersection operation on these two sets to determine which items are identical, i.e., appear in both subsets. The resulting intersection set will contain the identical items .

A string is considered a pangram if it contains every letter of the English alphabet at least once, irrespective of case. In Python, transform the string to lowercase and convert it into a set to obtain unique characters, then compare this set against the set of all lowercase English alphabets. If the alphabet set is a subset of the characters set derived from the string, then the string is a pangram .

To determine if an NxN matrix is symmetric, check that each element a[i][j] is equal to a[j][i] for every row i and column j in the matrix, since a symmetric matrix is one that is equal to its transpose. Iterate through the matrix's rows and columns, comparing each corresponding element. If all such comparisons hold true, the matrix is symmetric; otherwise, it is not .

To find the element-wise difference between two matrices of the same dimensions, create a new matrix of the same size. Iterate over each row and column, subtracting corresponding elements from the two matrices and storing the result in the corresponding position in the new matrix. This operation will yield a difference matrix where each element represents the difference between elements of the original matrices at that position .

To find the second maximum and second minimum elements in a tuple of integers, first convert the tuple back to a list since lists allow easier sorting and manipulation. Once converted, sort the list in ascending order. After sorting, the second element and the second from last element will be the second minimum and second maximum, respectively. This assumes duplicates are not consolidated and there are at least two different elements in the tuple .

To count occurrences of a specific word in a text using Python, utilize the split method to break the text into words and iterate over them, checking each against the target word. Alternatively, use the count method directly if analyzing simple text, which provides the number of instances of a substring in a string. For example, given the text 'Hello, welcome to python programming lab! Happy Programming :)' and the word 'Programming', the output will be that 'Programming' occurred 2 times .

To sort elements in all columns of an MxN matrix, iterate over each column index from 0 to N-1, extracting the column into a separate list. Sort this list and then reassign the sorted values back to the original matrix's column. This converts the operation of sorting rows, which is straightforward, into a procedure for sorting columns—a sequence of row-wise operations per column .

To differentiate students registered for one or both subjects using Python lists of student names, convert each list into a set. For students registered in only one subject, perform symmetric difference between the two sets, which will yield students present in only one of the sets. To find students registered in both subjects, compute the intersection of both sets. For students registered only for the first subject, subtract the second set from the first set. Similarly, subtract the first set from the second set for those registered only for the second subject .

You might also like