-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
15c0ec9
commit 9270e16
Showing
7 changed files
with
524 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Sets\Sets.csproj" /> | ||
</ItemGroup> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
using MatCom.Logic; | ||
|
||
|
||
class Program | ||
{ | ||
static void Main() | ||
{ | ||
TestEmptySetHasSizeZero(); | ||
TestSingletonSetHasSizeOne(); | ||
TestContains(); | ||
TestSetRemovesDuplicates(); | ||
|
||
TestUnionOfDisjointSets(); | ||
TestUnionOfSameElements(); | ||
TestUnionOfSameAndDifferentElements(); | ||
|
||
TestIntersection(); | ||
|
||
Console.WriteLine("✅ Everything OK!"); | ||
} | ||
|
||
static void Assert(bool condition, string message = "") | ||
{ | ||
if (!condition) | ||
{ | ||
throw new Exception(message); | ||
} | ||
} | ||
|
||
static void TestEmptySetHasSizeZero() | ||
{ | ||
Set s = new Set(); | ||
Assert(s.Size == 0, "Size should be 0, not " + s.Size); | ||
} | ||
|
||
static void TestSingletonSetHasSizeOne() | ||
{ | ||
Set s = new Set(42); | ||
Assert(s.Size == 1, "Size should be 1, not " + s.Size); | ||
} | ||
|
||
static void TestContains() | ||
{ | ||
Set s = new Set(1, 2, 3); | ||
|
||
Assert(s.Contains(1), "Should contain 1"); | ||
Assert(s.Contains(2), "Should contain 2"); | ||
Assert(s.Contains(3), "Should contain 3"); | ||
Assert(!s.Contains(4), "Should not contain 4"); | ||
} | ||
|
||
static void TestSetRemovesDuplicates() | ||
{ | ||
Set s = new Set(1, 2, 1, 3, 2); | ||
|
||
Assert(s.Size == 3, "Size should be 3, not " + s.Size); | ||
} | ||
|
||
static void TestUnionOfDisjointSets() | ||
{ | ||
Set s1 = new Set(1, 2, 3); | ||
Set s2 = new Set(4, 5, 6, 7); | ||
Set s3 = s1.Union(s2); | ||
|
||
Assert(s3.Size == s1.Size + s2.Size, "Should have all of the elements"); | ||
} | ||
|
||
static void TestUnionOfSameElements() | ||
{ | ||
Set s1 = new Set(1, 2, 3); | ||
Set s2 = new Set(1, 2, 3); | ||
Set s3 = s1.Union(s2); | ||
|
||
Assert(s3.Size == s1.Size, "Should have exactly one copy of the elements"); | ||
} | ||
|
||
static void TestUnionOfSameAndDifferentElements() | ||
{ | ||
Set s1 = new Set(1, 2, 3); | ||
Set s2 = new Set(1, 2, 3, 4); | ||
Set s3 = s1.Union(s2); | ||
|
||
Assert(s3.Size == Math.Max(s1.Size, s2.Size), "Should have exactly one copy of the elements"); | ||
} | ||
|
||
static void TestIntersection() | ||
{ | ||
Set s1 = new Set(1, 2, 3, 4, 5); | ||
Set s2 = new Set(2, 4, 6, 8); | ||
Set s3 = s1.Intersection(s2); | ||
|
||
Assert(s3.Size == 2, "Should contain only 2 and 4"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 16 | ||
VisualStudioVersion = 16.0.30114.105 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sets", "Sets\Sets.csproj", "{FDECD36D-DDE4-45AA-B532-98189C7B2BBF}" | ||
EndProject | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "App", "App\App.csproj", "{F13681D9-7077-4337-9AC7-DFCAFFF723DF}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{FDECD36D-DDE4-45AA-B532-98189C7B2BBF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{FDECD36D-DDE4-45AA-B532-98189C7B2BBF}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{FDECD36D-DDE4-45AA-B532-98189C7B2BBF}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{FDECD36D-DDE4-45AA-B532-98189C7B2BBF}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{F13681D9-7077-4337-9AC7-DFCAFFF723DF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{F13681D9-7077-4337-9AC7-DFCAFFF723DF}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{F13681D9-7077-4337-9AC7-DFCAFFF723DF}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{F13681D9-7077-4337-9AC7-DFCAFFF723DF}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
namespace MatCom.Logic; | ||
|
||
public class Set | ||
{ | ||
private int[] elements; | ||
|
||
// Constructor privado que solo puedo llamar desde esta clase, | ||
// por lo tanto según el uso, a veces puedo confiar en que los | ||
// elementos no estén repetidos, y me ahorro tener que hacer esa | ||
// verificación 2 veces. | ||
private Set(int[] elements, bool safe) | ||
{ | ||
if (!safe) | ||
{ | ||
elements = Unique(elements); | ||
} | ||
|
||
this.elements = elements; | ||
} | ||
|
||
// Constructor público que llamarán los usuarios de la biblioteca, | ||
// por definición no puedo confiar en la entrada. | ||
public Set(params int[] elements) | ||
{ | ||
this.elements = Unique(elements); | ||
} | ||
|
||
public int Size | ||
{ | ||
get { return this.elements.Length; } | ||
} | ||
|
||
public bool Contains(int x) | ||
{ | ||
for (int i = 0; i < this.elements.Length; i++) | ||
{ | ||
if (this.elements[i] == x) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public Set Union(Set other) | ||
{ | ||
int[] union = new int[this.Size + other.Size]; | ||
int total = 0; | ||
|
||
// Como cada conjunto no tiene elementos repetidos, | ||
// podemos poner con total seguridad los del conjunto actual | ||
for (int i = 0; i < this.Size; i++) | ||
{ | ||
union[total++] = this.elements[i]; | ||
} | ||
|
||
// Ahora ponemos los del otro conjunto, que algunos estarán repetidos | ||
for (int i = 0; i < other.Size; i++) | ||
{ | ||
union[total++] = other.elements[i]; | ||
} | ||
|
||
return new Set(union); | ||
} | ||
|
||
public Set Intersection(Set other) | ||
{ | ||
int[] intersection = new int[Math.Min(this.Size, other.Size)]; | ||
int total = 0; | ||
|
||
// Por cada elemento del conjunto actual, lo ponemos si y solo si | ||
// también está en el otro conjunto | ||
// Usaremos un ciclo foreach porque no nos interesa el índice | ||
foreach (int x in this.elements) | ||
{ | ||
if (other.Contains(x)) | ||
{ | ||
intersection[total++] = x; | ||
} | ||
} | ||
|
||
// Finalmente devolvemos un Set nuevo pero solo con la cantidad | ||
// de elementos necesarios, usando el constructor privado | ||
return new Set(Resize(intersection, total), true); | ||
} | ||
|
||
#region Métodos auxiliares | ||
|
||
private static int[] Unique(int[] elements) | ||
{ | ||
int[] temp = new int[elements.Length]; | ||
int total = 0; | ||
|
||
// Poner en temp solamente aquellos elementos nuevos | ||
for (int i = 0; i < elements.Length; i++) | ||
{ | ||
if (!Find(elements[i], temp, total)) | ||
{ | ||
temp[total++] = elements[i]; | ||
} | ||
} | ||
|
||
return Resize(temp, total); | ||
} | ||
|
||
private static int[] Resize(int[] array, int newSize) | ||
{ | ||
// Crear un nuevo array con la cantidad justa de elementos | ||
int[] result = new int[newSize]; | ||
|
||
for (int i = 0; i < newSize; i++) | ||
{ | ||
result[i] = array[i]; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
private static bool Find(int x, int[] array, int length) | ||
{ | ||
for (int i = 0; i < length; i++) | ||
{ | ||
if (array[i] == x) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
#endregion | ||
} | ||
|
||
// EJERCICIOS | ||
|
||
// 1) Adicione el método de instancia `Set Difference(Set other)` que devuelve el conjunto diferencia entre el | ||
// conjunto actual y `other`, e implemente los tests que considere necesarios para evaluarlo. | ||
|
||
// 2) Adicione un método de instancia `bool Equivalent(Set other)` que devuelve `true` si y solo si ambos | ||
// conjuntos tienen exactamente los mismos elementos, e implemente los tests necesarios. | ||
// Recuerde que los conjuntos no tienen orden intrínseco. | ||
|
||
// 3) Adicione un método de instancia `string PrettyPrint()` que devuelve un `string` con los elementos del | ||
// conjunto, en la notación usual de lógica, e.j. `{ 1, 3, 2, 9, 0 }`. Los elementos pueden estar en cualquier orden. | ||
// Hint: Utilice la clase `StringBuilder` para construir un `string` de forma más eficiente que con el operador `+`. | ||
|
||
// 4) Modifique el método `Union` para que no necesite llamar al constructor público, sino que garantice directamente | ||
// que los elementos no están duplicados y por tanto se pueda llamar al constructor privado. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
test: | ||
dotnet run --project App |
Oops, something went wrong.