Skip to content

Add ArmstrongNumbers exercise #501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@
"unlocked_by": "sum-of-multiples",
"uuid": "c4efbf8a-8e76-4700-807d-830a4938f4d0"
},
{
"core": false,
"difficulty": 2,
"slug": "armstrong-numbers",
"topics": [
"mathematics"
],
"unlocked_by": "sum-of-multiples",
"uuid": "8150604d-4cdc-414a-a523-dd65ac536f0e"
},
{
"core": true,
"difficulty": 3,
Expand Down
8 changes: 7 additions & 1 deletion exercises/Exercises.sln
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27004.2008
VisualStudioVersion = 15.0.27004.2009
MinimumVisualStudioVersion = 15.0.26124.0
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Accumulate", "accumulate\Accumulate.csproj", "{F16C0EE1-6923-4328-B015-363CF24FF867}"
EndProject
Expand Down Expand Up @@ -224,6 +224,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IsbnVerifier", "isbn-verifi
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ReverseString", "reverse-string\ReverseString.csproj", "{D2105979-EE3B-432B-8016-172BA949DE2F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ArmstrongNumbers", "armstrong-numbers\ArmstrongNumbers.csproj", "{7015C2C4-4050-4631-9394-7EAF19AB0191}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -674,6 +676,10 @@ Global
{D2105979-EE3B-432B-8016-172BA949DE2F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D2105979-EE3B-432B-8016-172BA949DE2F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D2105979-EE3B-432B-8016-172BA949DE2F}.Release|Any CPU.Build.0 = Release|Any CPU
{7015C2C4-4050-4631-9394-7EAF19AB0191}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7015C2C4-4050-4631-9394-7EAF19AB0191}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7015C2C4-4050-4631-9394-7EAF19AB0191}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7015C2C4-4050-4631-9394-7EAF19AB0191}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
9 changes: 9 additions & 0 deletions exercises/armstrong-numbers/ArmstrongNumbers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

public static class ArmstrongNumbers
{
public static bool IsArmstrongNumber(int number)
{
throw new NotImplementedException("You need to implement this function.");
}
}
17 changes: 17 additions & 0 deletions exercises/armstrong-numbers/ArmstrongNumbers.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Remove="Example.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
</ItemGroup>

</Project>
54 changes: 54 additions & 0 deletions exercises/armstrong-numbers/ArmstrongNumbersTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// This file was auto-generated based on version 1.0.0 of the canonical data.

using Xunit;

public class ArmstrongNumbersTest
{
[Fact]
public void Single_digit_numbers_are_armstrong_numbers()
{
Assert.True(ArmstrongNumbers.IsArmstrongNumber(5));
}

[Fact(Skip = "Remove to run test")]
public void There_are_no_2_digit_armstrong_numbers()
{
Assert.False(ArmstrongNumbers.IsArmstrongNumber(10));
}

[Fact(Skip = "Remove to run test")]
public void Three_digit_number_that_is_an_armstrong_number()
{
Assert.True(ArmstrongNumbers.IsArmstrongNumber(153));
}

[Fact(Skip = "Remove to run test")]
public void Three_digit_number_that_is_not_an_armstrong_number()
{
Assert.False(ArmstrongNumbers.IsArmstrongNumber(100));
}

[Fact(Skip = "Remove to run test")]
public void Four_digit_number_that_is_an_armstrong_number()
{
Assert.True(ArmstrongNumbers.IsArmstrongNumber(9474));
}

[Fact(Skip = "Remove to run test")]
public void Four_digit_number_that_is_not_an_armstrong_number()
{
Assert.False(ArmstrongNumbers.IsArmstrongNumber(9475));
}

[Fact(Skip = "Remove to run test")]
public void Seven_digit_number_that_is_an_armstrong_number()
{
Assert.True(ArmstrongNumbers.IsArmstrongNumber(9926315));
}

[Fact(Skip = "Remove to run test")]
public void Seven_digit_number_that_is_not_an_armstrong_number()
{
Assert.False(ArmstrongNumbers.IsArmstrongNumber(9926314));
}
}
18 changes: 18 additions & 0 deletions exercises/armstrong-numbers/Example.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;

public static class ArmstrongNumbers
{
public static bool IsArmstrongNumber(int number)
{
var numString = number.ToString();
var length = numString.Length;

double total = 0;
for (int i = 0; i < length; i++)
{
total += Math.Pow(int.Parse(numString[i].ToString()), length);
}

return number == (int)total;
}
}
10 changes: 10 additions & 0 deletions exercises/armstrong-numbers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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.

For example:

- 9 is an Armstrong number, because `9 = 9^1 = 9`
- 10 is *not* an Armstrong number, because `10 != 1^2 + 0^2 = 2`
- 153 is an Armstrong number, because: `153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153`
- 154 is *not* an Armstrong number, because: `154 != 1^3 + 5^3 + 4^3 = 1 + 125 + 64 = 190`

Write some code to determine whether a number is an Armstrong number.
4 changes: 2 additions & 2 deletions exercises/bob/BobTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This file was auto-generated based on version 1.0.0 of the canonical data.
// This file was auto-generated based on version 1.1.0 of the canonical data.

using Xunit;

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

[Fact(Skip = "Remove to run test")]
Expand Down
2 changes: 2 additions & 0 deletions exercises/bob/Example.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ public static string Response(string statement)
{
if (IsSilence(statement))
return "Fine. Be that way!";
if (IsYelling(statement) && IsQuestion(statement))
return "Calm down, I know what I'm doing!";
if (IsYelling(statement))
return "Whoa, chill out!";
if (IsQuestion(statement))
Expand Down
9 changes: 8 additions & 1 deletion exercises/book-store/BookStoreTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This file was auto-generated based on version 1.0.1 of the canonical data.
// This file was auto-generated based on version 1.1.0 of the canonical data.

using Xunit;

Expand Down Expand Up @@ -94,4 +94,11 @@ public void Three_each_of_first_2_books_and_2_each_of_remaining_books()
var basket = new[] { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1, 2 };
Assert.Equal(75.2, BookStore.Total(basket));
}

[Fact(Skip = "Remove to run test")]
public void Four_groups_of_four_are_cheaper_than_two_groups_each_of_five_and_three()
{
var basket = new[] { 1, 1, 2, 2, 3, 3, 4, 5, 1, 1, 2, 2, 3, 3, 4, 5 };
Assert.Equal(102.4, BookStore.Total(basket));
}
}
21 changes: 1 addition & 20 deletions exercises/rna-transcription/RnaTranscriptionTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// This file was auto-generated based on version 1.0.1 of the canonical data.
// This file was auto-generated based on version 1.1.0 of the canonical data.

using Xunit;
using System;

public class RnaTranscriptionTest
{
Expand Down Expand Up @@ -34,22 +33,4 @@ public void Rna_complement()
{
Assert.Equal("UGCACCAGAAUU", RnaTranscription.ToRna("ACGTGGTCTTAA"));
}

[Fact(Skip = "Remove to run test")]
public void Correctly_handles_invalid_input_rna_instead_of_dna_()
{
Assert.Throws<ArgumentException>(() => RnaTranscription.ToRna("U"));
}

[Fact(Skip = "Remove to run test")]
public void Correctly_handles_completely_invalid_dna_input()
{
Assert.Throws<ArgumentException>(() => RnaTranscription.ToRna("XXX"));
}

[Fact(Skip = "Remove to run test")]
public void Correctly_handles_partially_invalid_dna_input()
{
Assert.Throws<ArgumentException>(() => RnaTranscription.ToRna("ACGTXXXCTTAA"));
}
}
12 changes: 12 additions & 0 deletions exercises/word-count/WordCountTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,16 @@ public void With_quotations()
};
Assert.Equal(expected, actual);
}

[Fact(Skip = "Remove to run test")]
public void Multiple_spaces_not_detected_as_a_word()
{
var actual = WordCount.Countwords(" multiple whitespaces");
var expected = new Dictionary<string, int>
{
["multiple"] = 1,
["whitespaces"] = 1
};
Assert.Equal(expected, actual);
}
}
6 changes: 6 additions & 0 deletions generators/Exercises/ArmstrongNumbers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Generators.Exercises
{
public class ArmstrongNumbers : GeneratorExercise
{
}
}