6
\$\begingroup\$

I just recently started reading the python.org documents and it looked like a really interesting language. Now, since Visual Studio 2017 supports Python, I decided to finally actually start learning it. My friend challenged me to write a program that checks if a number is an Armstrong number.

import sys
def isArmstrong(number):
 arr = str(number)
 count = len(arr)
 res = 0
 for x in range(0, count):
 res += int(arr[x]) ** count
 return res == number
if len(sys.argv) > 1:
 arg = sys.argv[1]
 print(arg + " is an armstrong number: " + str(isArmstrong(int(arg))))
else:
 print("No arguments passed! :-(");

Now this is the first solution that I could come up with. As you can see, it converts the number to a string to determine the amount of digits, and then handle them individually. Is this the smartest way to do it, or can it be done without converting to a string? If not: Is there anything I can do to improve this code performance-wise?

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Nov 22, 2016 at 1:02
\$\endgroup\$
2
  • 5
    \$\begingroup\$ Hi. Welcome to Code Review! I would find something like this easier to read if it explained what an Armstrong number is. Saves a trip to Google. \$\endgroup\$ Commented Nov 22, 2016 at 2:36
  • \$\begingroup\$ @mdfst13 You're right, that would've been smart. Thanks! \$\endgroup\$ Commented Nov 22, 2016 at 16:48

1 Answer 1

7
\$\begingroup\$

By PEP 8, Python's official style guide, function names should generally be lower_case_with_underscores. Also, the final semicolon should be omitted.

The function would typically be written more elegantly using the sum() builtin function, with a generator expression. (It's not any faster than your original code, though. Actually, a tiny bit slower.)

I don't recommend using arr as a variable name, since it's neither descriptive (what does the array represent?) nor accurate (it's actually a string).

For the printout, I recommend using str.format() or one of the other formatting mechanisms.

import sys
def is_armstrong(number):
 digits = str(number)
 length = len(digits)
 return number == sum(int(digit) ** length for digit in digits)
if len(sys.argv) > 1:
 arg = int(sys.argv[1])
 print('{} is an Armstrong number? {}'.format(arg, is_armstrong(arg)))
else:
 print("No arguments passed! :-(")
answered Nov 22, 2016 at 4:43
\$\endgroup\$
2
  • \$\begingroup\$ Wow, that style guide is really useful. Also had never heard of .format() or sum(), thanks for all this! Also I called it "arr" because I'm used to char[] for character arrays in C# haha \$\endgroup\$ Commented Nov 22, 2016 at 16:50
  • 1
    \$\begingroup\$ The only change I'd make to this answer is to use f-strings, ie f'{arg} is an Armstrong number? {is_armstrong(arg}'. The advantage is pretty obvious. It just looks so much nicer. The only downside is they need python 3.6 or newer. \$\endgroup\$ Commented Nov 23, 2018 at 8:59

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.