Steve
Steve

Reputation: 363

How can I make my function accept different types of input?

I want to create a function that takes in an input (can be in the form of an int, array or string) and returns whether its an narcissistic number or not.

So far I created a function that can take in an integer, and returns True if the narcissistic and False if not.

def is_narcissistic(num):

   total = []
   number = len(str(num)) #Find the length of the value
   power = int(number) #convert the length back to integer datatype
   digits = [int(x) for x in str(num)] #convert value to string and loop through, convert back to int and store in digits

   for i in digits:
     product = i**power # iterate through the number and take each i to the power of the length of int
     total.append(product) # append the result to the list total
     totalsum = sum(total)

   if totalsum == num:
     print (True)
   else:
     print(False)


print(is_narcissistic(153))

The problem however is that this should work for any kind of input like strings, or lists. For example:

input: (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) --- output: True, "All positive 1 digit integers are narcissistic"

input: (0, 1, 2, 3, 4, 5, 6, 7, 22, 9) --- output: False, "At least 1 integer is not narcissistic"

input: ("4") --- output: True, "Numbers as strings are ok")

input: ("words") --- output: False, "Strings that are not numbers are not ok")

So What should I add so that the function can take these inputs as well?

A Narcissistic number for example 153. Because when you take each digit apart and take it to the power of the length of the digit (3 in this case) you get the number itself.

1^3 + 5^3 + 3^3 = 153

Upvotes: 1

Views: 806

Answers (1)

MegaIng
MegaIng

Reputation: 7886

All you have to do is check these special cases:

def is_narcissistic(num):
    if isinstance(num, str): # is is a string?
        if num.isdigit(): # does is consist purely out of numbers?
            num = int(num)
        else:
            return False
    if isinstance(num, (tuple,list)):
        return all(is_narcissistic(v) for v in num)
    # the rest of your code

Also you have to change the end of your function. You should not print, but return to use the value later:

if totalsum == num:
    return True
else:
    return False

If you want to be able to call it without the extract parenthesis for tuple, you can use this:

def is_narcissistic(*args):
    if len(args) == 1:
        num = args[0]
    else:
        num = args
    # The code from above

Now you can call it like this:

print(is_narcissistic(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)) # True
print(is_narcissistic(0, 1, 2, 3, 4, 5, 6, 7, 22, 9)) # False
print(is_narcissistic(0, 1, "2", 3, "4", 5, 6, 7, "22", 9)) #False

The complete code is here:

def is_narcissistic(*args):
    if len(args) == 1:
        num = args[0]
    else:
        num = args
    if isinstance(num, str):
        if num.isdigit():
            num = int(num)
        else:
            return False
    if isinstance(num, (tuple, list)):
        return all(is_narcissistic(v) for v in num)
    total = []
    number = len(str(num))  # Find the length of the value
    power = int(number)  # convert the length back to integer datatype
    digits = [int(x) for x in str(num)]  # convert value to string and loop through, convert back to int and store in digits

    for i in digits:
        product = i ** power  # iterate through the number and take each i to the power of the length of int
        total.append(product)  # append the result to the list total
        totalsum = sum(total)

    if totalsum == num:
        return True
    else:
        return False

Upvotes: 3

Related Questions