-
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
b33bcc9
commit 2490f6f
Showing
12 changed files
with
695 additions
and
0 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
...4/20-dictionaries/code/01_OperacionesConDiccionarios/01_OperacionesConDiccionarios.csproj
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,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<RootNamespace>_01_OperacionesConDiccionarios</RootNamespace> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
106 changes: 106 additions & 0 deletions
106
conferences/2024/20-dictionaries/code/01_OperacionesConDiccionarios/ProgramAgenda.cs
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,106 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
|
||
namespace Programacion | ||
{ | ||
class Program | ||
{ | ||
[STAThread] | ||
static void Main(string[] args) | ||
{ | ||
|
||
Dictionary<string, string> agenda = new Dictionary<string, string>(); | ||
|
||
string nombre, telefono; | ||
while (true) | ||
{ | ||
#region CREANDO AGENDA VERSION 1 | ||
Console.Write("\nEntre nombre: "); | ||
nombre = Console.ReadLine(); | ||
if (nombre.Length == 0) break; | ||
|
||
if (agenda.ContainsKey(nombre)) //Aquí estaría buscando dos veces. Primero por el Contains | ||
Console.WriteLine("{0} ya esta en agenda su num es {1}", nombre, agenda[nombre]); | ||
else | ||
{ | ||
Console.Write("Entra su numero de telefono: "); | ||
telefono = Console.ReadLine(); | ||
agenda.Add(nombre, telefono); | ||
} | ||
#endregion | ||
|
||
#region VERSION 2 UN POCO MÁS EFICIENTE | ||
//Console.Write("\nEntra nombre en inglés (Enter para terminar): "); | ||
//nombre = Console.ReadLine(); | ||
//if (nombre.Length == 0) break; | ||
//if (agenda.TryGetValue(nombre, out telefono)) //Si está devuelve true y a la vez el telefono en el parámetro | ||
// Console.WriteLine("{0} => {1}", nombre, telefono); | ||
|
||
//else | ||
//{ | ||
// Console.Write("Entre su numero de telefono: "); | ||
// telefono = Console.ReadLine(); | ||
// if (telefono.Length == 0) continue; | ||
// else | ||
// agenda.Add(nombre, telefono); | ||
//} | ||
#endregion | ||
|
||
} | ||
//Ejecutar el codigo anterior pero entrando el nombre con alguna mayuscula | ||
//Dictionary se basa en el Equals de string que es sensible a la diferencia | ||
//y por tanto juan no es igual a Juan | ||
|
||
#region PRUEBA DE EXCEPCIÓN POR USAR LLAVE INEXISTENTE | ||
//Probar con un nombre que se diferencie en las mayusc minus | ||
//supongamos que esta juan pero no Juan | ||
//lo siguiente debe dar excepcion | ||
|
||
//Console.WriteLine(agenda["Juan"]); | ||
#endregion | ||
|
||
#region QUITANDO DEL DICCIONARIO | ||
//while (true) | ||
//{ | ||
// Console.Write("\nEntre nombre a quitar: "); | ||
// nombre = Console.ReadLine(); | ||
// if (nombre.Length == 0) break; | ||
// agenda.Remove(nombre); | ||
// //Si no esta da excepcion | ||
|
||
// ////De esta forma no da excepción si no está | ||
// //Console.WriteLine("{0} es {1}", | ||
// // nombre, | ||
// // agenda.TryGetValue(nombre, out telefono) ? telefono : "No esta en la agenda"); | ||
//} | ||
#endregion | ||
|
||
#region RECORRER DICCIONARIO COMO IENUMERABLE | ||
//Ver las tres formas de hacer lo mismo | ||
//Console.WriteLine("\nLISTANDO LOS NOMBRES DE LA AGENDA\n"); | ||
//Console.WriteLine(" Listar recorriendo los KeyValuePairs"); | ||
//foreach (KeyValuePair<string, string> kv in agenda) | ||
// Console.WriteLine(" {0, -20}{1, -20}", kv.Key, kv.Value); | ||
|
||
//Console.WriteLine("\nListar infiriendo el tipo segun la parte derecha"); | ||
//foreach (var kv1 in agenda) | ||
// Console.WriteLine(" {0, -20}{1, -20}", kv1.Key, kv1.Value); | ||
|
||
//Console.WriteLine("\nListar deconstruyendolo como tuplo"); | ||
//foreach ((string name,string phone) in agenda) | ||
// Console.WriteLine(" {0, -20}{1, -20}", name, phone); | ||
#endregion | ||
|
||
#region RECORRER DICCIONARIO ESCRIBIENDO SU GETHASHCODE | ||
//Ver las tres formas de hacer lo mismo | ||
Console.WriteLine("\nLISTANDO LOS NOMBRES DE LA AGENDA\n"); | ||
Console.WriteLine("\nListar tambien el GetHashCode"); | ||
foreach ((string name, string phone) in agenda) | ||
Console.WriteLine(" {0, -20}{1, -20}{2, -20}", name, phone, name.GetHashCode()); | ||
#endregion | ||
} | ||
} | ||
} | ||
|
11 changes: 11 additions & 0 deletions
11
...es/2024/20-dictionaries/code/02_UsandoIEqualityComparer/02_UsandoIEqualityComparer.csproj
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,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<RootNamespace>_02_UsandoIEqualityComparer</RootNamespace> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
73 changes: 73 additions & 0 deletions
73
conferences/2024/20-dictionaries/code/02_UsandoIEqualityComparer/ProgramIEqualityComparer.cs
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,73 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace Programacion | ||
{ | ||
class StringEqualityComparer : IEqualityComparer<string> | ||
{ | ||
public bool Equals(string? x, string? y) | ||
{ | ||
return x.ToUpper().Equals(y.ToUpper()); | ||
} | ||
|
||
public int GetHashCode(string obj) | ||
{ | ||
return obj.ToUpper().GetHashCode(); | ||
|
||
//Comentar arriba y descomentar abajo | ||
//Ver que funciona | ||
//Llaves iguales deben tener el mismo GetHashCode | ||
//pero llaves diferentes tambien pueden tener un mismo GetHashCode | ||
//es un concepto que se usa para eficiencia de implementacion como se vera | ||
//mas adelante | ||
|
||
//return 1000; | ||
} | ||
} | ||
static class ProgramIqualityComparer | ||
{ | ||
[STAThread] | ||
static void Main(string[] args) | ||
{ | ||
//Ver la sobrecarga del constructor de Dictionary | ||
Dictionary<string, string> agenda = | ||
new Dictionary<string, string>(new StringEqualityComparer()); | ||
|
||
string nombre, telefono; | ||
while (true) | ||
{ | ||
#region | ||
Console.Write("\nEntre nombre: "); | ||
nombre = Console.ReadLine(); | ||
if (nombre.Length == 0) break; | ||
|
||
if (agenda.ContainsKey(nombre)) //Aquí estaría buscando dos veces. Primero por el Contains | ||
Console.WriteLine("{0} ya esta en agenda su num es {1}", nombre, agenda[nombre]); | ||
else | ||
{ | ||
Console.Write("Entra su numero de telefono: "); | ||
telefono = Console.ReadLine(); | ||
agenda.Add(nombre, telefono); | ||
} | ||
} | ||
#endregion | ||
|
||
#region RECORRER DICCIONARIO ESCRIBIENDO SU GETHASHCODE | ||
//Ver las tres formas de hacer lo mismo | ||
Console.WriteLine("\nLISTANDO LOS NOMBRES DE LA AGENDA\n"); | ||
Console.WriteLine("\nListar tambien el GetHashCode"); | ||
foreach ((string name, string phone) in agenda) | ||
Console.WriteLine(" {0, -20}{1, -20}{2, -20}", name, phone, name.GetHashCode()); | ||
#endregion | ||
} | ||
} | ||
} | ||
#region EJERCICIOS PARA CLASE PRACTICA | ||
//1) Implemente un metodo que dado un diccionario dicc de tipo Dictionary<TKet, TValue) | ||
//devuelva un IEnumerable de la forma IEnumerable<TValue, IEnumerable<TKey>> | ||
//donde IEnumerable<TKey> regresenta a la coleccion de todos los valores que tienen | ||
//La misma llave en el diccionario original | ||
#endregion |
11 changes: 11 additions & 0 deletions
11
.../2024/20-dictionaries/code/03_FibonnaciConDiccionarios/03_FibonnaciConDiccionarios.csproj
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,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<RootNamespace>_03_FibonnaciConDiccionarios</RootNamespace> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
79 changes: 79 additions & 0 deletions
79
conferences/2024/20-dictionaries/code/03_FibonnaciConDiccionarios/ProgramFibMemorizado.cs
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,79 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
|
||
namespace Programacion | ||
{ | ||
|
||
class PruebaFibMemorizado | ||
{ | ||
[STAThread] | ||
|
||
#region Fibonacci Recursivo Ineficiente | ||
static long Fibonacci(int n) | ||
{ | ||
if (n == 1 || n == 2) return 1L; | ||
else return Fibonacci(n - 2) + | ||
Fibonacci(n - 1); | ||
} | ||
#endregion | ||
|
||
#region Fibonacci Memorizado con un diccionario | ||
static Dictionary<int, long> dic = new Dictionary<int, long>(); | ||
static long FibonacciMemorizado(int n) | ||
{ | ||
long result; | ||
if (!dic.TryGetValue(n, out result)) | ||
//Si está es porque ya ha sido calculado para ese valor. | ||
//Si no esta lo calculamos y lo guardamos en el diccionario | ||
{ | ||
//Si no está en el diccionario entonces no ha sido calculado. | ||
if (n == 1 || n == 2) result = 1L; | ||
else result = FibonacciMemorizado(n - 2) + | ||
FibonacciMemorizado(n - 1); | ||
dic.Add(n, result); | ||
//El nuevo Fibonacci valculado para n lo guardamos en el diccionarioCalcularlo y Guardarlo | ||
} | ||
return result; | ||
} | ||
|
||
#endregion | ||
|
||
static void Main(string[] args) | ||
{ | ||
#region USANDO DICCIONARIO PARA EL PATRÓN MEMOIZE CON FIBONACCI | ||
//Empezar con el recursivo para recordar por qué es ineficiente | ||
Stopwatch crono = new Stopwatch(); | ||
int valor; long result; | ||
while (true) | ||
{ | ||
Console.Write("\nEntre número a calcular Fibonacci "); | ||
string s = Console.ReadLine(); | ||
if (int.TryParse(s, out valor)) | ||
{ | ||
//Empezar con el ineficiente para mostrar demora | ||
crono.Start(); | ||
result = Fibonacci(valor); | ||
crono.Stop(); | ||
Console.WriteLine("Fibonacci Recursivo de {0} = {1} calculado en {2} ms", valor, result, crono.ElapsedMilliseconds); | ||
|
||
//PRUEBA DE FIBONACCI MEMORIZADO. Descomentar este para probar con diccionario | ||
//crono.Restart(); | ||
//result = FibonacciMemorizado(valor); | ||
//crono.Stop(); | ||
//Console.WriteLine("Fibonacci Memorizado de {0} = {1} calculado en {2} ms", valor, result, crono.ElapsedMilliseconds); | ||
|
||
//Descomentar para ver la cantidad de entradas(llaves) que se han guardado en el diccionario | ||
//Console.WriteLine("Hay {0} entradas en el diccionario", dic.Count); | ||
} | ||
else break; | ||
} | ||
#endregion | ||
} | ||
} | ||
} | ||
#region EJERCICIOS CLASE PRACTICA | ||
//Defina una funcion que reciba como parametro una funcion y devuelva una funcion | ||
//que haga lo mismo pero con capacidad de memorizacion | ||
#endregion |
11 changes: 11 additions & 0 deletions
11
...plementacionDeDiccionarioUsandoHashing/04_ImplementacionDeDiccionarioUsandoHashing.csproj
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,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<RootNamespace>_04_ImplementacionDeDiccionarioUsandoHashing</RootNamespace> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
Oops, something went wrong.