【每日一题20220421】自恋数字

:mage:‍ 自恋数字是指一个数字,假设长度为n,那么每个位置上的数字的n次幂的总和刚好等于原来的数字。请编写一个函数,给定一个数字,判断它是否为自恋数字。

【示例】
输入:153
输出:True
解释:因为153的长度是3,而13+53+33=153成立。

题目难度:简单
题目来源:CodeWars-Narcissistic Numbers

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

assert solution(153) is True
assert solution(201) is False
assert solution(407) is True
def solution(s: int)-> bool:
    # your code here
    return sum([pow(int(i),len(str(s))) for i in str(s)]) == s

assert solution(153) is True
assert solution(201) is False
assert solution(407) is True
from functools import reduce
def solution(s: int)-> bool:
    return reduce(lambda x,y:x+y,[int (i) **len(str(s))for i in str(s)])==s

def solution(s: int) -> bool:
    sum = 0
    for i in str(s):
        sum += int(i) ** len(str(s))
    return True if s == sum else False

assert solution(153) is True
assert solution(201) is False
assert solution(407) is True
def solution(s: int) -> bool:
    return s == sum([int(i) ** len(str(s)) for i in str(s)])

public static int p() {

    Random random = new Random();
    phone = random.nextInt(1000);
    String S = String.valueOf(phone);
    int sum=0;
    List<Double> po=new ArrayList<Double>();
    System.out.println("S:"+S);
    for (int i = 0; i < S.length(); i++) {
        double[] dou=new double[S.length()];
        double d = Double.valueOf(S.substring(i, i + 1));
        dou[i] = d;
        int l = S.length();
        double len = Double.valueOf(l);
        double[] dm=new double[S.length()];
        dm[i] = Math.pow(dou[i], len);
        po.add(dm[i]);
    }
    for (int j=0; j<po.size();j++){
        double in=po.get(j);
         int in1=(int) in;
        sum+=in1;
    }
    if (sum==phone){
        System.out.println("他们是自恋数字:"+sum);
    }else {
        System.out.println("他们不是自恋数字:"+sum);
    }
def solution(s: int)-> bool:
    # your code here
    num = str(s)
    lenth = len(num)
    return sum(int(i) ** lenth for i in num) == s
def test_solution(s: int) -> bool:
    # your code here
    return s == sum([int(i) ** len(str(s)) for i in str(s)])

if __name__ == '__main__':
    assert test_solution(153) is True
    assert test_solution(201) is False
    assert test_solution(407) is True
def solution(s: int) -> bool:
    sum = 0
    for i in str(s):
        sum += int(i) ** len(str(s))
    return sum == s
def solution(s: int)-> bool:
    b=lambda x:math.pow(x,len(str(s)))
    return ((sum(list(map(b,[int(i) for i in str(s)]))))==s)
def solution(s: int) -> bool:
    return sum([int(i) ** len(str(s)) for i in str(s)]) == s


assert solution(153) is True
assert solution(201) is False
assert solution(407) is True
def solution(s: int) -> bool:
    return sum([pow(int(i), len(str(s))) for i in str(s)]) == s


assert solution(153) is True
assert solution(201) is False
assert solution(407) is True
def solution(s: int)-> bool:
    # your code here
    return sum([int(i) ** len(str(s)) for i in str(s)]) == s

assert solution(153) is True
assert solution(201) is False
assert solution(407) is True

def solution(s: int)-> bool:
    str_con = str(s)
    sum = 0
    le = len(str_con)
    for i in str_con:
        M = int(i) ** 3
        sum += M
    if sum == s:
        return True
    else:
        return False


assert solution(153) is True
assert solution(201) is False
assert solution(407) is True
def solution(s: int)-> bool:
    return sum([int(str(s)[i])**len(str(s)) for i in range(len(str(s)))])==s

assert solution(153) is True
assert solution(201) is False
assert solution(407) is True