Knowee
Questions
Features
Study Tools

Imagine you are working as a software developer for a shipping company. Your task is to implement a search feature that allows users to find a specific package is delivered or not-delivered by its tracking number. The company has millions of packages and you want to make the search process as efficient as possible If all the tracking numbers of delivered packages are sorted in ascending order. Write a python program to find whether a package is delivered or not-delivered using the tracking number.Also, find the number of comparisons made by your algorithmInput format:Series of delivered tracking numbers in Ascending orderTracking number to be searchedOutput format:Delivered or Not-deliveredNumber of comparisons (if delivered)

Question

Imagine you are working as a software developer for a shipping company. Your task is to implement a search feature that allows users to find a specific package is delivered or not-delivered by its tracking number. The company has millions of packages and you want to make the search process as efficient as possible If all the tracking numbers of delivered packages are sorted in ascending order. Write a python program to find whether a package is delivered or not-delivered using the tracking number.Also, find the number of comparisons made by your algorithmInput format:Series of delivered tracking numbers in Ascending orderTracking number to be searchedOutput format:Delivered or Not-deliveredNumber of comparisons (if delivered)

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, I can help you with that. Here's a step-by-step solution in Python:

  1. First, we need to define a list of delivered tracking numbers in ascending order. Let's call it "delivered_numbers".

  2. Next, we need to prompt the user to enter the tracking number they want to search for. Let's call this input "search_number".

  3. We can use binary search algorithm to efficiently search for the tracking number in the "delivered_numbers" list. Binary search works by repeatedly dividing the search space in half until the target number is found or the search space is empty.

  4. To implement binary search, we need to define a function called "binary_search" that takes the "delivered_numbers" list and the "search_number" as parameters.

  5. Inside the "binary_search" function, we need to initialize two variables: "low" and "high". "low" will be the index of the first element in the search space, and "high" will be the index of the last element in the search space.

  6. We also need to initialize a variable called "comparisons" to keep track of the number of comparisons made during the search process. We start with 0 comparisons.

  7. Now, we can start the binary search algorithm. We will use a while loop that continues until the search space is empty (i.e., "low" becomes greater than "high").

  8. Inside the while loop, we calculate the middle index of the search space using the formula: middle = (low + high) // 2.

  9. We compare the tracking number at the middle index with the "search_number". If they are equal, we return "Delivered" and the number of comparisons made so far.

  10. If the tracking number at the middle index is greater than the "search_number", we update the "high" variable to be one less than the middle index.

  11. If the tracking number at the middle index is less than the "search_number", we update the "low" variable to be one more than the middle index.

  12. We also increment the "comparisons" variable by 1 for each comparison made.

  13. If the while loop finishes without finding a match, we return "Not-delivered" and the number of comparisons made.

  14. Finally, we call the "binary_search" function with the "delivered_numbers" list and the "search_number" inputted by the user. We print the result and the number of comparisons made.

Here's the Python code for the above steps:

def binary_search(delivered_numbers, search_number):
    low = 0
    high = len(delivered_numbers) - 1
    comparisons = 0

    while low <= high:
        middle = (low + high) // 2
        comparisons += 1

        if delivered_numbers[middle] == search_number:
            return "Delivered", comparisons
        elif delivered_numbers[middle] < search_number:
            low = middle + 1
        else:
            high = middle - 1

    return "Not-delivered", comparisons

# Example usage
delivered_numbers = [123, 456, 789, 101112, 131415]
search_number = int(input("Enter the tracking number to search: "))

result, comparisons = binary_search(delivered_numbers, search_number)
print(result)
print("Number of comparisons:", comparisons)

Please note that you need to replace the "delivered_numbers" list with your actual list of delivered tracking numbers in ascending order.

This problem has been solved

Similar Questions

Problem StatementAgalya oversees a logistics system that tracks shipments via unique package identifiers. These identifiers are meticulously organized in a sorted array. Her objective is to enhance the system's efficiency in pinpointing packages that are logged an odd number of times (such as 3, 5, 7, etc.), which indicates potential discrepancies in the shipment data. Agalya seeks assistance in developing a program that employs a binary search algorithm, known for its efficacy in handling sorted data. This program will specifically target and display any package identifier that appears an odd number of times, thereby expediting the process for the logistics teams to identify and address any anomalies in the shipment records.Input format :The first line of input consists of an integer N, representing the number of packages in the shipment.The second line consists of N space-separated integers, representing the list of package identifiers.Output format :The output prints an integer representing the package identifier that occurs an odd number of times.Refer to the sample output for formatting specifications.Code constraints :1 ≤ N ≤ 101 ≤ package identifier ≤ 100Sample test cases :Input 1 :717 25 38 38 38 62 79Output 1 :38Input 2 :641 41 41 41 41 72Output 2 :41

Problem StatementArsh is working on enhancing a recommendation system for a shopping application. He needs to develop a feature that suggests the closest product prices to a target price entered by a user. The list of product prices is sorted in non-decreasing order, and he wants the system to find the K closest prices to the target price. If two prices have the same difference from the target, the lower price should be preferred.To implement this feature, Arsh needs to write a program that identifies and prints the K closest prices to the given target price from the sorted list of prices.ExampleInput:52 4 6 8 1053Output:6 4 8 ExplanationThe closest prices to 5 are determined by their absolute differences: 6 (|6-5|=1), 4 (|4-5|=1), and 8 (|8-5|=3). Since ties are resolved by choosing the smaller number, the closest prices are 6, 4, and 8. Therefore, the output is 6 4 8.Input format :The first line contains an integer n, representing the number of product prices.The second line contains n space-separated integers, representing the sorted product prices.The third line contains an integer x, denoting the target price.The fourth line contains an integer k, representing the number of closest prices to find.Output format :The output displays k space-separated integers representing the k closest prices to the target value.

Sorting algorithms can be compared based on the number of comparison statements they make.Group of answer choicesTrueFalse

Write a program for binary search. The input will be the sorted file obtained as output of insertion sort or selection sort.The number to be searched should be given from the keyboard.The output will be the index of the location where the number is found. If the file contains unique numbers then a single index will be returned. If the file contains non-unique or duplicate numbers then the index of the first number in the list has to be returned.

Java Searching and Sorting Programs

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.