【每日一题20220831】可以倒立的数字

:magic_wand: 现在有数字69699116 。当您旋转它们180 degrees (倒置)时,这些数字保持不变。澄清一下,如果我们将它们写在纸上并将纸倒置,数字将是相同的。试试看!某些数字,例如25 在旋转时不产生数字。

给定一个范围,返回该范围内倒置数字的计数。例如solve(0,10) = 3 ,因为在范围>= 0 and < 10只有3 个颠倒的数字。他们是0, 1, 8

题目难度:一般
题目来源:https://www.codewars.com/kata/59f7597716049833200001eb

def solve(a: int, b: int) -> int:
    pass

assert solve(0,10) == 3
assert solve(10,100) == 4
assert solve(100,1000) == 12
assert solve(1000,10000) == 20
assert solve(10000,15000) == 6
assert solve(15000,20000) == 9
assert solve(60000,70000) == 15
assert solve(60000,130000) == 55

倒数第三断言少了一个后括号

def solve(a: int, b: int) -> int:
    t = 0
    dic = {0: 0, 1: 1, 6: 9, 8: 8, 9: 6}
    for i in range(a, b):
        xin_number = ""
        for j in str(i):
            if int(j) not in dic.keys():
                break
            else:
                xin_number += str(dic[int(j)])
        if xin_number[::-1] == str(i):
            # print(xin_number)
            t += 1
    return t


if __name__ == '__main__':
    assert solve(0, 10) == 3
    assert solve(10, 100) == 4
    assert solve(100, 1000) == 12
    assert solve(1000, 10000) == 20
    assert solve(10000, 15000) == 6
    assert solve(15000, 20000) == 9
    assert solve(60000, 70000) == 15
    assert solve(60000, 130000) == 55
def solve(a: int, b: int):
	dict = {0: 0, 1: 1, 2: -1, 3: -1, 4: -1, 5: -1, 6: 9, 7: -1, 8: 8, 9: 6}
	count=0
	for i in range(a,b):
		list_a = [int(m) for m in str(i)]
		##把整数拆成多个个位数的列表[9,1,1,6]
		##将列表里的每一个值调用dict方法,得到[6,1,1,9]
		##将新列表进行reverse,得到[9,1,1,6],再与原始数组[9,1,1,6]比对,相等则数量+1
		list_f=[dict[(list_a[j])] for j in range(len(list_a)) if dict[(list_a[j])]>=0 ]
		list_f.reverse()
		if  operator.eq(list_a,list_f):
			count = count + 1

	return count
assert solve(0,10) == 3
assert solve(10,100) == 4
assert solve(100,1000) == 12
assert solve(1000,10000) == 20
assert solve(10000,15000) == 6
assert solve(15000,20000 )== 9
assert solve(60000,70000) == 15
assert solve(60000,130000) == 55
def solve(a: int, b: int) → int:
    """
    给定一个范围,返回该范围内倒置数字的计数。
    :param a: 起始数字
    :param b: 结束数字
    :return: 倒置数字的计数值
    """
    # 定义数字翻转映射模板
    invert_map = {‘0’: ‘0’, ‘1’: ‘1’, ‘6’: ‘9’, ‘8’: ‘8’, ‘9’: ‘6’}
    count = 0
      
    for num in range(a, b):
        num = str(num)
        # 从模板中获取对应数字的翻转数字
        num_invert = "".join((invert_map.get(sub_num, "") for sub_num in num))
      
        # 如果翻转数字等于数字倒序,则计数加一
        if num_invert == num[::-1]:
            count += 1
      
    return count


assert solve(0, 10) == 3
assert solve(10, 100) == 4
assert solve(100, 1000) == 12
assert solve(1000, 10000) == 20
assert solve(10000, 15000) == 6
assert solve(15000, 20000) == 9
assert solve(60000, 70000) == 15
assert solve(60000, 130000) == 55
def solve(a: int, b: int) -> int:
    word_dict={'0':'0','1':'1','6':'9','8':'8','9':'6'}
    res_list=[]
    for i in range(a,b):
        word_list=[]
        for j in range(len(str(i))):
            if str(i)[j] in word_dict.keys():
                word_list.append(word_dict[str(i)[j]])
            else:
                break
        if str(i)==''.join(word_list)[::-1]:
            res_list.append(str(i))
    return len(res_list)

assert solve(0,10) == 3
assert solve(10,100) == 4
assert solve(100,1000) == 12
assert solve(1000,10000) == 20
assert solve(10000,15000) == 6
assert solve(15000,20000) == 9
assert solve(60000,70000) == 15
assert solve(60000,130000) == 55