From f8fe14407426b9ddc7543959426d0aa3a49880b4 Mon Sep 17 00:00:00 2001 From: tejasjokin Date: Mon, 25 Sep 2023 18:10:31 +0530 Subject: [PATCH] 4 assignments added --- A01 CricketBadFootball.py | 119 +++++++++++++++++++++++++++++++++++++ A02 ArrayOperations.py | 80 +++++++++++++++++++++++++ A05 String.py | 99 ++++++++++++++++++++++++++++++ B14 BubbleSelectionSort.py | 107 +++++++++++++++++++++++++++++++++ 4 files changed, 405 insertions(+) create mode 100644 A01 CricketBadFootball.py create mode 100644 A02 ArrayOperations.py create mode 100644 A05 String.py create mode 100644 B14 BubbleSelectionSort.py diff --git a/A01 CricketBadFootball.py b/A01 CricketBadFootball.py new file mode 100644 index 0000000..0bc2d78 --- /dev/null +++ b/A01 CricketBadFootball.py @@ -0,0 +1,119 @@ +Uni = [] +n = int(input("Enter number of students: ")) +for i in range(0,n): + element = input() + Uni.append(element) +print(Uni) +cricket = [] +b = int(input("Enter number students who play cricket: ")) +for j in range(0,b): + ele=input() + cricket.append(ele) +print(cricket) +bad = [] +c = int(input("Enter number students who play badminton: ")) +for k in range(0,c): + elem=input() + bad.append(elem) +print(bad) +foot = [] +d = int(input("Enter number students who play football: ")) +for j in range(0,d): + eleme=input() + foot.append(eleme) +print(foot) + +def CB(): + list3 = intersection(cricket,bad) + print("Names of common students who play cricket and badminton: ", list3) + print("Number of students who play cricket and badminton: ",len(list3)) + +def CF(): + list3 = intersection(cricket,foot) + print("Names of common students who play cricket and football: ", list3) + print("Number of students who play cricket and football: ",len(list3)) + +def BF(): + list3 = intersection(bad,foot) + print("Names of common students who play football and badminton: ", list3) + print("Number of students who play badminton and football: ",len(list3)) + +def intersection(list1,list2): + list3=[] + for val in list1: + if val in list2: + list3.append(val) + return list3 + +def eCeB(): + D1=diff(cricket,bad) + D2=diff(bad,cricket) + # print("C-B = ",D1) + # print("B-C = ",D2) + DUB=union(D1,D2) + print("People who play either cricket or badminton but not both: ",DUB) +def eCeF(): + D1=diff(cricket,foot) + D2=diff(foot,cricket) + # print("C-B = ",D1) + # print("B-C = ",D2) + DUB=union(D1,D2) + print("People who play either cricket or football but not both: ",DUB) +def eFeB(): + D1=diff(foot,bad) + D2=diff(bad,foot) + # print("C-B = ",D1) + # print("B-C = ",D2) + DUB=union(D1,D2) + print("People who play either football or badminton but not both: ",DUB) +def diff(list1, list2): + list3 = [] + for val in list1: + if val not in list2: + list3.append(val) + return list3 +def union(list1,list2): + list3 = list1.copy() + for val in list2: + if val not in list3: + list3.append(val) + return list3 +def nCnB(): + list4 = diff(Uni,union(cricket,bad)) + print("Students who play neither cricket nor badminton: ",len(list4)) +def nCnF(): + list4 = diff(Uni,union(cricket,foot)) + print("Students who play neither cricket nor football: ",len(list4)) +def nFnB(): + list4 = diff(Uni,union(foot,bad)) + print("Students who play neither football nor badminton: ",len(list4)) +def pCpF(): + list4 = intersection(cricket,foot) + list5 = diff(list4,bad) + print("People who play cricket and football but not badminton: ", len(list5)) +def pCpB(): + list4 = intersection(cricket,bad) + list5 = diff(list4,foot) + print("People who play cricket and badminton but not football: ",len(list5)) +def pBpF(): + list4 = intersection(foot,bad) + list5 = diff(list4,cricket) + print("People who play badminton and football but not cricket: ",len(list5)) +flag = 1 +while flag == 1: + print("1. List of students who play both cricket and badminton: ") + print("2. List of students who play either cricket or badminton but not football: ") + print("3. Number of students who play neither cricket not badminton: ") + print("4. Number of students who play cricket and football but not badmitnon: ") + print("5. Exit") + ch = int(input("Enter your choice: ")) + if(ch == 1): + CB() + elif(ch == 2): + eCeB() + elif(ch == 3): + nCnB() + elif(ch == 4): + pCpF() + else: + flag = 0 \ No newline at end of file diff --git a/A02 ArrayOperations.py b/A02 ArrayOperations.py new file mode 100644 index 0000000..b3e8b1c --- /dev/null +++ b/A02 ArrayOperations.py @@ -0,0 +1,80 @@ +NoOfStudents = int(input("Enter number of students: ")) +list1 = [] +list2 = [] +for i in range(0,NoOfStudents): + n = float(input("Enter student percentage: ")) + list2.append(n) +for j in list2: + if(j != -1): + list1.append(j) + +def avg(): + sum1 = 0 + for i in list1: + sum1 = sum1 + i + avg = sum1/NoOfStudents + print("Sum is: ",sum1) + print("Average is: ",avg) + +def largest(): + largest = None + for i in list1: + if(largest==None or largesti): + smallest = i + print("The lowest marks is: ",smallest) + +def absent(): + abs = 0 + for j in list2: + if(j == -1): + abs = abs + 1 + print("Number of absent students are: ",abs) + +def frequency(): + temp = 0 + for i in list1: + max1 = 0 + for j in range(0,len(list1)): + if(i == list1[j]): + max1 = max1 + 1 + else: + continue + if(max1>temp): + temp = max1 + k = i + else: + continue + print("Marks with highest frequency is: ",k) + print("Frequency of the number is: ",temp) + +flag = 1 +while flag == 1: + print("\nThe options are: ") + print("1. Total and average marks are: ") + print("2. Highest marks among the students: ") + print("3. Lowest marks among the students: ") + print("4. Number of absent students: ") + print("5. Marks with highest frequency is: ") + print("6. Exit") + ch = int(input("Enter choice: ")) + if(ch == 1): + avg() + elif(ch == 2): + largest() + elif(ch == 3): + smallest() + elif(ch == 4): + absent() + elif(ch == 5): + frequency() + elif(ch == 6): + flag = 0 + elif(ch>6 or ch<1): + print("Enter valid choice") \ No newline at end of file diff --git a/A05 String.py b/A05 String.py new file mode 100644 index 0000000..848a3b8 --- /dev/null +++ b/A05 String.py @@ -0,0 +1,99 @@ +str1 = input("Enter string: ") +substr1 = input("Enter substring: ") +occur1 = input("Enter character whose occurence is to be calculated: ") +occur2 = str1.split() +index1 = [] +for i in occur2: + for j in occur2: + if(i == j and j not in index1): + index1.append(i) + else: + continue + +def longestWord(): + globalMax = 0 + currentMax = 0 + list1 = [] + for i in str1: + if (i != " "): + currentMax = currentMax + 1 + list1.append(i) + else: + if (currentMax > globalMax): + globalMax = currentMax + temp = list1 + currentMax = 0 + list1 = [] + if (i == str1[len(str1) - 1]): + if (currentMax > globalMax): + globalMax = currentMax + temp = list1 + print("Longest word is: ", "".join(temp)) + print("Length of largest word is: ", globalMax) + + +def palindrome(): + rev1 = str1 + if (str1[::-1] == rev1): + print("String is palindrome") + else: + print("String is not palindrome") + + +def charfrequency(): + temp = 0 + for i in str1: + max1 = 0 + for j in range(0, len(str1)): + if (str1[j] != " " and str1[j] == occur1): + max1 = max1 + 1 + else: + continue + if (max1 > temp): + temp = max1 + else: + continue + print("Number of times the character '", occur1, "' occurs: ", temp) + + +def indexSubstring(): + for i in range(len(str1) - len(substr1) + 1): + if str1[i:i + len(substr1)] == substr1: + print("Substring present in string at index: ", i) + break + else: + print('Substring not present') + +def occurWord(): + for i in index1: + max1 = 0 + for j in occur2: + if(i == j): + max1 = max1 + 1 + else: + continue + print("The number of times '",i,"' is repeated is: ",max1) + +flag = 1 +while flag == 1: + print("1. Display word with longest word: \n") + print("2. The occurence of '",occur1,"' in the string is: \n") + print("3. Check whether is string is palindrome or not: \n") + print("4. Display index of first appearance of the substring: \n") + print("5. Occurrence of each word in the given string: \n") + print("6. Exit") + ch = int(input("Enter your choice: ")) + if(ch == 1): + longestWord() + elif(ch == 2): + charfrequency() + elif(ch == 3): + palindrome() + elif(ch == 4): + indexSubstring() + elif(ch == 5): + occurWord() + elif(ch>6 or ch<1): + print("Enter valid choice") + elif(ch == 6): + flag = 0 \ No newline at end of file diff --git a/B14 BubbleSelectionSort.py b/B14 BubbleSelectionSort.py new file mode 100644 index 0000000..dd9d755 --- /dev/null +++ b/B14 BubbleSelectionSort.py @@ -0,0 +1,107 @@ +NoOfStudents = int(input("Enter number of students: ")) +list1 = [] +list2 = [] +for i in range(0,NoOfStudents): + n = float(input("Enter student percentage: ")) + list2.append(n) +for j in list2: + if(j != -1): + list1.append(j) + +def avg(): + sum1 = 0 + for i in list1: + sum1 = sum1 + i + avg = sum1/NoOfStudents + print("Sum is: ",sum1) + print("Average is: ",avg) + +def largest(): + largest = None + for i in list1: + if(largest==None or largesti): + smallest = i + print("The lowest marks is: ",smallest) + +def frequency(): + temp = 0 + for i in list1: + max1 = 0 + for j in range(0,len(list1)): + if(i == list1[j]): + max1 = max1 + 1 + else: + continue + if(max1>temp): + temp = max1 + k = i + else: + continue + print("Marks with highest frequency is: ",k) + print("Frequency of the number is: ",temp) + +def BubbleSort(): + temp = 0 + for i in range(0,len(list1)-1): + for j in range(0,len(list1)-1): + if(list1[j] > list1[j+1]): + temp = list1[j] + list1[j] = list1[j+1] + list1[j+1] = temp + else: + continue + print("Sorted list is: ",list1) +def top5(): + list2 = list1[::-1] + for j in range(0,len(list2)): + print("Rank ",j+1," score is",list2[j]) + +def selection(): + temp = 0 + for i in range(0,len(list1)): + min1 = i + for j in range(i+1,len(list1)): + if(list1[min1]>list1[j]): + min1 = j + temp = list1[i] + list1[i] = list1[min1] + list1[min1] = temp + print(list1) + +flag = 1 +while flag == 1: + print("\nThe options are: ") + print("1. Total and average marks are: ") + print("2. Highest marks among the students: ") + print("3. Lowest marks among the students: ") + print("4. Marks with highest frequency is: ") + print("5. Top ",len(list1)," scores using bubble sort") + print("6. Top ",len(list1)," scores using selection sort") + print("7. Top ",len(list1)," scores are: ") + print("8. Exit") + ch = int(input("Enter choice: ")) + if(ch == 1): + avg() + elif(ch == 2): + largest() + elif(ch == 3): + smallest() + elif(ch == 4): + frequency() + elif(ch == 5): + BubbleSort() + elif(ch == 6): + selection() + elif(ch == 7): + top5() + elif(ch == 8): + flag = 0 + elif(ch>8 or ch<1): + print("Enter valid choice") \ No newline at end of file