PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, October 17, 2022

[FIXED] How can I check whether neighbor (previous and next) digits in a row differ in 1 (Python 3)

 October 17, 2022     compare, integer, list, python-3.x     No comments   

Issue

is it possible to create a code(without list comprehensions, please) that will compare whether neighbor (previous and next) digits in a row differ in 1 so if the condition is met returns the row and returns "good", if not met - "bad". Also, the difference between 9 and 0 doesn't consider as 1. Besides, I need to check whether the only one-digit number and return "one digit" then. I see for me just now it's tricky, so please help! For example:

for (12345432): 
    return "good"                                                                  
for (1793):
    return "bad"
for (7):
    return "one digit"                                                                         

`


Solution

Another solution (with check for one digit):

i = 12345432

if i < 10:
    print("One Digit")
else:
    for a, b in zip(str(i), str(i)[1:]):
        if abs(int(a) - int(b)) != 1:
            print("Bad")
            break
    else:
        print("Good")

Prints:

Good


Answered By - Andrej Kesely
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing