-
-
Notifications
You must be signed in to change notification settings - Fork 709
armstrong-numbers: add to track #1073
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
70481a9
armstrong-numbers: add to track
sjwarner a6c9002
amrstrong-numbers: add readme
sjwarner 79228f0
armstrong-numbers: add tests and ref solution
sjwarner f92b88d
armstrong-numbers: add reference solution
sjwarner a8225a4
armstrong-numbers: improve tests as per @FridaTveit's suggestions
sjwarner 1450b71
armstrong-numbers: update tests
sjwarner 11e1df4
armstrong-numbers: improve reference solution
sjwarner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
exercises/armstrong-numbers/.meta/src/reference/java/ArmstrongNumbers.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import static java.lang.Math.pow; | ||
|
||
class ArmstrongNumbers { | ||
|
||
boolean isArmstrongNumber(int numberToCheck) { | ||
|
||
int number = numberToCheck; | ||
int calculation = 0; | ||
int length = String.valueOf(number).length(); | ||
|
||
while (number > 0) { | ||
calculation += pow((number % 10), length); | ||
number = number / 10; | ||
} | ||
|
||
return numberToCheck == calculation; | ||
|
||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1.0.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Armstrong Numbers | ||
|
||
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. | ||
|
||
# Running the tests | ||
|
||
You can run all the tests for an exercise by entering | ||
|
||
```sh | ||
$ gradle test | ||
``` | ||
|
||
in your terminal. | ||
|
||
## Source | ||
|
||
Wikipedia [https://en.wikipedia.org/wiki/Narcissistic_number](https://en.wikipedia.org/wiki/Narcissistic_number) | ||
|
||
## Submitting Incomplete Solutions | ||
|
||
It's possible to submit an incomplete solution so you can see how others have completed the exercise. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
apply plugin: "java" | ||
apply plugin: "eclipse" | ||
apply plugin: "idea" | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
testCompile "junit:junit:4.12" | ||
} | ||
|
||
test { | ||
testLogging { | ||
exceptionFormat = 'full' | ||
events = ["passed", "failed", "skipped"] | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
exercises/armstrong-numbers/src/main/java/ArmstrongNumbers.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class ArmstrongNumbers { | ||
|
||
boolean isArmstrongNumber(int numberToCheck) { | ||
|
||
throw new UnsupportedOperationException("Delete this statement and write your own implementation."); | ||
|
||
} | ||
|
||
} |
80 changes: 80 additions & 0 deletions
80
exercises/armstrong-numbers/src/test/java/ArmstrongNumbersTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import org.junit.Before; | ||
import org.junit.Ignore; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.Assert.assertFalse; | ||
|
||
public class ArmstrongNumbersTest { | ||
|
||
private ArmstrongNumbers armstrongNumbers; | ||
|
||
@Before | ||
public void setup() { | ||
armstrongNumbers = new ArmstrongNumbers(); | ||
} | ||
|
||
@Test | ||
public void singleDigitsAreArmstrongNumbers() { | ||
int input = 5; | ||
|
||
assertTrue(armstrongNumbers.isArmstrongNumber(input)); | ||
} | ||
|
||
@Ignore("Remove to run test") | ||
@Test | ||
public void noTwoDigitArmstrongNumbers() { | ||
int input = 10; | ||
|
||
assertFalse(armstrongNumbers.isArmstrongNumber(input)); | ||
} | ||
|
||
@Ignore("Remove to run test") | ||
@Test | ||
public void threeDigitNumberIsArmstrongNumber() { | ||
int input = 153; | ||
|
||
assertTrue(armstrongNumbers.isArmstrongNumber(input)); | ||
} | ||
|
||
@Ignore("Remove to run test") | ||
@Test | ||
public void threeDigitNumberIsNotArmstrongNumber() { | ||
int input = 100; | ||
|
||
assertFalse(armstrongNumbers.isArmstrongNumber(input)); | ||
} | ||
|
||
@Ignore("Remove to run test") | ||
@Test | ||
public void fourDigitNumberIsArmstrongNumber() { | ||
int input = 9474; | ||
|
||
assertTrue(armstrongNumbers.isArmstrongNumber(input)); | ||
} | ||
|
||
@Ignore("Remove to run test") | ||
@Test | ||
public void fourDigitNumberIsNotArmstrongNumber() { | ||
int input = 9475; | ||
|
||
assertFalse(armstrongNumbers.isArmstrongNumber(input)); | ||
} | ||
|
||
@Ignore("Remove to run test") | ||
@Test | ||
public void sevenDigitNumberIsArmstrongNumber() { | ||
int input = 9926315; | ||
|
||
assertTrue(armstrongNumbers.isArmstrongNumber(input)); | ||
} | ||
|
||
@Ignore("Remove to run test") | ||
@Test | ||
public void sevenDigitNumberIsNotArmstrongNumber() { | ||
int input = 9926314; | ||
|
||
assertFalse(armstrongNumbers.isArmstrongNumber(input)); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this should be unlocked by some other exercise? Or do you think it should be always accessible? I think either is fine :)