Most frequent k chars distance: Difference between revisions

m
No edit summary
m (→‎{{header|Java}}: improved)
Line 154:
Translation of the pseudo-code of the Wikipedia article [[wp:Most frequent k characters]] to [[wp:java]] implementation of three functions given in the definition section are given below with [[wp:JavaDoc]] comments:
 
<lang java>import java.util.Collections;
<lang Java>
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedListArrayList;
import java.util.List;
import java.util.Map;
Line 189 ⟶ 187:
* @return sorted HashMap
*/
private static HashMap<K, sortByValues(HashMapV map)extends {Comparable<? super V>> HashMap<K, V>
descendingSortByValues(HashMap<K, V> map) {
List list = new LinkedList(map.entrySet());
List<Map.Entry<K, V>> list = new ArrayList<Map.Entry<K, V>>(map.entrySet());
// Defined Custom Comparator here
// Defined Collections.sort(list, newCustom Comparator() {here
Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
public int compare(ObjectMap.Entry<K, V> o1, ObjectMap.Entry<K, V> o2) {
return -1*((Comparable) ((Map.Entry) (o1)).getValue())
return o2.compareTogetValue(((Map).Entry) compareTo(o2))o1.getValue());
}
}
});
 
// Here I am copying the sorted list in HashMap
// using LinkedHashMap to preserve the insertion order
HashMap<K, HashMapV> sortedHashMap = new LinkedHashMap<K, V>();
for (IteratorMap.Entry<K, itV> =entry : list.iterator(); it.hasNext();) {
MapsortedHashMap.Entry put(entry = .getKey(Map.Entry), itentry.nextgetValue());
}
sortedHashMap.put(entry.getKey(), entry.getValue());
return sortedHashMap;
}
return sortedHashMap;
} /**
* get most frequent k characters
/**
* get most* @param frequentarray kof characters
* @param arraylimit of charactersk
* @paramreturn limithashed ofString k
*/
* @return hashed String
*/
public static String mostOcurrencesElement(char[] array, int k) {
HashMap<Character, Integer> countMap = countElementOcurrences(array);
System.out.println(countMap);
Map<IntegerCharacter, StringInteger> map = sortByValuesdescendingSortByValues(countMap);
System.out.println(map);
Iterator it = map.entrySet().iterator();
int i = 0;
String output = new String()"";
whilefor (itMap.hasNextEntry<Character, Integer> pairs : map.entrySet()&&i++<k) {
if (i++ >= k)
Map.Entry pairs = (Map.Entry)it.next();
break;
output += ""+pairs.getKey()+pairs.getValue();
output += sortedHashMap.put(entry"" + pairs.getKey(), entry+ pairs.getValue());
it.remove(); // avoids a ConcurrentModificationException
}
return output;
Line 236 ⟶ 232:
* @return distance as integer
*/
public static int getDiff(String str1, String str2, int limit) {
int similarity = 0;
for (int i = 0; i < str1.length(); i += 2) {
System.out.println(i);
int pos = str2.indexOf(str1.charAt(i));
System.out.println(str2.charAt(i) + " - " + pos);
if (pos >= 0) {
similarity += Integer.parseInt(str2.substring(pos+1, pos+2)) + Character.getNumericValue(str1.charAt(i+1)) ;
}
Line 257 ⟶ 253:
* @return distance as integer
*/
public static int SDFfunc(String str1, String str2, int limit) {
return getDiff(mostOcurrencesElement(str1.toCharArray(), 2), mostOcurrencesElement(str2.toCharArray(), 2), limit);
}
 
public static void main(String[] args) {
String input1 = new String("LCLYTHIGRNIYYGSYLYSETWNTGIMLLLITMATAFMGYVLPWGQMSFWGATVITNLFSAIPYIGTNLV");
String input2 = new String("EWIWGGFSVDKATLNRFFAFHFILPFTMVALAGVHLTFLHETGSNNPLGLTSDSDKIPFHPYYTIKDFLG");
System.out.println(SDF sdf = new SDF.SDFfunc(input1,input2,100));
System.out.println (sdf.SDFfunc(input1,input2,100));
 
}
 
}</lang Java>
}
</lang>
 
 
 
==References==
Anonymous user