Created
September 21, 2020 15:43
-
-
Save jmlon/dad7b6bd375aef510e69b39e87855a17 to your computer and use it in GitHub Desktop.
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
import java.util.HashMap; | |
import edu.princeton.cs.algs4.Bag; | |
import edu.princeton.cs.algs4.MinPQ; | |
import edu.princeton.cs.algs4.Queue; | |
import edu.princeton.cs.algs4.SeparateChainingHashST; | |
import edu.princeton.cs.algs4.StdIn; | |
import edu.princeton.cs.algs4.StdOut; | |
public class Hashcodes { | |
public static void hashcodes() { | |
// final String s = "Hello"; | |
// StdOut.println(s + " : " + s.hashCode()); | |
// final String r = StdIn.readString(); | |
// StdOut.println(r + " : " + r.hashCode()); | |
// // Chequear consistencia con el equals | |
// StdOut.println(s.equals(r) && s.hashCode() == r.hashCode()); | |
Punto2DCartesiano p1 = new Punto2DCartesiano(1.0, 2.0); | |
Punto2DCartesiano p2 = new Punto2DCartesiano(1.0, 2.0); | |
StdOut.println(p1.hashCode()); | |
StdOut.println(p2.hashCode()); | |
StdOut.println(p1.hashCode() == p2.hashCode()); | |
} | |
public static <T> Iterable<T> interseccion(final T[] a, final T[] b) { | |
SeparateChainingHashST<T, Boolean> sta = new SeparateChainingHashST<>(a.length); | |
Bag<T> resultado = new Bag<>(); | |
for (T k : a) | |
sta.put(k, true); | |
for (T k : b) | |
if (sta.contains(k)) | |
resultado.add(k); | |
return resultado; | |
} | |
public static void demoInterseccion() { | |
Integer[] a = { 6, 3, 8, 12, 7 }; | |
Integer[] b = { 1, 7, 14, 3, 12 }; | |
Iterable<Integer> resultado = interseccion(a, b); | |
StdOut.println("Interseccion: "); | |
for (Integer k : resultado) | |
StdOut.println(k); | |
} | |
public static HashMap<String, Integer> wordCount(int minLen) { | |
HashMap<String, Integer> conteos = new HashMap<>(); | |
while (!StdIn.isEmpty()) { | |
String word = StdIn.readString(); | |
if (word.length() >= minLen) { | |
if (!conteos.containsKey(word)) | |
conteos.put(word, 0); | |
conteos.put(word, conteos.get(word) + 1); | |
} | |
} | |
return conteos; | |
} | |
static class Pareja implements Comparable<Pareja> { | |
String palabra; | |
Integer conteo; | |
Pareja(String p, Integer c) { | |
palabra = p; | |
conteo = c; | |
} | |
public int compareTo(Pareja b) { | |
return this.conteo - b.conteo; | |
} | |
public String toString() { | |
return palabra + " : " + conteo; | |
} | |
} | |
public static MinPQ<Pareja> topM(HashMap<String, Integer> conteos, int m) { | |
MinPQ<Pareja> pq = new MinPQ<>(); | |
int i=0; | |
for(String w: conteos.keySet()) { | |
Pareja p = new Pareja(w,conteos.get(w)); | |
if (i++<m) pq.insert(p); | |
else { | |
if (p.conteo>pq.min().conteo) { | |
pq.delMin(); | |
pq.insert(p); | |
} | |
} | |
} | |
return pq; | |
} | |
public static void main(final String[] args) { | |
// hashcodes(); | |
// demoInterseccion(); | |
HashMap<String,Integer> conteosPalabras = wordCount(3); | |
// StdOut.println("Conteos por palabra:"); | |
// for(String w: conteosPalabras.keySet()) { | |
// StdOut.println(w+" : "+conteosPalabras.get(w)); | |
// } | |
MinPQ<Pareja> pq = topM(conteosPalabras, 10); | |
StdOut.println("Las Top10 son:"); | |
while(! pq.isEmpty()) { | |
StdOut.println(pq.delMin()); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment