initial commit
This commit is contained in:
9
BAI1/BAI1.csproj
Normal file
9
BAI1/BAI1.csproj
Normal file
@ -0,0 +1,9 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<RootNamespace>BAI</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
156
BAI1/Program.cs
Normal file
156
BAI1/Program.cs
Normal file
@ -0,0 +1,156 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace BAI
|
||||
{
|
||||
public class BAI_Afteken1
|
||||
{
|
||||
/// ------------------------------------------------------------
|
||||
/// <summary>
|
||||
/// Filtert een lijst. Hierbij worden alle elementen die maar
|
||||
/// 1x voorkomen verwijderd
|
||||
/// </summary>
|
||||
/// <param name="lijst">De lijst die wordt doorlopen
|
||||
/// (wordt in functie veranderd)</param>
|
||||
/// ------------------------------------------------------------
|
||||
public static void Opdr1FilterList(List<int> lijst)
|
||||
{
|
||||
Dictionary<int, int> occurences = [];
|
||||
foreach (int i in lijst)
|
||||
{
|
||||
if (occurences.TryGetValue(i, out int value))
|
||||
{
|
||||
occurences[i] = ++value;
|
||||
}
|
||||
else
|
||||
{
|
||||
occurences[i] = 1;
|
||||
}
|
||||
}
|
||||
foreach (int i in occurences.Keys)
|
||||
{
|
||||
if (occurences[i] == 1)
|
||||
{
|
||||
lijst.Remove(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// ------------------------------------------------------------
|
||||
/// <summary>
|
||||
/// Maakt een queue van de getallen 1 t/m 50 (in die volgorde
|
||||
/// toegevoegd)
|
||||
/// </summary>
|
||||
/// <returns>Een queue met hierin 1, 2, 3, .., 50</returns>
|
||||
/// ------------------------------------------------------------
|
||||
public static Queue<int> Opdr2aQueue50()
|
||||
{
|
||||
Queue<int> queue = new();
|
||||
for (int i = 1; i <= 50; i++)
|
||||
{
|
||||
queue.Enqueue(i);
|
||||
}
|
||||
return queue;
|
||||
}
|
||||
|
||||
|
||||
/// ------------------------------------------------------------
|
||||
/// <summary>
|
||||
/// Haalt alle elementen uit een queue. Voegt elk element dat
|
||||
/// deelbaar is door 4 toe aan een stack
|
||||
/// </summary>
|
||||
/// <param name="queue">De queue die uitgelezen wordt</param>
|
||||
/// <returns>De stack met hierin de elementen die deelbaar zijn
|
||||
/// door 4</returns>
|
||||
/// ------------------------------------------------------------
|
||||
public static Stack<int> Opdr2bStackFromQueue(Queue<int> queue)
|
||||
{
|
||||
Stack<int> stack = new();
|
||||
|
||||
while (queue.TryDequeue(out int value))
|
||||
{
|
||||
if (value % 4 == 0)
|
||||
{
|
||||
stack.Push(value);
|
||||
}
|
||||
}
|
||||
|
||||
return stack;
|
||||
}
|
||||
|
||||
|
||||
/// ------------------------------------------------------------
|
||||
/// <summary>
|
||||
/// Maakt een stack met hierin unieke random getallen</summary>
|
||||
/// <param name="lower">De ondergrens voor elk getal (inclusief)</param>
|
||||
/// <param name="upper">De bovengrens voor elk getal (inclusief)</param>
|
||||
/// <param name="count">Het aantal getallen</param>
|
||||
/// <returns>De stack met unieke random getallen</returns>
|
||||
/// ------------------------------------------------------------
|
||||
public static Stack<int> Opdr3RandomNumbers(int lower, int upper, int count)
|
||||
{
|
||||
Stack<int> stack = new();
|
||||
Dictionary<int, bool> exists = [];
|
||||
Random random = new();
|
||||
int next;
|
||||
for (int i = 0; i < count;)
|
||||
{
|
||||
next = random.Next(lower, upper + 1);
|
||||
if (!exists.ContainsKey(next))
|
||||
{
|
||||
stack.Push(next);
|
||||
exists[next] = true;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return stack;
|
||||
}
|
||||
|
||||
|
||||
/// ------------------------------------------------------------
|
||||
/// <summary>
|
||||
/// Drukt een IEnumerable (List, Stack, Queue, ..) van getallen
|
||||
/// af naar de Console
|
||||
/// <param name="enu">De IEnumerable om af te drukken</param>
|
||||
/// ------------------------------------------------------------
|
||||
static void PrintEnumerable(IEnumerable<int> enu)
|
||||
{
|
||||
foreach (int i in enu)
|
||||
{
|
||||
Console.Write($"{i} ");
|
||||
}
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
List<int> list;
|
||||
Queue<int> queue;
|
||||
Stack<int> stack;
|
||||
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("=== Opdracht 1 : FilterList ===");
|
||||
list = new List<int>() { 1, 3, 5, 7, 3, 8, 9, 5 };
|
||||
PrintEnumerable(list);
|
||||
Opdr1FilterList(list);
|
||||
PrintEnumerable(list);
|
||||
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("=== Opdracht 2 : Stack / Queue ===");
|
||||
queue = Opdr2aQueue50();
|
||||
PrintEnumerable(queue);
|
||||
stack = Opdr2bStackFromQueue(queue);
|
||||
PrintEnumerable(stack);
|
||||
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("=== Opdracht 3 : Random number ===");
|
||||
stack = Opdr3RandomNumbers(100, 150, 10);
|
||||
PrintEnumerable(stack);
|
||||
stack = Opdr3RandomNumbers(10, 15, 6);
|
||||
PrintEnumerable(stack);
|
||||
stack = Opdr3RandomNumbers(10_000, 50_000, 40_001);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user