From 914532c7b5fa9b27ca9fd5a054f218926990b701 Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Mon, 12 Mar 2018 15:21:16 -0400 Subject: [PATCH] csharp --- csharp/armstrong-numbers/ArmstrongNumbers.cs | 15 ++++ .../armstrong-numbers/ArmstrongNumbers.csproj | 17 +++++ .../armstrong-numbers/ArmstrongNumbersTest.cs | 54 +++++++++++++++ csharp/armstrong-numbers/README.md | 20 ++++++ .../collatz-conjecture/CollatzConjecture.cs | 17 +++++ .../CollatzConjecture.csproj | 17 +++++ .../CollatzConjectureTest.cs | 43 ++++++++++++ csharp/collatz-conjecture/README.md | 35 ++++++++++ csharp/grade-school/GradeSchool.cs | 24 ++++--- csharp/grade-school/GradeSchoolTest.cs | 24 +++---- .../kindergarten-garden/KindergartenGarden.cs | 26 +++++++ .../KindergartenGarden.csproj | 17 +++++ .../KindergartenGardenTest.cs | 69 +++++++++++++++++++ csharp/kindergarten-garden/README.md | 68 ++++++++++++++++++ 14 files changed, 425 insertions(+), 21 deletions(-) create mode 100644 csharp/armstrong-numbers/ArmstrongNumbers.cs create mode 100644 csharp/armstrong-numbers/ArmstrongNumbers.csproj create mode 100644 csharp/armstrong-numbers/ArmstrongNumbersTest.cs create mode 100644 csharp/armstrong-numbers/README.md create mode 100644 csharp/collatz-conjecture/CollatzConjecture.cs create mode 100644 csharp/collatz-conjecture/CollatzConjecture.csproj create mode 100644 csharp/collatz-conjecture/CollatzConjectureTest.cs create mode 100644 csharp/collatz-conjecture/README.md create mode 100644 csharp/kindergarten-garden/KindergartenGarden.cs create mode 100644 csharp/kindergarten-garden/KindergartenGarden.csproj create mode 100644 csharp/kindergarten-garden/KindergartenGardenTest.cs create mode 100644 csharp/kindergarten-garden/README.md diff --git a/csharp/armstrong-numbers/ArmstrongNumbers.cs b/csharp/armstrong-numbers/ArmstrongNumbers.cs new file mode 100644 index 0000000..fc82bb6 --- /dev/null +++ b/csharp/armstrong-numbers/ArmstrongNumbers.cs @@ -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; + } +} diff --git a/csharp/armstrong-numbers/ArmstrongNumbers.csproj b/csharp/armstrong-numbers/ArmstrongNumbers.csproj new file mode 100644 index 0000000..082b3bb --- /dev/null +++ b/csharp/armstrong-numbers/ArmstrongNumbers.csproj @@ -0,0 +1,17 @@ + + + + netcoreapp2.0 + + + + + + + + + + + + + diff --git a/csharp/armstrong-numbers/ArmstrongNumbersTest.cs b/csharp/armstrong-numbers/ArmstrongNumbersTest.cs new file mode 100644 index 0000000..183b720 --- /dev/null +++ b/csharp/armstrong-numbers/ArmstrongNumbersTest.cs @@ -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)); + } +} \ No newline at end of file diff --git a/csharp/armstrong-numbers/README.md b/csharp/armstrong-numbers/README.md new file mode 100644 index 0000000..29be991 --- /dev/null +++ b/csharp/armstrong-numbers/README.md @@ -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. diff --git a/csharp/collatz-conjecture/CollatzConjecture.cs b/csharp/collatz-conjecture/CollatzConjecture.cs new file mode 100644 index 0000000..5ce0bff --- /dev/null +++ b/csharp/collatz-conjecture/CollatzConjecture.cs @@ -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; + } +} diff --git a/csharp/collatz-conjecture/CollatzConjecture.csproj b/csharp/collatz-conjecture/CollatzConjecture.csproj new file mode 100644 index 0000000..c4945ac --- /dev/null +++ b/csharp/collatz-conjecture/CollatzConjecture.csproj @@ -0,0 +1,17 @@ + + + + netcoreapp2.0 + + + + + + + + + + + + + \ No newline at end of file diff --git a/csharp/collatz-conjecture/CollatzConjectureTest.cs b/csharp/collatz-conjecture/CollatzConjectureTest.cs new file mode 100644 index 0000000..f582040 --- /dev/null +++ b/csharp/collatz-conjecture/CollatzConjectureTest.cs @@ -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(() => CollatzConjecture.Steps(0)); + } + + [Fact] + public void Negative_value_is_an_error() + { + Assert.Throws(() => CollatzConjecture.Steps(-15)); + } +} \ No newline at end of file diff --git a/csharp/collatz-conjecture/README.md b/csharp/collatz-conjecture/README.md new file mode 100644 index 0000000..afc2216 --- /dev/null +++ b/csharp/collatz-conjecture/README.md @@ -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. diff --git a/csharp/grade-school/GradeSchool.cs b/csharp/grade-school/GradeSchool.cs index 9d9ccb8..c83b96f 100644 --- a/csharp/grade-school/GradeSchool.cs +++ b/csharp/grade-school/GradeSchool.cs @@ -1,20 +1,26 @@ -using System; using System.Collections.Generic; +using System.Linq; public class School { + private Dictionary> roster = new Dictionary>(); + 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 {student}); } public IEnumerable Roster() - { - throw new NotImplementedException("You need to implement this function."); - } + => roster.Keys + .OrderBy(g => g) + .SelectMany(g => Grade(g)) + .ToList(); public IEnumerable Grade(int grade) - { - throw new NotImplementedException("You need to implement this function."); - } -} \ No newline at end of file + => roster.ContainsKey(grade) + ? roster[grade].OrderBy(g => g).ToList() + : new List(); +} diff --git a/csharp/grade-school/GradeSchoolTest.cs b/csharp/grade-school/GradeSchoolTest.cs index 72402b6..2115ab4 100644 --- a/csharp/grade-school/GradeSchoolTest.cs +++ b/csharp/grade-school/GradeSchoolTest.cs @@ -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); } -} \ No newline at end of file +} diff --git a/csharp/kindergarten-garden/KindergartenGarden.cs b/csharp/kindergarten-garden/KindergartenGarden.cs new file mode 100644 index 0000000..aa5fd44 --- /dev/null +++ b/csharp/kindergarten-garden/KindergartenGarden.cs @@ -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 students) + { + } + + public IEnumerable Plants(string student) + { + throw new NotImplementedException("You need to implement this function."); + } +} \ No newline at end of file diff --git a/csharp/kindergarten-garden/KindergartenGarden.csproj b/csharp/kindergarten-garden/KindergartenGarden.csproj new file mode 100644 index 0000000..f9dbcdc --- /dev/null +++ b/csharp/kindergarten-garden/KindergartenGarden.csproj @@ -0,0 +1,17 @@ + + + + netcoreapp2.0 + + + + + + + + + + + + + diff --git a/csharp/kindergarten-garden/KindergartenGardenTest.cs b/csharp/kindergarten-garden/KindergartenGardenTest.cs new file mode 100644 index 0000000..550d98b --- /dev/null +++ b/csharp/kindergarten-garden/KindergartenGardenTest.cs @@ -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")); + } +} \ No newline at end of file diff --git a/csharp/kindergarten-garden/README.md b/csharp/kindergarten-garden/README.md new file mode 100644 index 0000000..fa9baa8 --- /dev/null +++ b/csharp/kindergarten-garden/README.md @@ -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.