【每日一题20220706】幸运票?

:mage:‍在前苏联的一些国家,有一种关于幸运票的信仰。如果票号左半部分的数字总和等于右半部分的数字总和,则任何种类的交通票都被认为是幸运的。以下是此类数字的示例:

003111    #             3 = 1 + 1 + 1
813372    #     8 + 1 + 3 = 3 + 7 + 2
17935     #         1 + 7 = 3 + 5  // 如果长度为单数则忽略掉中间的数字.
56328116  # 5 + 6 + 3 + 2 = 8 + 1 + 1 + 6

这些票要么在使用后被吃掉,要么被收集来炫耀。

您的任务是编写一个函数,如果参数是幸运票号的字符串十进制表示形式,或者所有其他数字luck_check(str) ,则返回True/False。它应该为空字符串或不代表十进制数的字符串返回 “invalid input”(原题是抛错,这边修改成了返回错误的提示)。

def luck_check(s: str) -> Union[bool, str]:
    # your code here


assert luck_check('683179') == True
assert luck_check('683000') == False
assert luck_check('') == "invalid input"
assert luck_check('6F43E8') == "invalid input"
assert luck_check('17935') == True

题目难度:简单
题目来源:https://www.codewars.com/kata/5314b3c6bb244a48ab00076c

from typing import Union
def luck_check(s: str) -> Union[bool, str]:
	sum1=0
	sum2=0
	list1=list(s)
	mid  =divmod(len(s),2)[0]
	if len(s)==0 or s.isnumeric()==False:
		return "invalid input"
	elif len(s)%2==1:
		for i in range(mid):
			sum1=int(list1[i])+sum1

		for j in range(mid+1,len(s)):
			sum2 = int(list1[j] )+ sum2
		return True if sum1==sum2 else False

	elif len(s)%2==0:
		for i in range(mid):
			sum1=int(list1[i])+sum1
		for j in range(mid,len(s)):
			sum2 = int(list1[j] )+ sum2

		return True if sum1==sum2 else False

assert luck_check('683179') == True
assert luck_check('683000') == False
assert luck_check('') == "invalid input"
assert luck_check('6F43E8') == "invalid input"
assert luck_check('17935') == True

编写代码的时候 使用三个反引号套起来

from typing import Union
def luck_check(s: str) -> Union[bool, str]:
    # your code here
    if s.isdigit():
        i = 1
        while i <= (n:=len(s)):
            if n % 2 != 0:
                m = round(n / 2)
                if sum(map(int, s[:m])) == sum(map(int, s[m+1:])):
                    return True
            else:
                if sum(map(int, s[:i])) == sum(map(int, s[i:])):
                    return True
            i+=1
        return False
    else:
        return "invalid input"


assert luck_check('683179') == True
assert luck_check('683000') == False
assert luck_check('') == "invalid input"
assert luck_check('6F43E8') == "invalid input"
assert luck_check('17935') == True
def luck_check(s: str):
    # your code here
    if s.isdecimal():
        if int(len(s) % 2) == 0:
            left = sum([int(i) for i in list(s[:int(len(s) / 2)])])
            right = sum(int(i) for i in list(s[int(len(s) / 2):]))
            if left == right:
                return True
            else:
                return False
        else:
            left = sum([int(i) for i in list(s[:int(len(s) / 2)])])
            right = sum(int(i) for i in list(s[int(len(s) / 2) + 1:]))
            if left == right:
                return True
            else:
                return False
    else:
        return "invalid input"

if __name__ == '__main__':

    assert luck_check('683179') == True
    assert luck_check('683000') == False
    assert luck_check('') == "invalid input"
    assert luck_check('6F43E8') == "invalid input"
    assert luck_check('17935') == True
def luck_check(s: str) -> Union[bool, str]:
    if not s.isnumeric() or len(s) < 0:
        return "invalid input"
    left_sum = 0
    right_sum = 0
    length = len(s)
    mid_index = length // 2
    for i in range(0, mid_index):
        left_sum += int(s[i])
    mid_index = mid_index if length % 2 == 0 else mid_index + 1
    for k in range(mid_index, length):
        right_sum += int(s[k])

    return left_sum == right_sum
def luck_check(string):
    #your code here
    if len(string)==0:
        raise "invalid input"
    for i in string:
        if i not in {str(j) for j in range(10)}:
            raise "invalid input"
    if len(string)%2!=0:
        return sum(map(int,string[:len(string)//2]))==sum(map(int,string[len(string)//2+1:]))
    else:
        return sum(map(int,string[:len(string)//2]))==sum(map(int,string[len(string)//2:]))

def luck_check(s: str) -> Union[bool, str]:
    if (s_len := len(s)) == 0 or not s.isdigit():  # 处理异常
        return "invalid input"
    else:
        if s_len % 2 == 1:  # 如果长度为单数则忽略掉中间的数字
            s = list[s].pop(s_len // 2)
        return True if sum(map(int, s[:len(s) // 2:])) == sum(map(int, s[len(s) // 2::])) else False
from typing import Union
import math
def luck_check(s: str) -> Union[bool, str]:
    if not s.isdigit() or s == "":
        return "invalid input"
    s_list = [int(i) for i in list(s)]
    return True if sum(s_list[:int(len(s_list)/2)]) == sum(s_list[math.ceil(len(s_list)/2):]) else False

assert luck_check('683179') == True
assert luck_check('683000') == False
assert luck_check('') == "invalid input"
assert luck_check('6F43E8') == "invalid input"
assert luck_check('17935') == True
from typing import Union


def luck_check(s: str) -> Union[bool, str]:
    # your code here
    if not s.isdigit():
        return "invalid input"
    if len(s) % 2 == 0 and sum(int(i) for i in s[0:len(s) // 2]) == sum(int(i) for i in s[-1:len(s) // 2 - 1:-1]):
        return True
    elif len(s) % 2 != 0 and sum(int(i) for i in s[0:len(s) // 2]) == sum(int(i) for i in s[-1:len(s) // 2:-1]):
        return True
    return False


assert luck_check('683179') == True
assert luck_check('683000') == False
assert luck_check('') == "invalid input"
assert luck_check('6F43E8') == "invalid input"
assert luck_check('17935') == True

def luck_check(s: str) -> Union[bool, str]:
    num_list=[]
    base_split=int(len(s)/2)+1 if len(s)%2!=0 else int(len(s)/2)
    if s.isnumeric()!=True:
        return 'invalid input'
    else:
        num_list.extend(s)
        num_list=[int(i)for i in num_list]
        if sum(num_list[:int(len(s)/2)])==sum(num_list[base_split:]):
            return True
    return False
def luck_check(s: str) -> Union[bool, str]:
    if s.isdigit() != True:
        return "invalid input"
    else:
        a = s[:len(s) // 2]
        if len(s) % 2 == 0:
            b = s[len(s) // 2:]
        else:
            b = s[len(s) // 2 + 1:]
        return True if sum([int(a[i]) for i in range(len(a))]) == sum([int(b[i]) for i in range(len(b))]) else False

assert luck_check('683179') == True
assert luck_check('683000') == False
assert luck_check('') == "invalid input"
assert luck_check('6F43E8') == "invalid input"
assert luck_check('17935') == True