Skip to content

Commit f07f4f3

Browse files
committed
Added Hard Challenges directory
Added class and test method for Kaprekars Constant code challenge.
1 parent b8d46d0 commit f07f4f3

21 files changed

+162
-31
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
The list of challenges were originally coded in C++. Challenge solution with
2+
dates have been coded in C# and exist in this solution.
3+
4+
Challenge: C# Date: Class:
5+
Kaprekars Constant 02/16/2021 Kaprekar
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Coderbyte_CSharp.Hard_Challenges
8+
{
9+
class Kaprekar
10+
{
11+
// Have the function KaprekarsConstant(num) take the num parameter being passed
12+
// which will be a 4-digit number with at least two distinct digits. Your program
13+
// should perform the following routine on the number: Arrange the digits in
14+
// descending order and in ascending order (adding zeroes to fit it to a 4-digit
15+
// number), and subtract the smaller number from the bigger number. Then repeat
16+
// the previous step. Performing this routine will always cause you to reach a
17+
// fixed number: 6174. Then performing the routine on 6174 will always give you
18+
// 6174 (7641 - 1467 = 6174). Your program should return the number of times
19+
// this routine must be performed until 6174 is reached.
20+
21+
// For example: if num is 3524 your program should return 3 because of the
22+
// following steps: (1) 5432 - 2345 = 3087, (2) 8730 - 0378 = 8352,
23+
// (3) 8532 - 2358 = 6174.
24+
25+
public int KaprekarsConstant(int num)
26+
{
27+
int count = 0;
28+
int remainder = num;
29+
30+
while (remainder != 6174)
31+
{
32+
remainder = descendInt(remainder) - ascendInt(remainder);
33+
count++;
34+
}
35+
36+
return count;
37+
}
38+
39+
protected int ascendInt(int num)
40+
{
41+
int result = 0;
42+
43+
result = adjustInt(num, false);
44+
45+
return result;
46+
}
47+
48+
protected int descendInt(int num)
49+
{
50+
int result = 0;
51+
52+
result = adjustInt(num, true);
53+
54+
return result;
55+
}
56+
57+
private int adjustInt(int num, bool descend)
58+
{
59+
int result = 0;
60+
int[] arr = new int[4];
61+
int temp = num;
62+
63+
arr[0] = temp / 1000; temp -= arr[0] * 1000;
64+
arr[1] = temp / 100; temp -= arr[1] * 100;
65+
arr[2] = temp / 10; temp -= arr[2] * 10;
66+
arr[3] = temp;
67+
68+
List<int> valueList = arr.ToList<int>();
69+
70+
if (descend)
71+
{
72+
valueList.Sort(SortDescend);
73+
}
74+
else
75+
{
76+
valueList.Sort();
77+
}
78+
79+
result = valueList[0] * 1000 + valueList[1] * 100 + valueList[2] * 10 + valueList[3];
80+
81+
return result;
82+
}
83+
84+
private int SortDescend(int a, int b)
85+
{
86+
int result = 0;
87+
88+
if (a > b)
89+
{
90+
result = -1;
91+
}
92+
93+
else if (a < b)
94+
{
95+
result = 1;
96+
}
97+
else
98+
{
99+
result = 0;
100+
}
101+
102+
return result;
103+
}
104+
105+
106+
}
107+
}

