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

View file

@ -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<string> PageStart;
public event Action<string, int> PageLoaded;
public event Action<string, int, int> LoadSummary;
public event Action<string> Timeout;
public void SetupEvents() {
IDictionary<string, Stopwatch> sw = new Dictionary<string, Stopwatch>();
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<string> UrlList = new List<string> {
"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<int> GetUrlAsync(string url) {
PageStart(url);
int size = await Task.Run(() => GetPageSize(url));
PageLoaded(url, size);
return size;
}
public async Task<int> GetUrlAsyncTimeout(string url, int millis) {
int size;
PageStart(url);
Task<int> 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<string> urlList, CallMode callMode) {
// get a list of web addresses.
int totalSize = 0;
Stopwatch sw = Stopwatch.StartNew();
Action<Func<string, Task<int>>> asyncRunner = (implementation) =>
{
Task<int[]> 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<string> PageStart;
public event Action<string, int> PageLoaded;
public event Action<string, int, int> LoadSummary;
public event Action<string> Timeout;
public void SetupEvents() {
IDictionary<string, Stopwatch> sw = new Dictionary<string, Stopwatch>();
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<string> UrlList = new List<string> {
"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<int> GetUrlAsync(string url) {
PageStart(url);
int size = await Task.Run(() => GetPageSize(url));
PageLoaded(url, size);
return size;
}
public async Task<int> GetUrlAsyncTimeout(string url, int millis) {
int size;
PageStart(url);
Task<int> 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<string> urlList, CallMode callMode) {
// get a list of web addresses.
int totalSize = 0;
Stopwatch sw = Stopwatch.StartNew();
Action<Func<string, Task<int>>> asyncRunner = (implementation) =>
{
Task<int[]> 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);
}
}
}