Skip to content

Commit

Permalink
added first version
Browse files Browse the repository at this point in the history
  • Loading branch information
mdewey committed Feb 16, 2020
1 parent 7c23ee2 commit 95f1a22
Show file tree
Hide file tree
Showing 3 changed files with 197 additions and 65 deletions.
101 changes: 101 additions & 0 deletions IterationTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections.Generic;
using Xunit;
using DotnetIteration.Models;


namespace DotnetIteration
{
Expand All @@ -14,5 +16,104 @@ public void Yelling()
Assert.Equal(expected, Iteration.Yelling(data));
}

[Fact]
public void Double()
{
var expected = new List<int> { 2, 4, 6, 8, 10 };
var data = new List<int> { 1, 2, 3, 4, 5 };
Assert.Equal(expected, Iteration.Double(data));
}

[Fact]
public void StringyIndexes()
{
var data = new List<string> { "how", "now", "brown", "cow" };
var expected = new List<string> { "how is at index 0",
"now is at index 1",
"brown is at index 2",
"cow is at index 3" };
Assert.Equal(expected, Iteration.StringyIndexes(data));
}



[Fact]
public void OnlyTheEvenSurvive()
{
var data = new List<int> { 42, 50, 100, 5, -43, 17, 44 };
var expected = new List<int> { 42, 50, 100, 44 };
Assert.Equal(expected, Iteration.OnlyTheEvenSurvive(data));
}


[Fact]
public void OnlyTheEvenIndexedSurvive()
{
var data = new List<int> { 31, 67, 64, 96, 14, 24, 43, 51, 48, 80, 58, 43, 64, 84, 98, 99, 69, 93, 5, 32, 29, 4, 28, 18, 86, 22, 20, 74, 35, 27, 85, 79, 65, 32, 56, 94, 93, 20, 29, 22, 72 };
var expected = new List<int> { 31, 64, 14, 43, 48, 58, 64, 98, 69, 5, 29, 28, 86, 20, 35, 85, 65, 56, 93, 29, 72 };
Assert.Equal(expected, Iteration.OnlyTheEvenIndexedSurvive(data));
}


[Fact]
public void BestMovieOfTheYear()
{
var data = new List<Movie> {
new Movie { Name= "The Grand Budapest Hotel", Year = 2014, Score= 91 },
new Movie { Name= "Birdman", Year = 2014, Score= 91 },
new Movie { Name= "Transformers: Age of Extinction", Year = 2014, Score= 18 },
new Movie { Name= "Rage", Year = 2014, Score= 14 },
new Movie { Name= "Get Out", Year = 2017, Score= 99 },
new Movie { Name= "Justice League", Year = 2017, Score= 40 },
new Movie { Name= "Ghost in the Shell", Year = 2017, Score= 46 },
new Movie { Name= "The Big Sick", Year = 2017, Score= 98 }

};
var movies2014 = new List<string> { "The Grand Budapest Hotel", "Birdman" };
var movies2017 = new List<string> { "Get Out", "The Big Sick" };
var movies2001 = new List<string> { };
Assert.Equal(movies2014, Iteration.BestMovieOfTheYear(data, 2014));
Assert.Equal(movies2017, Iteration.BestMovieOfTheYear(data, 2017));
Assert.Equal(movies2001, Iteration.BestMovieOfTheYear(data, 2001));
}



[Fact]
public void EveryoneIsOdd()
{
var trueData = new List<int> { 9, 15, 27, 101, 33 };
var falseData = new List<int> { 9, 23, 3, 4, 77 };
Assert.True(Iteration.EveryoneIsOdd(trueData));
Assert.False(Iteration.EveryoneIsOdd(falseData));
}

[Fact]
public void FindTheNeedle()
{
var data = new List<string> { "one", "time", "there was a needle at", "the market" };
var expected = "there was a needle at";
Assert.Equal(expected, Iteration.FindTheNeedle(data));
}


[Fact]
public void FindTheNeedleIndex()
{
var data = new List<string> { "one", "time", "there was a needle at", "the market" };
var expected = 2;
Assert.Equal(expected, Iteration.FindTheNeedleIndex(data));
}


[Fact]
public void SomeoneToLove()
{
var falseData = new List<string> { "how", "now", "brown", "cow" };
var trueData = new List<string> { "how", "now", "blue", "cow" };
Assert.True(Iteration.SomeoneToLove(trueData));
Assert.False(Iteration.SomeoneToLove(falseData));
}

}
}
152 changes: 87 additions & 65 deletions Iterations.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using DotnetIteration.Models;

