-
Notifications
You must be signed in to change notification settings - Fork 0
/
task2.cs
56 lines (48 loc) · 1.66 KB
/
task2.cs
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
/*
----------------------------------------------------------------
https://www.codewars.com/kata/515decfd9dcfc23bb6000006/train/csharp
----------------------------------------------------------------
*/
/* A good programmer's best nightmare down below */
using System;
using System.Collections.Generic;
namespace Solution
{
class Kata
{
public static bool IsValidIp(string ipAddres)
{
char[] _ip = ipAddres.ToCharArray();
List<int> curSector = new List<int> { };
int dotsCount = 0;
for(int i = 0; i < ipAddres.Length; i++)
{
if (_ip[i] == '.')
{
if ((i + 1 < _ip.Length && _ip[i + 1] == '.') || i == 0) return false;
if (i == ipAddres.Length-1) return false;
int num = 0;
foreach (int entry in curSector) { num = 10 * num + entry; }
if (num > 255) return false;
else curSector.Clear();
dotsCount++;
}
else
{
if (curSector.Count == 0 && _ip[i] == '0')
{
if (i + 1 < _ip.Length && _ip[i + 1] != '.') return false;
}
else
{
if (!Char.IsDigit(_ip[i])) return false;
curSector.Add(_ip[i] - '0');
}
}
}
int _num = 0;
foreach (int entry in curSector) { _num = 10 * _num + entry; }
return !(dotsCount != 3) && _num <= 255;
}
}
}