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
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): ... - oneWith 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") |