Word wheel: Difference between revisions

2,046 bytes added ,  10 months ago
Added code to complete the optional extra task.
(New post.)
(Added code to complete the optional extra task.)
Line 1,610:
=={{header|Java}}==
<syntaxhighlight lang="java">
 
import java.io.BufferedReader;
import java.io.IOException;
Line 1,616 ⟶ 1,615:
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
 
public final class WordWheelWordWheelExtended {
 
public static void main(String[] args) throws IOException {
String wordWheel = "N D E"
+ "O K G"
+ "E L W";
String url = "http://wiki.puzzlers.org/pub/wordlists/unixdict.txt";
String allLetters = wordWheel.toLowerCase().replace(SPACE_CHARACTER, EMPTY_STRING);
InputStream stream = URI.create(url).toURL().openStream();
BufferedReader reader = new BufferedReader( new InputStreamReader(stream) );
List<String> words = reader.lines().collect(Collectors.toList());
reader.close();
String allLetters = wordWheel.toLowerCase().replace(SPACE_CHARACTER" ", EMPTY_STRING"");
String middleLetter = allLetters.substring(4, 5);
Line 1,633 ⟶ 1,641:
}
for ( String letter : allLetters.split(EMPTY_STRING"") ) {
word = word.replaceFirst(letter, EMPTY_STRING"");
}
Line 1,640 ⟶ 1,648:
};
readerwords.linesstream().filter(correctWords).forEach(System.out::println);
String url = "http://wiki.puzzlers.org/pub/wordlists/unixdict.txt";
InputStream stream = URI.create(url).toURL().openStream();
// OPTIONAL EXTRA //
BufferedReader reader = new BufferedReader( new InputStreamReader(stream) );
reader.lines().filter(correctWords).forEach(System.out::println);
int maxWordsFound = 0;
List<String> bestWords9 = new ArrayList<String>();
List<Character> bestCentralLetters = new ArrayList<Character>();
List<String> words9 = words.stream().filter( word -> word.length() == 9 ).toList();
 
for ( String word9 : words9 ) {
List<Character> distinctLetters = word9.chars().mapToObj( i -> (char) i ).distinct().toList();
for ( char letter : distinctLetters ) {
int wordsFound = 0;
for ( String word : words ) {
if ( word.length() >= 3 && word.indexOf(letter) >= 0 ) {
List<Character> letters = new ArrayList<Character>(distinctLetters);
boolean validWord = true;
for ( char ch : word.toCharArray() ) {
final int index = letters.indexOf(ch);
if ( index == -1 ) {
validWord = false;
break;
}
letters.remove(index);
}
if ( validWord ) {
wordsFound += 1;
}
}
}
if ( wordsFound > maxWordsFound ) {
maxWordsFound = wordsFound;
bestWords9.clear();
bestWords9.add(word9);
bestCentralLetters.clear();
bestCentralLetters.add(letter);
} else if ( wordsFound == maxWordsFound ) {
bestWords9.add(word9);
bestCentralLetters.add(letter);
}
}
}
System.out.println(System.lineSeparator() + "Most words found = " + maxWordsFound);
System.out.println("The nine letter words producing this total are:");
for ( int i = 0; i < bestWords9.size(); i++ ) {
System.out.println(bestWords9.get(i) + " with central letter '" + bestCentralLetters.get(i) + "'");
}
}
private static final String EMPTY_STRING = "";
private static final String SPACE_CHARACTER = " ";
 
}
Line 1,669 ⟶ 1,719:
wok
woke
 
Most words found = 215
The nine letter words producing this total are:
claremont with central letter 'a'
spearmint with central letter 'a'
</pre>
 
908

edits