type: code

split: train

question: Write a Python function `add(a, b)` that returns the sum of two numbers.

tests:
assert add(2, 3) == 5
assert add(-1, 1) == 0
assert add(0, 0) == 0


type: code

split: train

question: Write a Python function `is_even(n)` that returns True if n is even, False otherwise.

tests:
assert is_even(4) is True
assert is_even(7) is False
assert is_even(0) is True


type: code

split: train

question: Write a Python function `reverse_string(s)` that returns the reverse of string s.

tests: assert reverse_string('hello') == 'olleh'


type: code

split: train

question: Write a Python function `factorial(n)` that returns n! for n >= 0.

tests:
assert factorial(0) == 1
assert factorial(5) == 120
assert factorial(1) == 1


type: code

split: train

question: Write a Python function `count_vowels(s)` that counts vowels (aeiou, case insensitive) in string s.

tests:
assert count_vowels('hello') == 2
assert count_vowels('AEIOU') == 5
assert count_vowels('xyz') == 0


type: code

split: train

question: Write a Python function `max_in_list(lst)` that returns the largest number in lst.

tests:
assert max_in_list([1, 2, 3]) == 3
assert max_in_list([-5, -1, -10]) == -1
assert max_in_list([7]) == 7


type: code

split: train

question: Write a Python function `is_palindrome(s)` that returns True if s reads the same forwards and backwards.

tests:
assert is_palindrome('racecar') is True
assert is_palindrome('hello') is False
assert is_palindrome('') is True


type: code

split: train

question: Write a Python function `fibonacci(n)` that returns the nth Fibonacci number (fib(0)=0, fib(1)=1).

tests:
assert fibonacci(0) == 0
assert fibonacci(1) == 1
assert fibonacci(7) == 13


type: code

split: train

question: Write a Python function `unique(lst)` that returns a list of unique elements preserving order.


type: code

split: train

question: Write a Python function `sum_digits(n)` that sums the digits of non-negative integer n.

tests:
assert sum_digits(123) == 6
assert sum_digits(0) == 0
assert sum_digits(9999) == 36


type: code

split: train

question: Write a Python function `gcd(a, b)` that returns the greatest common divisor.


type: code

split: train

question: Write a Python function `flatten(lst)` that flattens a list of lists by one level.


type: code

split: train

question: Write a Python function `is_prime(n)` that returns True if n is a prime > 1.

tests:
assert is_prime(2) is True
assert is_prime(15) is False
assert is_prime(17) is True
assert is_prime(1) is False


type: code

split: train

question: Write a Python function `word_count(s)` that returns the number of whitespace-separated words.

tests: assert word_count('  one  two   three ') == 3


type: code

split: train

question: Write a Python function `second_largest(lst)` that returns the second largest distinct number.

tests: assert second_largest([1, 2, 3, 4]) == 3


type: code

split: train

question: Write a Python function `caesar(s, k)` that shifts each letter by k positions, preserving case; non-letters unchanged.

tests: assert caesar('Hello, World!', 13) == 'Uryyb, Jbeyq!'


type: code

split: train

question: Write a Python function `merge_sorted(a, b)` that merges two sorted lists into one sorted list.


type: code

split: train

question: Write a Python function `most_common(lst)` that returns the most frequent element (any if tied).

tests: assert most_common(['a','b','a']) == 'a'


type: code

split: train

question: Write a Python function `chunks(lst, n)` that splits lst into chunks of size n.


type: code

split: train

question: Write a Python function `roman(n)` that converts integer 1-3999 to Roman numerals.

tests:
assert roman(1) == 'I'
assert roman(4) == 'IV'
assert roman(1994) == 'MCMXCIV'


type: code

split: test

question: Write a Python function `square(x)` that returns x squared.

tests:
assert square(3) == 9
assert square(-4) == 16
assert square(0) == 0


type: code

split: test

question: Write a Python function `is_anagram(a, b)` that checks if two strings are anagrams (case-insensitive, ignoring spaces).

tests: assert is_anagram('listen', 'silent') is True

tests: assert is_anagram('Hello', 'World') is False

tests: assert is_anagram('a gentleman', 'elegant man') is True


type: code

split: test

question: Write a Python function `count_words(s)` that returns a dict mapping each word to its count (whitespace-separated, lowercase).

tests: assert count_words('a a b') == {'a': 2, 'b': 1}

tests: assert count_words('Hi hi HI') == {'hi': 3}


type: code

split: test

question: Write a Python function `power(b, e)` that computes b**e for non-negative integer e using a loop (no ** operator).

tests:
assert power(2, 10) == 1024
assert power(5, 0) == 1
assert power(3, 3) == 27


type: code

split: test

question: Write a Python function `compress(s)` that returns 'a3b2c1' style run-length encoding.

tests:
assert compress('aaabbc') == 'a3b2c1'
assert compress('abc') == 'a1b1c1'
assert compress('') == ''


type: code

split: test

question: Write a Python function `balanced(s)` that returns True if all (), [], {} pairs are balanced and properly nested.

tests:
assert balanced('()[]{}') is True
assert balanced('([)]') is False
assert balanced('') is True


type: code

split: test

question: Write a Python function `lcm(a, b)` that returns the least common multiple of two positive integers.

tests:
assert lcm(4, 6) == 12
assert lcm(7, 13) == 91
assert lcm(1, 5) == 5


type: code

split: test

question: Write a Python function `rotate(lst, k)` that rotates list right by k positions.


type: code

split: test

question: Write a Python function `digit_sum_seq(n)` that repeatedly sums digits until single digit (digital root).

tests:
assert digit_sum_seq(38) == 2
assert digit_sum_seq(0) == 0
assert digit_sum_seq(123456) == 3


type: code

split: test

question: Write a Python function `pairs_summing_to(lst, target)` that returns list of unique sorted tuples (a,b) with a+b==target, a<=b.

tests: assert pairs_summing_to([1,2], 10) == []