-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
60 lines (52 loc) · 1.78 KB
/
Program.cs
File metadata and controls
60 lines (52 loc) · 1.78 KB
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
using System;
using System.Linq;
namespace dcp7
{
static class Program
{
static void Main(string[] args)
{
var result1 = Decode("1");
var result2 = Decode("11");
var result3 = Decode("111");
var result4 = Decode("112");
var result5 = Decode("1122");
var result6 = Decode("1122222");
var result7 = Decode("22112246");
Console.WriteLine($"Decode Result 1: {result1}");
Console.WriteLine($"Decode Result 11: {result2}");
Console.WriteLine($"Decode Result 111: {result3}");
Console.WriteLine($"Decode Result 112: {result4}");
Console.WriteLine($"Decode Result 1122: {result5}");
Console.WriteLine($"Decode Result 1122222: {result6}");
Console.WriteLine($"Decode Result 22112246: {result7}");
while(true);
}
static int Decode(string code)
{
if (!Int32.TryParse(code, out int numericalCode) || code.First() == '0')
{
throw new ArgumentException("Not a valid argument.");
}
var count = 0;
if (code.Length > 2)
{
count += IsLetter(int.Parse(code.Substring(0, 2))) ? Decode(code.Substring(2, code.Length - 2)) : 0;
count += Decode(code.Substring(1, code.Length - 1));
}
else if (code.Length == 2)
{
count += IsLetter(int.Parse(code)) ? 2 : 1;
}
else if (code.Length == 1)
{
count = 1;
}
return count;
}
static bool IsLetter(int numericalCode)
{
return numericalCode <= 26 && numericalCode >= 1;
}
}
}