Question

In: Computer Science

please write simple python 3 program with explanations and correct output Bilbo and problems on the...

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

Solutions

Expert Solution

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 ******************************


Related Solutions

please answer this in a simple python code 1. Write a Python program to construct the...
please answer this in a simple python code 1. Write a Python program to construct the following pattern (with alphabets in the reverse order). It will print the following if input is 5 that is, print z one time, y two times … v five times. The maximum value of n is 26. z yy xxx wwww vvvvvv
please write simple python code Write a program to replicate the behavior of UNIX utility “tail”....
please write simple python code Write a program to replicate the behavior of UNIX utility “tail”. It takes one or more files and displays requested number of lines from the ending. If number is not specified, then it print 10 lines by default.
USE Python 3, please type thanks! Write a Python 3 program to calculate the Body Mass...
USE Python 3, please type thanks! Write a Python 3 program to calculate the Body Mass Index (BMI) of a person if they enter their weight and their height to your program. Check out the formula here: http://www.freebmicalculator.net/calculate-bmi.php Your program should first print "Body Mass Index Calculator" The program will then ask the user if they want to enter Metric Units or English Units. Using the appropriate formula (see link above) calculate their BMI. Depending on their BMI show their...
Write a complete and syntactically correct Python program to solve the following problem: Write a program...
Write a complete and syntactically correct Python program to solve the following problem: Write a program for your professor that allows him to keep a record of the students’ average grade in his class. The program must be written in accordance with the following specs: 1. The input must be interactive from the keyboard. You will take input for 12 students. 2. You will input the students’ name and an average grade. The student cannot enter an average below zero...
Please write the following Python program. Also, show all output work. Computing the Fibonacci and Lucas...
Please write the following Python program. Also, show all output work. Computing the Fibonacci and Lucas Series¶ Goal:¶ The Fibonacci Series is a numeric series starting with the integers 0 and 1. In this series, the next integer is determined by summing the previous two. This gives us: 0, 1, 1, 2, 3, 5, 8, 13, ... We will write a function that computes this series – then generalize it. Step 1¶ Create a new module series.py in the session02...
Please write simple python program with explanation Problem statement: Stock markets are venues where buyers and...
Please write simple python program with explanation Problem statement: Stock markets are venues where buyers and sellers of shares meet and decide on a price to trade.Eddard is very interested in trading in stock market and he started gathering information of one stock.He has projected market prices for that share over the next n months.Eddard wants to purchase and resell the share to earn profits.He is much worried about the loss that might occur so he wanted to calculate "Minimum...
Please write in beginner level PYTHON code! Your job is to write a Python program that...
Please write in beginner level PYTHON code! Your job is to write a Python program that asks the user to make one of two choices: destruct or construct. - If the user chooses to destruct, prompt them for an alternade, and then output the 2 words from that alternade. - If the user chooses construct, prompt them for 2 words, and then output the alternade that would have produced those words. - You must enforce that the users enter real...
Please provide Python code that does the following: 3) Write a program that takes a string...
Please provide Python code that does the following: 3) Write a program that takes a string as input, checks to see if it is comprised entirely of letters, and if all those letters are lower case. The output should be one of three possible messages: Your string is comprised entirely of lower case letters. Your string is comprised entirely of letters but some or all are upper case. Your string is not comprised entirely of letters. Your program may NOT:...
for Python 3 Write a python program that Creates a list that has these values in...
for Python 3 Write a python program that Creates a list that has these values in this order, 'Python', 'JavaScript', and 'PHP' Define a displayMyClasses function that sorts the list and then displays each item on the list, one per line, in sorted order. Also, number the displayed list as shown in the "Running a Sample Program" shown below. Define a guessNext function that selects a random element from the list and returns it to the call of the function....
Please write in Python code please: Write a program that asks the user to enter 5...
Please write in Python code please: Write a program that asks the user to enter 5 test scores between 0 and 100. The program should display a letter grade for each score and the average test score. You will need to write the following functions, including main: calc_average – The function should accept a list of 5 test scores as an input argument, and return the average of the scores determine_grade – The function should accept a test score as...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT