-
Notifications
You must be signed in to change notification settings - Fork 6
/
FormattedNumber.py
78 lines (56 loc) · 2.14 KB
/
FormattedNumber.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""
Formatted Number
Question :
Have the function FormattedNumber(strArr) take the strArr parameter being passed, which
will only contain a single element, and return the string true if it is a valid number
that contains only digits with properly placed decimals and commas, otherwise return the
string false. For example: if strArr is ["1,093,222.04"] then your program should return
the string true, but if the input were ["1,093,22.04"] then your program should return
the string false. The input may contain characters other than digits.
Examples
Input: ["0.232567"]
Output: true
Input: ["2,567.00.2"]
Output: false
"""
def FormattedNumber(strArr):
# Splitting string by commas
l = strArr[0].split(",")
# Traversing through list l
for i in range(len(l)):
# Checking for 2 adjacent commas and more than 3 numbers between 2 commas
if len(l[i]) == 0:
return "false"
# Splitting element by decimal
f = l[i].split(".")
# Making sure decimal is not placed at the start or end of the element
if len(f[0]) == 0 or len(f[-1]) == 0:
return "false"
# Checking first element
if i == 0:
# Making sure not more than 1 decimal is placed
if len(f) > 2:
return "false"
# Making sure there are only 3 digits before decimal
if len(f[0]) > 3:
return "false"
# Checking for last element
elif i == (len(l) - 1) and len(l) > 1:
# Making sure not more than 1 decimal is placed
if len(f) > 2:
return "false"
# Making sure decimal is placed after 3 digits
if len(f[0]) != 3:
return "false"
# Checking middle elements
else:
# Making sure no decimal is placed
if len(f) > 1:
return "false"
# Making sure element length is 3 only
if len(l[i]) != 3:
return "false"
# Returning "true" if all the commas and decimals are placed correctly
return "true"
# keep this function call here
print(FormattedNumber(input()))