Python Programming Exam Paper 2023-24
Python Programming Exam Paper 2023-24
To create a program for this task, split the input string by commas, sort the resulting list, and then rejoin it: `def sort_words(input_sequence): words = input_sequence.split(','); words.sort(); return ','.join(words)`. Calling `sort_words('without, hello, bag, world')` will yield 'bag, hello, without, world'.
To construct such a program, read the file's content, reverse the string, and insert commas: `def reverse_characters(file_path): with open(file_path, 'r') as file: content = file.read(); reversed_content = ','.join(reversed(content)); with open('output_file.txt', 'w') as output: output.write(reversed_content)`. This reads, processes, and writes the transformed content to a new file.
Unpacking tuples involves assigning elements of a tuple to variables. For example, given `t = (1, 2, 3)`, executing `a, b, c = t` assigns 1 to a, 2 to b, and 3 to c. For mutable sequences, you can unpack like so: `lst = [4, 5, 6]`, and `d, e, f = lst` assigns 4 to d, 5 to e, and 6 to f. Unpacking is useful for multiple assignment in a single statement.
In Python, the `/` operator performs floating-point division, which returns a float result, while the `//` operator performs floor division, returning an integer result by discarding the fractional part. For instance, `7 / 3` yields `2.3333`, and `7 // 3` yields `2`.
To use functions from `library.py` in `main.py`, import the desired functions using the import statement. Place `library.py` in the same directory or ensure its path is accessible. In `main.py`, utilize `import library` to import all functions or `from library import specific_function` to import specific ones. Then call the imported functions within `main.py`.
The password validation criteria require these conditions: at least one lowercase letter, one uppercase letter, one digit, one special character from [@$#], and a length between 6 and 12. Validating involves checking each condition using regex: `import re; def validate_password(password): conditions = [r'(?=.*[a-z])', r'(?=.*[A-Z])', r'(?=.*\d)', r'(?=.*[@#$])', r'.{6,12}$']; return all(re.search(cond, password) for cond in conditions)`. This function checks all validations with regular expressions.
`linspace` is a function in NumPy used to generate evenly spaced numbers over a specified range, useful for plotting graphs or creating data arrays. However, `argspace` is not a standard function in Python libraries and may refer to user-defined utilities, which means it does not exist by default in modules like NumPy. Context usually defines `argspace`.
The error occurs because `x[1]` is a string ('hello'), and the assignment `x[1][1] = 'bye'` tries to modify an immutable string's character, which is not allowed in Python. Strings do not support item assignment.
The function can be implemented by determining if the square root of the number is an integer: `def perfect_square(number): import math; root = math.sqrt(number); return number if root.is_integer() else -1`. This checks the integer nature of the root to determine perfect square status.
List comprehension offers a concise way to create lists in Python. It can generate a new list by applying an expression to each item in an iterable, optionally filtering items using a condition. For example, consider generating a list of squares for the numbers 1 to 5: `[x**2 for x in range(1, 6)]`. This results in the list `[1, 4, 9, 16, 25]`.