All test cases are getting passed

pls check why all test cases are not giving correct output or just give me example where it is failing

prev=0 n=int(input()) inc=True ans=“true” for i in range(n): m=int(input()) if ans!=“false”: if inc==True: if m<=prev: inc=False if m==prev: ans=“false” else: if m>=prev: ans=“false” prev=m print(ans)

I am trying to send the code again but it is not allowing because it says body is too similar to the previou one !

Hey @stutijain578, first of all please attach the link of the online ide where you practice this code. It is very difficult to figure out without proper indentation. Secondly, there can be 4 cases of sequences possible in this question :

  1. Only increasing sequence
  2. Only decreasing sequence
  3. Increasing and then decreasing sequence
  4. Decreasing and then increasing sequence

The output should be true for cases 1,2,4 only. For case 3 your program should return false. Please check if your code is working fine for all these 4 cases or not. May be your code isn’t handling any one of these cases.
Also when two adjacent numbers are the same in any sequence, then what does your program output?? This is also an important case.

Hope you get your mistake.
Happy coding :slightly_smiling_face:

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

Hey @stutijain578

I notice you have reopened this doubt. Is there anything left unresolved? Also, share your code through the Coding Blocks IDE. It would be a lot easier for me to help you then.

Hey Stuti

I made some modifications to your code:

prev = float('inf')
n = int(input())
dec = True
ans = "true"
for i in range(n):
	m = int(input())
	
	if ans=="false":
		break
	
	
	if dec:
		if m>=prev:
			dec = False
	else:
		if m<=prev:
			ans = "false"
	prev = m
print(ans)

It’s working now. In your earlier code, you were missing some edge cases like [3, 2, 1, 1, 4] (The answer should be true for this case) and also I think you were checking for First Increasing and then Decreasing sequences rather than the inverse.

Hope this helps!

1 Like

:sweat_smile:
thanks
btw why did you make typecaste inf to float ?

float('inf') is a special variable. It’s the way to declare infinity in python.
The reason I used that is because I want m>=prev to always evaluate to False in the first iteration because all variables are less than float('inf'). This way, the value of dec is never changed in the first iteration.