diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4271669 --- /dev/null +++ b/.gitignore @@ -0,0 +1,41 @@ +.DS_Store +.huskyrc.json +out +log.log +**/node_modules +*.pyc +*.vsix +**/.vscode/.ropeproject/** +**/testFiles/**/.cache/** +*.noseids +.nyc_output +.vscode-test +__pycache__ +npm-debug.log +**/.mypy_cache/** +!yarn.lock +coverage/ +cucumber-report.json +**/.vscode-test/** +**/.vscode test/** +**/.vscode-smoke/** +**/.venv*/ +port.txt +precommit.hook +pythonFiles/experimental/ptvsd/** +pythonFiles/lib/** +debug_coverage*/** +languageServer/** +languageServer.*/** +!uitests/features/languageServer/** +!uitests/src/languageServer/** +!uitests/code/**/languageServer/** +bin/** +obj/** +.pytest_cache +tmp/** +.python-version +.vs/ +test-results.xml +uitests/out/** +!build/ \ No newline at end of file diff --git a/input.txt b/input.txt new file mode 100644 index 0000000..e9dec78 --- /dev/null +++ b/input.txt @@ -0,0 +1,4 @@ +IT Crowd +The Office +Test +Iron Man 1 \ No newline at end of file diff --git a/keyboard_path.py b/keyboard_path.py new file mode 100644 index 0000000..8afdb11 --- /dev/null +++ b/keyboard_path.py @@ -0,0 +1,92 @@ +import sys + +keyboard = [ + ['A', 'B', 'C', 'D', 'E', 'F'], + ['G', 'H', 'I', 'J', 'K', 'L'], + ['M', 'N', 'O', 'P', 'Q', 'R'], + ['S', 'T', 'U', 'V', 'W', 'X'], + ['Y', 'Z', '1', '2', '3', '4'], + ['5', '6', '7', '8', '9', '0'] +] # DVR keyboard + +def get_position(c): + """ + input: character c + output: tuple + purpose: returns the position of character in in form (row, column) + """ + for i in range(len(keyboard)): + if c in keyboard[i]: + return (i, keyboard[i].index(c)) + return (-1, -1) + +def get_cursor_path(word): + """ + input: string word + output: list of character directions + purpose: returns the cursor path of the inputed word ex: [U, U, L, ...] + """ + start = (0, 0) # cursor starts at A + word = word.upper() + path = [] # path of the cursor will append directions to this + for c in word: + if c == ' ': # just add 'S' if the current character is a space + path.append('S') + continue + + + position = get_position(c) # find the position of the character in question + if position == (-1, -1): + sys.exit('invalid search query!') + + + path.extend(get_path(start, position)) # add the path to the total path + + start = position # set the new starting position to be the current position + return path + + + +def get_path(start_tuple, target_position): + """ + input: tuple start_tuple in form (row, column), tuple target_position in form (row, column) + output: list of direction + purpose: finds the list of directions from the start tuple position to the target tuple position + """ + directions_list = [] # list of directions from the start position to the target position + + row_difference = target_position[0] - start_tuple[0] # find row difference + + # append directions based on if the target row is above or below the starting row + if row_difference < 0: + for i in range(abs(row_difference)): + directions_list.append('U') + else: + for i in range(row_difference): + directions_list.append('D') + + column_difference = target_position[1] - start_tuple[1] # find column difference + + # append directions based on if the target column is to the left or right of the starting row + if column_difference < 0: + for i in range(abs(column_difference)): + directions_list.append('L') + else: + for i in range(column_difference): + directions_list.append('R') + + directions_list.append('#') + return directions_list + +file_inputed = sys.argv[1] # take inputed file + +file = open(file_inputed, 'r') +phrases = file.readlines() # read lines into phrases list + + +for word in phrases: + word = word.replace('\n', '') # remove any new lines + print(*get_cursor_path(word), sep=',') # convert list into comma separated string and print + + + \ No newline at end of file diff --git a/keyboard_path_test.py b/keyboard_path_test.py new file mode 100644 index 0000000..2a24d6f --- /dev/null +++ b/keyboard_path_test.py @@ -0,0 +1,27 @@ +import unittest +from keyboard_path import get_position, get_path, get_cursor_path + + +class TestKeyboardPath(unittest.TestCase): + + def test_position(self): # tests the get_position function + self.assertEqual(get_position('3'), (4, 4), "Should be (4, 4)") + self.assertEqual(get_position('0'), (5, 5), "Should be (5, 5)") + self.assertEqual(get_position('A'), (0, 0), "Should be (0, 0)") + self.assertEqual(get_position('T'), (3, 1), "Should be (3, 1)") + + def test_get_path(self): # tests the get_path function + self.assertEqual(get_path((1, 2), (0, 0)), ['U', 'L', 'L', '#'], "Should be ['U', 'L', 'L']") + self.assertEqual(get_path((3, 5), (2, 1)), ['U', 'L', 'L', 'L', 'L', '#'], "Should be ['U', 'L', 'L', 'L' 'L']") + self.assertEqual(get_path((1, 1), (4, 5)), ['D', 'D', 'D', 'R', 'R', 'R', 'R', '#'], "Should be ['D', 'D', 'D', 'R', 'R', 'R', 'R']") + + + def test_whole_path(self): # tests the get_cursor_path function (which uses the other two functions) + self.assertEqual(','.join(get_cursor_path('IT Crowd')), 'D,R,R,#,D,D,L,#,S,U,U,U,R,#,D,D,R,R,R,#,L,L,L,#,D,R,R,#,U,U,U,L,#', "Should be D,R,R,#,D,D,L,#,S,U,U,U,R,#,D,D,R,R,R,#,L,L,L,#,D,R,R,#,U,U,U,L,#") + self.assertEqual(','.join(get_cursor_path('The Office')), 'D,D,D,R,#,U,U,#,U,R,R,R,#,S,D,D,L,L,#,U,U,R,R,R,#,#,D,L,L,L,#,U,#,R,R,#', "Should be D,D,D,R,#,U,U,#,U,R,R,R,#,S,D,D,L,L,#,U,U,R,R,R,#,#,D,L,L,L,#,U,#,R,R,#") + self.assertEqual(','.join(get_cursor_path('Iron Man 1')), 'D,R,R,#,D,R,R,R,#,L,L,L,#,L,#,S,L,#,U,U,#,D,D,R,#,S,D,D,R,#', "Should be D,R,R,#,D,R,R,R,#,L,L,L,#,L,#,S,L,#,U,U,#,D,D,R,#,S,D,D,R,#") + + +if __name__ == '__main__': + unittest.main(argv=['input.txt'], exit=False) +