Skip to content

Instantly share code, notes, and snippets.

@lealLuo
Last active July 23, 2018 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lealLuo/402596c531ef4d7b4d7962f1b1edaceb to your computer and use it in GitHub Desktop.
Save lealLuo/402596c531ef4d7b4d7962f1b1edaceb to your computer and use it in GitHub Desktop.
URL: https://leetcode.com/problems/keyboard-row/description/ I write a function in a function. It's important to know the scope of the variable, and use print to test the simple code
class Solution:
def findWords(self, words):
"""
:type words: List[str]
:rtype: List[str]
"""
# judge which row the input char belongs to
def judgeRow(c):
row1 =['q','w','e','r','t','y','u','i','o','p','Q','W','E','R','T','Y','U','I','O','P']
row2 =['a','s','d','f','g','h','j','k','l','A','S','D','F','G','H','J','K','L']
row3 =['z','x','c','v','b','n','m','Z','X','C','V','B','N','M']
if(row1.count(c)):
return 1
elif(row2.count(c)):
return 2
else:
return 3
listReturn = [] # to store the final retured list, outside the loop
for word in words:
wordRecover = ''
rowNum = judgeRow(word[0]) # judge the row from the first char
#print (rowNum)
for c in word:
rowCurrent = judgeRow(c)
print (rowCurrent)
if(rowCurrent != rowNum):
wordRecover = ''
break;
wordRecover += c
print (wordRecover)
if(wordRecover!=''):
listReturn.append(wordRecover)
return listReturn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment