fixed last option in assignment-1, changed variable names for better understanding, cleaner code. [perfect code]
This commit is contained in:
parent
efc3d6820b
commit
39e5ac734c
198
assignment-1.py
198
assignment-1.py
@ -1,117 +1,117 @@
|
||||
# DSL Assignment - 1
|
||||
'''
|
||||
Problem Statement: In second year computer engineering class, group A student’s play cricket, group B students play badminton and group C students play football.
|
||||
Write a Python program using functions to compute following:
|
||||
a) List of students who play both cricket and badminton
|
||||
b) List of students who play either cricket or badminton but not both
|
||||
c) Number of students who play neither cricket nor badminton
|
||||
d) Number of students who play cricket and football but not badminton.
|
||||
(Note- While realising the group, duplicate entries should be avoided, Do not use SET built-in functions)
|
||||
|
||||
# List of players
|
||||
Code from Data Structure Lab (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-codes/DSL
|
||||
'''
|
||||
|
||||
cricket_players=[]
|
||||
badminton_players=[]
|
||||
football_players=[]
|
||||
# BEGINNING OF CODE
|
||||
# List to store players
|
||||
cricketPlayers = []
|
||||
badmintonPlayers = []
|
||||
footballPlayers = []
|
||||
|
||||
# Input values
|
||||
|
||||
## List for cricket players
|
||||
def cricket_in():
|
||||
total=int(input("Total number of students playing cricket: \t"))
|
||||
# Input cricket
|
||||
def cricketInput():
|
||||
total = int(input("Total number of CRICKET players:\t"))
|
||||
for i in range(total):
|
||||
players=int(input("Enter roll number: \t"))
|
||||
cricket_players.append(players)
|
||||
print("Roll numbers of", total, "students playing cricket: \t", cricket_players)
|
||||
cricket_in()
|
||||
players = int(input(f"Roll number of player {i+1}:\t"))
|
||||
cricketPlayers.append(players)
|
||||
print(f"\n----------\nStudents playing cricket:\t {cricketPlayers}\n----------\n")
|
||||
|
||||
## List for badminton players
|
||||
def badminton_in():
|
||||
total=int(input("Total number of students playing badminton: \t"))
|
||||
# Input badminton
|
||||
def badmintonInput():
|
||||
total = int(input("Total number of BADMINTON players:\t"))
|
||||
for i in range(total):
|
||||
players=int(input("Enter roll number: \t"))
|
||||
badminton_players.append(players)
|
||||
print("Roll numbers of", total, "students playing badminton: \t", badminton_players)
|
||||
badminton_in()
|
||||
players = int(input(f"Roll number of player {i+1}:\t"))
|
||||
badmintonPlayers.append(players)
|
||||
print(f"\n----------\nStudents playing badminton:\t {badmintonPlayers}\n----------\n")
|
||||
|
||||
## List for football players
|
||||
def football_in():
|
||||
total=int(input("Total number of students playing football: \t"))
|
||||
# Input football
|
||||
def footballInput():
|
||||
total = int(input("Total number of FOOTBALL players:\t"))
|
||||
for i in range(total):
|
||||
players=int(input("Enter roll number: \t"))
|
||||
football_players.append(players)
|
||||
print("Roll numbers of", total, "students playing football: \t", football_players)
|
||||
football_in()
|
||||
players = int(input(f"Roll number of player {i+1}:\t"))
|
||||
footballPlayers.append(players)
|
||||
print(f"\n----------\nStudents playing football:\t {footballPlayers}\n----------\n")
|
||||
|
||||
# List of students playing cricket+badminton
|
||||
# Playing both cricket and badminton
|
||||
def CricketAndBadminton():
|
||||
listCricketAndBadminton = []
|
||||
for i in cricketPlayers:
|
||||
if i in badmintonPlayers:
|
||||
listCricketAndBadminton.append(i)
|
||||
print(f"\n----------\nStudents playing cricket and badminton:\t {listCricketAndBadminton}\n----------\n")
|
||||
|
||||
cricket_and_badminton=[]
|
||||
# Either cricket or badminton but not both
|
||||
def CricketOrBadminton():
|
||||
listCricketOrBadminton = []
|
||||
for i in cricketPlayers:
|
||||
if i not in badmintonPlayers:
|
||||
listCricketOrBadminton.append(i)
|
||||
for i in badmintonPlayers:
|
||||
if i not in cricketPlayers:
|
||||
listCricketOrBadminton.append(i)
|
||||
print(f"\n----------\nStudents playing cricket or badminton (but not both):\t {listCricketOrBadminton}\n----------\n")
|
||||
|
||||
def cricket_and_badminton_fn():
|
||||
for i in cricket_players:
|
||||
if i in badminton_players:
|
||||
cricket_and_badminton.append(i)
|
||||
print("Roll numbers of students playing cricket and badminton are: \t", cricket_and_badminton)
|
||||
# Neither cricket nor badminton, i.e. football only
|
||||
def FootballOnly():
|
||||
listCricketAndBadminton = []
|
||||
listFootballOnly = []
|
||||
for i in cricketPlayers:
|
||||
listCricketAndBadminton.append(i)
|
||||
for i in badmintonPlayers:
|
||||
if i not in cricketPlayers:
|
||||
listCricketAndBadminton.append(i)
|
||||
for i in footballPlayers:
|
||||
if i not in listCricketAndBadminton:
|
||||
listFootballOnly.append(i)
|
||||
print(f"\n----------\nTotal students playing football only are:\t{len(listFootballOnly)}\nList:\t{listFootballOnly}\n----------\n")
|
||||
|
||||
# List of students playing either cricket or badminton but not both
|
||||
# Cricket and football but not badminton
|
||||
def notBadminton():
|
||||
listNotBadminton = []
|
||||
for i in cricketPlayers:
|
||||
if i in footballPlayers:
|
||||
if i not in badmintonPlayers:
|
||||
listNotBadminton.append(i)
|
||||
print(f"\n----------\nTotal students not playing badminton (but do play cricket and football):\t{len(listNotBadminton)}\nList:\t{listNotBadminton}\n----------\n")
|
||||
|
||||
cricket_or_badminton=[]
|
||||
# Main function with options
|
||||
def main():
|
||||
print("----- ENTER ROLL NUMBERS IN LIST -----")
|
||||
cricketInput()
|
||||
badmintonInput()
|
||||
footballInput()
|
||||
print("----- ROLL NUMBERS SAVED -----\n")
|
||||
while (1):
|
||||
print("\n----- MAIN MENU -----")
|
||||
print("1 -> Students playing both cricket and badminton")
|
||||
print("2 -> Students playing either cricket or badminton, but not both")
|
||||
print("3 -> Students playing neither cricket nor badminton (i.e. football only)")
|
||||
print("4 -> Students playing cricket and football, but not badminton")
|
||||
print("5 -> Exit")
|
||||
|
||||
def cricket_or_badminton_fn():
|
||||
for i in cricket_players:
|
||||
if i not in badminton_players:
|
||||
cricket_or_badminton.append(i)
|
||||
for i in badminton_players:
|
||||
if i not in cricket_players:
|
||||
cricket_or_badminton.append(i)
|
||||
print("Roll numbers of students playing either cricket or badminton are: \t", cricket_or_badminton)
|
||||
optn = int(input("Choose an option (1-5):\t"))
|
||||
|
||||
# Number of students playing neither cricket nor badminton
|
||||
|
||||
football_only=[]
|
||||
football_only_1=[]
|
||||
|
||||
def football_only_fn():
|
||||
for i in cricket_players:
|
||||
football_only.append(i)
|
||||
for i in badminton_players:
|
||||
if i not in cricket_players:
|
||||
football_only.append(i)
|
||||
for i in football_players:
|
||||
if i not in football_only:
|
||||
football_only_1.append(i)
|
||||
print("Number of students playing neither cricket nor badminton: \t", len(football_only_1), "\n Roll numbers are:\t", football_only_1)
|
||||
|
||||
# Number of students playing cricket and football but not badminton
|
||||
|
||||
not_badminton=[]
|
||||
|
||||
def not_badminton_fn():
|
||||
for i in badminton_players:
|
||||
if i not in cricket_players:
|
||||
not_badminton.append(i)
|
||||
for i in badminton_players:
|
||||
if i not in football_players:
|
||||
not_badminton.append(i)
|
||||
print("Number of students playing cricket and football but not badminton: \t", len(not_badminton), "\n Roll numbers are:\t", not_badminton)
|
||||
|
||||
# Choosing an option
|
||||
def choose_optn():
|
||||
while True:
|
||||
print("Choose an operation to perform:")
|
||||
print("1. List of students playing cricket and badminton both")
|
||||
print("2. List of students playing either cricket or badminton")
|
||||
print("3. List of students playing neither cricket nor badminton")
|
||||
print("4. List of students playing cricket and football but not badminton")
|
||||
print("5. Exit")
|
||||
optn=int(input("Enter option number (1-5): \t"))
|
||||
if optn==1:
|
||||
cricket_and_badminton_fn()
|
||||
if optn==2:
|
||||
cricket_or_badminton_fn()
|
||||
if optn==3:
|
||||
football_only_fn()
|
||||
if optn==4:
|
||||
not_badminton_fn()
|
||||
if optn==5:
|
||||
print("## DESIGNED AND ENGINEERED BY KSHITIJ\n## END OF CODE")
|
||||
if (optn == 1):
|
||||
CricketAndBadminton()
|
||||
elif (optn == 2):
|
||||
CricketOrBadminton()
|
||||
elif (optn == 3):
|
||||
FootballOnly()
|
||||
elif (optn == 4):
|
||||
notBadminton()
|
||||
elif (optn ==5):
|
||||
print("\n\n## END OF CODE\n\n")
|
||||
quit()
|
||||
else:
|
||||
print("\nPlease choose a valid option.\n")
|
||||
choose_optn()
|
||||
choose_optn()
|
||||
|
||||
## END OF CODE
|
||||
print("\n\nPlease choose a valid option (1-5)\n\n")
|
||||
|
||||
main()
|
||||
# END OF CODE
|
||||
|
Loading…
Reference in New Issue
Block a user