Coderbyte_CSharp/Coderbyte_CSharp.csproj

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,21 @@
4343
<Reference Include="System.Xml" />
4444
</ItemGroup>
4545
<ItemGroup>
46-
<Compile Include="Easy Challenges\AlphabetSorter.cs" />
47-
<Compile Include="Easy Challenges\MathSequence.cs" />
48-
<Compile Include="Easy Challenges\NumberCheck.cs" />
49-
<Compile Include="Easy Challenges\PalindromeChecker.cs" />
50-
<Compile Include="Easy Challenges\StringIntersection.cs" />
51-
<Compile Include="Easy Challenges\TimeConverter.cs" />
52-
<Compile Include="Medium Challenges\ConsecutiveNumbers.cs" />
53-
<Compile Include="Medium Challenges\NumberEncoder.cs" />
54-
<Compile Include="Medium Challenges\PrimeNumber.cs" />
55-
<Compile Include="Medium Challenges\StringCompression.cs" />
56-
<Compile Include="Medium Challenges\StringMinimumWindow.cs" />
57-
<Compile Include="Medium Challenges\StringReducer.cs" />
58-
<Compile Include="Medium Challenges\StringUniqueSubstring.cs" />
59-
<Compile Include="Medium Challenges\TreeGraphs.cs" />
46+
<Compile Include="1 Easy Challenges\AlphabetSorter.cs" />
47+
<Compile Include="1 Easy Challenges\MathSequence.cs" />
48+
<Compile Include="1 Easy Challenges\NumberCheck.cs" />
49+
<Compile Include="1 Easy Challenges\PalindromeChecker.cs" />
50+
<Compile Include="1 Easy Challenges\StringIntersection.cs" />
51+
<Compile Include="1 Easy Challenges\TimeConverter.cs" />
52+
<Compile Include="2 Medium Challenges\ConsecutiveNumbers.cs" />
53+
<Compile Include="2 Medium Challenges\NumberEncoder.cs" />
54+
<Compile Include="2 Medium Challenges\PrimeNumber.cs" />
55+
<Compile Include="2 Medium Challenges\StringCompression.cs" />
56+
<Compile Include="2 Medium Challenges\StringMinimumWindow.cs" />
57+
<Compile Include="2 Medium Challenges\StringReducer.cs" />
58+
<Compile Include="2 Medium Challenges\StringUniqueSubstring.cs" />
59+
<Compile Include="2 Medium Challenges\TreeGraphs.cs" />
60+
<Compile Include="3 Hard Challenges\Kaprekar.cs" />
6061
<Compile Include="Program.cs" />
6162
<Compile Include="Properties\AssemblyInfo.cs" />
6263
<Compile Include="TestChallenge.cs" />
@@ -66,8 +67,9 @@
6667
</ItemGroup>
6768
<ItemGroup />
6869
<ItemGroup>
69-
<Content Include="Easy Challenges\EasyChallenges.txt" />
70-
<Content Include="Medium Challenges\MediumChallenges.txt" />
70+
<Content Include="1 Easy Challenges\EasyChallenges.txt" />
71+
<Content Include="2 Medium Challenges\MediumChallenges.txt" />
72+
<Content Include="3 Hard Challenges\HardChallenges.txt" />
7173
</ItemGroup>
7274
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
7375
</Project>

Coderbyte_CSharp/Program.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,25 @@ static void Main(string[] args)
1616
TestChallenge tc = new TestChallenge();
1717

1818
// Easy
19-
tc.Test_TimeConvert();
20-
tc.Test_AlphabetSoup();
21-
tc.Test_ArithGeoSequence();
22-
tc.Test_Palindrome();
23-
tc.Test_NumberCheck();
24-
tc.Test_FindIntersection();
19+
//tc.Test_TimeConvert();
20+
//tc.Test_AlphabetSoup();
21+
//tc.Test_ArithGeoSequence();
22+
//tc.Test_Palindrome();
23+
//tc.Test_NumberCheck();
24+
//tc.Test_FindIntersection();
2525

2626
// Medium
27-
tc.test_Consecutive();
28-
tc.Test_KUniqueCharacters();
29-
tc.Test_NumberEncoding();
30-
tc.Test_PrimeMover();
31-
tc.Test_MinWindowSubstring();
32-
tc.Test_RunLength();
33-
tc.Test_StringReduction();
34-
tc.Test_TreeConstructor();
27+
//tc.Test_Consecutive();
28+
//tc.Test_KUniqueCharacters();
29+
//tc.Test_NumberEncoding();
30+
//tc.Test_PrimeMover();
31+
//tc.Test_MinWindowSubstring();
32+
//tc.Test_RunLength();
33+
//tc.Test_StringReduction();
34+
//tc.Test_TreeConstructor();
35+
36+
// Hard
37+
tc.Test_KaprekarsConstant();
3538
}
3639
}
3740
}

Coderbyte_CSharp/TestChallenge.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
using Coderbyte_CSharp.Easy_Challenges;
88
using Coderbyte_CSharp.Medium_Challenges;
9+
using Coderbyte_CSharp.Hard_Challenges;
910

1011
namespace Coderbyte_CSharp
1112
{
@@ -158,7 +159,7 @@ public void Test_FindIntersection()
158159
#endregion
159160

160161
#region Medium Challenges
161-
public void test_Consecutive()
162+
public void Test_Consecutive()
162163
{
163164
ConsecutiveNumbers numbers = new ConsecutiveNumbers();
164165

@@ -309,5 +310,18 @@ public void Test_TreeConstructor()
309310
}
310311
#endregion
311312

313+
#region Hard Challenges
314+
public void Test_KaprekarsConstant()
315+
{
316+
Kaprekar k = new Kaprekar();
317+
int value = 3524;
318+
319+
Console.WriteLine("Kaprekars Constant:");
320+
Console.WriteLine("Input: {0} ", value);
321+
Console.WriteLine("Output: {0}", k.KaprekarsConstant(value));
322+
Console.WriteLine();
323+
324+
}
325+
#endregion
312326
}
313327
}

0 commit comments

Comments
 (0)