0
点赞
收藏
分享

微信扫一扫

Max Digit

灯火南山 2022-03-26 阅读 98
python

You have a number and you need to determine which digit in this number is the biggest.

Input: A positive int.

Output: An Int(0-9)

Example:

max_digit(0) == 0

max_digit(52) == 5

max_digit(634) == 6

max_digit(1) == 1

max_digit(10000) == 1

思路分析:

返回数字中的最大值,首先想到的是将数字转换为列表,用list(map(int, str(number))),即将输入的数字先转换为字符串再用map映射成为列表。

代码:

def max_digit(number: int) -> int:
    NtoL = list(map(int, str(number)))
    m = max(NtoL)
    return m



if __name__ == '__main__':
    print("Example:")
    # print(max_digit(0))

    # These "asserts" are used for self-checking and not for an auto-testing
    # assert max_digit(0) == 0
    assert max_digit(52) == 5
    assert max_digit(634) == 6
    assert max_digit(1) == 1
    assert max_digit(10000) == 1
    print("Coding complete? Click 'Check' to earn cool rewards!")
举报

相关推荐

0 条评论