Word wheel: Difference between revisions
Content added Content deleted
(New post.) |
(Added code to complete the optional extra task.) |
||
Line 1,610: | Line 1,610: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
<syntaxhighlight lang="java"> |
<syntaxhighlight lang="java"> |
||
import java.io.BufferedReader; |
import java.io.BufferedReader; |
||
import java.io.IOException; |
import java.io.IOException; |
||
Line 1,616: | Line 1,615: | ||
import java.io.InputStreamReader; |
import java.io.InputStreamReader; |
||
import java.net.URI; |
import java.net.URI; |
||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
import java.util.function.Predicate; |
import java.util.function.Predicate; |
||
import java.util.stream.Collectors; |
|||
public final class |
public final class WordWheelExtended { |
||
public static void main(String[] args) throws IOException { |
public static void main(String[] args) throws IOException { |
||
String wordWheel = "N D E" |
String wordWheel = "N D E" |
||
+ "O K G" |
+ "O K G" |
||
+ "E L W"; |
+ "E L W"; |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
List<String> words = reader.lines().collect(Collectors.toList()); |
|||
reader.close(); |
|||
⚫ | |||
String middleLetter = allLetters.substring(4, 5); |
String middleLetter = allLetters.substring(4, 5); |
||
Line 1,633: | Line 1,641: | ||
} |
} |
||
for ( String letter : allLetters.split( |
for ( String letter : allLetters.split("") ) { |
||
word = word.replaceFirst(letter, |
word = word.replaceFirst(letter, ""); |
||
} |
} |
||
Line 1,640: | Line 1,648: | ||
}; |
}; |
||
⚫ | |||
⚫ | |||
⚫ | |||
// OPTIONAL EXTRA // |
|||
⚫ | |||
⚫ | |||
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: | Line 1,719: | ||
wok |
wok |
||
woke |
woke |
||
Most words found = 215 |
|||
The nine letter words producing this total are: |
|||
claremont with central letter 'a' |
|||
spearmint with central letter 'a' |
|||
</pre> |
</pre> |
||