Sunday, September 16, 2018

AIO Sample question with answer 2: Sitting or Standing?

Please click on the link to get the full question. Sitting or Standing ?


Input File: sitin.txt
Output File: sitout.txtTime Limit: 1 second
Input
Output


A local musician is putting on a concert to raise money for charity. The concert will be held in the town hall, a spacious venue perfectly suited for such an event. There are r rows of seats, each containing exactly s seats. At most one person can sit on a single seat (that is, two people cannot share a seat).
There is a problem - the concert may have been overbooked! This means that if everybody who bought tickets comes to the concert, some of them might have to stand. Now the musician has aproached you, not for advice, but for the answer to the following question: if everybody who bought tickets arrives and tries to find a seat, how many people will end up sitting, and how many people will be standing?
The first line of the input file will consist of the two integers r and s: the number of rows and the number of seats per row in that order. It is guaranteed that 0 <= rs <= 10,000. The second line will contain a single integer between 0 and 1,000,000,000: the number of tickets that have been bought.
Your output file should consist of two integers separated by a space: the number of people sitting and standing respectively.
Remember, everybody tries to sit if they can. If the concert has been overbooked, you will not be able to seat them all.

I had try the question using Python. 
The logic of program should be as below:
  • get input data (number of seats per rows, number of rows and number of tickets sold)
  • calculate the number of seats in total provided, number of tickets sold, get the number of standing people, number of sitting people
  • output the result to file
You may get the code from here python code
or
viewing the python code as follows:
#################################################################################

# open the input file to obtain the number of rows and seats
with open('sitin.txt', 'r') as inputFile1: 
    mylist = list(inputFile1) # assign the row to list
    
inputFile1.close() # remember that always close the file

# the input data is optained row by row and is assigned to list
# first list is first line of input file (no.of.rows and seats)
# second list is second line of input file (no.of.tickets sold)
row, seats = mylist[0].split(' ', 1)  # split the first element of list to get row and seats per row
tickets = mylist[1] # second row is only have the no.of.tickets sold

# calculate the total number of seats provided
# need to type casting the row and seats to integer datatype
# if number of seats is smaller or equal to number of tickets sold, number of people sitting = number of sets, the rest will be standing
# if number of seats is larger than the tickets solds, number of people sitting equal to number of tickets sold , and no people is standing
noOfSeats = int(row) * int(seats)
noOfTickets = int(tickets)
if noOfSeats <= noOfTickets:
    standing = noOfTickets - noOfSeats
    seatNo = noOfSeats
else:
    seatNo = noOfTickets
    standing = 0

# open output file, and write the result into it
# must close the file
outputFile = open('sitout.txt','w') 
outputFile.write(str(seatNo) + ' ' + str(standing)); 
outputFile.close()


2 comments:

Superior of partial fraction

What is complex fraction? Complex fraction is a fraction whereby its numerator or denominator or both consists of fractions. Partial frac...