This commit is contained in:
Ben Harris 2018-03-12 15:21:16 -04:00
parent c6720da38d
commit 914532c7b5
14 changed files with 425 additions and 21 deletions

View File

@ -0,0 +1,15 @@
using System;
using System.Linq;
public static class ArmstrongNumbers
{
public static bool IsArmstrongNumber(int number)
{
var digits = number.ToString().Select(digit => int.Parse(digit.ToString())).ToList();
var num_digits = digits.Count;
var sum = 0;
foreach (var digit in digits)
sum += (int) Math.Pow(digit, num_digits);
return number == sum;
}
}

View File

@ -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>

View File

@ -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]
public void There_are_no_2_digit_armstrong_numbers()
{
Assert.False(ArmstrongNumbers.IsArmstrongNumber(10));
}
[Fact]
public void Three_digit_number_that_is_an_armstrong_number()
{
Assert.True(ArmstrongNumbers.IsArmstrongNumber(153));
}
[Fact]
public void Three_digit_number_that_is_not_an_armstrong_number()
{
Assert.False(ArmstrongNumbers.IsArmstrongNumber(100));
}
[Fact]
public void Four_digit_number_that_is_an_armstrong_number()
{
Assert.True(ArmstrongNumbers.IsArmstrongNumber(9474));
}
[Fact]
public void Four_digit_number_that_is_not_an_armstrong_number()
{
Assert.False(ArmstrongNumbers.IsArmstrongNumber(9475));
}
[Fact]
public void Seven_digit_number_that_is_an_armstrong_number()
{
Assert.True(ArmstrongNumbers.IsArmstrongNumber(9926315));
}
[Fact]
public void Seven_digit_number_that_is_not_an_armstrong_number()
{
Assert.False(ArmstrongNumbers.IsArmstrongNumber(9926314));
}
}

View File

@ -0,0 +1,20 @@
# 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.
## 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.

View File

@ -0,0 +1,17 @@
using System;
public static class CollatzConjecture
{
// If n is even, divide n by 2 to get n / 2.
// If n is odd, multiply n by 3 and add 1 to get 3n + 1.
public static int Steps(int number)
{
if (number < 1) throw new ArgumentException();
int cnt = 0;
while (number != 1) {
number = number % 2 == 0 ? number / 2 : 3 * number + 1;
cnt ++;
}
return cnt;
}
}

View File

@ -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>

View File

@ -0,0 +1,43 @@
// This file was auto-generated based on version 1.1.1 of the canonical data.
using Xunit;
using System;
public class CollatzConjectureTest
{
[Fact]
public void Zero_steps_for_one()
{
Assert.Equal(0, CollatzConjecture.Steps(1));
}
[Fact]
public void Divide_if_even()
{
Assert.Equal(4, CollatzConjecture.Steps(16));
}
[Fact]
public void Even_and_odd_steps()
{
Assert.Equal(9, CollatzConjecture.Steps(12));
}
[Fact]
public void Large_number_of_even_and_odd_steps()
{
Assert.Equal(152, CollatzConjecture.Steps(1000000));
}
[Fact]
public void Zero_is_an_error()
{
Assert.Throws<ArgumentException>(() => CollatzConjecture.Steps(0));
}
[Fact]
public void Negative_value_is_an_error()
{
Assert.Throws<ArgumentException>(() => CollatzConjecture.Steps(-15));
}
}

View File

