Python Solutions For iPA 10-Feb-23

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Contents

Python – First Problem Statement……………………………………………………………………………………………2


Question…………………………………………………………………………………………………………………………………2
Solution…………………………………………………………………………………………………………………………………..4
Test cases…………………………………………………………………………………………………………………………………4
Python – Second Problem Statement……………………………………………………………………………………...6
Question…………………………………………………………………………………………………………………………………6
Solution…………………………………………………………………………………………………………………………………10
Test cases………………………………………………………………………………………………………………………………19

1
Restricted for circulation outside TCS Xplore
Solutions for TCS Xplore iPA held on 10-Feb-23

Python – First Problem Statement


Question
Write a python code to print the total number of multiples of an integer taken from the user present in
a list of Integers taken as an input from the user.

Write a python code to print the total number of multiples of an integer.

The program will take 3 inputs:

1. The number of elements, lets say n

2. n integers(List of elements)

3. The number whose multiples are to be determined.

Write the program that includes a function will take an argument(integer type) whose multiples are to
be determined.This function will return the total number of multiples found in the list for the input
argument.

In case, no multiples are found, the method will return "Multiples not found".

Considering the above scenario into account, build the logic to print total number of multiples of passed
integer from a given list of elements.

Refer to below instructions and sample input-output for more clarity on the requirement.

Instructions to write main section of the code:

a. You would require to write the main section completely, hence please follow the below instructions
for the same.

b. You would require to write the main program which is inline to the "sample input description section"
mentioned below and to read the data in the same sequence.

Steps inside main section:

1. Create a list of Integers.To create the list,

i. First read the number of elements/integers you want to store in the list.

ii. Read an Integer and add it to the list. This point repeats for the number of elements/Integers to be
stored in the list

(considered in the first line of input i.e. in point #1.i).

2
Restricted for circulation outside TCS Xplore
2. Read an Integer from user whose multiples will be searched in the list of integers.

3.Call the function 'findMultiples' and pass the taken input whose multiples will be searched in the list
of Integers.

4. Print the output as “Number of Multiples of the passed Integer<Space>:<Space><value>” (excluding


quotes, value is the frequency of number of multiples).

You can use/refer the below given sample input and output to verify your solution.

Sample Input (below) description:

1.The first line of input taken in the main section contains an integer value representing the number of
elements/Integers to be added to the list.

2.The next lines of inputs are the strings to be added to the list one after another and is repeated for the
number of elements/Integers given in the first line of input.

(each line represents one Integer to be added to the list).

3.The last line of input is the Integer whose multiples is to be found.

Sample Input:

15

Sample Output:

Number of Multiples of the passed Integer : 3

3
Restricted for circulation outside TCS Xplore
Solution

def findMultiples(m):

c=0

for i in l:

if (i % m == 0):

c=c+1

return c

n = int(input())

l = []

for i in range(n):

e = int(input())

l.append(e)

m = int(input())

c = findMultiples(m)

if c == 0:

print("Multiples not found")

else:

print("Number of Multiples of the passed Integer :", c)

Testcases
Sample Input:

15

4
Restricted for circulation outside TCS Xplore
Sample Output:

Number of Multiples of the passed Integer : 3

Sample Input:

Sample Output:

Number of Multiples of the passed Integer : 2

Sample Input:

13

14

16

Sample Output:

Multiples not found

Sample Input:

5
Restricted for circulation outside TCS Xplore
1

Sample Output:

Number of Multiples of the passed Integer : 2

Java – Second Problem Statement


Question
Define a class to create a Team object with the below attributes:

teamRanking : of type integer representing the rank of the Team Eg: 10

teamConf : of the type String representing the Confederation of the Team Eg:"AFC"

teamName: of the type String representing the Name of the Team Eg:"Spain"

Define the method to initialize the attributes when a Team object is created.

Write the code to define a Class to Create a Tournament object with the below attributes:

1.A list containing elements of type Team.Eg-L=[T1,T2]

Where L is a list of Team objects T1 and T2.

2.Details of the Tournament , a dictionary containing the details of the Tournament in the format as
follows:

{Name: "FIFA2022", Host:"Qatar",Edition: 22, Year: 2022,Participants:32,Time:1}

Define a method to initialize the attributes when a Tournament object is created.

Define the first method inside the Tournament class as below:

The method will find the count of Teams from each Confederation from the Team List present in the first
tournament of the Tournament list

and returns a dictionary having the confederation and teamCount (count of Teams) as key : value pairs.

Define the second method outside both the classes.

This method will take Number of times the Tournament was being hosted by the Host and a list of
Tournaments objects as parameters.

6
Restricted for circulation outside TCS Xplore
It will return the list of Teams from all the Tournaments from the list of Tournaments which has been
hosted(Time-given in the tournament details) same as the Number of times passed as a parameter.

If there is no Tournament in the given list which has the given host name as host in the Tournament,

the method should return None else it will return the List of Teams in ascending order of teamRanking
for each tournament object

i.e. If the Number of time is matching with the tournament A and tournament B present in the list of
Tournaments, the method will display the teamNames of all the Teams

from tournament A and tournament B in the ascending order of teamRanking.Please refer the sample
input and output below for more clarity.

Note : All String Comparison should be case insensitive.

Instructions to write main function:

You would require to write the main section completely, hence please follow the below instructions for
the same.

You would require to write the main program which is in line to the "sample input description section"
mentioned below and to read the data in the same sequence.

Create the respective objects of the classes defined referring to the below instructions.

Main Function Instructions:

A. Create a list of Tournament objects which will be passed as argument while calling the function in
main. To create the list:

1.Read a number for the count of Tournament objects to be created and added to the list.

2.Create a Tournament object to be added to the list. To create the Tournament object, do the
following:

2.1 Read a number for the count of Team objects to be added to the list of Team objects for the
Tournament objects.

2.2 Create a Team object after reading the data (teamRanking, teamConf, the name of the team) related
to it and add to the list of Team objects.

This point repeats for the count of Team objects to be created as per point #A.2.1.

2.3 Read values for attributes such as Name,Host,Edition,Year,Participants and Time which are for the
dictionary that you are passing as an parameter

when you create the Tournament objects.Considering these attributes as keys, store the values read as
key : value pairs in a dictionary for the details.

7
Restricted for circulation outside TCS Xplore
Eg.{Name: "FIFA2022", Host:"Qatar",Edition: 22, Year: 2022,Participants:32,Time:1}

2.4 Create a tournament object by passing the list of Team objects and dictionary created above and add
it to the Tournament list.

3. This repeats for the number of Tournament objects to be created (considered in the first line of
input) as per point #A.1.

B. Read an integer value as input depicting Number of times to be passed as argument to the second
function(function defined outside the class).

C. Call the first method to find count of Teams confederation wise mentioned above from the main
section using the first element of the List of Tournament objects created in point #A(i.e the First
tournament Object created)

D. Display the values returned by the above function. Display the Confedeartion names and count of
Team returned by the function as per the requirement given.

E. Call the second function from the main section, by passing the times and the list of tournament
objects created above.

F. Display the list of team names returned by the second function.

- If function returns None, then display “No Team Found” (excluding the quotes).

Sample Input (below) description:

1.The first input taken in the main section is the number of Tournament Objects to be created(suppose
n).

2.The next set of inputs are related to n Tournament objects to be created as follows:

2.1 Count of Team objects(Suppose m) to be created and added to the list of Teams for the Tournament.

2.2 The next set of inputs are values for attributes of m teams -teamRanking, teamConf, teamName (for
each team object taken one after other and is repeated for m number of team objects).

2.3 Next set of values are for the following keys of the address for the Tournament object .

Name

Host

Edition

Year

Participants

Time

3. Last line of input represents the integer -Time, supplied as a argument to the second function.

8
Restricted for circulation outside TCS Xplore
You can use/refer the below given sample input and output for more details of the format for input and
output.

Consider below sample input and output to test your code:

Sample Input :

12

UEFA

Portugal

106

AFC

India

86

AFC

China

TriNationSeries2020

India

2020

SA

Argentina

UEFA

9
Restricted for circulation outside TCS Xplore
England

12

UEFA

Portugal

UEFA

Netherlands

ChampionsCup2021

England

2021

Sample Output :

UEFA - 1

AFC - 2

Portugal

China

India

Testcases

Sample Input :

12

10
Restricted for circulation outside TCS Xplore
UEFA

