Python – Equidistant consecutive characters Strings

Equidistant consecutive character strings are strings where the ASCII difference between consecutive characters remains constant throughout the string. For example, in "abc", each character is 1 ASCII value apart, while in "agms", each character is 6 ASCII values apart.

Understanding the Concept

To check if a string has equidistant consecutive characters, we need to ?

  • Calculate the ASCII difference between the first two characters
  • Verify that all other consecutive pairs have the same difference
  • Use ord() to get ASCII values and all() to check the condition

Example

Here's how to find all equidistant consecutive character strings from a list ?

strings_list = ["abc", "egfg", "mpsv", "abed", 'xzbd', 'agms']

print("The list is :")
print(strings_list)

# Find strings with equidistant consecutive characters
result = [string for string in strings_list 
          if all(ord(string[i + 1]) - ord(string[i]) == ord(string[1]) - ord(string[0]) 
                 for i in range(len(string) - 1))]

print("The resultant list is :")
print(result)
The list is :
['abc', 'egfg', 'mpsv', 'abed', 'xzbd', 'agms']
The resultant list is :
['abc', 'mpsv', 'agms']

How It Works

Let's break down the logic step by step ?

# Check individual strings to understand the pattern
test_strings = ["abc", "egfg", "mpsv"]

for string in test_strings:
    print(f"\nAnalyzing '{string}':")
    differences = []
    
    for i in range(len(string) - 1):
        diff = ord(string[i + 1]) - ord(string[i])
        differences.append(diff)
        print(f"  {string[i]} to {string[i+1]}: {ord(string[i])} to {ord(string[i+1])} = {diff}")
    
    is_equidistant = all(d == differences[0] for d in differences)
    print(f"  All differences equal? {is_equidistant}")
Analyzing 'abc':
  a to b: 97 to 98 = 1
  b to c: 98 to 99 = 1
  All differences equal? True

Analyzing 'egfg':
  e to g: 101 to 103 = 2
  g to f: 103 to 102 = -1
  f to g: 102 to 103 = 1
  All differences equal? False

Analyzing 'mpsv':
  m to p: 109 to 112 = 3
  p to s: 112 to 115 = 3
  s to v: 115 to 118 = 3
  All differences equal? True

Alternative Approach Using Function

For better readability, we can create a dedicated function ?

def is_equidistant(string):
    """Check if a string has equidistant consecutive characters."""
    if len(string) < 2:
        return True
    
    expected_diff = ord(string[1]) - ord(string[0])
    
    for i in range(len(string) - 1):
        if ord(string[i + 1]) - ord(string[i]) != expected_diff:
            return False
    
    return True

# Test the function
strings_list = ["abc", "egfg", "mpsv", "abed", 'xzbd', 'agms']
result = [string for string in strings_list if is_equidistant(string)]

print("Equidistant strings:", result)
Equidistant strings: ['abc', 'mpsv', 'agms']

Comparison

Method Readability Performance Best For
List Comprehension with all() Compact Fast One-liners
Dedicated Function High Fast Reusable code

Conclusion

Use ord() to get ASCII values and all() to check if consecutive character differences are equal. The list comprehension approach is concise, while a dedicated function improves code readability and reusability.

Updated on: 2026-03-26T01:50:59+05:30

288 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements