简体   繁体   English

写一个 function 确定每行连续的 BA, CA 字符对的最大数量

[英]Write a function that determines the maximum number of consecutive BA, CA character pairs per line

My respects, colleagues.尊敬的同事们。 I need to write a function that determines the maximum number of consecutive BA, CA character pairs per line.我需要写一个 function 来确定每行连续 BA、CA 字符对的最大数量。

print(f("BABABA125"))  # -> 3
print(f("234CA4BACA"))  # -> 2
print(f("BABACABACA56"))  # -> 5
print(f("1BABA24CA"))  # -> 2

Actually, I've written a function, but, to my mind, it's not very good.实际上,我已经写了一个 function,但是,在我看来,它不是很好。

def f(s: str) -> int:

    res = 0

    if not s:
        return res

    cur = 0
    i = len(s) - 1

    while i >= 0:
        if s[i] == "A" and (s[i-1] == "B" or s[i-1] == "C"):
            cur += 1
            i -= 2
        else:
            if cur > res:
                res = cur
                cur = 0
            i -= 1
    else:
        if cur > res:
            res = cur

    return res

In addition, I'm not allowed to use libraries and regular expressions (only string and list methods) .此外,我不允许使用库和正则表达式(仅限字符串和列表方法) Could you please help me or rate my code in this context.在这种情况下,您能否帮助我或评价我的代码。 I'll be very grateful.我将不胜感激。

Here's a function f2 that performs this operation.这是执行此操作的 function f2

  1. if not re.search('(BA|CA)', s): return 0
    First check if the string actually contains any BA or CA (to prevent ValueError: max() arg is an empty sequence on step 3), and return 0 if there aren't any.首先检查字符串是否实际包含任何BACA (以防止ValueError: max() arg is an empty sequence on step 3),如果没有则返回 0。
  2. matches = re.finditer(r'(?:CA|BA)+', s)
    Find all consecutive sequences of CA or BA , using non-capturing groups to ensure re.finditer outputs only full matches instead of partial matches.查找CABA的所有连续序列,使用非捕获组确保re.finditer只输出完全匹配而不是部分匹配。
  3. res = max(matches, key=lambda m: len(m.group(0)))
    Then, among the matches ( re.Match objects), fetch the matched substring using m.group(0) and compare their lengths to find the longest one.然后,在匹配项( re.Match对象)中,使用m.group(0)获取匹配的 substring 并比较它们的长度以找到最长的一个。
  4. return len(res.group(0))//2
    Divide the length of the longest result by 2 to get the number of BA or CA s in this substring. Here we use floor division // to coerce the output into an int , since division would normally convert the answer to float .将最长结果的长度除以 2 以获得 substring 中BACA的数量。这里我们使用 floor 除法//将 output 强制转换为int ,因为除法通常会将答案转换为float
import re

strings = [
    "BABABA125",  # 3
    "234CA4BACA",  # 2
    "BABACABACA56",  # 5
    "1BABA24CA",  # 2
    "NO_MATCH_TO_BE_FOUND",  # 0
]

def f2(s: str):
    if not re.search('(BA|CA)', s): return 0
    matches = re.finditer(r'(?:CA|BA)+', s)
    res = max(matches, key=lambda m: len(m.group(0)))
    return len(res.group(0))//2

for s in strings:
    print(f2(s))

UPDATE: Thanks to @StevenRumbalski for providing a simpler version of the above answer.更新:感谢@StevenRumbalski 提供上述答案的更简单版本。 (I split it into multiple lines for readability) (为了便于阅读,我把它分成多行)

