【每日一题1130】可颠倒的数字

:woman_mage: 当我们仔细观察,例如6969和9116,我们会发现一个很有趣的现象,就是当我们将它们旋转180度后,得到的数字和原来是一模一样的。这种数字则称为可颠倒的数字。

请编写一个函数,接收一个开始和结束数字,找出这个范围内所有的可颠倒数字的个数。

示例:
输入:(0, 10),返回:3。因为在0≤x<10之间的0,1,8满足条件。

题目难度:中等
题目来源:codewars: Upside down numbers

def solution(start: int, end: int) -> int:
    # your code

assert solution(0, 10) == 3
assert solution(10, 100) == 4
assert solution(100, 1000) == 12
def solution(start: int, end: int) -> int:
    solution_dict = {
        '0': '0',
        '1': '1',
        '6': '9',
        '8': '8',
        '9': '6',
    }
    solution_num = 0
    for n in range(start, end):
        num_str = ''
        for i in str(n):
            if i in solution_dict.keys():
                num_str += solution_dict[i]
        if num_str != '' and n == int(num_str[::-1]):
            solution_num += 1
    return solution_num


assert solution(0, 10) == 3
assert solution(10, 100) == 4
assert solution(100, 1000) == 12
1 个赞
def solution(start: int, end: int) -> int:
    revert_int = {'0', '1', '6', '8', '9'}
    num = 0
    for i in range(start, end):
        if set(str(i)).issubset(revert_int):
            temp = ''
            for c in str(i)[::-1]:
                if c == '6':
                    temp += '9'
                elif c == '9':
                    temp += '6'
                else:
                    temp += c
            if int(temp) == i:
                num += 1
    return num


assert solution(0, 10) == 3
assert solution(10, 100) == 4
assert solution(100, 1000) == 12
1 个赞

Python

pairs = {
    "0": "0",
    "1": "1",
    "2": None,
    "3": None,
    "4": None,
    "5": None,
    "6": "9",
    "7": None,
    "8": "8",
    "9": "6"
}

def solution(start: int, end: int) -> int:
    counts = []
    for i in range(start, end):
        result = "".join([pairs.get(x) for x in str(i)[::-1] if pairs.get(x)])
        
        if str(i) == result:
            counts.append(result)
            
    return len(counts)

assert solution(0, 10) == 3
assert solution(10, 100) == 4
assert solution(100, 1000) == 12

参考思路:判断一个数是否是可颠倒的数字,可以将原来数字按条件进行颠倒,然后判断与原数字是否相同。具体步骤是:第一步进行翻转,第二步对应位置翻译成它的颠倒数字,得到一个颠倒效果的数字,第三步是比较。

1 个赞
def solution(start: int, end: int) -> int:
    dic1={'0':'0','1':'1','6':'9','8':'8','9':'6'}
    count=0
    for i in range(start,end+1):
        list1=[]
        for j in str(i):
            if j in dic1.keys():
                list1.append(dic1[j])
        if len(list1)==len(str(i)) and ''.join(list1[::-1])==str(i):
            count+=1
    return count

assert solution(0, 10) == 3
assert solution(10, 100) == 4
assert solution(100, 1000) == 12
def solution(start: int, end: int) -> int:
    num = 0
    error_num = ['2','3','4','5','7']
    for i in range(start,end):
        ok = True
        for w in error_num:
            if w in str(i):
                ok = False
                break
        if ok:
            rever_i = str(i)[::-1].replace('6', 'a').replace('9','6').replace('a','9')
            if rever_i == str(i):
                num+=1
    return num
def solution(start: int, end: int) -> int:
    data_map = {
        '0': '0',
        '1': '1',
        '2': None,
        '3': None,
        '4': None,
        '5': None,
        '6': '9',
        '7': None,
        '8': '8',
        '9': '6'
    }
    count = 0
    for num in range(start, end + 1):
        list_num = [data_map[i] for i in str(num) if data_map[i] is not None][::-1]
        if len(list_num) > 0:
            if int(''.join(list_num)) == num:
                count += 1
    return count


assert solution(0, 10) == 3
assert solution(10, 100) == 4
assert solution(100, 1000) == 12