A string is entered, if there are 3 consecutive identical characters in it, then you must output NO, if not - YES

  • Regulars? Or who prompts a beautiful solution on Monty? - smackmychi
  • I did not understand both questions. - Hashirama
  • A strange cycle is needed like this: for k in range (0, len (str) -3): if same3 (str [k: k + 3]): ... Or a regular schedule: (.) ​​\ 1 {2} If not more than three in a row: (.) ​​\ 1 {2} (?! \ 1) - ReinRaus
  • ReinRaus, thank you, I used your cycle, everything is almost fine, except that if there are three characters in a row after more than 3 different characters, it displays YES several times and NO once. How to fix this error? - Hashirama

2 answers 2

Regular expressions rush to the rescue!

import re str1 = 'qwerty' str2 = 'qwerttty' regex = re.compile(r'^.*(.)(\1)(\1).*$') print 'YES' if regex.match(str1) else 'NO' print 'YES' if regex.match(str2) else 'NO' 

And your program does not work because the slice that you take on each iteration is only 2 elements.

 >>> s = [1, 2, 3, 4, 5] >>> i = 2 >>> j = 0 >>> s[j:i] [1, 2] 

Well, or something like this:

 same3 = lambda s: min([len(x) for x in [set(x) for x in [s[x:x+3] for x in range(len(s) - 2)]]]) == 1 same3('qwerty') // False same3('qwerttty') // True 

Yes! I did it! I wrote a program that is less readable than a regular program, which is equivalent to it!

But seriously, this program does the following:

 s = 'qwerty' triplets = [s[x:x+3] for x in range(len(s) - 2)] // Π½Π°Ρ…ΠΎΠ΄ΠΈΠΌ всС Ρ‚Ρ€ΠΎΠΉΠΊΠΈ подряд ΠΈΠ΄ΡƒΡ‰ΠΈΡ… символов // для `qwerty` ΠΏΠΎΠ»ΡƒΡ‡ΠΈΠΌ ['qwe', 'wer', 'ert', 'rty'] sets = [set(x) for x in triplets] // ΠΊΠ°ΠΆΠ΄ΡƒΡŽ Ρ‚Ρ€ΠΎΠΉΠΊΡƒ ΠΏΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΡƒΠ΅ΠΌ Π² мноТСство (set) // ΠΎΡΠΎΠ±Π΅Π½Π½ΠΎΡΡ‚ΡŒ мноТСства - Π² Π½Ρ‘ΠΌ Π½Π΅Ρ‚ ΠΏΠΎΠ²Ρ‚ΠΎΡ€ΡΡŽΡ‰ΠΈΡ…ΡΡ элСмСнтов lengths = [len(x) for x in sets] // вычисляСм Ρ€Π°Π·ΠΌΠ΅Ρ€ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ мноТСства // Ссли Ρ€Π°Π·ΠΌΠ΅Ρ€ ΠΊΠ°ΠΊΠΎΠ³ΠΎ-Π»ΠΈΠ±ΠΎ мноТСства Ρ€Π°Π²Π΅Π½ 1, Ρ‚ΠΎ всС элСмСнты, входящиС Π² Π½Π΅Π³ΠΎ, Ρ€Π°Π²Π½Ρ‹ minlen = min(lengths) // Π½Π°Ρ…ΠΎΠ΄ΠΈΠΌ мноТСство минимальной Π΄Π»ΠΈΠ½Ρ‹ print minlen == 1 

Well, the declaration of the function same3 = lambda s: ... equivalent to the classical one:

 def same3(s): ... 
  • one
    With such a regular string containing more than 3 identical letters in a row, "qwqrtttttttqff" will also be counted. Is it possible to change this behavior by the forces of reg. expressions? - moron
  • one
    @moron: Any string containing 4 single letters in a row also contains 3 identical letters in a row. If you change the condition and require that exactly 3 consecutive identical symbols go in a row, then this can also be done using reg. inc .: (.)(?<!\1.)\1{2}(?!\1) - jfs

A string is entered, if there are 3 consecutive identical characters in it, then you must output NO, if not - YES

 import re print("NO" if re.search(r"(.)" + r"\1" * (3 - 1), s) else "YES") 

Or without regular expressions:

 from itertools import groupby print("NO" if any(len(list(dups)) > (3 - 1) for _, dups in groupby(s)) else "YES")