Olivier Liu
Olivier Liu

Reputation: 21

Print has "No Output" in STDOUT

I have been writing code in HackerRank to count how many valleys that a hiker (Gary) walked through in a hike. Right now, it looks something like this.

#Defining variables for later use and a list of Gary's steps
steps = ["U", "D", "D", "D", "D", "D", "U", "U"]
sea_level = 0
valleys = 0

#For loop to calculate how many valleys Gary hiked through
for step in steps:
    step_ud = step
    if step_ud == "U":
        sea_level += 1
    elif sea_level == 0:
        valleys += 1
    elif step_ud == "D":
        sea_level -= 1
    elif sea_level == 0:
        valleys += 1
        print(valleys)

When I run the code, however I receive no output. My expected output was 1, knowing that Gary only walked through 1 valley.

The term valley was defined as: "A valley is a non-empty sequence of consecutive steps below sea level, starting with a step down from sea level and ending with a step up to sea level."

The question was written as: "Given Gary's sequence of up and down steps during his last hike, find and print the number of valleys he walked through."

I have taken a look at these 3 posts:

How to flush output of Python print?

python `print` does not work in loop

I have also tried some other methods, but they haven't helped. These are things I've tried.

I imported the sys module and used the sys.stdout.flush() function to flush the stdout.

import sys
...
#Loop with lines to determine whether it's a valley.
...
print(valleys)
sys.stdout.flush()

I've also tried making my own function of flushing the stdout, but that didn't work either.

def my_print(text):
sys.stdout.write(str(text))
sys.stdout.flush()

Then I used the function after printing to flush.

import sys
...
#Loop with lines to determine whether it's a valley.
...
print(valleys)
my_print(text)

Currently I'm pretty lost in knowing what I have to fix. Thanks for the help.

Upvotes: 2

Views: 1178

Answers (1)

bhansa
bhansa

Reputation: 7504

Lets try out some debugging here:
I know its a bad idea when you are working on big solutions, anyway when you
will run this code you will see that you are never getting the condition elif step_ud="D" because it will check elif sea_level==0 and continue the for loop.

steps = ["U", "D", "D", "D", "D", "D", "U", "U"]
sea_level = 0
valleys = 0

#For loop to calculate how many valleys Gary hiked through
for step in steps:
    step_ud = step
    if step_ud == "U":
        print "U"
        sea_level += 1
    elif sea_level == 0:
        print "0 sea"
        valleys += 1
    elif step_ud == "D":
        print "D"
        sea_level -= 1
    elif sea_level == 0:
        print "0 sea 2"
        valleys += 1
    print sea_level

Here is my solution which I submitted during the contest:

n = int(raw_input())
stra = raw_input()
lev = 0
arr = []
valleys = 0
for i in stra:
    if i=='U':
        lev +=1
        arr.append(lev)
    elif i=='D':
        lev -=1
        arr.append(lev)
#print arr
for i in range(len(arr)):
    if arr[i]==0 and arr[i-1]<0:
        valleys +=1
print valleys


My idea behind this program:


Output

C:\Users\bhansa\Desktop\Stack>python valley.py
8
DDUUDDUDUUUD
[-1, -2, -1, 0, -1, -2, -1, -2, -1, 0, 1, 0]
2

See the above list, if there is 0 that means gary is on the see level again
check if the value before the element 0 is negative then it is sure it
came from a valley.

I hope it helped.

Upvotes: 2

Related Questions