Skip to content

Commit 23e6a86

Browse files
Merge pull request #501 from robkeim/armstrong-number
Add ArmstrongNumbers exercise
2 parents 8ebeb57 + bbb8334 commit 23e6a86

File tree

13 files changed

+156
-24
lines changed

13 files changed

+156
-24
lines changed

config.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,16 @@
125125
"unlocked_by": "sum-of-multiples",
126126
"uuid": "c4efbf8a-8e76-4700-807d-830a4938f4d0"
127127
},
128+
{
129+
"core": false,
130+
"difficulty": 2,
131+
"slug": "armstrong-numbers",
132+
"topics": [
133+
"mathematics"
134+
],
135+
"unlocked_by": "sum-of-multiples",
136+
"uuid": "8150604d-4cdc-414a-a523-dd65ac536f0e"
137+
},
128138
{
129139
"core": true,
130140
"difficulty": 3,

exercises/Exercises.sln

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Microsoft Visual Studio Solution File, Format Version 12.00
22
# Visual Studio 15
3-
VisualStudioVersion = 15.0.27004.2008
3+
VisualStudioVersion = 15.0.27004.2009
44
MinimumVisualStudioVersion = 15.0.26124.0
55
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Accumulate", "accumulate\Accumulate.csproj", "{F16C0EE1-6923-4328-B015-363CF24FF867}"
66
EndProject
@@ -224,6 +224,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IsbnVerifier", "isbn-verifi
224224
EndProject
225225
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ReverseString", "reverse-string\ReverseString.csproj", "{D2105979-EE3B-432B-8016-172BA949DE2F}"
226226
EndProject
227+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ArmstrongNumbers", "armstrong-numbers\ArmstrongNumbers.csproj", "{7015C2C4-4050-4631-9394-7EAF19AB0191}"
228+
EndProject
227229
Global
228230
GlobalSection(SolutionConfigurationPlatforms) = preSolution
229231
Debug|Any CPU = Debug|Any CPU
@@ -674,6 +676,10 @@ Global
674676
{D2105979-EE3B-432B-8016-172BA949DE2F}.Debug|Any CPU.Build.0 = Debug|Any CPU
675677
{D2105979-EE3B-432B-8016-172BA949DE2F}.Release|Any CPU.ActiveCfg = Release|Any CPU
676678
{D2105979-EE3B-432B-8016-172BA949DE2F}.Release|Any CPU.Build.0 = Release|Any CPU
679+
{7015C2C4-4050-4631-9394-7EAF19AB0191}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
680+
{7015C2C4-4050-4631-9394-7EAF19AB0191}.Debug|Any CPU.Build.0 = Debug|Any CPU
681+
{7015C2C4-4050-4631-9394-7EAF19AB0191}.Release|Any CPU.ActiveCfg = Release|Any CPU
682+
{7015C2C4-4050-4631-9394-7EAF19AB0191}.Release|Any CPU.Build.0 = Release|Any CPU
677683
EndGlobalSection
678684
GlobalSection(SolutionProperties) = preSolution
679685
HideSolutionNode = FALSE
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System;
2+
3+
public static class ArmstrongNumbers
4+
{
5+
public static bool IsArmstrongNumber(int number)
6+
{
7+
throw new NotImplementedException("You need to implement this function.");
8+
}
9+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Remove="Example.cs" />
9+
</ItemGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
13+
<PackageReference Include="xunit" Version="2.3.1" />
14+
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
15+
</ItemGroup>
16+
17+
</Project>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// This file was auto-generated based on version 1.0.0 of the canonical data.
2+
3+
using Xunit;
4+
5+
public class ArmstrongNumbersTest
6+
{
7+
[Fact]
8+
public void Single_digit_numbers_are_armstrong_numbers()
9+
{
10+
Assert.True(ArmstrongNumbers.IsArmstrongNumber(5));
11+
}
12+
13+
[Fact(Skip = "Remove to run test")]
14+
public void There_are_no_2_digit_armstrong_numbers()
15+
{
16+
Assert.False(ArmstrongNumbers.IsArmstrongNumber(10));
17+
}
18+
19+
[Fact(Skip = "Remove to run test")]
20+
public void Three_digit_number_that_is_an_armstrong_number()
21+
{
22+
Assert.True(ArmstrongNumbers.IsArmstrongNumber(153));
23+
}
24+
25+
[Fact(Skip = "Remove to run test")]
26+
public void Three_digit_number_that_is_not_an_armstrong_number()
27+
{
28+
Assert.False(ArmstrongNumbers.IsArmstrongNumber(100));
29+
}
30+
31+
[Fact(Skip = "Remove to run test")]
32+
public void Four_digit_number_that_is_an_armstrong_number()
33+
{
34+
Assert.True(ArmstrongNumbers.IsArmstrongNumber(9474));
35+
}
36+
37+
[Fact(Skip = "Remove to run test")]
38+
public void Four_digit_number_that_is_not_an_armstrong_number()
39+
{
40+
Assert.False(ArmstrongNumbers.IsArmstrongNumber(9475));
41+
}
42+
43+
[Fact(Skip = "Remove to run test")]
44+
public void Seven_digit_number_that_is_an_armstrong_number()
45+
{
46+
Assert.True(ArmstrongNumbers.IsArmstrongNumber(9926315));
47+
}
48+
49+
[Fact(Skip = "Remove to run test")]
50+
public void Seven_digit_number_that_is_not_an_armstrong_number()
51+
{
52+
Assert.False(ArmstrongNumbers.IsArmstrongNumber(9926314));
53+
}
54+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
3+
public static class ArmstrongNumbers
4+
{
5+
public static bool IsArmstrongNumber(int number)
6+
{
7+
var numString = number.ToString();
8+
var length = numString.Length;
9+
10+
double total = 0;
11+
for (int i = 0; i < length; i++)
12+
{
13+
total += Math.Pow(int.Parse(numString[i].ToString()), length);
14+
}
15+
16+
return number == (int)total;
17+
}
18+
}

exercises/armstrong-numbers/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
An [Armstrong number](https://en.wikipedia.org/wiki/Narcissistic_number) is a number that is the sum of its own digits each raised to the power of the number of digits.
2+
3+
For example:
4+
5+
- 9 is an Armstrong number, because `9 = 9^1 = 9`
6+
- 10 is *not* an Armstrong number, because `10 != 1^2 + 0^2 = 2`
7+
- 153 is an Armstrong number, because: `153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153`
8+
- 154 is *not* an Armstrong number, because: `154 != 1^3 + 5^3 + 4^3 = 1 + 125 + 64 = 190`
9+
10+
Write some code to determine whether a number is an Armstrong number.

exercises/bob/BobTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// This file was auto-generated based on version 1.0.0 of the canonical data.
1+
// This file was auto-generated based on version 1.1.0 of the canonical data.
22

33
using Xunit;
44

@@ -55,7 +55,7 @@ public void Using_acronyms_in_regular_speech()
5555
[Fact(Skip = "Remove to run test")]
5656
public void Forceful_question()
5757
{
58-
Assert.Equal("Whoa, chill out!", Bob.Response("WHAT THE HELL WERE YOU THINKING?"));
58+
Assert.Equal("Calm down, I know what I'm doing!", Bob.Response("WHAT THE HELL WERE YOU THINKING?"));
5959
}
6060

6161
[Fact(Skip = "Remove to run test")]

exercises/bob/Example.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ public static string Response(string statement)
44
{
55
if (IsSilence(statement))
66
return "Fine. Be that way!";
7+
if (IsYelling(statement) && IsQuestion(statement))
8+
return "Calm down, I know what I'm doing!";
79
if (IsYelling(statement))
810
return "Whoa, chill out!";
911
if (IsQuestion(statement))

exercises/book-store/BookStoreTest.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// This file was auto-generated based on version 1.0.1 of the canonical data.
1+
// This file was auto-generated based on version 1.1.0 of the canonical data.
22

33
using Xunit;
44

@@ -94,4 +94,11 @@ public void Three_each_of_first_2_books_and_2_each_of_remaining_books()
9494
var basket = new[] { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1, 2 };
9595
Assert.Equal(75.2, BookStore.Total(basket));
9696
}
97+
98+
[Fact(Skip = "Remove to run test")]
99+
public void Four_groups_of_four_are_cheaper_than_two_groups_each_of_five_and_three()
100+
{
101+
var basket = new[] { 1, 1, 2, 2, 3, 3, 4, 5, 1, 1, 2, 2, 3, 3, 4, 5 };
102+
Assert.Equal(102.4, BookStore.Total(basket));
103+
}
97104
}

exercises/rna-transcription/RnaTranscriptionTest.cs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
// This file was auto-generated based on version 1.0.1 of the canonical data.
1+
// This file was auto-generated based on version 1.1.0 of the canonical data.
22

33
using Xunit;
4-
using System;
54

65
public class RnaTranscriptionTest
76
{
@@ -34,22 +33,4 @@ public void Rna_complement()
3433
{
3534
Assert.Equal("UGCACCAGAAUU", RnaTranscription.ToRna("ACGTGGTCTTAA"));
3635
}
37-
38-
[Fact(Skip = "Remove to run test")]
39-
public void Correctly_handles_invalid_input_rna_instead_of_dna_()
40-
{
41-
Assert.Throws<ArgumentException>(() => RnaTranscription.ToRna("U"));
42-
}
43-
44-
[Fact(Skip = "Remove to run test")]
45-
public void Correctly_handles_completely_invalid_dna_input()
46-
{
47-
Assert.Throws<ArgumentException>(() => RnaTranscription.ToRna("XXX"));
48-
}
49-
50-
[Fact(Skip = "Remove to run test")]
51-
public void Correctly_handles_partially_invalid_dna_input()
52-
{
53-
Assert.Throws<ArgumentException>(() => RnaTranscription.ToRna("ACGTXXXCTTAA"));
54-
}
5536
}

exercises/word-count/WordCountTest.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,16 @@ public void With_quotations()
140140
};
141141
Assert.Equal(expected, actual);
142142
}
143+
144+
[Fact(Skip = "Remove to run test")]
145+
public void Multiple_spaces_not_detected_as_a_word()
146+
{
147+
var actual = WordCount.Countwords(" multiple whitespaces");
148+
var expected = new Dictionary<string, int>
149+
{
150+
["multiple"] = 1,
151+
["whitespaces"] = 1
152+
};
153+
Assert.Equal(expected, actual);
154+
}
143155
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Generators.Exercises
2+
{
3+
public class ArmstrongNumbers : GeneratorExercise
4+
{
5+
}
6+
}

0 commit comments

Comments
 (0)