Rename projects based on topic

This commit is contained in:
Manuel Thalmann 2024-03-21 22:55:54 +01:00
parent d22de33732
commit 972d364963
7 changed files with 286 additions and 286 deletions

View file

@ -1,122 +1,122 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
namespace DT1 { namespace DT1 {
public delegate double GradeCalc(List<int> s); public delegate double GradeCalc(List<int> s);
public enum GradeLevel { FirstYear = 1, SecondYear, ThirdYear, FourthYear }; public enum GradeLevel { FirstYear = 1, SecondYear, ThirdYear, FourthYear };
public class Student { public class Student {
public string FirstName { get; set; } public string FirstName { get; set; }
public string LastName { get; set; } public string LastName { get; set; }
public int ID { get; set; } public int ID { get; set; }
public GradeLevel Year; public GradeLevel Year;
public List<int> ExamScores; public List<int> ExamScores;
public double Grade { get; set; } public double Grade { get; set; }
} }
public class StudentClass { public class StudentClass {
public Func<Student, string, bool> studentSelector = (student, surname) => public Func<Student, string, bool> studentSelector = (student, surname) =>
{ {
return string.Compare(student.LastName, surname, true) == 0; return string.Compare(student.LastName, surname, true) == 0;
}; };
public GradeCalc gradeCalculator = (grades) => public GradeCalc gradeCalculator = (grades) =>
{ {
return Math.Round(((grades.Average() / 100 * 5) + 1) * 2, MidpointRounding.AwayFromZero) / 2; return Math.Round(((grades.Average() / 100 * 5) + 1) * 2, MidpointRounding.AwayFromZero) / 2;
}; };
public Func<Student, bool> passedSelector = (student) => public Func<Student, bool> passedSelector = (student) =>
{ {
return student.Year == GradeLevel.FourthYear && student.Grade >= 4; return student.Year == GradeLevel.FourthYear && student.Grade >= 4;
}; };
#region data #region data
public List<Student> students = new List<Student> { public List<Student> students = new List<Student> {
new Student {FirstName = "Terry", LastName = "Adams", ID = 120, new Student {FirstName = "Terry", LastName = "Adams", ID = 120,
Year = GradeLevel.SecondYear, Year = GradeLevel.SecondYear,
ExamScores = new List<int>{ 99, 82, 81, 79}}, ExamScores = new List<int>{ 99, 82, 81, 79}},
new Student {FirstName = "Fadi", LastName = "Fakhouri", ID = 116, new Student {FirstName = "Fadi", LastName = "Fakhouri", ID = 116,
Year = GradeLevel.ThirdYear, Year = GradeLevel.ThirdYear,
ExamScores = new List<int>{ 99, 86, 90, 94}}, ExamScores = new List<int>{ 99, 86, 90, 94}},
new Student {FirstName = "Hanying", LastName = "Feng", ID = 117, new Student {FirstName = "Hanying", LastName = "Feng", ID = 117,
Year = GradeLevel.FirstYear, Year = GradeLevel.FirstYear,
ExamScores = new List<int>{ 93, 92, 80, 87}}, ExamScores = new List<int>{ 93, 92, 80, 87}},
new Student {FirstName = "Cesar", LastName = "Garcia", ID = 114, new Student {FirstName = "Cesar", LastName = "Garcia", ID = 114,
Year = GradeLevel.FourthYear, Year = GradeLevel.FourthYear,
ExamScores = new List<int>{ 97, 89, 85, 82}}, ExamScores = new List<int>{ 97, 89, 85, 82}},
new Student {FirstName = "Debra", LastName = "Garcia", ID = 115, new Student {FirstName = "Debra", LastName = "Garcia", ID = 115,
Year = GradeLevel.ThirdYear, Year = GradeLevel.ThirdYear,
ExamScores = new List<int>{ 35, 72, 91, 70}}, ExamScores = new List<int>{ 35, 72, 91, 70}},
new Student {FirstName = "Hugo", LastName = "Garcia", ID = 118, new Student {FirstName = "Hugo", LastName = "Garcia", ID = 118,
Year = GradeLevel.SecondYear, Year = GradeLevel.SecondYear,
ExamScores = new List<int>{ 92, 90, 83, 78}}, ExamScores = new List<int>{ 92, 90, 83, 78}},
new Student {FirstName = "Sven", LastName = "Mortensen", ID = 113, new Student {FirstName = "Sven", LastName = "Mortensen", ID = 113,
Year = GradeLevel.FirstYear, Year = GradeLevel.FirstYear,
ExamScores = new List<int>{ 88, 94, 65, 91}}, ExamScores = new List<int>{ 88, 94, 65, 91}},
new Student {FirstName = "Claire", LastName = "O'Donnell", ID = 112, new Student {FirstName = "Claire", LastName = "O'Donnell", ID = 112,
Year = GradeLevel.FourthYear, Year = GradeLevel.FourthYear,
ExamScores = new List<int>{ 75, 84, 91, 39}}, ExamScores = new List<int>{ 75, 84, 91, 39}},
new Student {FirstName = "Svetlana", LastName = "Omelchenko", ID = 111, new Student {FirstName = "Svetlana", LastName = "Omelchenko", ID = 111,
Year = GradeLevel.SecondYear, Year = GradeLevel.SecondYear,
ExamScores = new List<int>{ 97, 92, 81, 60}}, ExamScores = new List<int>{ 97, 92, 81, 60}},
new Student {FirstName = "Lance", LastName = "Tucker", ID = 119, new Student {FirstName = "Lance", LastName = "Tucker", ID = 119,
Year = GradeLevel.ThirdYear, Year = GradeLevel.ThirdYear,
ExamScores = new List<int>{ 68, 79, 88, 92}}, ExamScores = new List<int>{ 68, 79, 88, 92}},
new Student {FirstName = "Michael", LastName = "Tucker", ID = 122, new Student {FirstName = "Michael", LastName = "Tucker", ID = 122,
Year = GradeLevel.FirstYear, Year = GradeLevel.FirstYear,
ExamScores = new List<int>{ 94, 92, 91, 91}}, ExamScores = new List<int>{ 94, 92, 91, 91}},
new Student {FirstName = "Eugene", LastName = "Zabokritski", ID = 121, new Student {FirstName = "Eugene", LastName = "Zabokritski", ID = 121,
Year = GradeLevel.FourthYear, Year = GradeLevel.FourthYear,
ExamScores = new List<int>{ 96, 85, 91, 60}} ExamScores = new List<int>{ 96, 85, 91, 60}}
}; };
#endregion #endregion
public List<Student> AddGrades(GradeCalc calc) { public List<Student> AddGrades(GradeCalc calc) {
foreach (Student s in students) { foreach (Student s in students) {
s.Grade = calc(s.ExamScores); s.Grade = calc(s.ExamScores);
} }
return students; return students;
} }
public List<Student> SearchStudent(Func<Student, string, bool> select, String lastName) { public List<Student> SearchStudent(Func<Student, string, bool> select, String lastName) {
List<Student> result = new() { }; List<Student> result = new() { };
foreach (Student s in students) { foreach (Student s in students) {
if (select(s, lastName)) result.Add(s); if (select(s, lastName)) result.Add(s);
} }
return result; return result;
} }
public IEnumerable<Student> QueryPassed(Func<Student,bool> passed) public IEnumerable<Student> QueryPassed(Func<Student,bool> passed)
{ {
return students.Where(passed); return students.Where(passed);
} }
static void show(String titel, List<Student> students) { static void show(String titel, List<Student> students) {
Console.WriteLine(titel); Console.WriteLine(titel);
foreach (var item in students) { foreach (var item in students) {
Console.WriteLine(String.Format("{0} {1} {2}", item.FirstName, item.LastName, item.Grade)); Console.WriteLine(String.Format("{0} {1} {2}", item.FirstName, item.LastName, item.Grade));
} }
} }
public static void Main(string[] args) { public static void Main(string[] args) {
StudentClass sc = new StudentClass(); StudentClass sc = new StudentClass();
show("Grades", sc.AddGrades(sc.gradeCalculator)); show("Grades", sc.AddGrades(sc.gradeCalculator));
show("\nStudent Carcia",sc.SearchStudent(sc.studentSelector, "Garcia")); show("\nStudent Carcia",sc.SearchStudent(sc.studentSelector, "Garcia"));
show("\nPassed 4th", sc.QueryPassed(sc.passedSelector).ToList<Student>()); show("\nPassed 4th", sc.QueryPassed(sc.passedSelector).ToList<Student>());
// Keep the console window open in debug mode. // Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit"); Console.WriteLine("Press any key to exit");
Console.ReadKey(); Console.ReadKey();
} }
} }
public static class ListUtils { public static class ListUtils {
public static IEnumerable<T> What<T>(this IList<T> list, Func<T, bool> selector) public static IEnumerable<T> What<T>(this IList<T> list, Func<T, bool> selector)
{ {
return list.Where(selector); return list.Where(selector);
} }
} }
} }

