From 954d9138d0d25d756c371fd90c43d0ca65030f5a Mon Sep 17 00:00:00 2001 From: sebi Date: Tue, 25 Mar 2025 17:02:17 +0100 Subject: [PATCH] E20 Ex12 --- Exercises/E20_Lists/Exercise_10.cs | 9 ++++++- Exercises/E20_Lists/Exercise_11.cs | 12 ++++++++- Exercises/E20_Lists/Exercise_12.cs | 9 +++++-- Exercises/E20_Lists/Exercise_6.cs | 9 +++++++ Exercises/E20_Lists/Exercise_7.cs | 25 ++++++++++++++++++- Exercises/E20_Lists/Exercise_8.cs | 5 +++- Exercises/E20_Lists/Exercise_9.cs | 39 ++++++++++++++++++++++++++++++ lastPoint | 4 +-- 8 files changed, 104 insertions(+), 8 deletions(-) diff --git a/Exercises/E20_Lists/Exercise_10.cs b/Exercises/E20_Lists/Exercise_10.cs index 7e2d923..07de41b 100644 --- a/Exercises/E20_Lists/Exercise_10.cs +++ b/Exercises/E20_Lists/Exercise_10.cs @@ -18,8 +18,15 @@ namespace Exercises_C_Sharp.E20_Lists static List Meth() { /*Code START*/ + List stringList = []; - return new List(); + Console.WriteLine("Bitte geben Sie Werte ein und bestätigen Sie diese mit 'Enter'. Bei einer leeren Eingabe bricht das System ab:"); + while(true) + { + string? temp = Console.ReadLine(); + if(string.IsNullOrWhiteSpace(temp)) return stringList; + stringList.Add(temp); + } /*Code ENDE*/ } } diff --git a/Exercises/E20_Lists/Exercise_11.cs b/Exercises/E20_Lists/Exercise_11.cs index a00c8b8..06f3876 100644 --- a/Exercises/E20_Lists/Exercise_11.cs +++ b/Exercises/E20_Lists/Exercise_11.cs @@ -18,8 +18,18 @@ namespace Exercises_C_Sharp.E20_Lists static List Meth() { /*Code START*/ + List intList = []; - return new List(); + Console.WriteLine("Bitte geben Sie eine Zahl ein. Eine leere Eingabe führt zum Abbruch"); + while(true) + { + string? tempString = Console.ReadLine(); + + if(string.IsNullOrWhiteSpace(tempString))return intList; + + if(int.TryParse(tempString, out int tempInt)) intList.Add(tempInt); + + } /*Code ENDE*/ } } diff --git a/Exercises/E20_Lists/Exercise_12.cs b/Exercises/E20_Lists/Exercise_12.cs index 98208f2..4c56a09 100644 --- a/Exercises/E20_Lists/Exercise_12.cs +++ b/Exercises/E20_Lists/Exercise_12.cs @@ -22,12 +22,17 @@ namespace Exercises_C_Sharp.E20_Lists } //In der folgenden Methode bekommen Sie eine Liste von Strings und einen Character. Sie sollen nun aus der Liste alle Wörter entfernen, die diesen Character beinhalten. - static List Meth(List lis, char c) + static List Meth(List lis, char variableFürEinenChar) { /*Code START*/ + List tempList = []; + foreach(var element in lis) + if(!element.Contains(variableFürEinenChar)) + tempList.Add(element); - return new List(); + return tempList; /*Code ENDE*/ } + static List MethEasy(List lis, char c) => lis.Where(k => !k.Contains(c)).ToList(); } } \ No newline at end of file diff --git a/Exercises/E20_Lists/Exercise_6.cs b/Exercises/E20_Lists/Exercise_6.cs index 7d53401..0e86bad 100644 --- a/Exercises/E20_Lists/Exercise_6.cs +++ b/Exercises/E20_Lists/Exercise_6.cs @@ -13,6 +13,15 @@ namespace Exercises_C_Sharp.E20_Lists /*Code START*/ + + + intList.Sort(); + for (int i = 0; i < intList.Count; i++) + { + Console.WriteLine("[{0}] {1}", i, intList[i]); + } + int mitte = intList.Count / 2; + Console.WriteLine(intList[mitte]); /*Code ENDE*/ diff --git a/Exercises/E20_Lists/Exercise_7.cs b/Exercises/E20_Lists/Exercise_7.cs index 8e88676..7a06e73 100644 --- a/Exercises/E20_Lists/Exercise_7.cs +++ b/Exercises/E20_Lists/Exercise_7.cs @@ -25,13 +25,36 @@ namespace Exercises_C_Sharp.E20_Lists { /*Code START*/ - return intList; + List tempList = []; + foreach(var element in intList) + if(element % 2 == 0) + tempList.Add(element); + + return tempList; /*Code ENDE*/ } + + static ListRemoveOddsEasy(List intList) => intList.Where(k => k % 2 == 0).ToList(); + static List RemoveEvens(List intList) { /*Code START*/ + for(int i = 0; i < intList.Count; i++) + if(intList[i] % 2 == 0) + { + intList.RemoveAt(i); + i--; + } + + //ODER: + for(int k = intList.Count - 1; k >= 0; k--) + if(intList[k] % 2 == 0) + intList.RemoveAt(k); + + //ODER: + intList.RemoveAll(k => k % 2 == 0); + return intList; /*Code ENDE*/ } diff --git a/Exercises/E20_Lists/Exercise_8.cs b/Exercises/E20_Lists/Exercise_8.cs index 64e0cc0..d54f743 100644 --- a/Exercises/E20_Lists/Exercise_8.cs +++ b/Exercises/E20_Lists/Exercise_8.cs @@ -17,11 +17,14 @@ namespace Exercises_C_Sharp.E20_Lists } - //Schreiben Sie eine Methode, die eine Liste von Doubles bekommt. Zusätzlich soll noch ein Wert übergeben werden, der die neue Größe der Liste angibt. Nehmen Sie solange Werte aus der Liste, bis diese übergebene Anzahl erreicht ist. Streichen Sie dabei immer die kleinsten Werte heraus. Änder Sie nicht die Ordnung der übergebenen Liste: + //Schreiben Sie eine Methode, die eine Liste von Doubles bekommt. Zusätzlich soll noch ein Wert übergeben werden, der die neue Größe der Liste angibt. Nehmen Sie solange Werte aus der Liste, bis diese übergebene Anzahl erreicht ist. Streichen Sie dabei immer den kleinsten Werte heraus. Ändern Sie nicht die Ordnung der übergebenen Liste: static List Meth(List doubleList, int i) { /*Code START*/ + while(doubleList.Count > i) + doubleList.Remove(doubleList.Min()); + return doubleList; /*Code ENDE*/ } diff --git a/Exercises/E20_Lists/Exercise_9.cs b/Exercises/E20_Lists/Exercise_9.cs index b0fc952..088b1bb 100644 --- a/Exercises/E20_Lists/Exercise_9.cs +++ b/Exercises/E20_Lists/Exercise_9.cs @@ -20,6 +20,45 @@ namespace Exercises_C_Sharp.E20_Lists static void Meth(List intList) { /*Code START*/ + List EinerList = ["Null", "Ein", "Zwei", "Drei", "Vier", "Fünf", "Sechs", "Sieben", "Acht", "Neun"]; + List ZehnerList = ["NULL", "Zehn", "Zwanzig", "Dreißig", "Vierzig", "Fünfzig", "Sechzig", "Siebzig", "Achtzig", "Neunzig"]; + + foreach(var element in intList) + { + int number = element; + string result = string.Empty; + + if(number < 0) + { + result = "Minus "; + number = Math.Abs(number); + } + int einer = number % 10; + int zehner = number / 10; + + if(number == 1) + result += "Eins"; + else if(number == 11) + result += "Elf"; + else if(number == 12) + result += "Zwölf"; + else if(number == 16) + result += "Sechzehn"; + else if(number == 17) + result += "Siebzehn"; + else if(number > 12 && number < 20) + result += EinerList[einer] + ZehnerList[zehner].ToLower(); + else if(zehner == 0) + result += EinerList[einer]; + else if(einer == 0) + result += ZehnerList[zehner]; + else + result += EinerList[einer] + "und" + ZehnerList[zehner].ToLower(); + + + Console.WriteLine(result); + + } /*Code ENDE*/ } diff --git a/lastPoint b/lastPoint index e5c8abe..a9554a8 100644 --- a/lastPoint +++ b/lastPoint @@ -1,2 +1,2 @@ -4 -17 +8 +19