-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathStrtoInt.rb
More file actions
28 lines (21 loc) · 805 Bytes
/
StrtoInt.rb
File metadata and controls
28 lines (21 loc) · 805 Bytes
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
=begin
t can be used like this:
parseInt("10") returns 10
parseInt("10 apples") also returns 10
We would like it to return "NaN" (as a string) for the second case because the
input string is not a valid number.
You are asked to write a myParseInt method with the following rules:
It should make the conversion if the given string only contains a single integer
value (and eventually spaces - including tabs, line feeds... - at both ends)
For all other strings (including the ones representing float values), it should
return NaN
It should assume that all numbers are not signed and written in base 10
=end
# My Solution
def my_parse_int(string)
string.strip =~ /^\d{1,}$/ ? string.to_i : "NaN"
end
# Another Solution
def my_parse_int(string)
string.strip.match(/\D+/) ? "NaN" : string.to_i
end