View file

@ -1,165 +1,165 @@
/* /*
* Created by SharpDevelop. * Created by SharpDevelop.
* User: Karl Rege * User: Karl Rege
* Date: 26.02.2021 * Date: 26.02.2021
* Time: 16:15 * Time: 16:15
* *
*/ */
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.IO; using System.IO;
using System.Web; using System.Web;
using System.Net; using System.Net;
using System.Net.Security; using System.Net.Security;
using System.Diagnostics; using System.Diagnostics;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Threading; using System.Threading;
using System.Buffers; using System.Buffers;
namespace DT2 { namespace DT2 {
public enum CallMode { Sync, Async, AsyncTimeout }; public enum CallMode { Sync, Async, AsyncTimeout };
public class UrlTester { public class UrlTester {
public event Action<string> PageStart; public event Action<string> PageStart;
public event Action<string, int> PageLoaded; public event Action<string, int> PageLoaded;
public event Action<string, int, int> LoadSummary; public event Action<string, int, int> LoadSummary;
public event Action<string> Timeout; public event Action<string> Timeout;
public void SetupEvents() { public void SetupEvents() {
IDictionary<string, Stopwatch> sw = new Dictionary<string, Stopwatch>(); IDictionary<string, Stopwatch> sw = new Dictionary<string, Stopwatch>();
PageStart = url => { Console.WriteLine(url + " ..."); sw.Add(url, Stopwatch.StartNew()); }; PageStart = url => { Console.WriteLine(url + " ..."); sw.Add(url, Stopwatch.StartNew()); };
PageLoaded += (url, size) => { PageLoaded += (url, size) => {
sw[url].Stop(); sw[url].Stop();
Console.WriteLine(url + " " + size + " bytes " + (int)sw[url].Elapsed.TotalMilliseconds + " ms"); Console.WriteLine(url + " " + size + " bytes " + (int)sw[url].Elapsed.TotalMilliseconds + " ms");
sw.Remove(url); sw.Remove(url);
}; };
Timeout = (url) => { Timeout = (url) => {
sw[url].Stop(); Console.WriteLine(url + " Timeout"); sw.Remove(url); sw[url].Stop(); Console.WriteLine(url + " Timeout"); sw.Remove(url);
}; };
LoadSummary += (s, size, time) => { Console.WriteLine(s + ": " + size + " bytes " + time + " ms"); }; LoadSummary += (s, size, time) => { Console.WriteLine(s + ": " + size + " bytes " + time + " ms"); };
} }
public List<string> UrlList = new List<string> { public List<string> UrlList = new List<string> {
"https://www.zhaw.ch", "https://www.zhaw.ch",
"https://waikiki.zhaw.ch/~rege", "https://waikiki.zhaw.ch/~rege",
"https://www.zhaw.ch/de/linguistik/", "https://www.zhaw.ch/de/linguistik/",
"https://www.zhaw.ch/de/psychologie/", "https://www.zhaw.ch/de/psychologie/",
"https://www.zhaw.ch/de/archbau/", "https://www.zhaw.ch/de/archbau/",
"https://www.zhaw.ch/de/gesundheit/", "https://www.zhaw.ch/de/gesundheit/",
"https://www.zhaw.ch/de/lsfm/", "https://www.zhaw.ch/de/lsfm/",
// very slow sites // very slow sites
/* /*
"http://www.airkoryo.com.kp/", "http://www.airkoryo.com.kp/",
"http://www.manmulsang.com.kp/", "http://www.manmulsang.com.kp/",
"http://tourismdprk.gov.kp/", "http://tourismdprk.gov.kp/",
"http://www.kut.edu.kp/" "http://www.kut.edu.kp/"
*/ */
}; };
public void IgnoreCertificateValidation() { public void IgnoreCertificateValidation() {
System.Net.ServicePointManager.Expect100Continue = true; System.Net.ServicePointManager.Expect100Continue = true;
System.Net.ServicePointManager.ServerCertificateValidationCallback System.Net.ServicePointManager.ServerCertificateValidationCallback
+= new RemoteCertificateValidationCallback((sender, certificate, chain, policyErrors) => true); += new RemoteCertificateValidationCallback((sender, certificate, chain, policyErrors) => true);
} }
public virtual int GetPageSize(string url) { public virtual int GetPageSize(string url) {
// The downloaded resource ends up in the variable named content. // The downloaded resource ends up in the variable named content.
byte[] byteContent; byte[] byteContent;
// Initialize an HttpWebRequest for the current URL. // Initialize an HttpWebRequest for the current URL.
var webReq = (HttpWebRequest)WebRequest.Create(url); var webReq = (HttpWebRequest)WebRequest.Create(url);
//if (PageStart != null) PageStart (url); //if (PageStart != null) PageStart (url);
Stopwatch sw = Stopwatch.StartNew(); Stopwatch sw = Stopwatch.StartNew();
// Send the request to the Internet resource and wait for // Send the request to the Internet resource and wait for
// the response. // the response.
using (WebResponse response = webReq.GetResponse()) { using (WebResponse response = webReq.GetResponse()) {
// Get the stream that is associated with the response. // Get the stream that is associated with the response.
using (Stream responseStream = response.GetResponseStream()) { using (Stream responseStream = response.GetResponseStream()) {
using (StreamReader sr = new StreamReader(responseStream)) { using (StreamReader sr = new StreamReader(responseStream)) {
String content = sr.ReadToEnd(); String content = sr.ReadToEnd();
byteContent = Encoding.UTF8.GetBytes(content); byteContent = Encoding.UTF8.GetBytes(content);
} }
} }
} }
return byteContent.Length; return byteContent.Length;
} }
public int GetUrlSync(string url) { public int GetUrlSync(string url) {
PageStart(url); PageStart(url);
int size = GetPageSize(url); int size = GetPageSize(url);
PageLoaded(url, size); PageLoaded(url, size);
return size; return size;
} }
public async Task<int> GetUrlAsync(string url) { public async Task<int> GetUrlAsync(string url) {
PageStart(url); PageStart(url);
int size = await Task.Run(() => GetPageSize(url)); int size = await Task.Run(() => GetPageSize(url));
PageLoaded(url, size); PageLoaded(url, size);
return size; return size;
} }
public async Task<int> GetUrlAsyncTimeout(string url, int millis) { public async Task<int> GetUrlAsyncTimeout(string url, int millis) {
int size; int size;
PageStart(url); PageStart(url);
Task<int> task = Task.Run(() => GetPageSize(url)); Task<int> task = Task.Run(() => GetPageSize(url));
try { try {
size = await task.WaitAsync(TimeSpan.FromMilliseconds(millis)); size = await task.WaitAsync(TimeSpan.FromMilliseconds(millis));
PageLoaded(url, size); PageLoaded(url, size);
} }
catch (TimeoutException) catch (TimeoutException)
{ {
Timeout(url); Timeout(url);
size = 0; size = 0;
} }
return size; return size;
} }
public void GetAllUrls(List<string> urlList, CallMode callMode) { public void GetAllUrls(List<string> urlList, CallMode callMode) {
// get a list of web addresses. // get a list of web addresses.
int totalSize = 0; int totalSize = 0;
Stopwatch sw = Stopwatch.StartNew(); Stopwatch sw = Stopwatch.StartNew();
Action<Func<string, Task<int>>> asyncRunner = (implementation) => Action<Func<string, Task<int>>> asyncRunner = (implementation) =>
{ {
Task<int[]> task = Task.WhenAll(urlList.Select((url) => implementation(url))); Task<int[]> task = Task.WhenAll(urlList.Select((url) => implementation(url)));
task.Wait(); task.Wait();
totalSize = task.Result.Sum(); totalSize = task.Result.Sum();
}; };
if (callMode == CallMode.Sync) { if (callMode == CallMode.Sync) {
Console.WriteLine("\nCalling Sync"); Console.WriteLine("\nCalling Sync");
foreach (var url in urlList) { foreach (var url in urlList) {
totalSize += GetUrlSync(url); totalSize += GetUrlSync(url);
} }
} }
else if (callMode == CallMode.Async) { else if (callMode == CallMode.Async) {
Console.WriteLine("\nCalling Async"); Console.WriteLine("\nCalling Async");
asyncRunner.Invoke(GetUrlAsync); asyncRunner.Invoke(GetUrlAsync);
} }
else if (callMode == CallMode.AsyncTimeout) { else if (callMode == CallMode.AsyncTimeout) {
Console.WriteLine("\nCalling Async Timeout"); Console.WriteLine("\nCalling Async Timeout");
asyncRunner.Invoke((url) => GetUrlAsyncTimeout(url, 100)); asyncRunner.Invoke((url) => GetUrlAsyncTimeout(url, 100));
} }
LoadSummary("Total", totalSize, (int)sw.Elapsed.TotalMilliseconds); LoadSummary("Total", totalSize, (int)sw.Elapsed.TotalMilliseconds);
} }
public static void Main(string[] args) { public static void Main(string[] args) {
Console.WriteLine("Starting ..."); Console.WriteLine("Starting ...");
UrlTester urlTester = new UrlTester(); UrlTester urlTester = new UrlTester();
urlTester.IgnoreCertificateValidation(); urlTester.IgnoreCertificateValidation();
ThreadPool.SetMinThreads(urlTester.UrlList.Count * 2, urlTester.UrlList.Count * 2); ThreadPool.SetMinThreads(urlTester.UrlList.Count * 2, urlTester.UrlList.Count * 2);
urlTester.SetupEvents(); urlTester.SetupEvents();
urlTester.GetAllUrls(urlTester.UrlList, CallMode.Sync); urlTester.GetAllUrls(urlTester.UrlList, CallMode.Sync);
urlTester.GetAllUrls(urlTester.UrlList, CallMode.Async); urlTester.GetAllUrls(urlTester.UrlList, CallMode.Async);
urlTester.GetAllUrls(urlTester.UrlList, CallMode.AsyncTimeout); urlTester.GetAllUrls(urlTester.UrlList, CallMode.AsyncTimeout);
Console.Write("Press any key to continue ...\n"); Console.Write("Press any key to continue ...\n");
Console.ReadKey(true); Console.ReadKey(true);
} }
} }
} }