0

I am new to Python 3 and am working on the "Breaking the Records" problem on Hackerrank. My code works properly in IDLE when I use strings for my input, but I don't know how to change Hackerrank's input into something "useable" like a string. I've had the same problem for basically every other Hackerrank exercise I've tried as well.

The problem says that for the input, "The first line contains an integer n denoting the number of games, and the second line contains n spaced-separated integers describing the respective values of s0, s1, ... s(n-1)"

Here is the code I've tried so far.

a = [int(x) for x in input().split()]
b = [int(x) for x in input().strip().split(' ')]

def results(a,b):
    highest = b[0]
    lowest = b[0]
    highest_changes = 0
    lowest_changes = 0
    for i in range(1,len(b)):
        if b[i] > b[i-1] and b[i] > highest:
            highest = b[i]
            highest_changes += 1
        if b[i] < b[i-1] and b[i] < lowest:
            lowest = b[i]
            lowest_changes += 1
    return highest_changes, lowest_changes
jcburda
  • 1
  • 1
  • 1
    What error do you get/what do you observe when putting your code in hackerrank? How are you inputting strings in IDLE? – mxbi Feb 21 '18 at 19:31
  • They even provide you with a basic parsing method, you just have to code the function for the result - where is your problem? – Patrick Artner Feb 21 '18 at 19:40
  • can you try something like: for _ in range(10): print(input()) and see what happens? – Riley Martine Feb 21 '18 at 19:41
  • @mxbi: the error I get is "~ no response on stdout ~". In IDLE, I just set two variables equal to strings containing the data and executed the function using those variables – jcburda Feb 22 '18 at 20:12
  • @MaxKolbe Hackerrank expects you to print() your answer - are you doing this? – mxbi Feb 22 '18 at 20:15
  • @Patrick: Hence my frustration with these exercises. Because I wanted to code the solution myself from start to finish, I wanted to explicitly convert their input into an iterable string as my first step. Since I've deleted the provided code, do you know how to "reset" one of those exercises so I can see the original code they provided? – jcburda Feb 22 '18 at 20:19
  • @mxbi: Originally, no, but I just added a print statement inside my function, and I'm still getting " ~ no response on stdout ~ " – jcburda Feb 22 '18 at 20:24
  • @RileyMartine do you mean outside the function? I did that but only got an EOF error – jcburda Feb 22 '18 at 20:26
  • @MaxKolbe, if you wanted to see the original code, you can always open the challenge in incognito mode. – mxbi Feb 22 '18 at 21:40

2 Answers2

0

Thats what I did:

#!/bin/python3 
import sys

def breakingRecords(score):
    # Complete this function
    min = -1
    minCount = 0
    maxCount = 0
    for s in score:
        if min == -1:
            min = s
            max = s
            continue
        if s < min:
            min = s
            minCount+=1
        if s > max:
            max = s
            maxCount+=1
    return maxCount, minCount

if __name__ == "__main__":
    n = int(input().strip())
    score = map(int,input().strip().split(' '))
    result = breakingRecords(score)
    print(" ".join(map(str, result)))

I think I left the original code part in - thats all below the if __name__ == "__main__":

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • Right, gotcha. The logic hoop that I'm not able to jump through is the fact that the input defined in the problem is 2 lines whereas the function that works only takes one input. Even in the code they provide, they're stripping "n" but not using it if I understand correctly. Here is a link about the __name__ == "__main__" part, though I don't fully understand it. – jcburda Feb 23 '18 at 00:12
  • @MaxKolbe I've completed about 40% of the python hackerthingies since you posted this here - most of them take unneeded input that you can discard. some are weird in that they forbid ifs and the whole challenge is forfeit if you leave the line `if __name__ == "__main__":` in it etc. I ignore whatever data they provide and I do not need. Hackerrank is not about "optimal" solutions, its just about "a solution that fits _k_ Testcases and most of the time you can skip thinking by looking into the discussion page of a problem so its kinda pointless. They less difficult then solving SO puzzles. – Patrick Artner Feb 23 '18 at 06:07
0
  // Complete the breakingRecords function below.
static int[] breakingRecords(int[] scores) {
  int high, low;
  high = low = -1;
  int h,l;
  h = l = 0;
  int[] list = new int[2];
  low = scores[0];
for(int i=1; i<=scores.length; i++){
  if(high < scores[i -1]){
    high = scores[i -1];
    h++;
  }
if(i < scores.length){
    if(low > scores[i]){
        low = scores[i];
        l++;
      }
  }}
list[0] = h - 1;
list[1] = l ;
return list;
}
apaderno
  • 28,547
  • 16
  • 75
  • 90