Home > Enterprise >  How to check if the characters in the corresponding positions in the second string are repeating in
How to check if the characters in the corresponding positions in the second string are repeating in

Time:01-30

It's from Educative.io 1st exam in "Learn Python 3 from scratch":

Detecting String Pattern

In this coding exercise, you are asked to write the body of a function called detect_pattern that returns true or false depending upon whether two strings have the same pattern of characters. More precisely, two strings have the same pattern if they are of the same length and if two characters in the first string are equal if and only if the characters in the corresponding positions in the second string are also equal.

Below are some examples of patterns that are the same and patterns that differ:

1st String 2nd String Same Pattern
“” “” True
“a” “a” True
“x” “y” True
“ab” “xy” True
“aba” “xyz” False
“- - -” “xyz” False
“- - -” “aaa” True
“xyzxyz” “toetoe” True
“xyzxyz” “toetoa” False
“aaabbbcccd” “eeefffgggz” True
“cbacbacba” “xyzxyzxyz” True
“abcdefghijk” “lmnopqrstuv” True
“asasasasas” “xxxxxyyyyy” False
“ascneencsa” “aeiouaeiou” False
“aaasssiiii” “gggdddfffh” False

For example, if two strings called s1 and s2 contain the following letters:

s1 = "aba"

s2 = "xyz"

then the call detect_pattern(s1, s2) should return False.

Note: The function detect_pattern takes two parameters: the two strings to compare. You are allowed to create new strings, but otherwise you are not allowed to construct extra data structures to solve this problem (no list, set, dictionary, etc). Keep in mind that the method should return the same value no matter what order the two strings are passed.

Good Luck!

My code:

import unittest
import re   # only needed if we use hint from 6th line

''' HINT:
if we add this to the end of 13th line before ending ":":
 and pattern.match(s1.replace(" ", "")) == pattern.match(s2.replace(" ", "")) and len(set(s1.replace(" ", ""))) == len(set(s2.replace(" ", "")))
it will solve more case's but we can't use set() method.
'''

pattern = re.compile("^([a-z][0-9] ) $") # only needed if we use hint from 6th line

def detect_pattern(s1, s2):
    if len(s1.replace(" ", "")) == len(s2.replace(" ", "")):
        return True
    else:
        return False

class TestDetectPattern(unittest.TestCase):

    def test_basics(self):
        self.assertEqual(detect_pattern("", ""), True)
        self.assertEqual(detect_pattern("a", "a"), True)
        self.assertEqual(detect_pattern("x", "y"), True)
        self.assertEqual(detect_pattern("ab", "xy"), True)
        # self.assertEqual(detect_pattern("aba", "xyz"), False) # can be solved with hint from 6th line but in this task we can't use set() method
        # self.assertEqual(detect_pattern("- - -", "xyz"), False) # can be solved with hint from 6th line but in this task we can't use set() method
        self.assertEqual(detect_pattern("- - -", "aaa"), True)
        self.assertEqual(detect_pattern("xyzxyz", "toetoe"), True)
        # self.assertEqual(detect_pattern("xyzxyz", "toetoa"), False) # can be solved with hint from 6th line but in this task we can't use set() method
        self.assertEqual(detect_pattern("aaabbbcccd", "eeefffgggz"), True)
        self.assertEqual(detect_pattern("cbacbacba", "xyzxyzxyz"), True)
        self.assertEqual(detect_pattern("abcdefghijk", "lmnopqrstuv"), True)
        # self.assertEqual(detect_pattern("asasasasas", "xxxxxyyyyy"), False)
        # self.assertEqual(detect_pattern("ascneencsa", "aeiouaeiou"), False)
        # self.assertEqual(detect_pattern("aaasssiiii", "gggdddfffh"), False) # can be solved with hint from 6th line but in this task we can't use set() method

if __name__ == '__main__':
    unittest.main()

Does anyone know how to check if the characters in the corresponding positions in the second string are repeating in same position like in first string? - i think this can solve whole problem without using set() method.

CodePudding user response:

Edit: There were some problems within the condition of the if-statement but this problem should be resolved now.

This approach should work. I am not very happy with the variable naming within the loop so feel free to change them. I tried it with all the examples you provided so semantically it should be correct.

def detect_pattern(s1: str, s2: str) -> bool:
    if len(s1) != len(s2):
        return False
    for idx, (let_1, let_2) in enumerate(zip(s1, s2)):
        for let_1_inner, let_2_inner in zip(s1[idx:], s2[idx:]):
            if (let_1_inner == let_1) != (let_2_inner == let_2):
                return False
    return True
  •  Tags:  
  • Related