namespace DotnetIteration
{
Expand All @@ -16,95 +18,115 @@ your work each time you save. If a test is failing you have not yet completed th
the next method is working.
*/

/**
* 1) Define a function named `yelling` that takes an list of
* strings as an argument and returns a new list with all
* the words forced to uppercase
*
/*
* 1) Define a function named `yelling` that takes an list of
* strings as an argument and returns a new list with all
* the words forced to uppercase
*
*/
public static IEnumerable<string> Yelling(List<string> words)
{
return words.Select(s => s.ToUpper());
}
/**
*
* 2) Define a function named `doubleTrouble` that takes an list of
* numbers as an argument and returns a new list with all
* the numbers multiplied by 2
*/

// ...
/*
* 2) Define a function named `doubleTrouble` that takes an list of
* numbers as an argument and returns a new list with all
* the numbers multiplied by 2
*/

public static IEnumerable<int> Double(List<int> numbers)
{
return numbers.Select(s => s * 2);
}
/*
* 3) Define a function stringyIndexes() that takes an list of
* strings as an argument and returns a new list with each string
* suffixed with " is at index X" where X is the index of the element
*/

// ...

* 3) Define a function stringyIndexes() that takes an list of
* strings as an argument and returns a new list with each string
* suffixed with " is at index X" where X is the index of the element
*/
public static IEnumerable<string> StringyIndexes(List<string> data)
{
return data.Select((s, i) => $"{s} is at index {i}");
}
/*
* 4) Define a function onlyTheEvenSurvive that accepts an list of
* numbers and returns only the elements that are even
*/

// ...
* 4) Define a function onlyTheEvenSurvive that accepts an list of
* numbers and returns only the elements that are even
*/

public static IEnumerable<int> OnlyTheEvenSurvive(List<int> data)
{
return data.Where(w => w % 2 == 0);
}
/*
* 5) Define a function onlyTheEvenIndexedSurvive that accepts an list of
* numbers and returns only the elements at indexes that are even
*/

// ...

* 5) Define a function onlyTheEvenIndexedSurvive that accepts an list of
* numbers and returns only the elements at indexes that are even
*/
public static IEnumerable<int> OnlyTheEvenIndexedSurvive(List<int> data)
{
return data.Where((w, i) => i % 2 == 0);
}
/*
* 6) Define a function bestMoviesOfTheYear that accepts an list of
* movie objects AND a year and returns the names of movies that are
* from that year AND have a score more than 90
*
* A movie object looks like this:
*
* {
* name: "Get Out",
* year: "2017",
* score: 99
* }
*/

// ...
* 6) Define a function bestMoviesOfTheYear that accepts an list of
* movie objects AND a year and returns the names of movies that are
* from that year AND have a score more than 90
*
* A movie object looks like this:
*
* {
* name: "Get Out",
* year: "2017",
* score: 99
* }
*/
public static IEnumerable<string> BestMovieOfTheYear(List<Movie> data, int year)
{
return data.Where(w => w.Year == year && w.Score >= 90).Select(s => s.Name);
}

/*
* 7) Define a function everyoneIsOdd that accepts an list of
* numbers and returns true if every element of the list is
* odd.
*/

// ...
* 7) Define a function everyoneIsOdd that accepts an list of
* numbers and returns true if every element of the list is
* odd.
*/

public static bool EveryoneIsOdd(List<int> data)
{
return data.All(a => a % 2 == 1);
}
/*
* 8) Define a function findTheNeedle that accepts an list of
* strings and returns the one string that contains the word
* `needle` inside
*/
* 8) Define a function findTheNeedle that accepts an list of
* strings and returns the one string that contains the word
* `needle`
*/
internal static string FindTheNeedle(List<string> data)
{
return data.FirstOrDefault(f => f.Contains("needle"));
}

// ...

/*
* 9) Define a function findTheNeedleIndex that accepts an list of
* strings and returns the index of the string that contains
* the word `needle` inside
*/
* 9) Define a function findTheNeedleIndex that accepts an list of
* strings and returns the index of the string that contains
* the word `needle` inside
*/

// ...
public static int FindTheNeedleIndex(List<string> data)
{
return data.FindIndex(f => f.Contains("needle"));
}

/*
*` 10) Define a function someoneToLove that accepts an list of
* strings and returns true if at least one string is exactly
* four characters long
*/
*` 10) Define a function someoneToLove that accepts an list of
* strings and returns true if at least one string is exactly
* four characters long
*/

// ...

internal static bool SomeoneToLove(List<string> data)
{
return data.Any(a => a.Length == 4);
}

/*
* 11) Define a function mapYourself that accepts an list of
Expand Down
9 changes: 9 additions & 0 deletions Models/Movie.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace DotnetIteration.Models
{
public class Movie
{
public string Name { get; set; }
public int Score { get; set; }
public int Year { get; set; }
}
}

0 comments on commit 95f1a22

Please sign in to comment.