Reputation: 137
I am very new to Python so am trying out the 'Counting Valleys' question on HackerRank. I have written my solution in PyCharm and it works fine/gives the correct answer to the expected output of the solution.
The problem is that when I port the code across to HackerRank, it just says 'Wrong Answer'.
I would like to understand what the issue is by using 'print' or anything else to get feedback.
Below I have added 'print' lines to different places to show areas I have tried too.
This is the second solution that I have run into this problem, any advice/suggestions would be appreciated as its super annoying and frustrating to continue working with, any help appreciated.
# !/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?')
Upvotes: 2
Views: 11900
Reputation: 191
This is how I solved my counting Valley Challenge
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()
Upvotes: 1
Reputation: 1416
you have indentation wrong. Try something like:
#!/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')
....
and it should work. you will see the output in the Debug output
box at the bottom
Upvotes: 0