Skip to content

Commit 3e92d47

Browse files
committed
first working submission - all tests passed
1 parent b98abf0 commit 3e92d47

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

string-to-int/src/Solution.cs

+25-11
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,6 @@
22
using System.Linq;
33
using System.Collections.Generic;
44

5-
6-
// 1. Read in and ignore any leading whitespace.
7-
// 2. Check if the next character (if not already at the end of the string) is '-' or '+'. Read this character in if it is either. This determines if the final result is negative or positive respectively. Assume the result is positive if neither is present.
8-
// 3. Read in next the characters until the next non-digit charcter or the end of the input is reached. The rest of the string is ignored.
9-
// 4. Convert these digits into an integer (i.e. "123" -> 123, "0032" -> 32). If no digits were read, then the integer is 0. Change the sign as necessary (from step 2).
10-
// 5. If the integer is out of the 32-bit signed integer range [-231, 231 - 1], then clamp the integer so that it remains in the range. Specifically, integers less than -231 should be clamped to -231, and integers greater than 231 - 1 should be clamped to 231 - 1.
11-
// [-2147483648 to 2147483647].
12-
// 6. Return the integer as the final result.
13-
14-
155
public class Solution {
166
const string MIN_VALUE = "2147483648";
177
const string MAX_VALUE = "2147483647";
@@ -28,23 +18,47 @@ public int MyAtoi(string s) {
2818
for(int i = 0; i < s.Length; i++){
2919
check = s[i];
3020
if(digits.Count == 0){
31-
if(char.IsWhiteSpace(check) || check == '+'){
21+
if(char.IsWhiteSpace(check)){
22+
continue;
23+
}
24+
if(i == s.Length - 1 && (!char.IsNumber(check) || check == ZERO) ){
25+
return 0;
26+
}
27+
if(check == ZERO && !char.IsNumber(s[i+1])){
28+
return 0;
29+
}
30+
if(check == ZERO){
3231
continue;
3332
}
3433
if(check == '-'){
34+
if(!char.IsNumber(s[i+1])){
35+
return 0;
36+
}
3537
IsNegative = true;
3638
continue;
3739
}
40+
if(check == '+' ){
41+
if(!char.IsNumber(s[i+1])){
42+
return 0;
43+
}
44+
continue;
45+
}
3846
if(check < ZERO || check > NINE){
3947
return 0;
4048
}
4149
}
4250
if(char.IsNumber(check)){
4351
digits.Add(check);
52+
if(digits.Count > 10){
53+
return IsNegative ? int.MinValue : int.MaxValue;
54+
}
4455
continue;
4556
}
4657
break;
4758
}
59+
if(digits.Count == 0){
60+
return 0;
61+
}
4862
if(digits.Count <= 10){
4963
try{
5064
return int.Parse(string.Join(null, digits)) * (IsNegative ? -1 : 1);

string-to-int/test/StringToIntTests.cs

+17-3
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,28 @@ public class StringToIntTests
1010
[DataRow("42", 42)]
1111
[DataRow("", 0)]
1212
[DataRow("-42", -42)]
13+
[DataRow("+-12", 0)]
14+
[DataRow("--12", 0)]
15+
[DataRow("-+12", 0)]
16+
[DataRow("000000000000000000000000000000000012", 12)]
17+
[DataRow("00000000000000000000000000000000", 0)]
18+
[DataRow("0000000000000000000000000000000000129999999999999999999999999999999999999999", int.MaxValue)]
19+
[DataRow("-0000000000000000000000000000000000129999999999999999999999999999999999999999", int.MinValue)]
1320
[DataRow(" -42", -42)]
1421
[DataRow("4193 with words", 4193)]
22+
[DataRow("+", 0)]
23+
[DataRow("-", 0)]
24+
[DataRow(" +", 0)]
25+
[DataRow(" -", 0)]
1526
[DataRow("words and 987", 0)]
16-
[DataRow("-91283472332", -2147483648)]
17-
[DataRow("-9128347233", -2147483648)] // 10 digit negative number that is out of bounds
18-
27+
[DataRow("-91283472332", int.MinValue)]
28+
[DataRow("-9128347233", int.MinValue)] // 10 digit negative number that is out of bounds
29+
[DataRow("00000-42a1234", 0)]
30+
[DataRow("00000+42a1234", 0)]
31+
1932
public void Should_return_expected_value(string input, int expected)
2033
{
34+
System.Console.WriteLine($"Beginning Test: `{input}` {expected}");
2135
int actual = new Solution().MyAtoi(input);
2236
Assert.AreEqual(expected, actual, $"TESTED: '{input}' expected: {expected} actual: {actual}");
2337
}

0 commit comments

Comments
 (0)