Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

WHOEVER POSTED THIS SOLUTION . IT EXACLY WHAT I NEED HOWEVER THERE THE D AND R FUNCTIONS ARE THROWING ME ERRORS. I NEED HELP TO

WHOEVER POSTED THIS SOLUTION . IT EXACLY WHAT I NEED HOWEVER THERE THE "D" AND "R" FUNCTIONS ARE THROWING ME ERRORS. I NEED HELP TO FIX THOSE ERRORS PLEASE. OR IF SOMEONE CAN GIVE A DETAILED SOLUTION JJUST LIKE IT GIVEN HERE WITH NO ERROR I WILL REALLY APPRECIATE. INCLUDING "COMMENTS" JUST LIKE THE EXPERT DID HERE.

NOTE: PLEASE DO NOT COPY AND PASTE ANSWER FROM PREVIOUS POSTS. I AM POSTING THIS QUESTION THIRD TIME BECAUSE THE GIVEN ANSWER WAS INCOMPLETE AND NOBODY RESPONDED TO COMMENT SECTION. PLEASE ANSWER ALL THE PARTS OF QUESTIONS EXPLAINING EACH STEPS INCLUDING PURPOSE OF EACH VARIABLES. PLEASE EMAIL ME IF YOU NEED ADDITIONAL INFORMATION ABOUT THIS QUESTION

An automated car park (parking lot) is controlled using a sensor platform. The sensor platform updates carpark.txt with the latest status of the carpark. Text file carpark.txt contains the layout of the car park with 10 rows and 11 columns (which includes 3 empty columns). Available slots are marked with the character ?.? and occupied slots are marked with character ?X?.Design and develop a Python program to remotely book a free slot in the car park. First, the data should be read into an appropriate python data structure (a list, tuple, or a dictionary).I am using a txt file for the car park.

AB CD EF GH

1 XXXXXXX.

2 XX .X XX ..

3 XX .X X. XX

4 XX XX .. XX

5 XX .XXX.X

6 .. XX X. X.

7 .. .. .. ..

8 XX .X .X XX

9 .. X. .. .X

10.. XX.. X. .

1. There should be a repeatable text-based menu with help text as below:

Select your choice from the menu: D to display current status of the car park R to reserve an empty slot in the car park S to display % occupancy of the car park Q to quit

2. The program should quit only if a user enters Q.

3. The display option should: 1) read the data from the text file, and 2) display the data (display row numbers down the left side and column letters - A, B, C, D, E, F, G, and H - across the top line). ? There should be blank columns similar to the text file indicating the roads (pathways).

4. The reserve function should suggest any of the closest available slots from the building entrance (assume that the building entrance is at the top left corner where row = 0 and column = 0).

a) Users can also provide a row number and column letter to reserve an empty slot.

b) If the data entered (row and column) is out of range, report an error message and the user can try again

next pass of the menu loop.

c) If the seat is not available report an error message.

d) If the seat is available report so and update the data structure and text file (replace . with X).

To find the closest available slot, you should develop a nearest neighbor searching algorithm as illustrated. (start searching for an empty slot in neighbors from the top left)

Nearest Neighbor Searching: develop an algorithm to search in terms of matrices, first, 2x2 (top left of the data set), then 3x3, followed by 4x4 and continue until 8x8. If no free slot can be found before 8x8 stop searching and ask for manual booking.

5. The statistics function should report the % occupancy of the carpark. This is computed as the number of occupied slots divided by total slots. Set precision to 1 decimal.

occupancy % = (number of occupied slots / total slots) * 100

Comment the purpose of each variable.

Comment major sections of code such as input, processing, output, functions, loops.

Expert Answer

100% (4 ratings)

Anonymous answered this119 answers

Please find the attached code below:

