Untitled
unknown
plain_text
5 months ago
1.5 kB
5
Indexable
def is_attractive(code, k): """ Check if a code is attractive (digits repeat every k positions) """ n = len(code) for i in range(n - k): if code[i] != code[i + k]: return False return True def generate_attractive_code(org_code, k): """ Generate the smallest attractive code that is >= the original code Args: org_code (str): Original code as string k (int): Pattern repeat length Returns: str: Smallest attractive code >= org_code """ n = len(org_code) # If already attractive, return original if is_attractive(org_code, k): return org_code def make_pattern(prefix): """Create a pattern by repeating the prefix""" result = prefix while len(result) < n: result += result[len(result) - k] return result[:n] # Try patterns starting with first k digits prefix = org_code[:k] while True: # Create candidate by repeating current prefix candidate = make_pattern(prefix) # If candidate is >= original code, return it if candidate >= org_code: return candidate # Increment prefix prefix_num = int(prefix) + 1 prefix = str(prefix_num).zfill(k) # Handle case where prefix becomes longer than k if len(str(prefix_num)) > k: prefix = '1' + '0' * (k-1)
Editor is loading...
Leave a Comment