@ -0,0 +1,35 @@
# Collatz Conjecture
The Collatz Conjecture or 3x+1 problem can be summarized as follows:
Take any positive integer n. If n is even, divide n by 2 to get n / 2. If n is
odd, multiply n by 3 and add 1 to get 3n + 1. Repeat the process indefinitely.
The conjecture states that no matter which number you start with, you will
always reach 1 eventually.
Given a number n, return the number of steps required to reach 1.
## Examples
Starting with n = 12, the steps would be as follows:
0. 12
1. 6
2. 3
3. 10
4. 5
5. 16
6. 8
7. 4
8. 2
9. 1
Resulting in 9 steps. So for input n = 12, the return value would be 9.
## Source
An unsolved problem in mathematics named after mathematician Lothar Collatz [https://en.wikipedia.org/wiki/3x_%2B_1_problem](https://en.wikipedia.org/wiki/3x_%2B_1_problem)
## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

View File

@ -1,20 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
public class School
{
private Dictionary<int, List<string>> roster = new Dictionary<int, List<string>>();
public void Add(string student, int grade)
{
throw new NotImplementedException("You need to implement this function.");
if (roster.ContainsKey(grade))
roster[grade].Add(student);
else
roster.Add(grade, new List<string> {student});
}
public IEnumerable<string> Roster()
{
throw new NotImplementedException("You need to implement this function.");
}
=> roster.Keys
.OrderBy(g => g)
.SelectMany(g => Grade(g))
.ToList();
public IEnumerable<string> Grade(int grade)
{
throw new NotImplementedException("You need to implement this function.");
}
}
=> roster.ContainsKey(grade)
? roster[grade].OrderBy(g => g).ToList()
: new List<string>();
}

View File

@ -14,21 +14,21 @@ public class GradeSchoolTest
Assert.Equal(expected, actual);
}
[Fact(Skip = "Remove to run test")]
[Fact]
public void Adding_more_students_adds_them_to_the_sorted_roster()
{
var school = new School();
school.Add("Blair", 2);
school.Add("James", 2);
school.Add("Paul", 2);
var actual = school.Roster();
var expected = new[] { "Blair", "James", "Paul" };
Assert.Equal(expected, actual );
Assert.Equal(expected, actual);
}
[Fact(Skip = "Remove to run test")]
[Fact]
public void Adding_students_to_different_grades_adds_them_to_the_same_sorted_roster()
{
var school = new School();
@ -41,31 +41,31 @@ public class GradeSchoolTest
Assert.Equal(expected, actual);
}
[Fact(Skip = "Remove to run test")]
[Fact]
public void Grade_returns_the_students_in_that_grade_in_alphabetical_order()
{
var school = new School();
school.Add("Franklin", 5);
school.Add("Bradley", 5);
school.Add("Jeff", 1);
var actual = school.Grade(5);
var expected = new[] { "Bradley", "Franklin" };
Assert.Equal(expected, actual);
}
[Fact(Skip = "Remove to run test")]
[Fact]
public void Grade_returns_an_empty_list_if_there_are_no_students_in_that_grade()
{
var school = new School();
var actual = school.Grade(1);
var actual = school.Grade(1);
Assert.Empty(actual);
}
[Fact(Skip = "Remove to run test")]
[Fact]
public void Student_names_with_grades_are_displayed_in_the_same_sorted_roster()
{
var school = new School();
@ -76,10 +76,10 @@ public class GradeSchoolTest
school.Add("Alex", 2);
school.Add("Jim", 3);
school.Add("Charlie", 1);
var actual = school.Roster();
var expected = new[] { "Anna", "Barb", "Charlie", "Alex", "Peter", "Zoe", "Jim" };
Assert.Equal(expected, actual);
}
}
}

View File

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
public enum Plant
{
Violets,
Radishes,
Clover,
Grass
}
public class KindergartenGarden
{
public KindergartenGarden(string diagram)
{
}
public KindergartenGarden(string diagram, IEnumerable<string> students)
{
}
public IEnumerable<Plant> Plants(string student)
{
throw new NotImplementedException("You need to implement this function.");
}
}

View File

@ -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>

View File

