Skip to main content
added 11 characters in body
Source Link
Reinderien
  • 65.3k
  • 5
  • 69
  • 187

Write an efficient function in the Python language called SmartSearch, this function will receive a sorted arr array of integers of size \$n\$ containing at the beginning \$m\$ "significant numbers" and the rest of the numbers are "fictitious numbers" whose value is 9999. The function also receives a number \$x\$ to be searched for in the array. If the number is in the array, the function must return its index (position), if the number \$x\$ does not exist, it must return -1. Important note: the size \$m\$ is unknown. Required efficiency: \$O(\log m)\$

Write an efficient function in the Python language called SmartSearch. This function will receive a sorted arr array of integers of size \$n\$ containing at the beginning \$m\$ "significant numbers" and the rest of the numbers are "fictitious numbers" whose value is 9999. The function also receives a number \$x\$ to be searched for in the array. If the number is in the array, the function must return its index (position). If the number \$x\$ does not exist, it must return -1. Important note: the size \$m\$ is unknown. Required efficiency: \$O(\log m)\$

I tried that, but I don't think it quite answers the question. I would appreciate corrections please. I realized that I should first find in which index the fictitious numbers start, and then refer only to the list without the fictitious numbers. How do you do that?

def smartSearch(arr, x):
    if x == 9999:
        return -1

    low = 0
    high = len(arr) - 1

    while low <= high:
        mid = (high + low) // 2

        if arr[mid] == x:
            return mid
        elif arr[mid] < x:
            low = mid + 1
        else:
            high = mid - 1

    return -1
def smartSearch(arr, x):
    if x == 9999:
        return -1

    low = 0
    high = len(arr) - 1

    while low <= high:
        mid = (high + low) // 2

        if arr[mid] == x:
            return mid
        elif arr[mid] < x:
            low = mid + 1
        else:
            high = mid - 1

    return -1

Write an efficient function in the Python language called SmartSearch, this function will receive a sorted arr array of integers of size \$n\$ containing at the beginning \$m\$ "significant numbers" and the rest of the numbers are "fictitious numbers" whose value is 9999. The function also receives a number \$x\$ to be searched for in the array. If the number is in the array, the function must return its index (position), if the number \$x\$ does not exist, it must return -1. Important note: the size \$m\$ is unknown. Required efficiency: \$O(\log m)\$

I tried that, but I don't think it quite answers the question I would appreciate corrections please I realized that I should first find in which index the fictitious numbers start, and then refer only to the list without the fictitious numbers How do you do that?

def smartSearch(arr, x):
    if x == 9999:
        return -1

    low = 0
    high = len(arr) - 1

    while low <= high:
        mid = (high + low) // 2

        if arr[mid] == x:
            return mid
        elif arr[mid] < x:
            low = mid + 1
        else:
            high = mid - 1

    return -1

Write an efficient function in the Python language called SmartSearch. This function will receive a sorted arr array of integers of size \$n\$ containing at the beginning \$m\$ "significant numbers" and the rest of the numbers are "fictitious numbers" whose value is 9999. The function also receives a number \$x\$ to be searched for in the array. If the number is in the array, the function must return its index (position). If the number \$x\$ does not exist, it must return -1. Important note: the size \$m\$ is unknown. Required efficiency: \$O(\log m)\$

I tried that, but I don't think it quite answers the question. I would appreciate corrections please. I realized that I should first find in which index the fictitious numbers start, and then refer only to the list without the fictitious numbers. How do you do that?

def smartSearch(arr, x):
    if x == 9999:
        return -1

    low = 0
    high = len(arr) - 1

    while low <= high:
        mid = (high + low) // 2

        if arr[mid] == x:
            return mid
        elif arr[mid] < x:
            low = mid + 1
        else:
            high = mid - 1

    return -1
edited title
Link

Creating a sortsearch function

MathJax
Source Link
Peilonrayz
  • 43.5k
  • 7
  • 76
  • 155

Write an efficient function in the Python language called SmartSearch, this function will receive a sorted arrarr array of integers of size n\$n\$ containing at the beginning m\$m\$ "significant numbers" and the rest of the numbers are "fictitious numbers" whose value is 9999. The function also receives a number x\$x\$ to be searched for in the array. If the number is in the array, the function must return its index (position), if the number x\$x\$ does not exist, it must return -1. Important note: the size m\$m\$ is unknown. Required efficiency: O(logm)\$O(\log m)\$

I tried that, but I don't think it quite answers the question I would appreciate corrections please I realized that I should first find in which index the fictitious numbers start, and then refer only to the list without the fictitious numbers How do you do that?

def smartSearch(arr, x):
    if x == 9999:
        return -1

    low = 0
    high = len(arr) - 1

    while low <= high:
        mid = (high + low) // 2

        if arr[mid] == x:
            return mid
        elif arr[mid] < x:
            low = mid + 1
        else:
            high = mid - 1

    return -1

Write an efficient function in the Python language called SmartSearch, this function will receive a sorted arr array of integers of size n containing at the beginning m "significant numbers" and the rest of the numbers are "fictitious numbers" whose value is 9999. The function also receives a number x to be searched for in the array. If the number is in the array, the function must return its index (position), if the number x does not exist, it must return -1. Important note: the size m is unknown. Required efficiency: O(logm)

I tried that, but I don't think it quite answers the question I would appreciate corrections please I realized that I should first find in which index the fictitious numbers start, and then refer only to the list without the fictitious numbers How do you do that?

def smartSearch(arr, x):
    if x == 9999:
        return -1

    low = 0
    high = len(arr) - 1

    while low <= high:
        mid = (high + low) // 2

        if arr[mid] == x:
            return mid
        elif arr[mid] < x:
            low = mid + 1
        else:
            high = mid - 1

    return -1

Write an efficient function in the Python language called SmartSearch, this function will receive a sorted arr array of integers of size \$n\$ containing at the beginning \$m\$ "significant numbers" and the rest of the numbers are "fictitious numbers" whose value is 9999. The function also receives a number \$x\$ to be searched for in the array. If the number is in the array, the function must return its index (position), if the number \$x\$ does not exist, it must return -1. Important note: the size \$m\$ is unknown. Required efficiency: \$O(\log m)\$

I tried that, but I don't think it quite answers the question I would appreciate corrections please I realized that I should first find in which index the fictitious numbers start, and then refer only to the list without the fictitious numbers How do you do that?

def smartSearch(arr, x):
    if x == 9999:
        return -1

    low = 0
    high = len(arr) - 1

    while low <= high:
        mid = (high + low) // 2

        if arr[mid] == x:
            return mid
        elif arr[mid] < x:
            low = mid + 1
        else:
            high = mid - 1

    return -1
added 12 characters in body
Source Link
toolic
  • 6.1k
  • 4
  • 20
  • 73
Loading
Source Link
Loading