该程序计算较大字符串内重叠字符串的次数。
输入:
较大的字符串(var name:search_space)<< abababa
较小的字符串(var name:search_string)<< aba
在Pycharm 2016.3: 3使用“调试代码”
在Pycharm 2016.3: 7使用'运行代码'回答
预期的答案: 3
我的代码:
import sys def count_substring(search_space, search_string): search_space = search_space.lower() search_string = search_string.lower() if search_space: search_space = search_space.split('\n')[0] if search_string: search_string = search_string.split('\n')[0] lower = 0 upper = len(search_string) curr = ' ' count = 0 for curr in search_space: selected = search_space[lower:upper] if search_string == selected: count += 1 lower += 1 upper += 1 if upper > len(search_space): break return count if __name__ == '__main__': s = sys.stdin.read() t = sys.stdin.read() occurance_count = count_substring(s, t) sys.stdout.write(str(occurance_count))代码被修改以帮助阐明错误:
for idx, curr in enumerate(search_space): print(idx, curr) print(count) selected = search_space[lower:upper]使用“调试代码”输出:(按预期工作)
>? abababa <ENTER> >? aba <ENTER> 0 a 0 1 b 1 2 a 1 3 b 2 4 a 2 3使用'运行代码'输出:
abababa aba ^D 0 a 0 1 b 1 2 a 2 3 b 3 4 a 4 5 b 5 6 a 6 7注意:^ D ie Ctrl + D,因为在运行期间按下ENTER会插入一个换行符。 Ctrl + D是我设法让剩余代码运行的唯一方法。 但是,即使没有CTRL + D,当代码的远程测试完成时(这是自我指导的非证书课程的一部分),差异仍然存在。
注意:我必须在本练习中使用标准输入和输出。
在我看来, for curr in search_space:使用for curr in search_space:正在使循环遍历search_space的整个长度,因此比它应该更多的次数。
为什么Break命令不起作用?
我的问题:
这是为什么发生? 阅读更多关于理解和预见此类事件的概念? 为什么调试和运行模式中的差异? 我如何在未来的代码中预见和防止这个错误? 有没有比Ctrl + D更好的运行方式? 这个粗略的技巧在终端或IDLE中不起作用。This program counts the number of overlapping occurrences of a string within a larger string.
Input:
larger string (var name: search_space)<< abababa
smaller string (var name: search_string)<< aba
Answer using 'Debug Code' in Pycharm 2016.3: 3
Answer using 'Run Code' in Pycharm 2016.3: 7
Expected answer: 3
My code:
import sys def count_substring(search_space, search_string): search_space = search_space.lower() search_string = search_string.lower() if search_space: search_space = search_space.split('\n')[0] if search_string: search_string = search_string.split('\n')[0] lower = 0 upper = len(search_string) curr = ' ' count = 0 for curr in search_space: selected = search_space[lower:upper] if search_string == selected: count += 1 lower += 1 upper += 1 if upper > len(search_space): break return count if __name__ == '__main__': s = sys.stdin.read() t = sys.stdin.read() occurance_count = count_substring(s, t) sys.stdout.write(str(occurance_count))The code was modified to help elucidate the error:
for idx, curr in enumerate(search_space): print(idx, curr) print(count) selected = search_space[lower:upper]Output Using 'Debug Code': (working as intended)
>? abababa <ENTER> >? aba <ENTER> 0 a 0 1 b 1 2 a 1 3 b 2 4 a 2 3Output using 'Run Code':
abababa aba ^D 0 a 0 1 b 1 2 a 2 3 b 3 4 a 4 5 b 5 6 a 6 7Note: ^D i.e. Ctrl+D, is pressed because pressing ENTER during Run inserts a newline. Ctrl+D is the only way I manage to get the remaining code to run. But even without CTRL+D the discrepancy remains when a remote test of the code is done (It's part of a self guided non-certificate course)
Note: I have to use standard input and output for this exercise.
It seems to me using for curr in search_space: is making the loop iterate throughout the length of search_space and hence more times than it should.
Why doesn't the Break command work?
My questions:
Why is this happening? & Which concept do I read up more on to understand and foresee such events? Why the discrepancy in Debug and Run modes? How can I foresee and prevent this error in future code? Is there a better way of running this than Ctrl+D ? This crude trick doesn't work in Terminal or IDLE.最满意答案
该代码从stdin读取两次: sys.stdin.read() 。 该方法从该流中读取直到其结束(不像“运行”示例中的行结束)。 它只能被调用一次,之后你不应该触碰stdin 。
如果您想逐行读取输入行,请使用sys.stdin.readline() 。
The code reads twice from stdin: sys.stdin.read(). This method reads from that stream until its end (not the end of the line as in the "run" example). It should only be called once, and you should not touch stdin afterwards.
If you want to read the input line by line instead, use sys.stdin.readline().
调试并运行给出不同的输出python(debug and run give different output python)该程序计算较大字符串内重叠字符串的次数。
输入:
较大的字符串(var name:search_space)<< abababa
较小的字符串(var name:search_string)<< aba
在Pycharm 2016.3: 3使用“调试代码”
在Pycharm 2016.3: 7使用'运行代码'回答
预期的答案: 3
我的代码:
import sys def count_substring(search_space, search_string): search_space = search_space.lower() search_string = search_string.lower() if search_space: search_space = search_space.split('\n')[0] if search_string: search_string = search_string.split('\n')[0] lower = 0 upper = len(search_string) curr = ' ' count = 0 for curr in search_space: selected = search_space[lower:upper] if search_string == selected: count += 1 lower += 1 upper += 1 if upper > len(search_space): break return count if __name__ == '__main__': s = sys.stdin.read() t = sys.stdin.read() occurance_count = count_substring(s, t) sys.stdout.write(str(occurance_count))代码被修改以帮助阐明错误:
for idx, curr in enumerate(search_space): print(idx, curr) print(count) selected = search_space[lower:upper]使用“调试代码”输出:(按预期工作)
>? abababa <ENTER> >? aba <ENTER> 0 a 0 1 b 1 2 a 1 3 b 2 4 a 2 3使用'运行代码'输出:
abababa aba ^D 0 a 0 1 b 1 2 a 2 3 b 3 4 a 4 5 b 5 6 a 6 7注意:^ D ie Ctrl + D,因为在运行期间按下ENTER会插入一个换行符。 Ctrl + D是我设法让剩余代码运行的唯一方法。 但是,即使没有CTRL + D,当代码的远程测试完成时(这是自我指导的非证书课程的一部分),差异仍然存在。
注意:我必须在本练习中使用标准输入和输出。
在我看来, for curr in search_space:使用for curr in search_space:正在使循环遍历search_space的整个长度,因此比它应该更多的次数。
为什么Break命令不起作用?
我的问题:
这是为什么发生? 阅读更多关于理解和预见此类事件的概念? 为什么调试和运行模式中的差异? 我如何在未来的代码中预见和防止这个错误? 有没有比Ctrl + D更好的运行方式? 这个粗略的技巧在终端或IDLE中不起作用。This program counts the number of overlapping occurrences of a string within a larger string.
Input:
larger string (var name: search_space)<< abababa
smaller string (var name: search_string)<< aba
Answer using 'Debug Code' in Pycharm 2016.3: 3
Answer using 'Run Code' in Pycharm 2016.3: 7
Expected answer: 3
My code:
import sys def count_substring(search_space, search_string): search_space = search_space.lower() search_string = search_string.lower() if search_space: search_space = search_space.split('\n')[0] if search_string: search_string = search_string.split('\n')[0] lower = 0 upper = len(search_string) curr = ' ' count = 0 for curr in search_space: selected = search_space[lower:upper] if search_string == selected: count += 1 lower += 1 upper += 1 if upper > len(search_space): break return count if __name__ == '__main__': s = sys.stdin.read() t = sys.stdin.read() occurance_count = count_substring(s, t) sys.stdout.write(str(occurance_count))The code was modified to help elucidate the error:
for idx, curr in enumerate(search_space): print(idx, curr) print(count) selected = search_space[lower:upper]Output Using 'Debug Code': (working as intended)
>? abababa <ENTER> >? aba <ENTER> 0 a 0 1 b 1 2 a 1 3 b 2 4 a 2 3Output using 'Run Code':
abababa aba ^D 0 a 0 1 b 1 2 a 2 3 b 3 4 a 4 5 b 5 6 a 6 7Note: ^D i.e. Ctrl+D, is pressed because pressing ENTER during Run inserts a newline. Ctrl+D is the only way I manage to get the remaining code to run. But even without CTRL+D the discrepancy remains when a remote test of the code is done (It's part of a self guided non-certificate course)
Note: I have to use standard input and output for this exercise.
It seems to me using for curr in search_space: is making the loop iterate throughout the length of search_space and hence more times than it should.
Why doesn't the Break command work?
My questions:
Why is this happening? & Which concept do I read up more on to understand and foresee such events? Why the discrepancy in Debug and Run modes? How can I foresee and prevent this error in future code? Is there a better way of running this than Ctrl+D ? This crude trick doesn't work in Terminal or IDLE.最满意答案
该代码从stdin读取两次: sys.stdin.read() 。 该方法从该流中读取直到其结束(不像“运行”示例中的行结束)。 它只能被调用一次,之后你不应该触碰stdin 。
如果您想逐行读取输入行,请使用sys.stdin.readline() 。
The code reads twice from stdin: sys.stdin.read(). This method reads from that stream until its end (not the end of the line as in the "run" example). It should only be called once, and you should not touch stdin afterwards.
If you want to read the input line by line instead, use sys.stdin.readline().
发布评论