Given N numbers determine if they are in ascending order.
Click here to read the complete problem statement.
If you need help solving this problem, mention your approach and ask specific questions. Please avoid sharing your code and asking the Community to figure out “what’s wrong”.
@Rabeya The problem is in line 11-12 in your code. change,
for(i=0;i<n;i++){
if(a[i]>a[i+1]){
with
for(i=1;i<n;i++){
if(a[i-1]>a[i]){
The problem occurs when i becomes n -1 , anything like a[i+1] doesn’t exist. cause i +1 = n - 1 + 1 = n. and calling a[n] will ofcousre will show an error cause the index you are refering to is out of the array range. Therefore, I have change those lines such that this thing never occurs and your code should be accepted.
It is actually funny. You have done the same mistake as @Rabeya In your case the problem is in line 13-15. Read what I have written above and your problem should be solved.
And of course, @STS_12 and @adnan99, welcome to the Toph Community.
is there anyone in python,only one test case is not approving,but why?
N=int(input())
bul=False
if 0 < N < 100:
a=1
A=input()
A1=A.split()
for x in A1:
x=int(x)
if 1<=x<=1000 and x>=a:
a=x
bul=True
else:
bul=False
if bul:
print("Yes")
else:
print("No")
There are many things in your code that are unnecessary. For example, lines 3-4. You don’t need to test if there the input size is 1 hundred or 1 million. Many people do it but it is unnecessary.
However, the problem is not in lines 3-4 but in line 8. You have already used x in the loop so use another variable. I will change the loop variable with i traditionally. However It doesn’t matter in your case. And also, in line 4, The value of the first number will not always be 1 so you have to set it first. and also checking if 1 <= x <= 1000 is also unnecessary. Your code with my edits:
input()
A = input()
A1 = A.split()
a = int(A1[0])
for i in A1:
x = int(i)
if x >= a:
a = x
bul = True
else:
bul = False
break
if bul:
print("Yes")
else:
print("No")
See my submission using your code here.
See how I have initialized a in line 4.
Actually, the real problem in your code is not any of the things I have mentioned above. Simply putting the break statement would do. Everything else was right in case of this problem. However, you should keep in mind the things I have mentioned above in the future.
Your code with just the break statement included can be found here.
But why only adding break fixes it? Find it out yourself!!!
The Problem is Simple. You are taking a list of strings as input but trying to compare them as numbers.
Even in Python strings can also be compared the same way as numbers, it doesn’t mean the comparison between numbers and their equivalent string expressions will be the same.
Simply, even your code runs successfully, your submission won’t be accepted. You have to convert the strings of the list to numbers first before comparing them.
And again, Please remove the link of the accepted solution you have posted here. Although not punishable, it is usually discouraged by the moderators of Toph.
This is another poorly defined problem. It should ask to check for either strictly ascending or non-descending values. “Ascending” can mean either of these two things. Or perhaps it is asking for something else? Should [1,1,1,1,1,1,1,1] be considered “ascending”?