In: Computer Science
PYTHON
Given a 2D array with the layout of the floor of a concert hall and the height (in dwillyinches, a logarithmic measurement of height) of each concert attendee, write a program that determines if every attendee can see the front stage. An attendee can see the front stage if they are strickly taller than all of the attendees in front of them.
Everyone can see the front-stage in the example below:
# FRONT STAGE [[1, 2, 3, 2, 1, 1], [2, 4, 4, 3, 2, 2], [5, 5, 5, 5, 4, 4], [6, 6, 7, 6, 5, 5]] # Starting from the left, the 6 > 5 > 2 > 1, so all four of these attendees can see. # 6 > 5 > 4 > 2 - so all four of these attendees can see
# FRONT STAGE [[1, 2, 3, 20, 1, 1], [2, 4, 4, 3, 2, 2], [5, 5, 5, 10, 4, 4], [6, 6, 7, 6, 5, 5]] # The 10 is in front of the 6 and blocking its view; also the 20 blocks the 3, 10, and 6.
Your program should print True if every number (i.e., attendee) can see the front-stage, and False if even a single number/attendee cannot.
Sample Input 1
3 1 2 4 5 7 8
Sample Output 1
True
Sample Input 2
4 2 0 0 0 6 3 1 1 1 6 4 2 2 2 9 5 2 3 4 11
Sample Output 1
False
Please up vote ,comment if any query . Thanks for question .Be safe .
Note : check attached image for output ,code compiled and tested in Spyder python3.
Program :
#function takes 2d list and number of rows as input
#return true or false if concert hall siting is valid returns
true
def concert(Array,length):
j=0#column index
for j in range(len(Array[j])): #from 0 to total column
for i in range(length-1): #from 0 to length(total row)-1
#if current element greater or equal to next element return
false
#because next element due to less height than current element
#can not see concert stage
#return False
#else after end of loop scanning return True
if Array[i][j]>=Array[i+1][j]:#
return False
j+=1#increment column number
return True#if all have valid height can see stage return
true
#main program function starts from here
if __name__=="__main__":
DataList=[] #empty list
row=int(input()) #get number of rows
for i in range(row):#run a loop from 0 to row
string=input() #get a space separated string from user
userInputList=string.split() #split it by space in list type
userInputList=[int(i) for i in userInputList]#traverse list and
convert it into integer
DataList.append(userInputList)#append new list to DataList
print(concert(DataList, row))#pass DataList as 2dList and number of
rows
Program :
Output :
Please up vote ,