lots of exercises in java... from https://github.com/exercism/java

WordSearcher.java 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import java.util.*;
  2. import java.util.function.Function;
  3. import java.util.stream.Collectors;
  4. class WordSearcher {
  5. private static final List<Pair> DIRECTIONS = Arrays.asList(
  6. new Pair( 1, 0), // N
  7. new Pair( 1, 1), // NE
  8. new Pair( 0, 1), // E
  9. new Pair(-1, 1), // SE
  10. new Pair(-1, 0), // S
  11. new Pair(-1, -1), // SW
  12. new Pair( 0, -1), // W
  13. new Pair( 1, -1)); // NW
  14. /*
  15. * Search for multiple words in the given grid.
  16. */
  17. Map<String, Optional<WordLocation>> search(final Set<String> words, final char[][] grid) {
  18. return words
  19. .stream()
  20. .collect(Collectors.toMap(Function.identity(), s -> search(s, grid)));
  21. }
  22. /*
  23. * Search for a single word in the given grid.
  24. */
  25. private Optional<WordLocation> search(final CharSequence word, final char[][] grid) {
  26. final int nRows = grid.length;
  27. final int nCols = grid[0].length;
  28. // 1-indexed
  29. for (int c = 1; c <= nCols; c++) {
  30. for (int r = 1; r <= nRows; r++) {
  31. final Optional<WordLocation> wordLocation = search(word, grid, new Pair(c, r));
  32. if (wordLocation.isPresent()) return wordLocation;
  33. }
  34. }
  35. return Optional.empty();
  36. }
  37. /*
  38. * Search for a single word starting at a given coordinate within the given grid.
  39. */
  40. private Optional<WordLocation> search(final CharSequence word, final char[][] grid, final Pair startCoord) {
  41. if (grid[startCoord.getY() - 1][startCoord.getX() - 1] != word.charAt(0)) return Optional.empty();
  42. for (final Pair direction : DIRECTIONS) {
  43. final Optional<WordLocation> wordLocation = check(word, grid, startCoord, direction);
  44. if (wordLocation.isPresent()) return wordLocation;
  45. }
  46. return Optional.empty();
  47. }
  48. /*
  49. * Check whether a single word starts at a given coordinate and is aligned in a given direction within the given
  50. * grid.
  51. */
  52. private Optional<WordLocation> check(
  53. final CharSequence word,
  54. final char[][] grid,
  55. final Pair startCoord,
  56. final Pair direction) {
  57. final int nRows = grid.length;
  58. final int nCols = grid[0].length;
  59. Pair nextCoord = startCoord;
  60. for (int charIndex = 1; charIndex < word.length(); charIndex++) {
  61. nextCoord = new Pair(
  62. startCoord.getX() + charIndex * direction.getX(),
  63. startCoord.getY() + charIndex * direction.getY());
  64. if (nextCoord.getY() < 1 ||
  65. nextCoord.getY() > nRows ||
  66. nextCoord.getX() < 1 ||
  67. nextCoord.getX() > nCols) {
  68. return Optional.empty();
  69. }
  70. if (grid[nextCoord.getY() - 1][nextCoord.getX() - 1] != word.charAt(charIndex)) {
  71. return Optional.empty();
  72. }
  73. }
  74. return Optional.of(new WordLocation(startCoord, nextCoord));
  75. }
  76. }