In: Computer Science
please write simple python 3 program with explanations and correct output
Bilbo and problems on the board
Problem Statement
Bilbo once reached his maths claws earlier than anyone else.He saw some N unique numbers greater than 0 written on the board.He thought of challenging his classmates when they come.So for each number i,he wrote the number before and after it on a paper slip.If it is the last number then the number after it is assumed to be 0.If it the first number then number before it is assumed to be 0.Now he shuffled the slips.
Whenhis classmates came,he showed them the slips and asked to make the original list of numbers that was there on the board and promised to share his lunch box with the winner.
You being a avid programmer , decided to write a code to solve this problem
Inpout format:
First line contains N<=100000 , number of slips.
Next N lines contain 2 number, A and B where A and B are number before and after some numbers from list.
Output Format:
N integers that are in the original list that was in board.
Sample Input1:
3
2 0
0 2
1 3
Sample Output1:
1 2 3
Explanation
If you look at solution ,1 has 0 and 2 as numbers before and after it, 2 has 1 and 3, 3 has 2 and 0 .
Sample input 2:
4
101 0
0 102
102 100
103 101
Sample Output 2:
103 102 101 100
here i am uplaoding the code with the proper comment so that you can understand it in a better way
********************** code *********************
# in this program # here each slip is named as ab_list(containing two item A and B) # and slips_list is a list holding all slips # and unique_number_list is a list for holding all the unique number that were on the board # for shuffling of, created all slips , i have imported the module random for using of shuffle function import random # this is the list for containing all the slips(each slip is holding two number A and B so the item of slip_list is # itself a list of two item A and B) slips_list = [] # this is the list for containing the n unique number which was on the board unique_num_list = [] # defining a function for creating the slip with number A and number B def creating_Slips(num_of_slips): i = 0 # this is a simple variable for just maintaining the counting of slip while num_of_slips > 0: print(f'******* for slip {i + 1} *******\n') i = i + 1 A = int(input("Enter A: ")) B = int(input("Enter B: ")) print() # each slip is a itself a list of two item, here i have named them as ab_list ab_list = [A, B] # inserting ab_list in to the slips_list (means inserting slips into the slips list) slips_list.append(ab_list) num_of_slips = num_of_slips - 1 def final_number_list(): # slips_list is a list which is containing the sublist named as ab_list, now our job is to print the unique # number that was on the board after shuffling of all slips(each ab_list) so our logic is : i have just created a # unique_number_list which is empty initially, and while iterating through the slips_list which item is itself a # list of two item A and B, i am going to take A and B if they are not in the unique_number_list and i will not # take the number 0 because all the number on the board were positive integer, so this is how we can create our # unique_number_list and then print their element last_elem = 0 for slips in slips_list: if slips[0] == 0 and slips[1] != 0: temp = slips[1] - 1 if temp not in unique_num_list and temp != last_elem: unique_num_list.append(temp) elif slips[0] != 0 and slips[1] == 0: last_elem = slips[0] + 1 else: mid = int((slips[0] + slips[1])/2) if mid not in unique_num_list and mid != last_elem: unique_num_list.append(mid) unique_num_list.append(last_elem) # iterating through the unique_number_list for printing each unique number for item in unique_num_list: print(item, end=" ") if __name__ == '__main__': # asking for the number of slip, which will be equal to the number of unique number on the borad num_of_slips = int(input('Number of slips: ')) # calling creating_Slips function for creating the slip, and this will create slip for N unique number creating_Slips(num_of_slips) # shuffling of created slips_list after creation of all slips random.shuffle(slips_list) # calling final_number_list, which will print all the unique number that was on the board final_number_list()
******************* end of code ***********************
************************** snippet ******************************