c++行strd.erase(strd.length() - 1)的python代码是什么?其中STRD是字符串
What would be python code for c++ line strd.erase(strd.length() - 1); where strd is a string
我试过转换下面的c++代码行
strd.erase(strd.length() - 1);
其中STRD为字符串STRD
pythonstrd = strd[:len(strd) - 1]
但是它不起作用
在c++中你使用二维数组visited
,但在Python中你使用一些字典,所以你的代码不相似。我试着改变它,现在Python使用二维列表visited
和打印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 presentn"
findWords(boggle)
if __name__ == "__main__":
main()
当然Python代码可以更Python化,例如
def isWord(strd):
return strd in dictionary_words
但是我不会改变它
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- C++避免重复声明的语法是什么
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- C++从另一个类访问公共静态向量的正确方法是什么
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- C++中名称篡改的目的是什么
- 在 c++ 中拥有一组结构的正确方法是什么?
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 是什么阻止DOMTimerCoordinator::NextID进入无休止的循环
- 派生类销毁的最佳实践是什么
- 这个语法std::class<>{}(arg1, arg2) 在C++中是什么意思?
- 通过JNI传递数据数组的最快方法是什么
- "using namespace std;"在C++的作用是什么?
- 在两台机器之间进行时间戳的最佳c++chrono函数是什么
- 文件系统:复制功能的速度秘诀是什么
- 用常见虚拟函数实现的任意组合来实现派生类的正确方法是什么
- 使用QQuickFramebufferObject时同步数据的最佳方式是什么
- 是什么原因导致它无法编译?它是声明签名还是在函数本身的实现中
- c++行strd.erase(strd.length() - 1)的python代码是什么?其中STRD是字符串