Portugal

106

AFC

India

86

AFC

China

TriNationSeries2020

India

2020

SA

Argentina

UEFA

England

12

UEFA

Portugal

UEFA

Netherlands

ChampionsCup2021

England

11
Restricted for circulation outside TCS Xplore
7

2021

Sample Output :

UEFA - 1

AFC - 2

Portugal

China

India

Input 2

12

UEFA

Portugal

106

CAF

Cameoon

86

AFC

China

TriNationSeries2020

India

2020

12
Restricted for circulation outside TCS Xplore
3

SA

Argentina

UEFA

England

12

UEFA

Portugal

UEFA

Netherlands

ChampionsCup2021

England

2021

11

34

UEFA

Wales

10

UEFA

Sweden

81

13
Restricted for circulation outside TCS Xplore
CAF

Nigeria

KnockoutCup2022

India

2022

Output 2

UEFA - 1

CAF - 1

AFC - 1

No Team Found

Input 3

23

OFC

Australia

168

AFC

Bangladesh

90

AFC

Mongolia

AsiaSeries2020

14
Restricted for circulation outside TCS Xplore
India

2020

SA

Argentina

UEFA

England

UEFA

Portugal

16

UEFA

Netherlands

EuroCup2021

England

2021

56

UEFA

Wales

13

15
Restricted for circulation outside TCS Xplore
UEFA

Sweden

89

CAF

Nigeria

KnockoutCup2022

India

2022

Output 3

OFC - 1

AFC - 2

England

Argentina

Portugal

Sweden

Netherlands

Australia

Wales

Nigeria

Mongolia

Bangladesh

16
Restricted for circulation outside TCS Xplore
Input 4

22

UEFA

France

189

NA

Canada

99

AFC

Mongolia

AsiaSeries2020

India

2020

SA

Argentina

UEFA

England

UEFA

Portugal

19

17
Restricted for circulation outside TCS Xplore
UEFA

Netherlands

EuroCup2021

England

2021

56

UEFA

Wales

13

UEFA

Sweden

89

CAF

Nigeria

KnockoutCup2022

India

2022

Output 4

UEFA - 1

18
Restricted for circulation outside TCS Xplore
NA - 1

AFC - 1

England

Portugal

Argentina

Netherlands

France

Mongolia

Canada

Solution

class Team:

def __init__(self,teamRanking, teamConf,teamName):

self.teamRanking=teamRanking

self.teamConf = teamConf

self.teamName = teamName

class Tournament:

def __init__(self,teamList,tournDetails):

self.teamList=teamList

self.tournDetails=tournDetails

def confCount(self):

ans={}

for i in self.teamList:

if(i.teamConf in ans):

ans[i.teamConf]+=1

else:

ans[i.teamConf]= 1

return ans

19
Restricted for circulation outside TCS Xplore
def tCount(n,tournList):

matchedtoulist=[]

for i in tournList:

if n==i.tournDetails["Time"]:

matchedtoulist.append(i)

finalTeamDict={}

for i in matchedtoulist:

for o in i.teamList:

finalTeamDict[o.teamRanking]=o.teamName

ansList=[]

ansDict=dict(sorted(finalTeamDict.items()))

for i in ansDict.keys():

ansList.append(ansDict[i])

if(ansList==[]):

return None

else:

return ansList

tournList=[]

ntourn=int(input())

for i in range(ntourn):

teamList=[]

nteam=int(input())

for i in range(nteam):

teamRanking=int(input())

teamConf = input()

teamName =input()

teamList.append(Team(teamRanking,teamConf,teamName))

detailDict={}

20
Restricted for circulation outside TCS Xplore
detailDict["Name"]=input()

detailDict["Host"]=input()

detailDict["Edition"]=int(input())

detailDict["Year"]=int(input())

detailDict["Participants"]=int(input())

detailDict["Time"]=int(input())

tournList.append(Tournament(teamList,detailDict))

ninput=int(input())

ans1=tournList[0].confCount()

for i in ans1.keys():

print(i,"-",ans1[i])

ans2=tCount(ninput,tournList)

if ans2==None:

print("No Team Found")

else:

for i in ans2:

print(i)

21
Restricted for circulation outside TCS Xplore

You might also like