Last active
October 8, 2016 08:29
-
-
Save bakkiraju/dbce740e02c7dbcf79b10d7bc65faff7 to your computer and use it in GitHub Desktop.
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/python | |
| #kb_map = [ ['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', ' ', ' ', ' ', ' '] | |
| # ] | |
| def getPath(input,kb_width): | |
| if kb_width == 0: | |
| return None | |
| if len(input) == 0: | |
| return None | |
| i = 0 | |
| command = '' | |
| srcR = srcC = 0 | |
| while i < len(input): | |
| dstR = (ord(input[i].lower()) - 97) / kb_width | |
| dstC = (ord(input[i].lower()) - 97) % kb_width | |
| while dstR != srcR or dstC != srcC: | |
| if dstR > srcR: | |
| command += 'D' | |
| srcR += 1 | |
| if dstR < srcR: | |
| command += 'T' | |
| srcR -= 1 | |
| if dstC > srcC: | |
| command += 'R' | |
| srcC += 1 | |
| if dstC < srcC: | |
| command += 'L' | |
| srcC -= 1 | |
| i += 1 | |
| command +='|' | |
| return command | |
| print(getPath("con",5)) | |
| print(getPath("",5)) | |
| print(getPath("con",0)) | |
| print(getPath("catch",5)) | |
| print(getPath("california",5)) | |
| print(getPath("swift",5)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment