HackerRank Python 打印 STDOUT 似乎不起作用
HackerRank Python print STDOUT doesn't seem to work
我是 Python 的新手,所以正在尝试 'Counting Valleys' 关于 HackerRank 的问题。
我已经在 PyCharm 中编写了我的解决方案,它可以 fine/gives 解决方案预期输出的正确答案。
- 我试图在该站点上进行搜索,但似乎没有找到具体的内容,所以我一定是漏掉了一些明显的东西。
问题是当我将代码移植到 HackerRank 时,它只显示 'Wrong Answer'。
我想通过使用 'print' 或其他任何方式获得反馈来了解问题所在。
下面我在不同的地方添加了 'print' 行以显示我也尝试过的区域。
这是我 运行 解决此问题的第二个解决方案,任何 advice/suggestions 都将不胜感激,因为继续使用它非常烦人和令人沮丧,感谢任何帮助。
# !/bin/python
import math
import os
import random
import re
import sys
import logging
# Complete the countingValleys function below.
def countingValleys(n, s):
print('Please print')
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
n = int(raw_input())
s = raw_input()
sea_level = 0
valleys = 0
last_step = ''
in_same_valley = False
print('Ok maybe here?')
for step in s:
if step == 'D':
if last_step == 'D' and sea_level <= 0:
if not in_same_valley:
valleys += 1
in_same_valley = True
sea_level -= 1
else:
sea_level += 1
in_same_valley = False
last_step = step
print('Ok perhaps here?')
fptr.write(str('valleys') + '\n')
fptr.close()
print('Ok try here?')
你的缩进错了。尝试类似的东西:
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the countingValleys function below.
def countingValleys(n, s):
print('hi')
# The below line should not be inside the function countingValleys
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
....
它应该可以工作。您将在底部的 Debug output
框中看到输出
数谷挑战
这就是我解决数谷挑战的方法
def countingValleys(n, s):
ls = list(s)
seeLevel = 0
valley = 0
for i in ls:
if i == 'U':
seeLevel += 1
else:
if seeLevel == 0:
valley +=1
seeLevel-= 1
return valley
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
n = int(raw_input())
s = raw_input()
我是 Python 的新手,所以正在尝试 'Counting Valleys' 关于 HackerRank 的问题。 我已经在 PyCharm 中编写了我的解决方案,它可以 fine/gives 解决方案预期输出的正确答案。
- 我试图在该站点上进行搜索,但似乎没有找到具体的内容,所以我一定是漏掉了一些明显的东西。
问题是当我将代码移植到 HackerRank 时,它只显示 'Wrong Answer'。
我想通过使用 'print' 或其他任何方式获得反馈来了解问题所在。
下面我在不同的地方添加了 'print' 行以显示我也尝试过的区域。
这是我 运行 解决此问题的第二个解决方案,任何 advice/suggestions 都将不胜感激,因为继续使用它非常烦人和令人沮丧,感谢任何帮助。
# !/bin/python
import math
import os
import random
import re
import sys
import logging
# Complete the countingValleys function below.
def countingValleys(n, s):
print('Please print')
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
n = int(raw_input())
s = raw_input()
sea_level = 0
valleys = 0
last_step = ''
in_same_valley = False
print('Ok maybe here?')
for step in s:
if step == 'D':
if last_step == 'D' and sea_level <= 0:
if not in_same_valley:
valleys += 1
in_same_valley = True
sea_level -= 1
else:
sea_level += 1
in_same_valley = False
last_step = step
print('Ok perhaps here?')
fptr.write(str('valleys') + '\n')
fptr.close()
print('Ok try here?')
你的缩进错了。尝试类似的东西:
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the countingValleys function below.
def countingValleys(n, s):
print('hi')
# The below line should not be inside the function countingValleys
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
....
它应该可以工作。您将在底部的 Debug output
框中看到输出
数谷挑战
这就是我解决数谷挑战的方法
def countingValleys(n, s):
ls = list(s)
seeLevel = 0
valley = 0
for i in ls:
if i == 'U':
seeLevel += 1
else:
if seeLevel == 0:
valley +=1
seeLevel-= 1
return valley
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
n = int(raw_input())
s = raw_input()