Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion 01-Introduction/Alarms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,17 @@ public class Alarms
{
public int countAlarms(int[] volume, int S)
{
return default(int);
var counter = 0;
while(S > 0){
counter++;
var currentAlarm = volume[0];
S = S - currentAlarm;
for(int i = 0; i < volume.Length-1; i++){
volume[i] = volume[i+1];
}
volume[volume.Length-1] = currentAlarm;
}
return counter;
}

#region Testing code
Expand Down
9 changes: 8 additions & 1 deletion 01-Introduction/Ameba.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ public class Ameba
{
public int simulate(int[] X, int A)
{
return default(int);
var MonteCarlo = A;
for (int i = 0; X.Length > i; ++i) {
if(X[i] == MonteCarlo){
MonteCarlo = MonteCarlo + X[i];
}
}

return MonteCarlo;
}

#region Testing code
Expand Down
3 changes: 3 additions & 0 deletions 03-LINQ/GoldSavings.App/GoldSavings.App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup>
<Folder Include="documents\" />
</ItemGroup>

</Project>
88 changes: 87 additions & 1 deletion 03-LINQ/GoldSavings.App/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GoldSavings.App.Model;
using System.Xml.Linq;
using GoldSavings.App.Model;
using GoldSavings.App.Client;
namespace GoldSavings.App;

Expand All @@ -18,6 +19,91 @@ static void Main(string[] args)
{
Console.WriteLine($"The price for {goldPrice.Date} is {goldPrice.Price}");
}

var orderedPrices = goldClient.GetGoldPrices(new DateTime(2024, 01, 01), new DateTime(2024, 12, 31)).GetAwaiter().GetResult()
.OrderBy(price => price.Price);

var last3 = orderedPrices.TakeLast(3).ToList();
foreach (var goldPrice in last3)
{
Console.WriteLine($"The worst prices are {goldPrice.Price} at date: {goldPrice.Date}");
}

var top3 = orderedPrices.Take(3).ToList();
foreach (var goldPrice in top3)
{
Console.WriteLine($"The best prices are {goldPrice.Price} at date: {goldPrice.Date}");
}

var avgJanuary2020GoldPrice = goldClient.GetGoldPrices(new DateTime(2020, 01, 01), new DateTime(2020, 01, 31))
.GetAwaiter().GetResult().Average(price => price.Price) * 1.05;
Console.WriteLine(avgJanuary2020GoldPrice);

var goldPricesForBeating2020 = goldClient.GetGoldPrices(new DateTime(2024, 01, 01), new DateTime(2024, 12, 31)).GetAwaiter().GetResult()
.Where(price => price.Price >= avgJanuary2020GoldPrice);
foreach (var goldPrice in goldPricesForBeating2020)
{
Console.WriteLine($"The prices and dates that you would gain 5 percent if you bought in January 2020: {goldPrice.Date} is {goldPrice.Price}");
}

List<GoldPrice> prices2019 = goldClient.GetGoldPrices(new DateTime(2019, 01, 01), new DateTime(2019, 12, 31)).GetAwaiter().GetResult();
List<GoldPrice> prices2020 = goldClient.GetGoldPrices(new DateTime(2020, 01, 01), new DateTime(2020, 12, 31)).GetAwaiter().GetResult();
List<GoldPrice> prices2021 = goldClient.GetGoldPrices(new DateTime(2021, 01, 01), new DateTime(2021, 12, 31)).GetAwaiter().GetResult();
List<GoldPrice> prices2022 = goldClient.GetGoldPrices(new DateTime(2022, 01, 01), new DateTime(2022, 12, 31)).GetAwaiter().GetResult();

var worstSecondTenthThreePrices = prices2019.Concat(prices2020).Concat(prices2021).Concat(prices2022).OrderBy(price => price.Price).Take(13).TakeLast(3).ToList();
Console.WriteLine("Worst prices for 2019-2022");
foreach(var goldPrice in worstSecondTenthThreePrices)
{
Console.WriteLine($"The price for {goldPrice.Date} is {goldPrice.Price}");
}

List<GoldPrice> prices2023 = goldClient.GetGoldPrices(new DateTime(2023, 01, 01), new DateTime(2023, 12, 31)).GetAwaiter().GetResult();
List<GoldPrice> prices2024 = goldClient.GetGoldPrices(new DateTime(2024, 01, 01), new DateTime(2024, 12, 31)).GetAwaiter().GetResult();

Console.WriteLine($"Average prices for 2020, 2023, 2024: {prices2020.Average(price => price.Price)}, {prices2023.Average(price => price.Price)}, {prices2024.Average(price => price.Price)}");

var prices20202024 = prices2020.Concat(prices2021)
.Concat(prices2022)
.Concat(prices2023)
.Concat(prices2024).OrderBy(price => price.Price).ToList();
var bestDayToBuy = prices20202024.First();
var bestDayToSell = prices20202024.Last();
Console.WriteLine($"Best day to buy Price and Date: {bestDayToBuy.Price} {bestDayToBuy.Date}, Best day to sell Price and Date: {bestDayToSell.Price} {bestDayToSell.Date}");

Console.WriteLine("Writing prices to XML");
SaveToXml(prices2024);

Console.WriteLine("Reading prices from XML");
var goldPricesFromXml = LoadFromXml("GoldPrices.xml");
foreach(var goldPrice in goldPricesFromXml)
{
Console.WriteLine($"The price for {goldPrice.Date} is {goldPrice.Price}");
}
}

public static void SaveToXml(List<GoldPrice> goldPrices)
{
XDocument objXDoc = new XDocument(
new XElement("GoldPrices",
goldPrices.Select(price =>
new XElement("GoldPrice",
new XElement("Date", price.Date.ToString("yyyy-MM-dd")),
new XElement("Price", price.Price)
)
)
)
);

objXDoc.Save("GoldPrices.xml");
}

public static List<GoldPrice> LoadFromXml(string filePath)
{
return XDocument.Load(filePath).Descendants("GoldPrice").Select(x => new GoldPrice
{
Date = DateTime.Parse(x.Element("Date").Value),
Price = double.Parse(x.Element("Price").Value)
}).ToList();
}
}