# read parking information from specified file in dictionary and return dictionary def readParkingData(filename): parkingData = {} # initialize dictionary for storing parking information with open(filename) as f: # open file to read parking information headers = list(f.readline().replace(" ", "").strip()) # read first line into headers list and remove white spaces using strip() index = 1 # initialize index to 1 for keeping row record for line in f: # iterate line by line # read all rows in the file and remove white spaces and store into dictionary in format {row:{column:'X'}} parkingData[index] = dict(zip(headers, list(line.replace(" ", "").strip())[1:])) index += 1 # increment row counter # return headers and parking dictionary return headers, parkingData # update specified file with the given parking dictionary def updateFile(filename, headers, parkingData): f = open(filename, "w") # open file in write mode to update the file f.write('{0: <3}'.format('')) # leave 3 spaces at the left corner heading = ''.join(headers) # join all headers into string f.write(' '.join(heading[i:i + 2] for i in range(0, len(heading), 2))) # add headers in the file with blank space denoting empty path # iterate through dictionary for writing all rows into the file for i in parkingData: # on each new line write row number and one space for empty path which we will consider as 3 characters f.write(' {0: <3}'.format(str(i))) row = "" # initialize row string for each row for j in parkingData[i]: # iterate through row specific dictionary row += parkingData[i][j] # append to row f.write(' '.join(row[i:i + 2] for i in range(0, len(row), 2))) # write each row into file f.close() # close file # display status of current def displayStatus(filename): # read parking information from file headers, parkingData = readParkingData(filename) # join headers displayString = ''.join(headers) # format the display to show parking stuatus in readable format with keeping 4 precision for first column with empty path displayString = '{0: <4}'.format(' ') + ' '.join(displayString[i:i + 2] for i in range(0, len(displayString), 2)) # print each row data for r in parkingData: displayString += " " + '{0: <4}'.format(str(r)) row = "" for c in headers: row += parkingData[r][c] displayString += ' '.join(row[i:i + 2] for i in range(0, len(row), 2)) print(displayString) # get nearest available slots def getNearestAvailableSlot(headers, parkingData): # loop for 1*1, 2*2 , 3*3 .... 8*8 matrix check for cnt in range(1, 9): iCnt = 1 # increment counter for i in range(1, cnt + 1): # logic return to iterate in cnt*cnt matrix jCnt = 1 for j in headers: # iterate through headers in the parkingData if parkingData[i][j] == '.': # check if slot available then return corresponding position return i, j jCnt += 1 # increment column counter # if column count > cnt skip to next row if jCnt > cnt: break # increment row cnt iCnt += 1 # if row count > cnt skip to next matrix if iCnt > cnt: break # if no available slot in 8*8 matrix , return -1,-1 denoting to enter manually row,column return -1, -1 # check slot availability for given row and column def checkAvailability(parkingData, row, column): # if row and column in given parking Information if (row in parkingData) and (column in parkingData[row]): if parkingData[row][column] == 'X': # if not available return 0 return 0; else: return 1 # if available return 1 else: return -1 # if row, column does not exist in dictionary , denotes out of bounds # method to reserve the parking slot def reserve(headers, parkingData): # get nearest available slots row, column = getNearestAvailableSlot(headers, parkingData) # if row or column is -1 - denotes the no slots available nearby if row == -1 or column == -1: print("No nearest slots available") else: # if slots available ask user to continue with available slots print('Nearest available slots from building entrance are row:%d, column :%s' % (row, column)) choice = input(' Do you want to continue with nearest available slot?(y/n)') if choice == 'y' or choice == 'Y': # if yes , reserve the available slots otherwise read slot position from usre parkingData[row][column] = 'X' updateFile(filename, headers, parkingData) return row = int(input(" Enter row number : ")) # read manual row from user column = input(" Enter column letter : ") # read manual column from usre flag = checkAvailability(parkingData, row, column) # check availability of entered row, column slot if flag == 1: # if available ,reserve parkingData[row][column] = 'X' updateFile(filename, headers, parkingData) # update file elif flag == 0: print("Entered row and column postion is not available") # if row , column is reserved show message to user elif flag == -1: print( "Entered row and column postion is out of range") # if entered row column is out of bounds show error message to user # get occupancy percentage def getOccupancyPercentage(parkingData): total = 0 # initialize total to 0 , to count total slots occupied = 0 # initialize occupied to 0 , to count occupied slots for i in parkingData: # iterate through parking data for j in parkingData[i]: if parkingData[i][j] == 'X': # check if slot is occupied if yes - increment count of occipied occupied += 1 total += 1; # increment total slots return (occupied * 100) / total # count percentage of occupancy # Main execution if __name__ == '__main__': # create menu filename = 'carpark.txt' menu = " (D)isplay the current status" menu += " (R)eserve an empty slot" menu += " (S)how Percent(%) occupancy" menu += " (Q)uit" menu += " Select your choice : " # read file for parking data information headers, parkingData = readParkingData(filename) # loop until user enters Q(Quit) while (True): choice = input(menu) if choice == 'D' or choice == 'd': displayStatus(filename) elif choice == 'R' or choice == 'r': reserve(headers, parkingData) elif choice == 'S' or choice == 's': print("Occupancy % = ", getOccupancyPercentage(parkingData)) elif choice == 'Q' or choice == 'q': break

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Management Accounting Text Problems And Cases

Authors: M. Y. Khan, P K Jain

7th Edition

9352606787, 978-9352606788

More Books

Students also viewed these Accounting questions