aboutsummaryrefslogtreecommitdiff
path: root/challenge-287/paulo-custodio/python/ch-2.py
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-09-18 19:59:32 +0100
committerGitHub <noreply@github.com>2024-09-18 19:59:32 +0100
commit85073f31dae3decce780d1fd66cdcb79c82c7f65 (patch)
tree318d3bc02836f4cde215f7f15e9ec875c74a1490 /challenge-287/paulo-custodio/python/ch-2.py
parentbfe139d559d498bcf1ae48cc08520bfc99ee9b85 (diff)
parent51c035e04c5ae5f9f02555206f57eb675a737617 (diff)
downloadperlweeklychallenge-club-85073f31dae3decce780d1fd66cdcb79c82c7f65.tar.gz
perlweeklychallenge-club-85073f31dae3decce780d1fd66cdcb79c82c7f65.tar.bz2
perlweeklychallenge-club-85073f31dae3decce780d1fd66cdcb79c82c7f65.zip
Merge pull request #10860 from pauloscustodio/master
Add Python solutions
Diffstat (limited to 'challenge-287/paulo-custodio/python/ch-2.py')
-rw-r--r--challenge-287/paulo-custodio/python/ch-2.py77
1 files changed, 77 insertions, 0 deletions
diff --git a/challenge-287/paulo-custodio/python/ch-2.py b/challenge-287/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..9e3a00ea6a
--- /dev/null
+++ b/challenge-287/paulo-custodio/python/ch-2.py
@@ -0,0 +1,77 @@
+#!/usr/bin/env python3
+
+# Challenge 287
+#
+# Task 2: Valid Number
+# Submitted by: Mohammad Sajid Anwar
+#
+# You are given a string, $str.
+#
+# Write a script to find if it is a valid number.
+#
+# Conditions for a valid number:
+#
+# - An integer number followed by an optional exponent.
+# - A decimal number followed by an optional exponent.
+# - An integer number is defined with an optional sign '-' or '+' followed by digits.
+#
+# Decimal Number:
+#
+# A decimal number is defined with an optional sign '-' or '+' followed by one
+# of the following definitions:
+# - Digits followed by a dot '.'.
+# - Digits followed by a dot '.' followed by digits.
+# - A dot '.' followed by digits.
+#
+# Exponent:
+#
+# An exponent is defined with an exponent notation 'e' or 'E' followed by an integer number.
+#
+# Example 1
+#
+# Input: $str = "1"
+# Output: true
+#
+# Example 2
+#
+# Input: $str = "a"
+# Output: false
+#
+# Example 3
+#
+# Input: $str = "."
+# Output: false
+#
+# Example 4
+#
+# Input: $str = "1.2e4.2"
+# Output: false
+#
+# Example 5
+#
+# Input: $str = "-1."
+# Output: true
+#
+# Example 6
+#
+# Input: $str = "+1E-8"
+# Output: true
+#
+# Example 7
+#
+# Input: $str = ".44"
+# Output: true
+
+import re
+import sys
+
+if re.search(r' ^ [-+]? '+
+ r' (?: \d+ \. \d* | '+
+ r' \d* \. \d+ | '+
+ r' \d+ '+
+ r' ) '+
+ r' (?: e [-+]? \d+ )? '+
+ r' $', sys.argv[1], re.X+re.I):
+ print('true')
+else:
+ print('false')