@ -0,0 +1,69 @@
// This file was auto-generated based on version 1.0.0 of the canonical data.
using Xunit;
public class KindergartenGardenTest
{
[Fact]
public void Partial_garden_garden_with_single_student()
{
var sut = new KindergartenGarden("RC\nGG");
Assert.Equal(new[] { Plant.Radishes, Plant.Clover, Plant.Grass, Plant.Grass }, sut.Plants("Alice"));
}
[Fact(Skip = "Remove to run test")]
public void Partial_garden_different_garden_with_single_student()
{
var sut = new KindergartenGarden("VC\nRC");
Assert.Equal(new[] { Plant.Violets, Plant.Clover, Plant.Radishes, Plant.Clover }, sut.Plants("Alice"));
}
[Fact(Skip = "Remove to run test")]
public void Partial_garden_garden_with_two_students()
{
var sut = new KindergartenGarden("VVCG\nVVRC");
Assert.Equal(new[] { Plant.Clover, Plant.Grass, Plant.Radishes, Plant.Clover }, sut.Plants("Bob"));
}
[Fact(Skip = "Remove to run test")]
public void Partial_garden_multiple_students_for_the_same_garden_with_three_students_second_students_garden()
{
var sut = new KindergartenGarden("VVCCGG\nVVCCGG");
Assert.Equal(new[] { Plant.Clover, Plant.Clover, Plant.Clover, Plant.Clover }, sut.Plants("Bob"));
}
[Fact(Skip = "Remove to run test")]
public void Partial_garden_multiple_students_for_the_same_garden_with_three_students_third_students_garden()
{
var sut = new KindergartenGarden("VVCCGG\nVVCCGG");
Assert.Equal(new[] { Plant.Grass, Plant.Grass, Plant.Grass, Plant.Grass }, sut.Plants("Charlie"));
}
[Fact(Skip = "Remove to run test")]
public void Full_garden_first_students_garden()
{
var sut = new KindergartenGarden("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV");
Assert.Equal(new[] { Plant.Violets, Plant.Radishes, Plant.Violets, Plant.Radishes }, sut.Plants("Alice"));
}
[Fact(Skip = "Remove to run test")]
public void Full_garden_second_students_garden()
{
var sut = new KindergartenGarden("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV");
Assert.Equal(new[] { Plant.Clover, Plant.Grass, Plant.Clover, Plant.Clover }, sut.Plants("Bob"));
}
[Fact(Skip = "Remove to run test")]
public void Full_garden_second_to_last_students_garden()
{
var sut = new KindergartenGarden("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV");
Assert.Equal(new[] { Plant.Grass, Plant.Clover, Plant.Clover, Plant.Grass }, sut.Plants("Kincaid"));
}
[Fact(Skip = "Remove to run test")]
public void Full_garden_last_students_garden()
{
var sut = new KindergartenGarden("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV");
Assert.Equal(new[] { Plant.Grass, Plant.Violets, Plant.Clover, Plant.Violets }, sut.Plants("Larry"));
}
}

View File

@ -0,0 +1,68 @@
# Kindergarten Garden
Given a diagram, determine which plants each child in the kindergarten class is
responsible for.
The kindergarten class is learning about growing plants. The teacher
thought it would be a good idea to give them actual seeds, plant them in
actual dirt, and grow actual plants.
They've chosen to grow grass, clover, radishes, and violets.
To this end, the children have put little cups along the window sills, and
planted one type of plant in each cup, choosing randomly from the available
types of seeds.
```text
[window][window][window]
........................ # each dot represents a cup
........................
```
There are 12 children in the class:
- Alice, Bob, Charlie, David,
- Eve, Fred, Ginny, Harriet,
- Ileana, Joseph, Kincaid, and Larry.
Each child gets 4 cups, two on each row. Their teacher assigns cups to
the children alphabetically by their names.
The following diagram represents Alice's plants:
```text
[window][window][window]
VR......................
RG......................
```
In the first row, nearest the windows, she has a violet and a radish. In the
second row she has a radish and some grass.
Your program will be given the plants from left-to-right starting with
the row nearest the windows. From this, it should be able to determine
which plants belong to each student.
For example, if it's told that the garden looks like so:
```text
[window][window][window]
VRCGVVRVCGGCCGVRGCVCGCGV
VRCCCGCRRGVCGCRVVCVGCGCV
```
Then if asked for Alice's plants, it should provide:
- Violets, radishes, violets, radishes
While asking for Bob's plants would yield:
- Clover, grass, clover, clover
## Source
Random musings during airplane trip. [http://jumpstartlab.com](http://jumpstartlab.com)
## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.