Appearance
question:def evaluate_expression(expression: str) -> int: Evaluate a mathematical expression containing only positive integers and +, -, *, considering operator precedence and ignoring whitespace. Args: expression (str): A string representing the mathematical expression. Returns: int: The result of evaluating the expression. Examples: >>> evaluate_expression("3 + 5 * 2 - 4") 9 >>> evaluate_expression("10 + 2 * 6") 22 >>> evaluate_expression("100 * 2 + 12") 212 >>> evaluate_expression("100 * ( 2 + 12 )") 1400 >>> evaluate_expression("10 - (2 + 3) * 2") 0 >>> evaluate_expression("2 * (5 + 5 - 3) * 2") 28
answer:def evaluate_expression(expression): Evaluate a mathematical expression containing only positive integers and +, -, *, considering operator precedence and ignoring whitespace. def calculate(ops, values): rhs = values.pop() lhs = values.pop() op = ops.pop() if op == '+': values.append(lhs + rhs) elif op == '-': values.append(lhs - rhs) elif op == '*': values.append(lhs * rhs) def precedence(op): if op in ('+', '-'): return 1 if op == '*': return 2 return 0 ops = [] values = [] i = 0 while i < len(expression): if expression[i] == ' ': i += 1 continue elif expression[i] == '(': ops.append(expression[i]) elif expression[i].isdigit(): val = 0 while (i < len(expression) and expression[i].isdigit()): val = (val * 10) + int(expression[i]) i += 1 values.append(val) i -= 1 elif expression[i] == ')': while len(ops) != 0 and ops[-1] != '(': calculate(ops, values) ops.pop() else: while (len(ops) != 0 and precedence(ops[-1]) >= precedence(expression[i])): calculate(ops, values) ops.append(expression[i]) i += 1 while len(ops) != 0: calculate(ops, values) return values[-1]
question:def smallest_string_with_abc(n: int) -> str: Returns the lexicographically smallest string of length n that contains at least one 'a', one 'b', and one 'c'. It uses only the characters 'a', 'b', and 'c'. Examples: >>> smallest_string_with_abc(3) 'abc' >>> smallest_string_with_abc(4) 'aabc' >>> smallest_string_with_abc(5) 'aaabc' >>> smallest_string_with_abc(7) 'aaaaabc' >>> smallest_string_with_abc(10) 'aaaaaaaabc'
answer:def smallest_string_with_abc(n: int) -> str: Returns the lexicographically smallest string of length n that contains at least one 'a', one 'b', and one 'c'. It uses only the characters 'a', 'b', and 'c'. # Base length is 3 for the minimum requirement 'a', 'b', and 'c' # For minimal lexicographic order, we begin with repeated 'a' followed by 'b' and 'c' if n == 3: return "abc" else: return "a" * (n - 2) + "bc"
question:def cyberville_cipher(text: str, shift: int) -> str: Encrypts the given text by shifting the letters by the specified number of positions in the alphabet, wrapping around if necessary. Only letters are shifted; all other characters remain unchanged. Parameters: text (str): The string to be encrypted. shift (int): The number of positions each letter in the string is to be shifted. Returns: str: The encrypted string. >>> cyberville_cipher('Hello, World!', 3) 'Khoor, Zruog!' >>> cyberville_cipher('Python 3.9', 5) 'Udymts 3.9'
answer:def cyberville_cipher(text, shift): Encrypts the given text by shifting the letters by the specified number of positions in the alphabet, wrapping around if necessary. Only letters are shifted; all other characters remain unchanged. Parameters: text (str): The string to be encrypted. shift (int): The number of positions each letter in the string is to be shifted. Returns: str: The encrypted string. def shift_char(c, shift): if 'a' <= c <= 'z': return chr((ord(c) - ord('a') + shift) % 26 + ord('a')) elif 'A' <= c <= 'Z': return chr((ord(c) - ord('A') + shift) % 26 + ord('A')) return c return ''.join(shift_char(c, shift) for c in text)
question:def to_snake_case(s: str) -> str: Convert a given PascalCase string to snake_case. :param s: The PascalCase string :return: The snake_case string >>> to_snake_case("ThisIsAnExample") 'this_is_an_example' >>> to_snake_case("WhereIsMyCamel") 'where_is_my_camel' >>> to_snake_case("AnotherExample") 'another_example'
answer:def to_snake_case(s): Convert a given PascalCase string to snake_case. :param s: The PascalCase string :return: The snake_case string result = [] for char in s: if char.isupper(): if result: # Add underscore if result is not empty result.append('_') result.append(char.lower()) else: result.append(char) return ''.join(result)