def f3(s):
    if not re.search('(BA|CA)', s): return 0
    matches = re.findall(r'(?:CA|BA)+', s)
    max_length = max(map(len, matches))
    return max_length // 2
  1. if not re.search('(BA|CA)', s): return 0
    Same as above同上
  2. matches = re.findall(r'(?:CA|BA)+', s)
    Find all consecutive sequences of CA or BA , but each value in matches is a str instead of a re.Match , which is easier to handle.查找CABA的所有连续序列,但matches中的每个值都是str而不是re.Match ,这样更容易处理。
  3. max_length = max(map(len, matches))
    Map each matched substring to its length and find the maximum length among them. Map 分别匹配 substring 到它的长度,找出其中的最大长度。
  4. return max_length // 2
    Floor divide the length of the longest matching substring by the length of BA , CA to get the number of consecutive occurrences of BA or CA in this string. Floor 将最长匹配的 substring 的长度除以BA , CA的长度,得到该字符串中BACA连续出现的次数。

Here's an alternative implementation without any imports.这是没有任何导入的替代实现。 Do note however that it's quite slow compared to your C-style implementation.但是请注意,与您的 C 风格实现相比,它非常慢。

The idea is simple: Transform the input string into a string consisting of only two types of characters c1 and c2 , with c1 representing CA or BA , and c2 representing anything else.思路很简单:将输入字符串转换为仅由两种字符c1c2组成的字符串,其中c1代表CABAc2代表任何其他字符。 Then find the longest substring of consecutive c1 s.然后找到连续的c1最长的substring。

The implementation is as follows:实现如下:

  1. Pick a char that is guaranteed not to appear in the input string;选择一个保证不会出现在输入字符串中的字符; here we use + as an example.这里我们以+为例。 Then pick a char different from the previous one;然后选择一个与前一个不同的字符; here we use - .这里我们使用-
  2. Replace each occurrence of CA and BA with a + .将每次出现的CABA替换为+
  3. Replace everything else in the string (that is not a + ) with a - (this is why + cannot be present in the original input string).-替换字符串中的所有其他内容(不是+ )(这就是+不能出现在原始输入字符串中的原因)。 Now we have a string consisting purely of + s and - s.现在我们有一个完全由+ s 和- s 组成的字符串。
  4. Split the string with - as delimiter, and map each resulting substring to their length.使用-作为分隔符拆分字符串,并且 map 每个结果 substring 到它们的长度。
  5. Return the maximum of these substring lengths.返回这 substring 个长度中的最大值。
strings = [
    "BABABA125",  # 3
    "234CA4BACA",  # 2
    "BABACABACA56",  # 5
    "1BABA24CA",  # 2
    "NO_MATCH_TO_BE_FOUND",  # 0
]

def f4(string: str):
    string = string.replace("CA", "+")
    string = string.replace("BA", "+")
    string = "".join([(c if c == "+" else "-") for c in string])
    str_list = string.split("-")
    str_lengths = map(len, str_list)
    return max(str_lengths)

for s in strings:
    print(f4(s))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 计算每组连续 1 的最大数量 - Count maximum number of consecutive 1s per group 编写一个以列表 l 作为参数的 function 对。 它应该返回一个新列表,其中包含 l 的所有连续重叠对 - Write a function pairs that takes a list l as an argument. It should return a new list, which contains all consecutive overlapping pairs of l Python:字典的键/值对子集确定要调用的 function - Python: subset of key/value pairs of a dict determines the function to call 将字符编码映射到每个字符的最大字节数 - Mapping of character encodings to maximum bytes per character 如何在python中查找数字的最大连续出现次数 - How to find the maximum consecutive occurrences of a number in python 如何查找多列的最大连续数? - How to find the maximum consecutive number for multiple columns? 在 python 中打印最大连续 1 数 - printing maximum number of consecutive 1's in python 在 Tensorflow 中找到最大连续数 - Find the maximum number of consecutive ones in Tensorflow 给定错误的二进制中的最大连续数 - maximum number of consecutive 's in the binary for a given wrong 写一个 function,给定自然数 n,m,确定最小的自然数 k 使得 n^k >= m,时间为 O(log k) - Write a function that, given natural numbers n, m, determines the smallest natural number k such that n^k >= m, in time O(log k)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM