【每日一题20220413】7上9下

:mage:‍ 给定一个包含纯数字的字符串,如果其中所有出现两个7中包裹一个9的情形,并将9移除。返回余下的字符串。

【示例】
输入:79797
输出:777
解释:其中索引1和索引3的9都被两边的7所夹住,因此移除,最终剩下777

题目难度:简单
题目来源:CodeWars-SevenAte9

def solution(num: int)-> int:
    # your code here

assert solution(797) == 77
assert solution(7979797) == 7777
assert solution(165561786121789797) == 16556178612178977
def solution(num):
    nums = str(num)
    while '797' in nums:
        nums = nums.replace('797', '77')
    return int(nums)
def solution(num: int)-> int:
    # your code here   
    num_str = str(num)
    if len(num_str) <= 2 and num != 77:
        return "输入有误"
    if num == 77:
        return 77
    while '797' in num_str:
        num_str = num_str.replace('797', '77')  
    return int(num_str)

assert solution(99) == "输入有误"
assert solution(77) == 77
assert solution(797) == 77
assert solution(7979797) == 7777
assert solution(165561786121789797) == 16556178612178977
def solution(num:int)->it:
    str_num=str(num)
    while '797' in str_num:
        str_num=str_num.replace('797','77')
    return int(str_num)
def solution(num: int) -> int:
    result = ''
    for i in range(1, len(str(num)) - 1):
        if str(num)[i] != '9':
            result += str(num)[i]
        else:
            if str(num)[i - 1] == '7' and str(num)[i + 1] == '7':
                result += ''
            else:
                result += str(num)[i]
    return int(str(num)[0] + result + str(num)[-1])


assert solution(797) == 77
assert solution(7979797) == 7777
assert solution(165561786121789797) == 16556178612178977
def solution(num: int) -> int:
   nums = list(str(num))
   b = ['7', '9', '7']
   for i in range(len(nums)):
      if nums[i : i + 3] == b:
         nums.pop(i+1)
   return int(''.join(nums))

def solution(num: int)-> int::
new_list=

for i,n in enumerate(str(num)):
        if n != "9" :
            new_list.append(n)
        elif n=="9" and (str(num)[i - 1] != "7" or str(num)[i + 1] != "7"):
            new_list.append(n)

return int("".join(new_list))

assert solution(797) == 77
assert solution(7979797) == 7777
assert solution(165561786121789797) == 16556178612178977

# Daily_20220413
def solution(num: int)-> int:
    # your code here
    if '797' not in str(num):
        return num
    else:
        return solution(int(str(num).replace('797', '77')))


assert solution(797) == 77
assert solution(7979797) == 7777
assert solution(165561786121789797) == 16556178612178977
1 个赞
def solution(num:int)->int:
    while '797' in str(num):
        pattern='797'
        num=re.sub(pattern,'77',str(num))
    return int(num)
    def solution(num: int) -> int:
        s = list(str(num))
        l = len(s)
        for i in range(l):
            if s[i] == '7':
                try:
                    if s[i+1] == '9' and s[i+2] == '7':
                        s.pop(i+1)
                except IndexError :
                    return int(''.join(s))
        return num
def solution(num: int)-> int:
    # your code here
    st = str(num)
    li = list(st)
    for i in range(len(li)):
        if li[i:i+3]==["7","9","7"]:
            li.pop(i+1)
    return int("".join(li))

assert solution(797) == 77
assert solution(7979797) == 7777
assert solution(165561786121789797) == 16556178612178977
def solution(num: int) -> int:
    str_num = str(num)
    while '797' in str_num:
        str_num = str_num.replace('797', '77')
    return int(str_num)


assert solution(797) == 77
assert solution(7979797) == 7777
assert solution(79797979) == 77779
assert solution(165561786121789797) == 16556178612178977