Created
April 27, 2016 17:11
-
-
Save vadim8kiselev/4bb2f2dd952fe1ca8ca12f25000f0672 to your computer and use it in GitHub Desktop.
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.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Diagnostics; | |
namespace LINQ | |
{ | |
public static class ArrayExtensions | |
{ | |
public static int Total(this int[] param) | |
{ | |
int sum = 0; | |
foreach (int item in param) | |
{ | |
sum += item; | |
} | |
return sum; | |
} | |
public static IEnumerable<int> FindPositive(this int[] param) | |
{ | |
foreach (int item in param) | |
{ | |
if (item > 0) | |
yield return item; | |
} | |
} | |
} | |
public class Good | |
{ | |
public string Title { get; set; } | |
public double Weight { get; set; } | |
public decimal Price { get; set; } | |
public DateTime DateOfArrival { get; set; } | |
public int StorageNumber { get; set; } | |
} | |
public class GoodManager | |
{ | |
private List<Good> list; | |
private String template = "Title: {0}\n" + | |
"Weight: {1}\n" + | |
"Price {2}\n" + | |
"DateOfArrival {3}\n" + | |
"StorageNumber {4}\n"; | |
public GoodManager(List<Good> list) | |
{ | |
this.list = list; | |
} | |
public String GetInfo() | |
{ | |
IEnumerable<String> data = list | |
.Select(item => String.Format(template, item.Title, item.Weight, item.Price, item.DateOfArrival, item.StorageNumber)); | |
return String.Join("\n", data); | |
} | |
public String GetInfoByTitlePattern(String pattern) | |
{ | |
IEnumerable<String> data = list | |
.Where(item => item.Title.Contains(pattern)) | |
.Select(item => String.Format(template, item.Title, item.Weight, item.Price, item.DateOfArrival, item.StorageNumber)); | |
return String.Join("\n", data); | |
} | |
public String GetSortedInfo() | |
{ | |
IEnumerable<String> data = list | |
.OrderBy(item => item.Price) | |
.ThenBy(item => item.DateOfArrival) | |
.Select(item => String.Format(template, item.Title, item.Weight, item.Price, item.DateOfArrival, item.StorageNumber)); | |
return String.Join("\n", data); | |
} | |
public String GetGroupedInfo() | |
{ | |
IEnumerable<IGrouping<int, Good>> data = list | |
.GroupBy(good => good.StorageNumber) | |
.OrderBy(good => good.Key); | |
return String.Join("\n", | |
data.Select(group => "\n\nGroup: " + group.Key + "\n\n" + | |
String.Join("\n", | |
group.Select(field => String.Format(template, | |
field.Title, field.Weight, field.Price, field.DateOfArrival, field.StorageNumber)))) | |
); | |
} | |
public String Execute(String query) | |
{ | |
int numberOfItems = 3; | |
try | |
{ | |
String[] args = query.Split(' '); | |
int page = int.Parse(args[Array.IndexOf(args, "-page") + 1]); | |
int recordsCount = Math.Min(int.Parse(args[Array.IndexOf(args, "-recordsCount") + 1]), numberOfItems); | |
if (page == 0 || recordsCount == 0) | |
{ | |
throw new ArgumentException(); | |
} | |
if (page > (list.Count / numberOfItems) + 1) | |
{ | |
throw new ArgumentOutOfRangeException(); | |
} | |
IEnumerable<String> data = list.Skip(numberOfItems * (page - 1)).Take(recordsCount) | |
.Select(item => String.Format(template, item.Title, item.Weight, item.Price, item.DateOfArrival, item.StorageNumber)); | |
return String.Join("\n", data); | |
} | |
catch (ArgumentOutOfRangeException) | |
{ | |
Console.WriteLine("Index out of bounds"); | |
return null; | |
} | |
catch (ArgumentException) | |
{ | |
Console.WriteLine("Invalid query"); | |
return null; | |
} | |
} | |
} | |
class Program | |
{ | |
public delegate bool Logic(int value); | |
public static bool IsPositive(int value) | |
{ | |
return value > 0; | |
} | |
public static IEnumerable<int> FindPositive(int[] array, Logic check) | |
{ | |
foreach (int item in array) | |
{ | |
if (item > 0) | |
yield return item; | |
} | |
} | |
public static void Main(string[] args) | |
{ | |
Console.WriteLine("Sum of all values in array is " + new int[5] { 1, 2, 3, 4, 5 }.Total() + "\n"); | |
/* | |
Random random = new Random(); | |
int[] array = Enumerable.Repeat(0, 1000000).Select(i => random.Next(-30, 30)).ToArray(); | |
Test(array); | |
*/ | |
List<Good> list = new List<Good>() { | |
new Good() {Title = "One", Weight = 3.9, Price = 1.12M, DateOfArrival = new DateTime(2003, 03, 03), StorageNumber = 50}, | |
new Good() {Title = "Two", Weight = 4.0, Price = 1.5M, DateOfArrival = new DateTime(2004, 04, 04), StorageNumber = 10}, | |
new Good() {Title = "FirstName", Weight = 1.2, Price = 1.5M, DateOfArrival = new DateTime(2001, 01, 01), StorageNumber = 10}, | |
new Good() {Title = "Three", Weight = 5.3, Price = 2.5M, DateOfArrival = new DateTime(2005, 05, 05), StorageNumber = 50}, | |
new Good() {Title = "Somebody", Weight = 6.9, Price = 1.5M, DateOfArrival = new DateTime(2006, 06, 06), StorageNumber = 60}, | |
new Good() {Title = "LastName", Weight = 2.1, Price = 15.25M, DateOfArrival = new DateTime(2002, 02, 02), StorageNumber = 50}, | |
new Good() {Title = "Nobody", Weight = 7.5, Price = 1.1M, DateOfArrival = new DateTime(2007, 07, 07), StorageNumber = 10}, | |
}; | |
GoodManager gm = new GoodManager(list); | |
//Console.WriteLine(gm.GetInfo()); | |
//Console.WriteLine(gm.GetInfoByTitlePattern("Name")); | |
//Console.WriteLine(gm.GetSortedInfo()); | |
//Console.WriteLine(gm.GetGroupedInfo()); | |
//Console.WriteLine(gm.Execute("-page 3 -recordsCount 10")); | |
} | |
public static void Test(int[] array) | |
{ | |
double[] method = new double[88]; | |
double[] linq = new double[88]; | |
double[] extension = new double[88]; | |
Stopwatch time = new Stopwatch(); | |
for (int index = 0; index < 88; index++) | |
{ | |
Random random = new Random(); | |
array = array.OrderBy(item => random.Next()).ToArray(); | |
time.Start(); | |
FindPositive(array, IsPositive); | |
time.Stop(); | |
method[index] = time.ElapsedTicks; | |
time.Reset(); | |
time.Start(); | |
array.Where(x => x > 0); | |
time.Stop(); | |
linq[index] = time.ElapsedTicks; | |
time.Reset(); | |
time.Start(); | |
array.FindPositive(); | |
time.Stop(); | |
extension[index] = time.ElapsedTicks; | |
time.Reset(); | |
} | |
Console.WriteLine("Time for static method searching equals to {0}", method.OrderBy(x => x).ElementAt(44)); | |
Console.WriteLine("Time for linq method searching equals to {0}", linq.OrderBy(x => x).ElementAt(44)); | |
Console.WriteLine("Time for extension method searching equals to {0}", extension.OrderBy(x => x).ElementAt(44)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment