如果程序中有条件,则我有什么确切值?

时间:2019-03-25 06:31:58

标签: python python-3.x python-2.7

我有一个while循环,如果此程序和输出中的条件正常,那一切都很好。但是,我的问题在于 if arr[i]<dep[j]在这种情况下,arr[i]的意思是arr[1]。但是dep[j]的意思是,我不知道索引。谁能解释这段代码?

这是代码。

def bus(arr, dep, n):
    arr.sort()
    dep.sort()
    p = 1
    r = 1
    i = 1
    j = 0
    while i < n and j < n:
        if arr[i] < dep[j]:
            print(arr[i], dep[j])
            print(dep[j])
            p += 1
            print(p)
            i += 1
            if p > r:
                r = p
                print(r, p)
        else:
            p -= 1
            print(p)
            j += 1
    return r


arr = [900, 940, 950, 1100, 1500, 1800]
dep = [910, 1200, 1120, 1130, 1900, 2000]
n = len(arr)
print('min no.of platforms:', bus(arr, dep, n))

1 个答案:

答案 0 :(得分:0)

您的程序几乎没有重新格式化有意义的变量名。希望这能回答您的问题。

def bus(arr, dep, total_bus):
    arr.sort()
    dep.sort()

    if total_bus == 0:
        # when there is no bus
        return 0

    if dep[0] > arr[total_bus - 1]:
        # when all bus comes in and after bus leaving 1 by 1
        return n

    platform = 1
    arr_index = 1
    dep_index = 0
    cur_bus = 1
    while arr_index < total_bus and dep_index < total_bus:
        if arr[arr_index] > dep[dep_index]:
            dep_index += 1
            cur_bus -= 1
        else:
            cur_bus += 1
            arr_index += 1
        if cur_bus > platform:
            platform += 1

    return platform


arr = [900, 940, 950, 1100, 1500, 1800]
dep = [910, 1200, 1120, 1130, 1900, 2000]
n = len(arr)
print('min no.of platforms:', bus(arr, dep, n))