+ 3

What’s wrong?

The entered sequence in reverse order should be ascending, why doesn't the code output anything? https://code.sololearn.com/cduo0qPffVQj/?ref=app

4th Feb 2023, 12:50 PM
Anastasia Levi
Anastasia Levi - avatar
17 Respuestas
+ 3
For example, if input is 12345 then reversed is 54321 counter = 0 digit = 5 at first iteration so in inner while loop : digit < counter is false so by else counter = digit so counter = 5, iterating inner loop digit < counter false so by else counter = digit.. => counter = 5 ... .. This will be repeated for ever and it's an infinite loop... Hope it clears..
4th Feb 2023, 2:03 PM
Jayakrishna 🇮🇳
+ 9
Nastya , an other approach could be to compare the input sequence of digits against a sorted sequence: > split input string to a list of numbers: ... = list(map(int, list(input()))) > input '123': list(input()) => ['1', '2', '3'] > now we map the int constructor to each element of the list: list(map(int, ['1', '2', '3'])) => [1, 2, 3] > now we can sort the resulting list from above and compare it against the unsorted list. digits = list(map(int, list(input()))) if digits == sorted(digits): print('No') else: print('Yes')
4th Feb 2023, 7:37 PM
Lothar
Lothar - avatar
+ 8
Smith Welder , i suppose that you have not tested my code. for input: 1245, the output is 'NO'
5th Feb 2023, 4:20 PM
Lothar
Lothar - avatar
+ 7
Benjamin Kiplagat , it is better for you to start your own post, otherwise poeple will not get aware of you. please also include your current code in the post.
7th Feb 2023, 8:35 PM
Lothar
Lothar - avatar
+ 3
Smith Welder, number = list(map(int, input())) print("YES"if all(number[i] >= number[i+1] for i in range(len(number)-1)) else "NO")
5th Feb 2023, 11:31 AM
Anastasia Levi
Anastasia Levi - avatar
+ 2
I guess task is to check all digits should be in ascending order. There is no need of inner while loop in your code as you need to traverse sequencly in one time. Use index positions instead of values. So you can check list[0] > list[1] , list[1] > list[2], .. Without indexes, you can't get next value. As you dont need original value, replace original with number = numbers[::-1] #reversed list numbers Then use for i in range(len(numbers)) : ...
4th Feb 2023, 1:39 PM
Jayakrishna 🇮🇳
+ 2
Thanks a lot, but I want to understand my mistake, why didn't my code work correctly? 🥺🥺
4th Feb 2023, 1:55 PM
Anastasia Levi
Anastasia Levi - avatar
4th Feb 2023, 2:56 PM
Anastasia Levi
Anastasia Levi - avatar
+ 2
Well done.Good logic.. 👏👍
4th Feb 2023, 3:06 PM
Jayakrishna 🇮🇳
+ 1
The while loop is infinite. What are you trying? Any example input and expected output?
4th Feb 2023, 1:02 PM
Jayakrishna 🇮🇳
+ 1
This code has a condition that breaks the loop 🤔 For example: input - 12345 -> output - NO input - 5321 -> output - YES
4th Feb 2023, 1:26 PM
Anastasia Levi
Anastasia Levi - avatar
+ 1
you cant use this operator ">" or this operator "<" to compire sequence, cause you'll get "yes" for this 54321 and you'll get "yes" for wrong sequence too , like this 5421. So you need use operator "==" for your solution and compire every number in sequence. You not need to use some flags, you can use function all() for that.. Your solution can be look like this: number = list(map(int, input())) print("YES"if all(number[i] == number[i+1]+1 for i in range(len(number)-1))else "NO")
5th Feb 2023, 10:56 AM
Smith Welder
Smith Welder - avatar
+ 1
Nastya, number = list(map(int, input())) print("YES"if all(number[i] >= number[i+1] for i in range(len(number)-1)) else "NO") this wrong too, try to input like this 5421 and you'll get "yes", cause here you are compire great or equals between two numbers, but dont check out sequence.. As i wrote, you can see that compire all numbers and chek out all sequence, plus you not need call reversed() function. Look at this again: number = list(map(int, input())) print("YES"if all(number[i] == number[i+1]+1 for i in range(len(number)-1))else "NO")
5th Feb 2023, 11:43 AM
Smith Welder
Smith Welder - avatar
+ 1
Smith Welder, This will be correct, because I need to check not that the numbers are in order, but that they are reversed in ascending order and each subsequent one is NOT less than the previous one For example: input - 12345 -> output - NO input - 5321 -> output - YES
5th Feb 2023, 11:52 AM
Anastasia Levi
Anastasia Levi - avatar
0
Lothar, function sorted() cant work right with sequence, cause you'll get "yes" whatever right sequence or not.. 12345 - "yes" but for wrong sequence will be "yes" too 1245 - "yes"
5th Feb 2023, 11:12 AM
Smith Welder
Smith Welder - avatar
0
May anyone help me to solve a problem
5th Feb 2023, 9:13 PM
Benjamin Kiplagat
Benjamin Kiplagat - avatar