Check if the digits in the number are in increasing sequence in pythonHow do I check if a list is empty?How...
Contest math problem about crossing out numbers in the table
Why don't American passenger airlines operate dedicated cargo flights any more?
How to prevent cleaner from hanging my lock screen in Ubuntu 16.04
How to convert a ListContourPlot into primitive usable with Graphics3D?
Disable the ">" operator in Rstudio linux terminal
Is there some relative to Dutch word "kijken" in German?
Why would the Pakistan airspace closure cancel flights not headed to Pakistan itself?
Can I write a book of my D&D game?
Does Windows 10's telemetry include sending *.doc files if Word crashed?
Why did other German political parties disband so fast when Hitler was appointed chancellor?
Checking for the existence of multiple directories
What is this metal M-shaped device for?
Jumping Numbers
Broken patches on a road
How to deal with an incendiary email that was recalled
What flying insects could re-enter the Earth's atmosphere from space without burning up?
Would these multi-classing house rules cause unintended problems?
Groups acting on trees
A universal method for left-hand alignment of a sequence of equalities
Chess tournament winning streaks
How to avoid being sexist when trying to employ someone to function in a very sexist environment?
What makes the Forgotten Realms "forgotten"?
How do you funnel food off a cutting board?
If I delete my router's history can my ISP still provide it to my parents?
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 3 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))
well, it doesn't work. and it doesn't handle the case 7890 either
– Jean-François Fabre
3 hours ago
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))
well, it doesn't work. and it doesn't handle the case 7890 either
– Jean-François Fabre
3 hours ago
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))
well, it doesn't work. and it doesn't handle the case 7890 either
– Jean-François Fabre
3 hours ago
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
well, it doesn't work. and it doesn't handle the case 7890 either
– Jean-François Fabre
3 hours ago
add a comment |
well, it doesn't work. and it doesn't handle the case 7890 either
– Jean-François Fabre
3 hours ago
well, it doesn't work. and it doesn't handle the case 7890 either
– Jean-François Fabre
3 hours ago
well, it doesn't work. and it doesn't handle the case 7890 either
– Jean-François Fabre
3 hours ago
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