We are doing the following programming exercise: COVID-19.
We have thought that the following pseudocode algorithm could fit for this task:
while recovered < people
if there are more healthy than sick
calculate new sick
calculate new healthy
else
sick are the last healthy ones
healthy is 0
add to the sicksPerDay list sick
if day >= days_to_recover
calculate new recovered
calculate new sick
So, we have translated it into c# as follows:
namespace Covid19 {
using NUnit.Framework;
using System;
using System.Linq;
using System.Collections.Generic;
public class Pandemy
{
public static int PandemyStops /*đ·đŠ */ (int sick, int people, int spread, int days_to_recover)
{
Console.WriteLine("sick: "+sick);
Console.WriteLine("people: "+people);
Console.WriteLine("spread: "+spread);
Console.WriteLine("days_to_recover: "+days_to_recover);
Console.WriteLine();
int healthy=people-sick, recovered=0, day=0, yesterdaySick=sick;
Console.WriteLine("healthy: "+healthy);
List<int> sicksPerDay = new List<int>();
Console.WriteLine("sicksPerDay: "+String.Join(", ",sicksPerDay));
for(; recovered<people;day++){
Console.WriteLine("day: "+day);
if(healthy > (sick*spread)){
Console.WriteLine("if(healthy > (sick*spread)){");
sick += sick*spread;
Console.WriteLine("sick: "+sick);
healthy -= sick - yesterdaySick;
Console.WriteLine("healthy: "+healthy);
yesterdaySick = sick;
Console.WriteLine("yesterdaySick: "+yesterdaySick);
}else if(healthy>0){
Console.WriteLine("else if(healthy>0){");
sick += healthy;
Console.WriteLine("sick: "+sick);
healthy = 0;
Console.WriteLine("healthy: "+healthy);
}
sicksPerDay.Add(sick-sicksPerDay.Sum());
Console.WriteLine("sicksPerDay: "+String.Join(", ",sicksPerDay));
if(day>=days_to_recover){
Console.WriteLine("if(day>=days_to_recover){");
recovered += sicksPerDay[day-days_to_recover];
Console.WriteLine("recovered: "+recovered);
if(sick>recovered){
sick -= recovered;
}else{
sick = 0;
}
Console.WriteLine("sick: "+sick);
}
}
Console.WriteLine("day: "+day);
return day;
}
}
}
Being the sample tests:
namespace Covid19 {
using NUnit.Framework;
using System;
using System.Linq;
[TestFixture]
public class PandemyTest
{
[Test]
public void Test1()
{
Assert.AreEqual(6, Pandemy.PandemyStops(1, 5, 1, 3));
}
[Test]
public void Test2()
{
Assert.AreEqual(8, Pandemy.PandemyStops(1, 100, 3, 4));
}
[Test]
public void Test3()
{
Assert.AreEqual(12, Pandemy.PandemyStops(5, 500, 2, 7));
}
}
}
An example trace for Test1:
sick: 1
people: 5
spread: 1
days_to_recover: 3
healthy: 4
sicksPerDay:
day: 0
if(healthy > (sick*spread)){
sick: 2
healthy: 3
yesterdaySick: 2
sicksPerDay: 2
day: 1
if(healthy > (sick*spread)){
sick: 4
healthy: 1
yesterdaySick: 4
sicksPerDay: 2, 2
day: 2
else if(healthy>0){
sick: 5
healthy: 0
sicksPerDay: 2, 2, 1
day: 3
sicksPerDay: 2, 2, 1, 0
if(day>=days_to_recover){
recovered: 2
sick: 3
day: 4
sicksPerDay: 2, 2, 1, 0, -2
if(day>=days_to_recover){
recovered: 4
sick: 0
day: 5
sicksPerDay: 2, 2, 1, 0, -2, -3
if(day>=days_to_recover){
recovered: 5
sick: 0
day: 6
How would you simplify this code to be more readable and use less variables?
In addition, random tests, make the code times out (execution time is longer than 12000ms). How would you shorten its execution time, to handle bigger inputs?
We have read:
https://docs.microsoft.com/es-es/dotnet/csharp/language-reference/builtin-types/integral-numeric-types
https://stackoverflow.com/questions/59011783/c-sharp-console-writeline
https://www.geeksforgeeks.org/c-sharp-list-class/
https://stackoverflow.com/questions/759133/how-to-display-list-items-on-console-window-in-c-sharp
https://stackoverflow.com/questions/793000/create-listint-with-values-at-compile-time
Aucun commentaire:
Enregistrer un commentaire