diff --git a/Tasks/DT1/.gitattributes b/Tasks/AdvancedConcepts/.gitattributes similarity index 100% rename from Tasks/DT1/.gitattributes rename to Tasks/AdvancedConcepts/.gitattributes diff --git a/Tasks/DT1/DT1.csproj b/Tasks/AdvancedConcepts/AdvancedConcepts.csproj similarity index 100% rename from Tasks/DT1/DT1.csproj rename to Tasks/AdvancedConcepts/AdvancedConcepts.csproj diff --git a/Tasks/DT1/DT1.cs b/Tasks/AdvancedConcepts/DT1.cs similarity index 97% rename from Tasks/DT1/DT1.cs rename to Tasks/AdvancedConcepts/DT1.cs index 309aa1d..5eeaa15 100644 --- a/Tasks/DT1/DT1.cs +++ b/Tasks/AdvancedConcepts/DT1.cs @@ -1,122 +1,122 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace DT1 { - public delegate double GradeCalc(List s); - public enum GradeLevel { FirstYear = 1, SecondYear, ThirdYear, FourthYear }; - - public class Student { - public string FirstName { get; set; } - public string LastName { get; set; } - public int ID { get; set; } - public GradeLevel Year; - public List ExamScores; - public double Grade { get; set; } - } - - public class StudentClass { - public Func studentSelector = (student, surname) => - { - return string.Compare(student.LastName, surname, true) == 0; - }; - - public GradeCalc gradeCalculator = (grades) => - { - return Math.Round(((grades.Average() / 100 * 5) + 1) * 2, MidpointRounding.AwayFromZero) / 2; - }; - - public Func passedSelector = (student) => - { - return student.Year == GradeLevel.FourthYear && student.Grade >= 4; - }; - - #region data - public List students = new List { - new Student {FirstName = "Terry", LastName = "Adams", ID = 120, - Year = GradeLevel.SecondYear, - ExamScores = new List{ 99, 82, 81, 79}}, - new Student {FirstName = "Fadi", LastName = "Fakhouri", ID = 116, - Year = GradeLevel.ThirdYear, - ExamScores = new List{ 99, 86, 90, 94}}, - new Student {FirstName = "Hanying", LastName = "Feng", ID = 117, - Year = GradeLevel.FirstYear, - ExamScores = new List{ 93, 92, 80, 87}}, - new Student {FirstName = "Cesar", LastName = "Garcia", ID = 114, - Year = GradeLevel.FourthYear, - ExamScores = new List{ 97, 89, 85, 82}}, - new Student {FirstName = "Debra", LastName = "Garcia", ID = 115, - Year = GradeLevel.ThirdYear, - ExamScores = new List{ 35, 72, 91, 70}}, - new Student {FirstName = "Hugo", LastName = "Garcia", ID = 118, - Year = GradeLevel.SecondYear, - ExamScores = new List{ 92, 90, 83, 78}}, - new Student {FirstName = "Sven", LastName = "Mortensen", ID = 113, - Year = GradeLevel.FirstYear, - ExamScores = new List{ 88, 94, 65, 91}}, - new Student {FirstName = "Claire", LastName = "O'Donnell", ID = 112, - Year = GradeLevel.FourthYear, - ExamScores = new List{ 75, 84, 91, 39}}, - new Student {FirstName = "Svetlana", LastName = "Omelchenko", ID = 111, - Year = GradeLevel.SecondYear, - ExamScores = new List{ 97, 92, 81, 60}}, - new Student {FirstName = "Lance", LastName = "Tucker", ID = 119, - Year = GradeLevel.ThirdYear, - ExamScores = new List{ 68, 79, 88, 92}}, - new Student {FirstName = "Michael", LastName = "Tucker", ID = 122, - Year = GradeLevel.FirstYear, - ExamScores = new List{ 94, 92, 91, 91}}, - new Student {FirstName = "Eugene", LastName = "Zabokritski", ID = 121, - Year = GradeLevel.FourthYear, - ExamScores = new List{ 96, 85, 91, 60}} - }; - #endregion - - public List AddGrades(GradeCalc calc) { - foreach (Student s in students) { - s.Grade = calc(s.ExamScores); - } - return students; - } - - public List SearchStudent(Func select, String lastName) { - List result = new() { }; - foreach (Student s in students) { - if (select(s, lastName)) result.Add(s); - } - return result; - } - - public IEnumerable QueryPassed(Func passed) - { - return students.Where(passed); - } - - static void show(String titel, List students) { - Console.WriteLine(titel); - foreach (var item in students) { - Console.WriteLine(String.Format("{0} {1} {2}", item.FirstName, item.LastName, item.Grade)); - } - } - - public static void Main(string[] args) { - StudentClass sc = new StudentClass(); - show("Grades", sc.AddGrades(sc.gradeCalculator)); - show("\nStudent Carcia",sc.SearchStudent(sc.studentSelector, "Garcia")); - show("\nPassed 4th", sc.QueryPassed(sc.passedSelector).ToList()); - // Keep the console window open in debug mode. - Console.WriteLine("Press any key to exit"); - Console.ReadKey(); - } - - - } - public static class ListUtils { - public static IEnumerable What(this IList list, Func selector) - { - return list.Where(selector); - } - } - -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace DT1 { + public delegate double GradeCalc(List s); + public enum GradeLevel { FirstYear = 1, SecondYear, ThirdYear, FourthYear }; + + public class Student { + public string FirstName { get; set; } + public string LastName { get; set; } + public int ID { get; set; } + public GradeLevel Year; + public List ExamScores; + public double Grade { get; set; } + } + + public class StudentClass { + public Func studentSelector = (student, surname) => + { + return string.Compare(student.LastName, surname, true) == 0; + }; + + public GradeCalc gradeCalculator = (grades) => + { + return Math.Round(((grades.Average() / 100 * 5) + 1) * 2, MidpointRounding.AwayFromZero) / 2; + }; + + public Func passedSelector = (student) => + { + return student.Year == GradeLevel.FourthYear && student.Grade >= 4; + }; + + #region data + public List students = new List { + new Student {FirstName = "Terry", LastName = "Adams", ID = 120, + Year = GradeLevel.SecondYear, + ExamScores = new List{ 99, 82, 81, 79}}, + new Student {FirstName = "Fadi", LastName = "Fakhouri", ID = 116, + Year = GradeLevel.ThirdYear, + ExamScores = new List{ 99, 86, 90, 94}}, + new Student {FirstName = "Hanying", LastName = "Feng", ID = 117, + Year = GradeLevel.FirstYear, + ExamScores = new List{ 93, 92, 80, 87}}, + new Student {FirstName = "Cesar", LastName = "Garcia", ID = 114, + Year = GradeLevel.FourthYear, + ExamScores = new List{ 97, 89, 85, 82}}, + new Student {FirstName = "Debra", LastName = "Garcia", ID = 115, + Year = GradeLevel.ThirdYear, + ExamScores = new List{ 35, 72, 91, 70}}, + new Student {FirstName = "Hugo", LastName = "Garcia", ID = 118, + Year = GradeLevel.SecondYear, + ExamScores = new List{ 92, 90, 83, 78}}, + new Student {FirstName = "Sven", LastName = "Mortensen", ID = 113, + Year = GradeLevel.FirstYear, + ExamScores = new List{ 88, 94, 65, 91}}, + new Student {FirstName = "Claire", LastName = "O'Donnell", ID = 112, + Year = GradeLevel.FourthYear, + ExamScores = new List{ 75, 84, 91, 39}}, + new Student {FirstName = "Svetlana", LastName = "Omelchenko", ID = 111, + Year = GradeLevel.SecondYear, + ExamScores = new List{ 97, 92, 81, 60}}, + new Student {FirstName = "Lance", LastName = "Tucker", ID = 119, + Year = GradeLevel.ThirdYear, + ExamScores = new List{ 68, 79, 88, 92}}, + new Student {FirstName = "Michael", LastName = "Tucker", ID = 122, + Year = GradeLevel.FirstYear, + ExamScores = new List{ 94, 92, 91, 91}}, + new Student {FirstName = "Eugene", LastName = "Zabokritski", ID = 121, + Year = GradeLevel.FourthYear, + ExamScores = new List{ 96, 85, 91, 60}} + }; + #endregion + + public List AddGrades(GradeCalc calc) { + foreach (Student s in students) { + s.Grade = calc(s.ExamScores); + } + return students; + } + + public List SearchStudent(Func select, String lastName) { + List result = new() { }; + foreach (Student s in students) { + if (select(s, lastName)) result.Add(s); + } + return result; + } + + public IEnumerable QueryPassed(Func passed) + { + return students.Where(passed); + } + + static void show(String titel, List students) { + Console.WriteLine(titel); + foreach (var item in students) { + Console.WriteLine(String.Format("{0} {1} {2}", item.FirstName, item.LastName, item.Grade)); + } + } + + public static void Main(string[] args) { + StudentClass sc = new StudentClass(); + show("Grades", sc.AddGrades(sc.gradeCalculator)); + show("\nStudent Carcia",sc.SearchStudent(sc.studentSelector, "Garcia")); + show("\nPassed 4th", sc.QueryPassed(sc.passedSelector).ToList()); + // Keep the console window open in debug mode. + Console.WriteLine("Press any key to exit"); + Console.ReadKey(); + } + + + } + public static class ListUtils { + public static IEnumerable What(this IList list, Func selector) + { + return list.Where(selector); + } + } + +} diff --git a/Tasks/DT1/DT1.pdf b/Tasks/AdvancedConcepts/DT1.pdf similarity index 100% rename from Tasks/DT1/DT1.pdf rename to Tasks/AdvancedConcepts/DT1.pdf diff --git a/Tasks/DT2/DT2.cs b/Tasks/UrlTester/DT2.cs similarity index 97% rename from Tasks/DT2/DT2.cs rename to Tasks/UrlTester/DT2.cs index f744801..6401c55 100644 --- a/Tasks/DT2/DT2.cs +++ b/Tasks/UrlTester/DT2.cs @@ -1,165 +1,165 @@ -/* - * Created by SharpDevelop. - * User: Karl Rege - * Date: 26.02.2021 - * Time: 16:15 - * - */ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.IO; -using System.Web; -using System.Net; -using System.Net.Security; -using System.Diagnostics; -using System.Threading.Tasks; -using System.Threading; -using System.Buffers; - -namespace DT2 { - public enum CallMode { Sync, Async, AsyncTimeout }; - - public class UrlTester { - public event Action PageStart; - public event Action PageLoaded; - public event Action LoadSummary; - public event Action Timeout; - - public void SetupEvents() { - IDictionary sw = new Dictionary(); - PageStart = url => { Console.WriteLine(url + " ..."); sw.Add(url, Stopwatch.StartNew()); }; - PageLoaded += (url, size) => { - sw[url].Stop(); - Console.WriteLine(url + " " + size + " bytes " + (int)sw[url].Elapsed.TotalMilliseconds + " ms"); - sw.Remove(url); - }; - Timeout = (url) => { - sw[url].Stop(); Console.WriteLine(url + " Timeout"); sw.Remove(url); - }; - LoadSummary += (s, size, time) => { Console.WriteLine(s + ": " + size + " bytes " + time + " ms"); }; - } - - public List UrlList = new List { - "https://www.zhaw.ch", - "https://waikiki.zhaw.ch/~rege", - "https://www.zhaw.ch/de/linguistik/", - "https://www.zhaw.ch/de/psychologie/", - "https://www.zhaw.ch/de/archbau/", - "https://www.zhaw.ch/de/gesundheit/", - "https://www.zhaw.ch/de/lsfm/", - // very slow sites - /* - "http://www.airkoryo.com.kp/", - "http://www.manmulsang.com.kp/", - "http://tourismdprk.gov.kp/", - "http://www.kut.edu.kp/" - */ - }; - - - public void IgnoreCertificateValidation() { - System.Net.ServicePointManager.Expect100Continue = true; - System.Net.ServicePointManager.ServerCertificateValidationCallback - += new RemoteCertificateValidationCallback((sender, certificate, chain, policyErrors) => true); - } - - public virtual int GetPageSize(string url) { - // The downloaded resource ends up in the variable named content. - byte[] byteContent; - // Initialize an HttpWebRequest for the current URL. - var webReq = (HttpWebRequest)WebRequest.Create(url); - //if (PageStart != null) PageStart (url); - Stopwatch sw = Stopwatch.StartNew(); - // Send the request to the Internet resource and wait for - // the response. - using (WebResponse response = webReq.GetResponse()) { - // Get the stream that is associated with the response. - using (Stream responseStream = response.GetResponseStream()) { - using (StreamReader sr = new StreamReader(responseStream)) { - String content = sr.ReadToEnd(); - byteContent = Encoding.UTF8.GetBytes(content); - } - } - } - - return byteContent.Length; - } - - public int GetUrlSync(string url) { - PageStart(url); - int size = GetPageSize(url); - PageLoaded(url, size); - return size; - } - - public async Task GetUrlAsync(string url) { - PageStart(url); - int size = await Task.Run(() => GetPageSize(url)); - PageLoaded(url, size); - return size; - } - - public async Task GetUrlAsyncTimeout(string url, int millis) { - int size; - PageStart(url); - Task task = Task.Run(() => GetPageSize(url)); - - try { - size = await task.WaitAsync(TimeSpan.FromMilliseconds(millis)); - PageLoaded(url, size); - } - catch (TimeoutException) - { - Timeout(url); - size = 0; - } - - return size; - } - - - public void GetAllUrls(List urlList, CallMode callMode) { - // get a list of web addresses. - int totalSize = 0; - Stopwatch sw = Stopwatch.StartNew(); - - Action>> asyncRunner = (implementation) => - { - Task task = Task.WhenAll(urlList.Select((url) => implementation(url))); - task.Wait(); - totalSize = task.Result.Sum(); - }; - - if (callMode == CallMode.Sync) { - Console.WriteLine("\nCalling Sync"); - foreach (var url in urlList) { - totalSize += GetUrlSync(url); - } - } - else if (callMode == CallMode.Async) { - Console.WriteLine("\nCalling Async"); - asyncRunner.Invoke(GetUrlAsync); - } - else if (callMode == CallMode.AsyncTimeout) { - Console.WriteLine("\nCalling Async Timeout"); - asyncRunner.Invoke((url) => GetUrlAsyncTimeout(url, 100)); - } - LoadSummary("Total", totalSize, (int)sw.Elapsed.TotalMilliseconds); - } - - public static void Main(string[] args) { - Console.WriteLine("Starting ..."); - UrlTester urlTester = new UrlTester(); - urlTester.IgnoreCertificateValidation(); - ThreadPool.SetMinThreads(urlTester.UrlList.Count * 2, urlTester.UrlList.Count * 2); - urlTester.SetupEvents(); - urlTester.GetAllUrls(urlTester.UrlList, CallMode.Sync); - urlTester.GetAllUrls(urlTester.UrlList, CallMode.Async); - urlTester.GetAllUrls(urlTester.UrlList, CallMode.AsyncTimeout); - Console.Write("Press any key to continue ...\n"); - Console.ReadKey(true); - } - } +/* + * Created by SharpDevelop. + * User: Karl Rege + * Date: 26.02.2021 + * Time: 16:15 + * + */ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.IO; +using System.Web; +using System.Net; +using System.Net.Security; +using System.Diagnostics; +using System.Threading.Tasks; +using System.Threading; +using System.Buffers; + +namespace DT2 { + public enum CallMode { Sync, Async, AsyncTimeout }; + + public class UrlTester { + public event Action PageStart; + public event Action PageLoaded; + public event Action LoadSummary; + public event Action Timeout; + + public void SetupEvents() { + IDictionary sw = new Dictionary(); + PageStart = url => { Console.WriteLine(url + " ..."); sw.Add(url, Stopwatch.StartNew()); }; + PageLoaded += (url, size) => { + sw[url].Stop(); + Console.WriteLine(url + " " + size + " bytes " + (int)sw[url].Elapsed.TotalMilliseconds + " ms"); + sw.Remove(url); + }; + Timeout = (url) => { + sw[url].Stop(); Console.WriteLine(url + " Timeout"); sw.Remove(url); + }; + LoadSummary += (s, size, time) => { Console.WriteLine(s + ": " + size + " bytes " + time + " ms"); }; + } + + public List UrlList = new List { + "https://www.zhaw.ch", + "https://waikiki.zhaw.ch/~rege", + "https://www.zhaw.ch/de/linguistik/", + "https://www.zhaw.ch/de/psychologie/", + "https://www.zhaw.ch/de/archbau/", + "https://www.zhaw.ch/de/gesundheit/", + "https://www.zhaw.ch/de/lsfm/", + // very slow sites + /* + "http://www.airkoryo.com.kp/", + "http://www.manmulsang.com.kp/", + "http://tourismdprk.gov.kp/", + "http://www.kut.edu.kp/" + */ + }; + + + public void IgnoreCertificateValidation() { + System.Net.ServicePointManager.Expect100Continue = true; + System.Net.ServicePointManager.ServerCertificateValidationCallback + += new RemoteCertificateValidationCallback((sender, certificate, chain, policyErrors) => true); + } + + public virtual int GetPageSize(string url) { + // The downloaded resource ends up in the variable named content. + byte[] byteContent; + // Initialize an HttpWebRequest for the current URL. + var webReq = (HttpWebRequest)WebRequest.Create(url); + //if (PageStart != null) PageStart (url); + Stopwatch sw = Stopwatch.StartNew(); + // Send the request to the Internet resource and wait for + // the response. + using (WebResponse response = webReq.GetResponse()) { + // Get the stream that is associated with the response. + using (Stream responseStream = response.GetResponseStream()) { + using (StreamReader sr = new StreamReader(responseStream)) { + String content = sr.ReadToEnd(); + byteContent = Encoding.UTF8.GetBytes(content); + } + } + } + + return byteContent.Length; + } + + public int GetUrlSync(string url) { + PageStart(url); + int size = GetPageSize(url); + PageLoaded(url, size); + return size; + } + + public async Task GetUrlAsync(string url) { + PageStart(url); + int size = await Task.Run(() => GetPageSize(url)); + PageLoaded(url, size); + return size; + } + + public async Task GetUrlAsyncTimeout(string url, int millis) { + int size; + PageStart(url); + Task task = Task.Run(() => GetPageSize(url)); + + try { + size = await task.WaitAsync(TimeSpan.FromMilliseconds(millis)); + PageLoaded(url, size); + } + catch (TimeoutException) + { + Timeout(url); + size = 0; + } + + return size; + } + + + public void GetAllUrls(List urlList, CallMode callMode) { + // get a list of web addresses. + int totalSize = 0; + Stopwatch sw = Stopwatch.StartNew(); + + Action>> asyncRunner = (implementation) => + { + Task task = Task.WhenAll(urlList.Select((url) => implementation(url))); + task.Wait(); + totalSize = task.Result.Sum(); + }; + + if (callMode == CallMode.Sync) { + Console.WriteLine("\nCalling Sync"); + foreach (var url in urlList) { + totalSize += GetUrlSync(url); + } + } + else if (callMode == CallMode.Async) { + Console.WriteLine("\nCalling Async"); + asyncRunner.Invoke(GetUrlAsync); + } + else if (callMode == CallMode.AsyncTimeout) { + Console.WriteLine("\nCalling Async Timeout"); + asyncRunner.Invoke((url) => GetUrlAsyncTimeout(url, 100)); + } + LoadSummary("Total", totalSize, (int)sw.Elapsed.TotalMilliseconds); + } + + public static void Main(string[] args) { + Console.WriteLine("Starting ..."); + UrlTester urlTester = new UrlTester(); + urlTester.IgnoreCertificateValidation(); + ThreadPool.SetMinThreads(urlTester.UrlList.Count * 2, urlTester.UrlList.Count * 2); + urlTester.SetupEvents(); + urlTester.GetAllUrls(urlTester.UrlList, CallMode.Sync); + urlTester.GetAllUrls(urlTester.UrlList, CallMode.Async); + urlTester.GetAllUrls(urlTester.UrlList, CallMode.AsyncTimeout); + Console.Write("Press any key to continue ...\n"); + Console.ReadKey(true); + } + } } \ No newline at end of file diff --git a/Tasks/DT2/DT2.pdf b/Tasks/UrlTester/DT2.pdf similarity index 100% rename from Tasks/DT2/DT2.pdf rename to Tasks/UrlTester/DT2.pdf diff --git a/Tasks/DT2/DT2.csproj b/Tasks/UrlTester/UrlTester.csproj similarity index 100% rename from Tasks/DT2/DT2.csproj rename to Tasks/UrlTester/UrlTester.csproj