【每日一题0628】街道门牌号

这是霍格沃兹学院的一条完全笔直的街道,路两边有完全相同的房子。当然,你想知道街道另一边人们的门牌号。街道看起来是这样的:

1|   |6
3|   |4
5|   |2

右边的偶数递减,左边的奇数递增。房屋数量从1开始增加,没有间隔。例如,当 n = 3时,1与6相对,3与4相对,5与2相对。

请编写一个函数over_the_road(x, n),给你门牌号x和街道长度 n,返回街道对面的门牌号。

难度:中等
来源:codewars: OVER THE ROAD

def over_the_road(x, n):
    pass

assert 6 == over_the_road(1, 3)
assert 4 == over_the_road(3, 3)
assert 5 == over_the_road(2, 3)
assert 8 == over_the_road(3, 5)

def over_the_load(x, n):
    list1 = []
    list2 = []
    for i in range(1, n+1):
        list1.append(2 * i)
        list2.append(2 * i - 1)
    list1 = list1[::-1]
    if x in list1:
        num =  list1.index(x)
        return  list2[num]
    else:
        num = list2.index(x)
        return list1[num]
assert 6 == over_the_load(1, 3)
assert 4 == over_the_load(3, 3)
assert 5 == over_the_load(2, 3)
assert 8 == over_the_load(3, 5)

image

解法1:推导数学规律

def over_the_road(x, n):
    return (n * 2 + 1) - x

assert 6 == over_the_road(1, 3)
assert 4 == over_the_road(3, 3)
assert 5 == over_the_road(2, 3)
assert 8 == over_the_road(3, 5)

解法2: 两组列表横向比较

def over_the_road(x, n):
    odds = list(filter(lambda x:x % 2 == 1, range(1, n*2+1)))
    evens = sorted(list(filter(lambda x:x % 2 == 0, range(1, n*2+1))), reverse=True)

    return evens[odds.index(x)] if x in odds else odds[evens.index(x)]

assert 6 == over_the_road(1, 3)
assert 4 == over_the_road(3, 3)
assert 5 == over_the_road(2, 3)
assert 8 == over_the_road(3, 5)

:partying_face: :partying_face: :partying_face: :partying_face: :partying_face: :partying_face:

#conding=utf-8
def over_the_road(x, n):
    """
    这是霍格沃兹学院的一条完全笔直的街道,路两边有完全相同的房子。当然,你想知道街道另一边人们的门牌号。街道看起来是这样的:
    1|   |6
    3|   |4
    5|   |2
    右边的偶数递减,左边的奇数递增。房屋数量从1开始增加,没有间隔。例如,当 n = 3时,1与6相对,3与4相对,5与2相对。
    请编写一个函数over_the_road(x, n),给你门牌号x和街道长度 n,返回街道对面的门牌号
    :return:
    """
    a=[]
    #创建空列表,把所有门牌号存进去
    for i in range(1,n*2+1):
        a.append(i)
    #判断左边还是右边,两边计算方法不一样
    # if x%2==0:
    #     print(f"{a[2*n-x]}|  |{a[x-1]}")
    # elif x%2==1:
    #     print(f"{a[x-1]}|  |{a[2*n-x]}")
    # if x%2==0:
    #     return(a[2*n-x])
    # elif x%2==1:
    #     return (a[2*n-x])
    return a[2*n-x]

if __name__ == '__main__':
    # over_the_road(3, 6)
    # over_the_road(4,4)
    assert 6 == over_the_road(1, 3)
    assert 4 == over_the_road(3, 3)
    assert 5 == over_the_road(2, 3)
    assert 8 == over_the_road(3, 5)
def over_the_road(x, n):
    result_a = {}
    result_b = {}
    list_a = [i  for i in range(1,2*n,2)]   # 先将左侧一列的值存入列表a中
    list_b = [j for j in range(2*n,1,-2)]    # 再将右侧一列的值存入列表b中

    # 结果1,左-右
    for m in range(n):
        result_a[list_a[m]] = list_b[m]
    # 结果2,右-左
    for o in range(n):
        result_b[list_b[o]] = list_a[o]

    # 取另一边人们的门牌号
    if x in result_a.keys():
        return result_a[x]
    elif x in result_b.keys():
        return result_b[x]


assert 6 == over_the_road(1, 3)
assert 4 == over_the_road(3, 3)
assert 5 == over_the_road(2, 3)
assert 8 == over_the_road(3, 5)
def over_the_road(x, n):
    """ 返回街道门牌号"""
    left_list = [i for i in range(1, 2*n + 1, 2)]
    right_list = [j for j in range(2*n, -1, -2)]
    if x in left_list:
        return right_list[left_list.index(x)]
    else:
        return left_list[right_list.index(x)]