Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0d0c7df

Browse files
author
Amogh Singhal
authored
Merge pull request devAmoghS#1 from luiz-surian/patch-1
Fix has_only_digits.py
2 parents 81cd29b + 5625194 commit 0d0c7df

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

‎has_only_digits.py‎

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
# Given a string, check if it only contains digits
1+
# Given a string, check if it only contains digits.
22

33
def is_digit(input_str):
4-
digits = dict()
5-
6-
for char in input_str:
7-
if char in digits:
8-
digits[char] += 1
9-
else:
10-
digits[char] = 1
11-
12-
if sum(digits.values()) == len(input_str):
13-
return True
14-
else:
15-
return False
4+
try:
5+
# If its possible to convert to a number, return True.
6+
int(input_str)
7+
return True
8+
except ValueError:
9+
# If the string contains letters, above will fail, returning False.
10+
return False
1611

1712
result = is_digit("095357973590759530")
1813
print(result) # True
14+
15+
result = is_digit("1234abc567")
16+
print(result) # False

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /