Created
May 4, 2022 17:43
-
-
Save Hesamedin/eb89ebec41c56cddc5bc19ab15a1168a to your computer and use it in GitHub Desktop.
Write a function top10words that takes as input a list of words and returns a list of 10 most frequent words in the input list
This file contains hidden or 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 'package:collection/collection.dart'; | |
// Write a function top10words that takes as input a list of words | |
// and returns a list of 10 most frequent words in the input list | |
void main() { | |
// TODO:: call top10words on words list below | |
// var top10 = top10words(words); | |
// expects top10 to include "Iris", "Elisa", "Jack" | |
// "George", "Henry", "Fan", "Dillon", "Cathy", "Bob", "Alice" | |
print('Hello'); | |
// 1. Get a HashMap | |
Map<String, int> map = getMap(words); | |
// 2. Find the 10 most releaed items | |
// List list = map.values.toList(); | |
// list.sort(); // [10, 9 ,8, ....] | |
// for(var i = 0; i < 10; i++) { | |
// print('${list[i]}-'); | |
// } | |
// Map the Map to List<Item> | |
List<Item> items = []; | |
map.forEach((key, i) => items.add(Item(key, i))); | |
items.sort((item1, item2) => item2.value.compareTo(item1.value)); | |
for(var i = 0; i < 10; i++) { | |
print('${items[i].key}-'); | |
} | |
} | |
class Item { | |
Item(this.key, this.value); | |
String key = ''; | |
int value = 0; | |
} | |
// 1. | |
Map<String, int> getMap(List<String> list) { | |
Map<String, int> map = Map<String, int>(); | |
for(var i = 0; i < list.length; i++) { | |
String key = list[i]; | |
if(map[key] == null) { | |
map[key] = 1; | |
} else { | |
int value = map[key]!; | |
value++; | |
map[key] = value; | |
} | |
} | |
return map; | |
} | |
const List<String> words = [ | |
"Quintilianus", | |
"Natalius", | |
"Euryalos", | |
"Pelagius", | |
"Maria", | |
"Yocheved", | |
"Vester", | |
"Mahesh", | |
"Judith", | |
"Gabriel", | |
"Flanagan", | |
"Kidlat", | |
"Henricus", | |
"Rhoda", | |
"Ala", | |
"Hac", | |
"Ivet", | |
"Domitila", | |
"Sendoa", | |
"Sebastian", | |
"Jagadisha", | |
"Hubertus", | |
"Nuka", | |
"Kishori", | |
"Tom", | |
"Alice", | |
"Alice", | |
"Alice", | |
"Alice", | |
"Alice", | |
"Alice", | |
"Alice", | |
"Alice", | |
"Alice", | |
"Alice", | |
"Bob", | |
"Bob", | |
"Bob", | |
"Bob", | |
"Bob", | |
"Bob", | |
"Bob", | |
"Bob", | |
"Bob", | |
"Cathy", | |
"Cathy", | |
"Cathy", | |
"Cathy", | |
"Cathy", | |
"Cathy", | |
"Cathy", | |
"Cathy", | |
"Dillon", | |
"Dillon", | |
"Dillon", | |
"Dillon", | |
"Dillon", | |
"Dillon", | |
"Dillon", | |
"Elisa", | |
"Elisa", | |
"Elisa", | |
"Elisa", | |
"Elisa", | |
"Elisa", | |
"Fan", | |
"Fan", | |
"Fan", | |
"Fan", | |
"Fan", | |
"George", | |
"George", | |
"George", | |
"George", | |
"George", | |
"George", | |
"Henry", | |
"Henry", | |
"Henry", | |
"Henry", | |
"Henry", | |
"Iris", | |
"Iris", | |
"Iris", | |
"Iris", | |
"Iris", | |
"Jack", | |
"Jack", | |
"Jack", | |
"Jack", | |
"Jack", | |
]; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment