def mystery(x):
if not isinstance(x, str):
return False
# The input string must be between 10 and 25 characters long (inclusive).
if not (10 <= len(x) <= 25):
return False
# The string must be split into exactly three parts by the '@' delimiter.
parts = x.split('@')
if len(parts) != 3:
return False
p1_str, p2_str, p3_str = parts
try:
# Part 1: Interpreted as a hexadecimal number.
n1 = int(p1_str, 16)
# Part 2: Interpreted as a binary number.
n2 = int(p2_str, 2)
# Part 3: Interpreted as a decimal number.
n3 = int(p3_str)
except ValueError:
# If any part fails to parse as its specified base, return False.
return False
# Condition 1: n1 must be a prime number.
if n1 < 2:
return False
for i in range(2, int(n1**0.5) + 1):
if n1 % i == 0:
return False
# Condition 2: n2 must be a power of 2 (e.g., 1, 2, 4, 8, 16...).
if n2 <= 0 or (n2 & (n2 - 1)) != 0:
return False
# Condition 3: n3 must be a perfect square (e.g., 0, 1, 4, 9, 16...).
if n3 < 0:
return False
sqrt_n3 = int(n3**0.5)
if sqrt_n3 * sqrt_n3 != n3:
return False
# Condition 4: The original string x must contain at least one uppercase letter.
if not any(c.isupper() for c in x):
return False
# Final Condition: The sum of the three numbers (n1 + n2 + n3) must be a multiple of 10.
if not ((n1 + n2 + n3) % 10 == 0):
return False
return True
def mystery(x):
# Check that x is a string
if not isinstance(x, str):
return False
# Split by colon
parts = x.split(':')
if len(parts) != 3:
return False
A_str, B_str, C_str = parts
# Each part must consist only of digits and have length at least 3
if not (A_str.isdigit() and B_str.isdigit() and C_str.isdigit()):
return False
if not (len(A_str) >= 3 and len(B_str) >= 3 and len(C_str) >= 3):
return False
# Interpret in respective bases
try:
A = int(A_str, 11)
B = int(B_str, 9)
C = int(C_str, 7)
except ValueError:
return False
# Check A is prime
if A < 2:
return False
for i in range(2, int(A**0.5) + 1):
if A % i == 0:
return False
# Check B is a perfect square
if B < 0:
return False
sqrt_B = int(B**0.5)
if sqrt_B * sqrt_B != B:
return False
# Check C is a power of 3
if C <= 0:
return False
temp = C
while temp % 3 == 0:
temp //= 3
if temp != 1:
return False
# Concatenate decimal representations without leading zeros
concat = str(A) + str(B) + str(C)
# Check palindrome
if concat != concat[::-1]:
return False
# Total length of x must be prime
total_len = len(x)
if total_len < 2:
return False
for i in range(2, int(total_len**0.5) + 1):
if total_len % i == 0:
return False
return True
def mystery(x):
# Helper functions (nested to keep them private to mystery)
def is_prime(n):
if n < 2:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
def is_fibonacci(n):
if n < 0: return False
if n == 0 or n == 1: return True
# A number n is Fibonacci if 5*n*n + 4 or 5*n*n - 4 is a perfect square
def is_perfect_square(k):
if k < 0: return False
s = int(k**0.5)
return s*s == k
return is_perfect_square(5*n*n + 4) or is_perfect_square(5*n*n - 4)
def is_perfect_cube(n):
if n < 0: return False
c = round(n**(1/3)) # Use round to handle floating point inaccuracies
return c*c*c == n
def is_power_of_2(n):
if n <= 0: return False
return (n & (n - 1)) == 0
def roman_to_int(s):
roman_map = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
n = 0
i = 0
while i < len(s):
current_val = roman_map.get(s[i])
if current_val is None: # Invalid Roman character
raise ValueError("Invalid Roman numeral character")
if i + 1 < len(s):
next_val = roman_map.get(s[i+1])
if next_val is None: # Invalid next Roman character
raise ValueError("Invalid Roman numeral character")
if current_val < next_val:
n += (next_val - current_val)
i += 2
else:
n += current_val
i += 1
else:
n += current_val
i += 1
return n
# --- Main mystery function logic ---
# 1. Input x must be a string
if not isinstance(x, str):
return False
# 2. The string must be split into exactly 4 parts by the '|' delimiter.
parts = x.split('|')
if len(parts) != 4:
return False
p1_str, p2_str, p3_str, p4_str = parts
n1, n2, n3, n4 = 0, 0, 0, 0
try:
# 3. Part 1 (p1_str): Must be a valid Roman numeral. Its decimal value (n1) must be a prime number.
n1 = roman_to_int(p1_str)
if not is_prime(n1):
return False
# 4. Part 2 (p2_str): Must be a hexadecimal string. Its decimal value (n2) must be a perfect cube.
n2 = int(p2_str, 16)
if not is_perfect_cube(n2):
return False
# 5. Part 3 (p3_str): Must consist only of lowercase English letters. The sum of the ASCII values
# of its characters (n3) must be a Fibonacci number.
if not (p3_str.islower() and p3_str.isalpha()):
return False
n3 = sum(ord(c) for c in p3_str)
if not is_fibonacci(n3):
return False
# 6. Part 4 (p4_str): Must be a binary string. Its decimal value (n4) must be a power of 2.
n4 = int(p4_str, 2)
if not is_power_of_2(n4):
return False
except ValueError:
# Catches issues with int conversions (hex, binary) or roman_to_int parsing.
return False
except IndexError: # Catches potential issues with empty parts if string parsing was less strict
return False
# 7. Inter-part condition: The sum of the first two numbers (n1 + n2) must be equal to
# the sum of the last two numbers (n3 + n4).
if not (n1 + n2 == n3 + n4):
return False
# 8. Overall string condition: The total length of the input string x must be a prime number.
if not is_prime(len(x)):
return False
# 9. Overall string condition: The input string x must contain at least one digit (0-9).
if not any(c.isdigit() for c in x):
return False
# 10. Overall string condition: The input string x must contain at least one uppercase letter.
if not any(c.isupper() for c in x):
return False
return True
def mystery(x):
# Check x is a string
if not isinstance(x, str):
return False
# Split by colon into exactly four parts
parts = x.split(':')
if len(parts) != 4:
return False
# Each part must consist only of digits
for p in parts:
if not p.isdigit():
return False
A, B, C, D = map(int, parts)
# Helper functions
def is_prime(n):
if n < 2:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
def is_triangular(n):
if n < 0:
return False
# Solve n = k(k+1)/2 for integer k
disc = 1 + 8 * n
sqrt_disc = int(disc**0.5)
if sqrt_disc * sqrt_disc != disc:
return False
k = (-1 + sqrt_disc) // 2
return k * (k + 1) // 2 == n
def is_perfect_square(n):
if n < 0:
return False
root = int(n**0.5)
return root * root == n
def is_fibonacci(n):
if n < 0:
return False
return is_perfect_square(5 * n * n + 4) or is_perfect_square(5 * n * n - 4)
def is_perfect_cube(n):
if n < 0:
return False
cube_root = round(n ** (1/3))
return cube_root ** 3 == n
# Condition 1: A is prime
if not is_prime(A):
return False
# Condition 2: B is triangular
if not is_triangular(B):
return False
# Condition 3: C is a perfect square
if not is_perfect_square(C):
return False
# Condition 4: D is a Fibonacci number
if not is_fibonacci(D):
return False
# Condition 5: A * B equals C * D
if A * B != C * D:
return False
# Condition 6: Total length of x is a prime number
if not is_prime(len(x)):
return False
# Condition 7: x is a palindrome
if x != x[::-1]:
return False
# Condition 8: Sum of digits in x (ignoring colons) is a perfect cube
digits = [ch for ch in x if ch.isdigit()]
digit_sum = sum(int(d) for d in digits)
if not is_perfect_cube(digit_sum):
return False
# Condition 9: Product of digits in x (ignoring colons) is a perfect square
digit_product = 1
for d in digits:
digit_product *= int(d)
if not is_perfect_square(digit_product):
return False
return True
def mystery(x):
# Helper functions (nested to keep them private to mystery)
def get_digits_sum(n):
s = 0
while n > 0:
s += n % 10
n //= 10
return s
def is_harshad(n):
"""A Harshad number (or Niven number) is an integer that is divisible by the sum of its digits."""
if n <= 0: return False # Harshad numbers are positive
s = get_digits_sum(n)
return n % s == 0
def is_happy(n):
"""A Happy number is a number which eventually reaches 1 when replaced by the sum of the square of its digits."""
if n <= 0: return False
seen = set()
current = n
while current != 1 and current not in seen:
seen.add(current)
sum_sq_digits = 0
while current > 0:
digit = current % 10
sum_sq_digits += digit * digit
current //= 10
current = sum_sq_digits
return current == 1
def get_proper_divisors_sum(n):
if n <= 1: return 0
s = 1 # 1 is always a proper divisor
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
s += i
if i * i != n:
s += n // i
return s
def is_perfect_number(n):
"""A Perfect number is a positive integer that is equal to the sum of its proper positive divisors."""
return n > 0 and get_proper_divisors_sum(n) == n
def is_prime(n):
"""Checks if a number is prime."""
if n < 2:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
def is_pronic(n):
"""A Pronic number (or oblong number) is a number that is the product of two consecutive integers, n*(n+1)."""
if n < 0: return False
# Solve k*(k+1) = n for integer k
# k^2 + k - n = 0
# k = (-1 + sqrt(1 - 4*1*(-n))) / 2 = (-1 + sqrt(1 + 4n)) / 2
# For k to be an integer, (1 + 4n) must be a perfect square, and (-1 + sqrt(1 + 4n)) must be even.
discriminant = 1 + 4 * n
sqrt_disc = int(discriminant**0.5)
if sqrt_disc * sqrt_disc != discriminant:
return False # Not a perfect square
# Check if (-1 + sqrt_disc) is even, which means sqrt_disc must be odd.
# If sqrt_disc is odd, then (-1 + sqrt_disc) is even, so division by 2 yields an integer.
return sqrt_disc % 2 == 1
# --- Main mystery function logic ---
# 1. Input x must be a string
if not isinstance(x, str):
return False
# 2. The string must contain exactly one uppercase 'S' and exactly one lowercase 'h'.
if x.count('S') != 1 or x.count('h') != 1:
return False
# 3. The 'S' delimiter must appear before the 'h' delimiter.
if x.index('S') >= x.index('h'):
return False
# 4. Split the string into three non-empty parts: p1_str S p2_str h p3_str
try:
parts_S = x.split('S')
p1_str = parts_S[0]
rest = parts_S[1]
parts_h = rest.split('h')
p2_str = parts_h[0]
p3_str = parts_h[1]
# Ensure no empty parts
if not (p1_str and p2_str and p3_str):
return False
except IndexError: # Should not happen with count checks, but good for robustness
return False
n1, n2, n3 = 0, 0, 0
try:
# 5. Part 1 (p1_str): Must be a valid hexadecimal string. Its decimal value (n1) must be a Harshad number.
n1 = int(p1_str, 16)
if not is_harshad(n1):
return False
# 6. Part 2 (p2_str): Must be a valid octal string. Its decimal value (n2) must be a Happy number.
n2 = int(p2_str, 8)
if not is_happy(n2):
return False
# 7. Part 3 (p3_str): Must consist only of lowercase English letters.
# Its length must be a prime number.
# The sum of its characters' 1-based alphabetical positions (a=1, b=2, ..., z=26) (n3)
# must be a perfect number.
if not (p3_str.islower() and p3_str.isalpha()):
return False
if not is_prime(len(p3_str)):
return False
n3 = sum(ord(c) - ord('a') + 1 for c in p3_str)
if not is_perfect_number(n3):
return False
except ValueError:
# Catches issues with int conversions (hex, octal) or empty strings for parsing.
return False
except IndexError: # For robustness, unlikely with previous checks.
return False
# 8. Inter-part condition: The product of the first two numbers (n1 * n2) must be equal to n3.
if not (n1 * n2 == n3):
return False
# 9. Overall string condition: The total length of the input string x must be a Pronic number.
if not is_pronic(len(x)):
return False
# 10. Overall string condition: The input string x must contain at least one digit (0-9).
if not any(c.isdigit() for c in x):
return False
# 11. Overall string condition: The input string x must contain at least one uppercase letter.
# (This is implicitly satisfied by the 'S' delimiter).
# 12. Overall string condition: The input string x must not contain any spaces.
if ' ' in x:
return False
return True
def mystery(x):
# Check x is a string
if not isinstance(x, str):
return False
# Try to parse as a tuple of three integers
try:
import ast
import re
t = ast.literal_eval(x)
if not isinstance(t, tuple):
return False
if len(t) != 3:
return False
a, b, c = t
if not (isinstance(a, int) and isinstance(b, int) and isinstance(c, int)):
return False
# All numbers must be positive
if a <= 0 or b <= 0 or c <= 0:
return False
# Helper functions
def is_prime(n):
if n < 2:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
def is_perfect_square(n):
if n < 0:
return False
r = int(n**0.5)
return r * r == n
def is_fibonacci(n):
if n < 0:
return False
# Check if 5*n*n + 4 or 5*n*n - 4 is a perfect square
return is_perfect_square(5*n*n + 4) or is_perfect_square(5*n*n - 4)
def is_perfect_cube(n):
if n < 0:
return False
cube_root = round(n ** (1/3))
return cube_root ** 3 == n
# Condition 1: a is prime
if not is_prime(a):
return False
# Condition 2: b is a perfect square
if not is_perfect_square(b):
return False
# Condition 3: c is a Fibonacci number
if not is_fibonacci(c):
return False
# Condition 4: sum is a perfect cube
if not is_perfect_cube(a + b + c):
return False
# Condition 5: product is a palindrome
prod = a * b * c
prod_str = str(prod)
if prod_str != prod_str[::-1]:
return False
# Condition 6: length of x is prime
if not is_prime(len(x)):
return False
# Condition 7: exactly two commas
if x.count(',') != 2:
return False
# Condition 8: only allowed characters
allowed_chars = set('0123456789,() ')
if not all(ch in allowed_chars for ch in x):
return False
# Condition 9: after removing spaces, matches the pattern for a triple
stripped = re.sub(r'\s+', '', x)
if not re.match(r'^\(\d+,\d+,\d+\)$', stripped):
return False
return True
except:
return False
def mystery(x):
# Helper functions (nested to keep them private to mystery)
def is_prime(n):
if n < 2:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
def is_perfect_square(k):
if k < 0: return False
s = int(k**0.5)
return s*s == k
def is_automorphic(n):
"""An Automorphic number is a number whose square ends in the same digits as the number itself."""
if n < 0: return False
if n == 0: return True # 0*0 = 0, ends in 0
s_n = str(n)
s_n_squared = str(n * n)
return s_n_squared.endswith(s_n)
def is_lucas(n):
"""A Lucas number, defined by L_0=2, L_1=1, L_n=L_{n-1}+L_{n-2}.
A number n is Lucas if 5*n*n + 20 or 5*n*n - 20 is a perfect square."""
if n < 0: return False
if n == 0: return False
if n == 2 or n == 1: return True # L0=2, L1=1 are Lucas numbers
return is_perfect_square(5*n*n + 20) or is_perfect_square(5*n*n - 20)
def is_narcissistic(n):
"""A Narcissistic number (or Armstrong number) is an integer that is the sum of its own digits
each raised to the power of the number of digits."""
if n < 0: return False
if n == 0: return True # 0 is often considered narcissistic (0^1 = 0)
s_n = str(n)
num_digits = len(s_n)
digit_sum_power = sum(int(d)**num_digits for d in s_n)
return digit_sum_power == n
def get_proper_divisors_sum(n):
if n <= 1: return 0
s = 1 # 1 is always a proper divisor
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
s += i
if i * i != n:
s += n // i
return s
def is_deficient(n):
"""A Deficient number is a positive integer for which the sum of its proper divisors is less than the number itself."""
return n > 0 and get_proper_divisors_sum(n) < n
def is_mersenne_number(n):
"""A Mersenne number is a number of the form 2^p - 1 for some integer p > 0."""
if n <= 0: return False
# Check if n+1 is a power of 2
temp = n + 1
if temp <= 0: return False
return (temp & (temp - 1)) == 0
# --- Main mystery function logic ---
# 1. Input x must be a string.
if not isinstance(x, str):
return False
# 2. The string must contain exactly one underscore '_' and exactly one hash '#' delimiter.
if x.count('_') != 1 or x.count('#') != 1:
return False
# 3. The '_' delimiter must appear before the '#' delimiter.
if x.index('_') >= x.index('#'):
return False
# 4. Split the string into three non-empty parts: p1_str _ p2_str # p3_str
try:
parts_underscore = x.split('_')
p1_str = parts_underscore[0]
rest = parts_underscore[1]
parts_hash = rest.split('#')
p2_str = parts_hash[0]
p3_str = parts_hash[1]
# Ensure no empty parts
if not (p1_str and p2_str and p3_str):
return False
except IndexError: # Should not happen with count checks, but good for robustness
return False
n1, n3 = 0, 0
try:
# 5. Part 1 (p1_str): Must consist only of digits. Its decimal value (n1) must be an Automorphic number.
if not p1_str.isdigit():
return False
n1 = int(p1_str)
if not is_automorphic(n1):
return False
# 6. Part 2 (p2_str): Must consist only of uppercase English letters.
# It must be a palindrome.
# Its length must be a Lucas number.
if not (p2_str.isupper() and p2_str.isalpha()):
return False
if p2_str != p2_str[::-1]:
return False
if not is_lucas(len(p2_str)):
return False
# 7. Part 3 (p3_str): Must consist only of digits '0', '1', '2', '3'.
# Its base-4 decimal value (n3) must be a Narcissistic number.
if not all(c in '0123' for c in p3_str):
return False
n3 = int(p3_str, 4)
if not is_narcissistic(n3):
return False
except ValueError:
# Catches issues with int conversions or empty strings for parsing.
return False
# 8. Inter-part condition: The sum of n1, the length of p2_str, and n3 must be a Deficient number.
if not is_deficient(n1 + len(p2_str) + n3):
return False
# 9. Overall string condition: The input string x must not contain any spaces.
if ' ' in x:
return False
# 10. Overall string condition: The input string x must contain at least one digit from '4' to '9'.
# This ensures p1_str isn't purely base-4 digits.
if not any(c.isdigit() and int(c) >= 4 for c in x):
return False
# 11. Overall string condition: The input string x must contain at least one digit from '0' to '3'.
# This ensures p3_str contains valid base-4 digits.
if not any(c.isdigit() and int(c) <= 3 for c in x):
return False
# 12. Overall string condition: The total length of the input string x must be a Mersenne number.
if not is_mersenne_number(len(x)):
return False
# 13. Overall string condition: The number of distinct characters in x must be a prime number.
if not is_prime(len(set(x))):
return False
return True
def mystery(x):
# Helper functions
def is_happy(n):
seen = set()
while n != 1 and n not in seen:
seen.add(n)
n = sum(int(d) ** 2 for d in str(n))
return n == 1
def is_harshad(n):
if n <= 0:
return False
s = sum(int(d) for d in str(n))
return s != 0 and n % s == 0
def is_perfect(n):
if n <= 0:
return False
total = 1
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
total += i
if i * i != n:
total += n // i
return total == n
def is_prime(n):
if n < 2:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True
def is_perfect_square(n):
if n < 0:
return False
r = int(n ** 0.5)
return r * r == n
def is_palindrome_num(n):
s = str(n)
return s == s[::-1]
# Main logic
if not isinstance(x, str):
return False
parts = x.split('#')
if len(parts) != 3:
return False
p1, p2, p3 = parts
if not (p1 and p2 and p3):
return False
# Part 1: decimal happy number
if not p1.isdigit():
return False
n1 = int(p1)
if not is_happy(n1):
return False
# Part 2: letters whose alphabetical sum is harshad
if not p2.isalpha():
return False
n2 = sum(ord(c.lower()) - ord('a') + 1 for c in p2)
if not is_harshad(n2):
return False
# Part 3: binary perfect number
if not all(c in '01' for c in p3):
return False
n3 = int(p3, 2)
if not is_perfect(n3):
return False
# Product palindrome
if not is_palindrome_num(n1 * n2 * n3):
return False
# Total length prime
if not is_prime(len(x)):
return False
# At least one uppercase letter
if not any(c.isupper() for c in x):
return False
# Distinct characters count is a perfect square
distinct_count = len(set(x))
if not is_perfect_square(distinct_count):
return False
return True
def mystery(x):
# Helper functions (nested to keep them private to mystery)
def is_prime(n):
if n < 2:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
def is_perfect_square(k):
if k < 0: return False
s = int(k**0.5)
return s*s == k
def is_catalan(n):
"""Checks if n is a Catalan number. C_n = (1/(n+1)) * (2n choose n)"""
if n <= 0: return False
# Pre-calculated small Catalan numbers for efficiency
catalan_numbers = {1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012}
return n in catalan_numbers
def is_bell(n):
"""Checks if n is a Bell number. B_n = number of partitions of a set of n elements."""
if n <= 0: return False
# Pre-calculated small Bell numbers for efficiency
bell_numbers = {1, 2, 5, 15, 52, 203, 877, 4140, 21147}
return n in bell_numbers
def is_smith_number(n):
"""A composite number for which the sum of its digits equals the sum of the digits of its prime factorization."""
if n < 4: return False # Smith numbers are composite
def sum_digits(num):
s = 0
while num > 0:
s += num % 10
num //= 10
return s
original_sum = sum_digits(n)
factor_sum = 0
temp_n = n
d = 2
is_composite = False
# Find prime factors and sum their digits
while d * d <= temp_n:
if temp_n % d == 0:
is_composite = True
factor_sum += sum_digits(d)
temp_n //= d
else:
d += 1
if temp_n > 1: # Remaining factor is prime
factor_sum += sum_digits(temp_n)
return is_composite and original_sum == factor_sum
def is_square_free(n):
"""A positive integer that is not divisible by any perfect square other than 1."""
if n <= 0: return False
if n == 1: return True
d = 2
while d * d <= n:
if n % (d * d) == 0:
return False
d += 1
return True
# --- Main mystery function logic ---
# 1. Input x must be a string.
if not isinstance(x, str):
return False
# 2. The string must contain exactly one '[' and exactly one ']' delimiter.
if x.count('[') != 1 or x.count(']') != 1:
return False
# 3. The '[' delimiter must appear before the ']' delimiter.
if x.index('[') >= x.index(']'):
return False
# 4. Split the string into three non-empty parts: p1_str [ p2_str ] p3_str
try:
parts_open = x.split('[')
p1_str = parts_open[0]
rest = parts_open[1]
parts_close = rest.split(']')
p2_str = parts_close[0]
p3_str = parts_close[1]
# Ensure no empty parts
if not (p1_str and p2_str and p3_str):
return False
except IndexError: # Should not happen with count checks, but good for robustness
return False
n1, n3 = 0, 0
len_p2 = len(p2_str)
try:
# 5. Part 1 (p1_str): Must consist only of digits '0' to '4'.
# Its base-5 decimal value (n1) must be a Catalan number.
if not all(c in '01234' for c in p1_str):
return False
n1 = int(p1_str, 5)
if not is_catalan(n1):
return False
# 6. Part 2 (p2_str): Must consist only of alternating uppercase and lowercase English letters.
# (e.g., 'aBcDe', 'AbCdE').
# Its length (len_p2) must be a Bell number.
# The sum of ASCII values of its characters must be a Smith number.
if not (p2_str.isalpha() and len_p2 > 0):
return False
for i, char in enumerate(p2_str):
if i % 2 == 0: # Even index (0, 2, 4...)
if not char.islower(): return False
else: # Odd index (1, 3, 5...)
if not char.isupper(): return False
if not is_bell(len_p2):
return False
ascii_sum_p2 = sum(ord(c) for c in p2_str)
if not is_smith_number(ascii_sum_p2):
return False
# 7. Part 3 (p3_str): Must consist only of digits '0' to '6'.
# Its base-7 decimal value (n3) must be a square-free number.
if not all(c in '0123456' for c in p3_str):
return False
n3 = int(p3_str, 7)
if not is_square_free(n3):
return False
except ValueError:
# Catches issues with int conversions (base-5, base-7).
return False
except IndexError: # For robustness
return False
# 8. Inter-part condition: The sum (n1 + len_p2 + n3) must be a multiple of 7.
if not ((n1 + len_p2 + n3) % 7 == 0):
return False
# 9. Overall string condition: The total length of the input string x must be a perfect square.
if not is_perfect_square(len(x)):
return False
# 10. Overall string condition: The input string x must contain at least one digit '0' and at least one digit '1'.
if '0' not in x or '1' not in x:
return False
# 11. Overall string condition: The number of distinct digits in x must be a prime number.
distinct_digits = set(c for c in x if c.isdigit())
if not is_prime(len(distinct_digits)):
return False
# 12. Overall string condition: The first character of x must be a digit.
if not x[0].isdigit():
return False
return True
def mystery(x):
# 1. Input must be a string without spaces.
if not isinstance(x, str) or ' ' in x:
return False
# 2. Exactly two '#' delimiters.
if x.count('#') != 2:
return False
# 3. Split into three non‑empty parts.
parts = x.split('#')
if len(parts) != 3:
return False
p1, p2, p3 = parts
if not (p1 and p2 and p3):
return False
# 4. Part1: decimal digits only.
if not p1.isdigit():
return False
n1 = int(p1)
# 5. Part2: lowercase letters only.
if not (p2.islower() and p2.isalpha()):
return False
len2 = len(p2)
# 6. Part3: binary digits only.
if not all(c in '01' for c in p3):
return False
n3 = int(p3, 2)
# 7. Inter‑part condition: n1 * len2 == n3.
if n1 * len2 != n3:
return False
# Helper: abundant number check.
def is_abundant(n):
if n < 2:
return False
s = 1
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
s += i
if i * i != n:
s += n // i
return s > n
# Helper: pentagonal number check.
def is_pentagonal(m):
if m <= 0:
return False
disc = 1 + 24 * m
sqrt_disc = int(disc**0.5)
if sqrt_disc * sqrt_disc != disc:
return False
return (1 + sqrt_disc) % 6 == 0
# Helper: hexagonal number check.
def is_hexagonal(h):
if h <= 0:
return False
disc = 1 + 8 * h
sqrt_disc = int(disc**0.5)
if sqrt_disc * sqrt_disc != disc:
return False
return (1 + sqrt_disc) % 4 == 0
# Helper: practical number check.
def is_practical(n):
if n <= 0:
return False
if n == 1:
return True
# Collect all divisors.
divisors = []
for i in range(1, int(n**0.5) + 1):
if n % i == 0:
divisors.append(i)
if i * i != n:
divisors.append(n // i)
divisors.sort()
# Determine which sums are achievable.
achievable = {0}
for d in divisors:
new_sums = set()
for s in achievable:
new_sum = s + d
if new_sum <= n:
new_sums.add(new_sum)
achievable.update(new_sums)
# Every integer 1..n must be achievable.
for i in range(1, n + 1):
if i not in achievable:
return False
return True
# Helper: perfect cube check.
def is_perfect_cube(val):
if val <= 0:
return False
cube_root = round(val ** (1/3))
return cube_root ** 3 == val
# Helper: prime check.
def is_prime(num):
if num < 2:
return False
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True
# 8. n1 must be abundant.
if not is_abundant(n1):
return False
# 9. len2 must be pentagonal.
if not is_pentagonal(len2):
return False
# 10. Alphabetical sum of p2 must be hexagonal.
sum2 = sum(ord(c) - 96 for c in p2) # a=1, b=2, ...
if not is_hexagonal(sum2):
return False
# 11. n3 must be practical.
if not is_practical(n3):
return False
# 12. Total length of x must be a perfect cube.
if not is_perfect_cube(len(x)):
return False
# 13. x must contain at least one digit '7' and one digit '8'.
if '7' not in x or '8' not in x:
return False
# 14. Number of distinct characters in x must be prime.
distinct_chars = set(x)
if not is_prime(len(distinct_chars)):
return False
# 15. First character of x must be a digit.
if not x[0].isdigit():
return False
# All conditions satisfied.
return True