Consider a book having many pages. Each page can hold atmost 100 names. Each name is…
- User
Consider a book having many pages. Each page can hold atmost 100 names. Each name is assigned an ID sequentially. The names are ordered in the reverse order of their ID's starting from the largest ID to the smallest ID.
Write a python script that does the following
- Ask for the old and new number of names in the book and then ask for a name which is specified by its page number and its position on that page when the total number of names is the old number.
- Show the specified name's location when the total number of names is changed to the new number
- Assistant
Here is a Python script that accomplishes the tasks as described. The script calculates the given name's new location after changing the total number of names in the book, based on the user's input.
# Function to calculate the new location of the name def find_new_location(old_total, new_total, old_page, old_position): # Calculate the old ID (1-based indexing) old_id = old_total - ((old_page - 1) * 100 + (old_position - 1)) # Calculate the new page and position based on the new total new_id_pos = new_total - old_id # This gives the 0-based index of the name in the new arrangement new_page = (new_id_pos // 100) + 1 # Calculate the new page number new_position = (new_id_pos % 100) + 1 # Calculate the new position on the page return new_page, new_position # Main script starts here if __name__ == "__main__": # Get the old and new total from user old_total = int(input("Enter the old total number of names: ")) new_total = int(input("Enter the new total number of names: ")) # Get the specifics about the name's current location old_page = int(input("Enter the page number of the name: ")) old_position = int(input("Enter the position of the name on its page: ")) # Calculate the new location new_page, new_position = find_new_location(old_total, new_total, old_page, old_position) # Print out the result print(f"The name's new location is on page {new_page}, position {new_position}.")