Profile image of CloudNinja
Submitted by CloudNinja
almost 13 years

1.3 Quick Solution, Can Anyone do It with Less Code ;)?

Here you have, and I’m just curious to see if you can solve it using even a character less:

def is_int(x):
    if x%1: return False
    else: return True

Ok, as lame as I could appear, I’ve just beated myself googline for “ternary operator in python” and ending up with this code:

def is_int(x):
    return False if x%1 else True

I’m fond of finding always new ways to do with less, so if anyone finds an even shorter working code, I’m with it :)!

Edit: Micheal Rochlin awesome idea is just:

def is_int(x):
    return x%1==0

Kudos to him :)

Answer 5149c4dfbaf2fa33320029a7

6 votes

Permalink

How about just return x%1==0?

Profile image of marisbest2
Submitted by marisbest2
almost 13 years

2 comments

Profile image of Bubbles7361
Submitted by Bubbles7361
almost 13 years

Very nice!

Profile image of CloudNinja
Submitted by CloudNinja
almost 13 years

Beated :D!

I edited my message to give you credit ;)

Answer 5149e4443902fa7ad8003297

0 votes

Permalink

Is there a way to do your if statements with true first, then false? Part of me is a little ocd about doing the statement with returning false first.

Profile image of anonymous
Submitted by anonymous
almost 13 years

1 comments

Profile image of CloudNinja
Submitted by CloudNinja
almost 13 years

Just put a “not” in front of the condition :)

Answer 514cbb827f927a7f38000342

0 votes

Permalink

lambda x: x%1==0
Profile image of cybercam
Submitted by cybercam
almost 13 years

Popular free courses

  • In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.
    • Beginner Friendly.
      4 Lessons
  • Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
    • Beginner Friendly.
      11 Lessons
  • Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.
    • Beginner Friendly.
      6 Lessons
Explore full catalog