#πŸ”’ help with kattis question

39 messages Β· Page 1 of 1 (latest)

dense patrol
#

https://open.kattis.com/problems/upsanddownsofinvesting
my solution works for the first two testcases but fails on the first real one.

To summarize the problem
given a graph you are supposed to find the peaks and throughs, i do this by calculating the number of consecutive changes in one direction so if i have the graph 5 6 9 8 5 4 i will collapse it to 2, -3.

I then iterate through the collapsed list and check if every i and i+1 is greater than the n or m (the number of consecutive rises/falls) they want and if so then it is a through. i cant think of a scenario where my solution fails can someone help me

x, n, m = list(map(int, input().split()))
n-=1
m-=1
l=list(map(int, input().split()))
upordown=[1 if ((l[i+1]-l[i])>0) else -1 if ((l[i+1]-l[i])<0) else 0 for i in range(len(l)-1)]
if (x==1) or (x==2):
    print("0 0")
else:
    collapsed=[]
    length=0
    for i in range(len(upordown)-1):
        if upordown[i]!=upordown[i+1]:
            length+=upordown[i]
            collapsed.append(length)
            length=0
        else:
            length+=upordown[i]
    collapsed.append(length+1 if length>0 else length-1)
    ind=0
    h=0
    l=0
    for i in range(len(collapsed)-1):
        ind+=abs(collapsed[i])
        if ((collapsed[i]>=n) and (abs(collapsed[i+1])>=n)):
            h+=1
        elif ((abs(collapsed[i])>=m) and (collapsed[i+1]>=m)):
            l+=1
    print(h,l)
        

ornate vortexBOT
#

@dense patrol

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

dense patrol
#

if anyone has any questions on my approach plz ask ive been stuck on this stupid problem for like 2 days

patent marlin
dense patrol
#

2 is the number of consecutive rises and falls to be considered the peak and 3 is the number of consecutive falls and rises to be considered the through

#

oh if ur talking about the collapsed array i absolute it before comparing in my if statement

#
    for i in range(len(collapsed)-1):
        ind+=abs(collapsed[i])
        if ((collapsed[i]>=n) and (abs(collapsed[i+1])>=n)):
            h+=1
        elif ((abs(collapsed[i])>=m) and (collapsed[i+1]>=m)):
            l+=1
#

im guessing i did something wrong here though

patent marlin
#

Does the kattis site have a test runner?

dense patrol
#

yeah but apart from the sample test cases the rest are hidden

#

i pass the sample test case but fail the first hidden one

patent marlin
#

passed? with print statements?

#

Are you not supposed to use functions and returns?

dense patrol
#

yeah like i got the same output as them

patent marlin
dense patrol
#

nope sadly

#

i hate this site haha i want to know what testcases i failed

sand radish
#

hm it doesn't even show what's being printed?

#

maybe there's an edge case where ppl would input n m 0 ?

dense patrol
dense patrol
sand radish
#

I see

dense patrol
#

oh i just found a testcase i fail lol

#

its when s is 3

#

argh i made a change but now it solves 4 testcases instead of 2 lol

patent marlin
#

✨ progress ✨

crystal scaffold
#

Can you try adding "collapsed = [c + 1 for c in collapsed]" after "collapsed.append(length+1 if length>0 else length-1)" ?

dense patrol
#

sure

#

that line was definitely the problem at least for testcase 3

#

i tried changing it to

collapsed.append(length+1 if ((length>0)and upordown[-1]>0) else length-1 if ((length<0)and upordown[-1]<0) else upordown[-1])
#

but it only solved two more cases haha

crystal scaffold
#

Actually I made a mistake

#

for i, c in enumerate(collapsed):
if c > 0:
collapsed[i] = c + 1
elif c < 0:
collapsed[i] = c - 1

Can you try this change?

#

I think the problem is an off-by-one error in how you measure the lenght of subsequent increases and decreases

dense patrol
#

yeah i think so too the collapse part of the code is wrong

#

argh the fix doesnt seem to work though, failing on sample testcases

ornate vortexBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.