w3resource

Python Exercises: Absolute difference between two consecutive digits


92. Consecutive Digits Difference Checker

Write a Python program that checks whether the absolute difference between two consecutive digits is two or not. Return true otherwise false.

Sample Data:
(666) -> False
(3579) -> True
(2468) -> True
(20420) -> False

Sample Solution-1:

Python Code:

def test(n):
    return all(abs(int(x) - int(y)) == 2 for x, y in zip(str(n), str(n)[1:]))
     
n = 666
print("Original number:", n)
print("Is absolute difference between two consecutive digits of the said number is 2 or not?")
print(test(n))
n = 3579
print("\nOriginal number:", n)
print("Is absolute difference between two consecutive digits of the said number is 2 or not?")
print(test(n))
n = 2468
print("\nOriginal number:", n)
print("Is absolute difference between two consecutive digits of the said number is 2 or not?")
print(test(n))
n = 20420
print("\nOriginal number:", n)
print("Is absolute difference between two consecutive digits of the said number is 2 or not?")
print(test(n))

Sample Output:

Original number: 666
Is absolute difference between two consecutive digits of the said number is 2 or not?
False

Original number: 3579
Is absolute difference between two consecutive digits of the said number is 2 or not?
True

Original number: 2468
Is absolute difference between two consecutive digits of the said number is 2 or not?
True

Original number: 20420
Is absolute difference between two consecutive digits of the said number is 2 or not?
False

Flowchart:

Flowchart: Absolute difference between two consecutive digits.

Sample Solution-2:

Python Code:

def test(n):
    text = str(n)
    r = [int(x) for x in text]
    return all(abs(r[i]-r[i+1])==2 for i in range(len(r)-2))
     
n = 666
print("Original number:", n)
print("Is absolute difference between two consecutive digits of the said number is 2 or not?")
print(test(n))
n = 3579
print("\nOriginal number:", n)
print("Is absolute difference between two consecutive digits of the said number is 2 or not?")
print(test(n))
n = 2468
print("\nOriginal number:", n)
print("Is absolute difference between two consecutive digits of the said number is 2 or not?")
print(test(n))
n = 20420
print("\nOriginal number:", n)
print("Is absolute difference between two consecutive digits of the said number is 2 or not?")
print(test(n))

Sample Output:

Original number: 666
Is absolute difference between two consecutive digits of the said number is 2 or not?
False

Original number: 3579
Is absolute difference between two consecutive digits of the said number is 2 or not?
True

Original number: 2468
Is absolute difference between two consecutive digits of the said number is 2 or not?
True

Original number: 20420
Is absolute difference between two consecutive digits of the said number is 2 or not?
False

Flowchart:

Flowchart: Absolute difference between two consecutive digits.

For more Practice: Solve these Related Problems:

  • Write a Python program to check if the absolute difference between every pair of consecutive digits in a number is exactly two, and print True or False.
  • Write a Python function that iterates through the digits of an integer and returns True if the condition holds for all adjacent digits, otherwise False.
  • Write a Python script to prompt for a number, compute the differences between consecutive digits, and output whether all differences equal two.
  • Write a Python program to test multiple numbers for the consecutive digit difference condition and then display the results in a formatted report.

Go to:


Previous Python Exercise: Next number containing only distinct digits.
Next Python Exercise: Rearrange the digits of a number.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.