Created
December 14, 2016 20:09
-
-
Save yeedle/db6fd6d1171106f74421cb45bf76c122 to your computer and use it in GitHub Desktop.
asgn06 created by Yeedle - https://repl.it/Eo5Y/37
This file contains 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
using System; | |
using System.Linq; | |
using System.Collections.Generic; | |
public static class ExtnesionMethods { | |
public static IEnumerable<T> MaxOverPrevious<T, S>(this IEnumerable<T> enumerable, Func<T, S> comparable) where S : IComparable<S> | |
{ | |
if (!enumerable.Any()) yield break; | |
// first element will always be max over previous | |
S max = comparable(enumerable.First()); | |
yield return enumerable.First(); | |
using (var enumerator = enumerable.GetEnumerator()) | |
{ | |
while(enumerator.MoveNext()) | |
{ | |
if (comparable(enumerator.Current).CompareTo(max) > 0) | |
{ | |
max = comparable(enumerator.Current); | |
yield return enumerator.Current; | |
} | |
} | |
} | |
} | |
public static IEnumerable<T> MaxOverPrevious<T>(this IEnumerable<T> enumerable) where T : IComparable<T> | |
{ | |
return enumerable.MaxOverPrevious(e => e); | |
} | |
public static IEnumerable<T> LocalMaxima<T, S>(this IEnumerable<T> enumerable, Func<T, S> comparable) where S : IComparable<S> | |
{ | |
// empty list returns nothing | |
if (!enumerable.Any()) yield break; | |
using (var enumerator = enumerable.GetEnumerator()) | |
{ | |
T prev , curr, next; | |
enumerator.MoveNext(); | |
prev = enumerator.Current; | |
if(!enumerator.MoveNext()) //1 item list | |
{ | |
yield return prev; | |
yield break; | |
} | |
else // 2 or more items | |
{ | |
curr = enumerator.Current; | |
if (comparable(prev).CompareTo(comparable(curr)) > 0) | |
yield return prev; | |
} | |
while (enumerator.MoveNext()) | |
{ | |
next = enumerator.Current; | |
if (comparable(curr).CompareTo(comparable(prev)) > 0 && comparable(curr).CompareTo(comparable(next)) > 0){ | |
yield return curr; | |
} | |
prev = curr; | |
curr = next; | |
} | |
if (comparable(curr).CompareTo(comparable(prev)) > 0) | |
yield return curr; | |
} | |
} | |
public static IEnumerable<T> LocalMaxima<T>(this IEnumerable<T> enumerable) where T : IComparable<T> | |
{ | |
return enumerable.LocalMaxima(e => e); | |
} | |
public static bool AtLeastK<T>(this IEnumerable<T> enumerable, int k, Func<T, bool> predicate) | |
{ | |
return enumerable.Where(e => predicate(e)).Take(k).Count() >= k; | |
} | |
public static bool AtLeastHalf<T>(this IEnumerable<T> enumerable, Func<T, bool> predicate) | |
{ | |
return enumerable.Where(e => predicate(e)).Count() >= enumerable.Count()/2; | |
} | |
public static bool AtLeastHalfLazy<T>(this IEnumerable<T> enumerable, Func<T, bool> predicate) | |
{ | |
int includedCount = enumerable.Where(e => predicate(e)).Count(); | |
int excludedCount = 0; | |
using (var enumerator = enumerable.GetEnumerator()) | |
{ | |
while(enumerator.MoveNext() && excludedCount/2 < includedCount) | |
{ | |
if (!predicate(enumerator.Current)) excludedCount++; | |
} | |
} | |
return includedCount >= excludedCount; | |
} | |
} |
This file contains 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
using System; | |
using System.Linq; | |
using System.Collections.Generic; | |
class Program { | |
public static void Main (string[] args) { | |
List<int> numbers = new List<int>(){ 5, 4, 6, 1, 3, 2, 8}; | |
List<Employee> employees = new List<Employee> { | |
new Employee() { Name = "Sam", Salary = 50000, Level = "Sr.", Manager = "82114" }, | |
new Employee() { Name = "Harold", Salary = 65000, Level = "Sr.", Manager = "92081" }, | |
new Employee() { Name = "Freidy", Salary = 45000, Level = "Jr.", Manager = "82114" }, | |
new Employee() { Name = "Tucker", Salary = 48500, Level = "Mid", Manager = "99087" }, | |
new Employee() { Name = "Phyllis", Salary = 50000, Level = "Jr.", Manager = "82114" }, | |
new Employee() { Name = "Deborah", Salary = 55000, Level = "Mid", Manager = "92081" }, | |
new Employee() { Name = "Frank", Salary = 62500, Level = "Sr.", Manager = "92081" }, | |
new Employee() { Name = "Shirley", Salary = 50000, Level = "Jr.", Manager = "82114" }, | |
new Employee() { Name = "Mortimer", Salary = 48500, Level = "Sr.", Manager = "82114" } | |
}; | |
Console.Write("List<int> numbers = new List<int>() {5, 4, 6, 1, 3, 2, 8};\n"); | |
Console.WriteLine("\ncall: numbers.MaxOverPrevious();"); | |
Console.WriteLine("result: {" + String.Join(",", numbers.MaxOverPrevious()) + "}"); | |
Console.WriteLine("\ncall: numbers.MaxOverPrevious(e => -e); // using it as MinOverPrevious"); | |
Console.WriteLine("result: {" + String.Join(",", numbers.MaxOverPrevious(e => -e)) + "}"); | |
Console.WriteLine("\ncall: numbers.LocalMaxima();"); | |
Console.WriteLine("result: {"+String.Join(",", numbers.LocalMaxima())+"}"); | |
Console.WriteLine("\ncall: numbers.LocalMaxima(e => -e) // using it as LocalMinima"); | |
Console.WriteLine("result: {" + String.Join(",", numbers.LocalMaxima(e => -e)) +"}"); | |
Console.WriteLine("\ncall: numbers.AtLeastK(2, e => e%2 == 0);"); | |
Console.WriteLine("result: " + numbers.AtLeastK(2, e => e%2 == 0)); | |
Console.WriteLine("\ncall: numbers.AtLeastHalf(e => e%2 ==0);"); | |
Console.WriteLine("result: " + numbers.AtLeastHalf(e => e%2 ==0)); | |
Console.WriteLine("employees = \n{\n" + String.Join(",\n", employees.Select(e => "\t"+e.Name + "-" + e.Salary)) + "\n}"); | |
Console.WriteLine("\ncall: employees.MaxOverPrevious(e => e.Salary);"); | |
Console.WriteLine("result: {" + String.Join(",", employees.MaxOverPrevious(e => e.Salary).Select(e => e.Name +"-"+e.Salary)) + "}"); | |
Console.WriteLine("\ncall: employees.LocalMaxima(e => e.Salary);"); | |
Console.WriteLine("result: {" + String.Join(",", employees.LocalMaxima(e => e.Salary)) + "}"); | |
Console.WriteLine("\ncall: employees.AtLeastK(2, e => e.Level == \"Sr.\" && e.Salary > 60000);"); | |
Console.WriteLine("result: " +employees.AtLeastK(2, e => e.Level == "Sr." && e.Salary > 60000)); | |
Console.WriteLine("\ncall: employees.AtLeastHalf(e => e.Level == \"Sr.\");"); | |
Console.WriteLine("result: " + employees.AtLeastHalf(e => e.Level == "Sr.")); | |
} | |
} | |
class Employee | |
{ | |
public String Name { get; set; } | |
public double Salary { get; set; } | |
public String Level { get; set; } | |
public String Manager { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment