Created
September 28, 2022 20:40
-
-
Save dantheman213/9f1218d1183af3a8fa731cb918e2013b to your computer and use it in GitHub Desktop.
word scramble solver
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
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
# | |
# Complete the 'SearchForWord' function below. | |
# | |
# The function is expected to return an INTEGER_ARRAY. | |
# The function accepts following parameters: | |
# 1. STRING word | |
# 2. 2D_STRING_ARRAY jumble | |
# | |
sub SearchForWord { | |
# Write your code here | |
# I think I could do this in Go - it'd be the same implementation | |
# although - given the assertian that a word only occurs once in the scramble | |
# - we could do the checks for the word in go routines. This way we would check | |
# all the direction at the same time ... just some fun. In fact, we could use | |
# a throttled go routines to check multiple rows to find the first character. Too much fun! | |
# assume that an empty string will not be provided | |
my ($string, $board) = @_; | |
#use Data::Dumper; | |
#print "Looking for '$string' in \n"; | |
#print Dumper($board) . "\n"; | |
# This is confusing - as it says that I failed test case 1 which is looking for WEB and does | |
# start on row 2 (3rd row) and col 9 (10th cell) and goes to the right | |
# Test 2 is looking for TESTING which start on row 0 cell 7 and goes down - not sur what' going on there. | |
# split string into characters | |
my @chars = split(//, $string); | |
my $start = [-1, -1]; | |
ROW: foreach my $row (0 .. (scalar(@{$board->[0]}) - 1)) { | |
COL: foreach my $col (0 .. (scalar(@{$board->[1]}) - 1)) { | |
#print " Checking: $row -- $col: " . $board->[$row][$col] . " equals $chars[0]\n"; | |
next unless $board->[$row][$col] eq $chars[0]; | |
#print __LINE__ . "\n"; | |
if (scalar(@chars) == 1) { | |
$start = [$row, $col]; | |
last ROW; | |
} | |
#print __LINE__ . "\n"; | |
# check right | |
if (check_next_cell($board, $row, $col, 0, 1, \@chars, 1)) { | |
$start = [$row, $col]; | |
last ROW; | |
} | |
#print __LINE__ . "\n"; | |
# check down | |
if (check_next_cell($board, $row, $col, 1, 0, \@chars, 1)) { | |
$start = [$row, $col]; | |
last ROW; | |
} | |
#print __LINE__ . "\n"; | |
# check diag | |
if (check_next_cell($board, $row, $col, 1, 1, \@chars, 1)) { | |
$start = [$row, $col]; | |
last ROW; | |
} | |
#print __LINE__ . "\n"; | |
} | |
} | |
# search for first character row by row | |
# when we find it we look right, down, and vertically for rest of word | |
# if we find it then we return coordinate | |
# else continue looking | |
#NOTES: | |
# optimizations can be made by getting the length of the word and not | |
# checking the cells that are grid lenght - word length and | |
# do the same for not checking the last length - word length rows | |
return @$start; | |
} | |
# this should be able to be done using slices of @chars, but I can't remember or find how to | |
# send a slice of 1 to end of array | |
sub check_next_cell { | |
my ($board, $row, $col, $row_shift, $col_shift, $chars, $char_index) = @_; | |
#print " checking $row + $row_shift and $col + $col_shift (" | |
#. $board->[$row + $row_shift][$col + $col_shift] . ") is equal to " | |
#. $chars->[$char_index] . " (" . @$chars . ") -- $char_index\n"; | |
if ($board->[$row + $row_shift][$col + $col_shift] eq $chars->[$char_index]) { | |
if (@$chars > $char_index + 1) { | |
return check_next_cell($board, $row + $row_shift, $col + $col_shift, | |
$row_shift, $col_shift, $chars, $char_index + 1); | |
} | |
else { | |
return 1; | |
} | |
} | |
else { | |
return 0; | |
} | |
} | |
open(my $fptr, '>', $ENV{'OUTPUT_PATH'}); | |
my $word = <STDIN>; | |
chomp($word); | |
my $jumble_rows = ltrim(rtrim(my $jumble_rows_temp = <STDIN>)); | |
my $jumble_columns = ltrim(rtrim(my $jumble_columns_temp = <STDIN>)); | |
my @jumble = (); | |
for (1..$jumble_rows) { | |
my $jumble_item = rtrim(my $jumble_item_temp = <STDIN>); | |
my @jumble_item = split /\s+/, $jumble_item; | |
push @jumble, \@jumble_item; | |
} | |
my @result = SearchForWord $word, \@jumble; | |
print $fptr join "\n", @result; | |
print $fptr "\n"; | |
close $fptr; | |
sub ltrim { | |
my $str = shift; | |
$str =~ s/^\s+//; | |
return $str; | |
} | |
sub rtrim { | |
my $str = shift; | |
$str =~ s/\s+$//; | |
return $str; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment