Check if the digits in the number are in increasing sequence in pythonHow do I check if a list is empty?How...
Process to change collation on a database
If I sold a PS4 game I owned the disc for, can I reinstall it digitally?
Does Improved Divine Smite trigger when a paladin makes an unarmed strike?
Why avoid shared user accounts?
What to do when being responsible for data protection in your lab, yet advice is ignored?
It took me a lot of time to make this, pls like. (YouTube Comments #1)
Eww, those bytes are gross
Using only 1s, make 29 with the minimum number of digits
why a subspace is closed?
How to avoid being sexist when trying to employ someone to function in a very sexist environment?
Disable the ">" operator in Rstudio linux terminal
How to deal with an incendiary email that was recalled
Why are the books in the Game of Thrones citadel library shelved spine inwards?
Placing an adverb between a verb and an object?
Is a debit card dangerous for an account with low balance and no overdraft protection?
Would these multi-classing house rules cause unintended problems?
Parsing a string of key-value pairs as a dictionary
Broken patches on a road
Difference between two quite-similar Terminal commands
How to tag distinct options/entities without giving any an implicit priority or suggested order?
How would one buy a used TIE Fighter or X-Wing?
Pre-1980's science fiction short story: alien disguised as a woman shot by a gangster, has tentacles coming out of her breasts when remaking her body
Check if the digits in the number are in increasing sequence in python
Why don't American passenger airlines operate dedicated cargo flights any more?
Check if the digits in the number are in increasing sequence in python
How do I check if a list is empty?How do I check whether a file exists without exceptions?Calling an external command in PythonWhat are metaclasses in Python?How do I check if an array includes an object in JavaScript?Does Python have a ternary conditional operator?Check if a given key already exists in a dictionaryHow to get the number of elements in a list in Python?Does Python have a string 'contains' substring method?Easy interview question got harder: given numbers 1..100, find the missing number(s)
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
python algorithm
add a comment |
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
python algorithm
1
Does each digit have to be exactly one bigger than the last?
– John Gordon
3 hours ago
yes @JohnGordon
– s326280
3 hours ago
1
The accepted answer currently seems to fail for 78901.
– גלעד ברקן
1 hour ago
Sorry, i didn't notice that !!.
– s326280
1 hour ago
add a comment |
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
python algorithm
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
python algorithm
python algorithm
edited 3 hours ago
s326280
asked 4 hours ago
s326280s326280
776
776
1
Does each digit have to be exactly one bigger than the last?
– John Gordon
3 hours ago
yes @JohnGordon
– s326280
3 hours ago
1
The accepted answer currently seems to fail for 78901.
– גלעד ברקן
1 hour ago
Sorry, i didn't notice that !!.
– s326280
1 hour ago
add a comment |
1
Does each digit have to be exactly one bigger than the last?
– John Gordon
3 hours ago
yes @JohnGordon
– s326280
3 hours ago
1
The accepted answer currently seems to fail for 78901.
– גלעד ברקן
1 hour ago
Sorry, i didn't notice that !!.
– s326280
1 hour ago
1
1
Does each digit have to be exactly one bigger than the last?
– John Gordon
3 hours ago
Does each digit have to be exactly one bigger than the last?
– John Gordon
3 hours ago
yes @JohnGordon
– s326280
3 hours ago
yes @JohnGordon
– s326280
3 hours ago
1
1
The accepted answer currently seems to fail for 78901.
– גלעד ברקן
1 hour ago
The accepted answer currently seems to fail for 78901.
– גלעד ברקן
1 hour ago
Sorry, i didn't notice that !!.
– s326280
1 hour ago
Sorry, i didn't notice that !!.
– s326280
1 hour ago
add a comment |
5 Answers
5
active
oldest
votes
you could zip the string representation of the number with a shifted self and iterate on consecutive digits together. Use all
to check that numbers follow, using a modulo 10 to handle the 0 case.
num = 7890
result = all((int(y)-int(x))%10 == 1 for x,y in zip(str(num),str(num)[1:]))
1
you can avoid the doublestr
'ing and slicing by usingzip(*[iter(str(num))] * 2)
instead but I imagine that's got way more overhead for such use in this case anyway... just throwing it out there...
– Jon Clements♦
3 hours ago
3
This would incorrectly returnTrue
for 78901, as the OP says "0 should come after 9, and not before 1".
– blhsing
2 hours ago
@JonClements nice but in that case I would create a string beforehand.
– Jean-François Fabre
1 hour ago
add a comment |
You can simply check if the number, when converted to a string, is a substring of '1234567890'
:
str(num) in '1234567890'
add a comment |
I would create a cycling generator and slice that:
from itertools import cycle, islice
num = 5678901234
num = tuple(str(num))
print(num == tuple(islice(cycle(map(str, range(10))), int(num[0]), int(num[0]) + len(num))))
This is faster than solutions that check differences between individual digits. Of course, you can sacrifice the length to make it faster:
def digits(num):
while num:
yield num % 10
num //= 10
def check(num):
num = list(digits(num))
num.reverse()
for i, j in zip(islice(cycle(range(10)), num[0], num[0] + len(num)), num):
if i != j:
return False
return True
add a comment |
Since you already have the zip version, here is an alternative solution:
import sys
order = dict(enumerate(range(10)))
order[0] = 10
def increasing(n):
n = abs(n)
o = order[n % 10] + 1
while n:
r = n % 10
n = n / 10
if o - order[r] != 1:
return False
o = order[r]
return True
for n in sys.argv[1:]:
print n, increasing(int(n))
add a comment |
Here's my take that just looks at the digits and exits as soon as there is a discrepancy:
def f(n):
while (n):
last = n % 10
n = n / 10
if n == 0:
return True
prev = n % 10
print last, prev
if prev == 0 or prev != (10 + last - 1) % 10:
return False
print f(1234)
print f(7890)
print f(78901)
print f(1345)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54954713%2fcheck-if-the-digits-in-the-number-are-in-increasing-sequence-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
you could zip the string representation of the number with a shifted self and iterate on consecutive digits together. Use all
to check that numbers follow, using a modulo 10 to handle the 0 case.
num = 7890
result = all((int(y)-int(x))%10 == 1 for x,y in zip(str(num),str(num)[1:]))
1
you can avoid the doublestr
'ing and slicing by usingzip(*[iter(str(num))] * 2)
instead but I imagine that's got way more overhead for such use in this case anyway... just throwing it out there...
– Jon Clements♦
3 hours ago
3
This would incorrectly returnTrue
for 78901, as the OP says "0 should come after 9, and not before 1".
– blhsing
2 hours ago
@JonClements nice but in that case I would create a string beforehand.
– Jean-François Fabre
1 hour ago
add a comment |
you could zip the string representation of the number with a shifted self and iterate on consecutive digits together. Use all
to check that numbers follow, using a modulo 10 to handle the 0 case.
num = 7890
result = all((int(y)-int(x))%10 == 1 for x,y in zip(str(num),str(num)[1:]))
1
you can avoid the doublestr
'ing and slicing by usingzip(*[iter(str(num))] * 2)
instead but I imagine that's got way more overhead for such use in this case anyway... just throwing it out there...
– Jon Clements♦
3 hours ago
3
This would incorrectly returnTrue
for 78901, as the OP says "0 should come after 9, and not before 1".
– blhsing
2 hours ago
@JonClements nice but in that case I would create a string beforehand.
– Jean-François Fabre
1 hour ago
add a comment |
you could zip the string representation of the number with a shifted self and iterate on consecutive digits together. Use all
to check that numbers follow, using a modulo 10 to handle the 0 case.
num = 7890
result = all((int(y)-int(x))%10 == 1 for x,y in zip(str(num),str(num)[1:]))
you could zip the string representation of the number with a shifted self and iterate on consecutive digits together. Use all
to check that numbers follow, using a modulo 10 to handle the 0 case.
num = 7890
result = all((int(y)-int(x))%10 == 1 for x,y in zip(str(num),str(num)[1:]))
answered 3 hours ago
Jean-François FabreJean-François Fabre
105k955112
105k955112
1
you can avoid the doublestr
'ing and slicing by usingzip(*[iter(str(num))] * 2)
instead but I imagine that's got way more overhead for such use in this case anyway... just throwing it out there...
– Jon Clements♦
3 hours ago
3
This would incorrectly returnTrue
for 78901, as the OP says "0 should come after 9, and not before 1".
– blhsing
2 hours ago
@JonClements nice but in that case I would create a string beforehand.
– Jean-François Fabre
1 hour ago
add a comment |
1
you can avoid the doublestr
'ing and slicing by usingzip(*[iter(str(num))] * 2)
instead but I imagine that's got way more overhead for such use in this case anyway... just throwing it out there...
– Jon Clements♦
3 hours ago
3
This would incorrectly returnTrue
for 78901, as the OP says "0 should come after 9, and not before 1".
– blhsing
2 hours ago
@JonClements nice but in that case I would create a string beforehand.
– Jean-François Fabre
1 hour ago
1
1
you can avoid the double
str
'ing and slicing by using zip(*[iter(str(num))] * 2)
instead but I imagine that's got way more overhead for such use in this case anyway... just throwing it out there...– Jon Clements♦
3 hours ago
you can avoid the double
str
'ing and slicing by using zip(*[iter(str(num))] * 2)
instead but I imagine that's got way more overhead for such use in this case anyway... just throwing it out there...– Jon Clements♦
3 hours ago
3
3
This would incorrectly return
True
for 78901, as the OP says "0 should come after 9, and not before 1".– blhsing
2 hours ago
This would incorrectly return
True
for 78901, as the OP says "0 should come after 9, and not before 1".– blhsing
2 hours ago
@JonClements nice but in that case I would create a string beforehand.
– Jean-François Fabre
1 hour ago
@JonClements nice but in that case I would create a string beforehand.
– Jean-François Fabre
1 hour ago
add a comment |
You can simply check if the number, when converted to a string, is a substring of '1234567890'
:
str(num) in '1234567890'
add a comment |
You can simply check if the number, when converted to a string, is a substring of '1234567890'
:
str(num) in '1234567890'
add a comment |
You can simply check if the number, when converted to a string, is a substring of '1234567890'
:
str(num) in '1234567890'
You can simply check if the number, when converted to a string, is a substring of '1234567890'
:
str(num) in '1234567890'
answered 2 hours ago
blhsingblhsing
36.2k41639
36.2k41639
add a comment |
add a comment |
I would create a cycling generator and slice that:
from itertools import cycle, islice
num = 5678901234
num = tuple(str(num))
print(num == tuple(islice(cycle(map(str, range(10))), int(num[0]), int(num[0]) + len(num))))
This is faster than solutions that check differences between individual digits. Of course, you can sacrifice the length to make it faster:
def digits(num):
while num:
yield num % 10
num //= 10
def check(num):
num = list(digits(num))
num.reverse()
for i, j in zip(islice(cycle(range(10)), num[0], num[0] + len(num)), num):
if i != j:
return False
return True
add a comment |
I would create a cycling generator and slice that:
from itertools import cycle, islice
num = 5678901234
num = tuple(str(num))
print(num == tuple(islice(cycle(map(str, range(10))), int(num[0]), int(num[0]) + len(num))))
This is faster than solutions that check differences between individual digits. Of course, you can sacrifice the length to make it faster:
def digits(num):
while num:
yield num % 10
num //= 10
def check(num):
num = list(digits(num))
num.reverse()
for i, j in zip(islice(cycle(range(10)), num[0], num[0] + len(num)), num):
if i != j:
return False
return True
add a comment |
I would create a cycling generator and slice that:
from itertools import cycle, islice
num = 5678901234
num = tuple(str(num))
print(num == tuple(islice(cycle(map(str, range(10))), int(num[0]), int(num[0]) + len(num))))
This is faster than solutions that check differences between individual digits. Of course, you can sacrifice the length to make it faster:
def digits(num):
while num:
yield num % 10
num //= 10
def check(num):
num = list(digits(num))
num.reverse()
for i, j in zip(islice(cycle(range(10)), num[0], num[0] + len(num)), num):
if i != j:
return False
return True
I would create a cycling generator and slice that:
from itertools import cycle, islice
num = 5678901234
num = tuple(str(num))
print(num == tuple(islice(cycle(map(str, range(10))), int(num[0]), int(num[0]) + len(num))))
This is faster than solutions that check differences between individual digits. Of course, you can sacrifice the length to make it faster:
def digits(num):
while num:
yield num % 10
num //= 10
def check(num):
num = list(digits(num))
num.reverse()
for i, j in zip(islice(cycle(range(10)), num[0], num[0] + len(num)), num):
if i != j:
return False
return True
edited 3 hours ago
answered 3 hours ago
Tomothy32Tomothy32
7,2721627
7,2721627
add a comment |
add a comment |
Since you already have the zip version, here is an alternative solution:
import sys
order = dict(enumerate(range(10)))
order[0] = 10
def increasing(n):
n = abs(n)
o = order[n % 10] + 1
while n:
r = n % 10
n = n / 10
if o - order[r] != 1:
return False
o = order[r]
return True
for n in sys.argv[1:]:
print n, increasing(int(n))
add a comment |
Since you already have the zip version, here is an alternative solution:
import sys
order = dict(enumerate(range(10)))
order[0] = 10
def increasing(n):
n = abs(n)
o = order[n % 10] + 1
while n:
r = n % 10
n = n / 10
if o - order[r] != 1:
return False
o = order[r]
return True
for n in sys.argv[1:]:
print n, increasing(int(n))
add a comment |
Since you already have the zip version, here is an alternative solution:
import sys
order = dict(enumerate(range(10)))
order[0] = 10
def increasing(n):
n = abs(n)
o = order[n % 10] + 1
while n:
r = n % 10
n = n / 10
if o - order[r] != 1:
return False
o = order[r]
return True
for n in sys.argv[1:]:
print n, increasing(int(n))
Since you already have the zip version, here is an alternative solution:
import sys
order = dict(enumerate(range(10)))
order[0] = 10
def increasing(n):
n = abs(n)
o = order[n % 10] + 1
while n:
r = n % 10
n = n / 10
if o - order[r] != 1:
return False
o = order[r]
return True
for n in sys.argv[1:]:
print n, increasing(int(n))
edited 3 hours ago
answered 3 hours ago
khachikkhachik
21.1k54381
21.1k54381
add a comment |
add a comment |
Here's my take that just looks at the digits and exits as soon as there is a discrepancy:
def f(n):
while (n):
last = n % 10
n = n / 10
if n == 0:
return True
prev = n % 10
print last, prev
if prev == 0 or prev != (10 + last - 1) % 10:
return False
print f(1234)
print f(7890)
print f(78901)
print f(1345)
add a comment |
Here's my take that just looks at the digits and exits as soon as there is a discrepancy:
def f(n):
while (n):
last = n % 10
n = n / 10
if n == 0:
return True
prev = n % 10
print last, prev
if prev == 0 or prev != (10 + last - 1) % 10:
return False
print f(1234)
print f(7890)
print f(78901)
print f(1345)
add a comment |
Here's my take that just looks at the digits and exits as soon as there is a discrepancy:
def f(n):
while (n):
last = n % 10
n = n / 10
if n == 0:
return True
prev = n % 10
print last, prev
if prev == 0 or prev != (10 + last - 1) % 10:
return False
print f(1234)
print f(7890)
print f(78901)
print f(1345)
Here's my take that just looks at the digits and exits as soon as there is a discrepancy:
def f(n):
while (n):
last = n % 10
n = n / 10
if n == 0:
return True
prev = n % 10
print last, prev
if prev == 0 or prev != (10 + last - 1) % 10:
return False
print f(1234)
print f(7890)
print f(78901)
print f(1345)
answered 1 hour ago
גלעד ברקןגלעד ברקן
13k21542
13k21542
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54954713%2fcheck-if-the-digits-in-the-number-are-in-increasing-sequence-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Does each digit have to be exactly one bigger than the last?
– John Gordon
3 hours ago
yes @JohnGordon
– s326280
3 hours ago
1
The accepted answer currently seems to fail for 78901.
– גלעד ברקן
1 hour ago
Sorry, i didn't notice that !!.
– s326280
1 hour ago