Star123 - 1 year ago 111

Python Question

I have tried converting the below c++ code line

`strd.erase(strd.length() - 1);`

where strd is a string strd

to python

`strd = strd[:len(strd) - 1]`

but it does not work

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

In C++ you use 2-dimensional array `visited`

but in Python you use some dictionary so your codes aren't similar. I try to change it and now Python use 2-dimensional list `visited`

and prints `GEEKS`

```
M = 3
N = 3
dictionary_words = ["GEEKS"]
length = len(dictionary_words)
visited = [[False]* M for x in range(N)]
def isWord(strd):
for i in range(0, length):
if strd == dictionary_words[i]:
return True
return False
def findWords(boggle):
for i in range(0, len(boggle)):
for j in range(0, len(boggle[i])):
visited[i][j] = False
strd = ""
for i in range(0, len(boggle)):
for j in range(0, len(boggle[i])):
findWordsUtil(boggle, visited, i, j, strd)
def findWordsUtil(boggle, visited, i, j, strd):
visited[i][j] = True
strd = strd + boggle[i][j]
if (isWord(strd)):
print strd + "\n"
row = i-1
while row <= i + 1 and row < len(boggle):
col = j - 1
while col <= j + 1 and col < len(boggle[row]):
if row>=0 and col>=0 and not(visited[row][col]):
findWordsUtil(boggle, visited, row, col, strd)
col = col + 1
row = row + 1
strd = strd[:len(strd)-1]
visited[i][j] = False
def main():
boggle = [
['G','I','Z'],
['U','E','K'],
['Q','S','E']
]
print "Following words of dictionary are present\n"
findWords(boggle)
if __name__ == "__main__":
main()
```

Of course Python code could be more Pythonic ie.

```
def isWord(strd):
return strd in dictionary_words
```

but I will not change it.

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**