Skip to content

Instantly share code, notes, and snippets.

@AyadLaouissi
Last active June 18, 2024 13:48
Show Gist options
  • Save AyadLaouissi/0e46839b3d0e68400721e7151f73e724 to your computer and use it in GitHub Desktop.
Save AyadLaouissi/0e46839b3d0e68400721e7151f73e724 to your computer and use it in GitHub Desktop.
Sorting and searching of words
import 'dart:io';
import 'package:board_generator_playground/board_generator_playground.dart';
import 'package:csv/csv.dart';
void main() {
final fileString = File('words.csv').readAsStringSync();
final rows = const CsvToListConverter().convert(fileString);
final wordsList = rows.map((row) {
return (row[0] as String).replaceAll(' ', '').trim().toLowerCase();
}).toList()
..removeWhere((element) => !RegExp(r'^[a-zA-Z]*$').hasMatch(element));
final watch = Stopwatch()..start();
final sortedWords = sortWords(wordsList);
print('Sorted list in ${watch.elapsedMicroseconds} Microseconds\n');
watch
..reset()
..start();
print('Searching with sorted list');
print(
'Find a word 10 characters with second character e: '
'${sortedWords[10]?[1]?['e']?.first}',
);
print(
'Find a word 8 characters with second character e: '
'${sortedWords[8]?[1]?['e']?.first}',
);
print(
'Find a word 6 characters with second character e: '
'${sortedWords[6]?[1]?['e']?.first}',
);
print(
'Find a word 12 characters with second character e: '
'${sortedWords[12]?[1]?['e']?.first}',
);
print(
'Find a word 16 characters with second character e: '
'${sortedWords[16]?[1]?['e']?.first}',
);
print('Executed in ${watch.elapsedMicroseconds} Microseconds\n');
watch
..reset()
..start();
print('Searching with list of words');
print(
'Find a word 10 characters with second character e: '
'${wordsList.firstWhere((word) => word.length == 10 && word[1] == 'e')}',
);
print(
'Find a word 8 characters with second character e: '
'${wordsList.firstWhere((word) => word.length == 8 && word[1] == 'e')}',
);
print(
'Find a word 6 characters with second character e: '
'${wordsList.firstWhere((word) => word.length == 6 && word[1] == 'e')}',
);
print(
'Find a word 12 characters with second character e: '
'${wordsList.firstWhere((word) => word.length == 12 && word[1] == 'e')}',
);
print(
'Find a word 16 characters with second character e: '
'${wordsList.firstWhere((word) => word.length == 16 && word[1] == 'e')}',
);
print('Executed in ${watch.elapsedMicroseconds} Microseconds');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment