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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
using System;
using System.Diagnostics;
using System.Reflection;
class CodingTest
{
//new int[] {},
//new int[,] {{}},
static void Main(string[] args)
{
//입력값
var input = new object[]
{
new int[] {5,1,3,7},
new int[] {2,2,6,8},
};
//기대값
var expectedResult = 3;
TestSolutionProgrammers(input, expectedResult);
Console.ReadKey();
}
#region Test
public static void TestSolutionProgrammers(object[] values, object expectedResult)
{
try
{
Console.WriteLine($"===============================================================================================================");
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
Solution sol = new Solution();
MethodInfo methodInfo = typeof(Solution).GetMethod("solution");
object[] methodParameters = values;
Console.Write($"입력값 : ");
int lastIndex = values.Length - 1;
for (int i = 0; i < values.Length; i++)
{
object value = values[i];
if (value.GetType().IsArray)
{
Array arrayValue = value as Array;
PrintArray(arrayValue);
}
else
{
Console.Write(value);
}
if (i < lastIndex)
{
Console.Write(" , ");
}
}
Console.WriteLine($"\n기대값 : {expectedResult}");
Console.WriteLine($"===============================================================================================================");
object result = methodInfo.Invoke(sol, methodParameters);
Console.WriteLine($"\n결과 : {result}");
Type returnType = methodInfo.ReturnType;
var expectedResultConverted = Convert.ChangeType(expectedResult, returnType);
if (result.Equals(expectedResultConverted))
{
Console.WriteLine($"\n<Success>");
}
else
{
Console.WriteLine($"\n<Fail>");
}
stopwatch.Stop();
long elapsedTicks = stopwatch.Elapsed.Ticks;
double elapsedMilliseconds = elapsedTicks * (1_000.0 / Stopwatch.Frequency);
Console.WriteLine($"\n실행시간 : {elapsedMilliseconds:F2} ms");
}
catch (Exception e)
{
Console.WriteLine(e);
Console.WriteLine(e.StackTrace);
}
}
public static void PrintArray(Array arr)
{
int rank = arr.Rank;
if (rank == 1)
{
Console.Write("[");
for (int i = 0; i < arr.Length; i++)
{
Console.Write($"{arr.GetValue(i)}");
if (i < arr.Length - 1)
Console.Write(",");
}
Console.Write("]");
}
else if (rank == 2)
{
Console.Write("[");
for (int i = 0; i < arr.GetLength(0); i++)
{
Console.Write("[");
for (int j = 0; j < arr.GetLength(1); j++)
{
Console.Write($"{arr.GetValue(i, j)}");
if (j < arr.GetLength(1) - 1)
Console.Write(",");
}
Console.Write("]");
if (i < arr.GetLength(0) - 1)
Console.Write(",");
}
Console.Write("]");
}
}
#endregion
}
|
cs |
using System;
public class Solution {
public int solution(string name) {
int answer = 0;
return answer;
}
}
Solution 클래스를 다른 cs 파일에 정의 후 실행하면 된다.
2차원 배열까지 출력 가능하다.