Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if the digits in the number are in increasing sequence in python

I was working on a problem that determines whether the digits in the numbers are in the increasing sequence. Now, the approach I took to solve the problem was, For instance, consider the number 5678.

To check whether 5678 is an increasing sequence, I took the first digit and the next digit and the last digit which is 5,6,8 and substitute in range function range(first,last,(diff of first digit and the next to first digit)) i.e range(5,8+1,abs(5-6)).The result is the list of digits in the ascending order

To this problem, there is a constraint saying

For incrementing sequences, 0 should come after 9, and not before 1, as in 7890. Now my program breaks at the input 7890. I don't know how to encode this logic. Can someone help me, please?.

The code for increasing sequence was

  len(set(['5','6','7','8']) - set(map(str,range(5,8+1,abs(5-6))))) == 0 
like image 468
James K J Avatar asked Mar 02 '19 02:03

James K J


1 Answers

You can simply check if the number, when converted to a string, is a substring of '1234567890':

str(num) in '1234567890'
like image 96
blhsing Avatar answered Sep 28 '22 21:09

blhsing