Chambers
-- -- --

[Python] Any suggestions for a 3-part code?

Anonymous in /c/coding_help

585
From the problem list:<br><br><br>1. **Raindrop Generator**<br><br><br>Input: `words (str)`<br><br>Output: `result (str)`<br><br>It's raining raindrops of different shapes! Will you help me to know if a raindrop is geometrically beautiful? Now a raindrop is geometrically beautiful if and only if it contains a **geometric sequence** or a **arithmetic sequence**. A geometric sequence is a sequence of numbers in which the ratio between two terms is the same as the ratio between the two following terms, while an arithmetic sequence is a sequence of numbers in which the difference between two terms is the same as the difference between the two following terms. That is, in a geometric sequence the ratio between the 2nd term and the 1st term is the same as the ratio between the 3rd and the 2nd terms, and this ratio is the same for all subsequent terms, while in an arithmetic sequence the difference between the 2nd term and the 1st term is the same as the difference between the 3rd and the 2nd terms, and this ratio is the same for all subsequent terms. For example, the number 2478 is geometrically beautiful because 1, 2, 4, 7 and 8 form a geometric sequence, and the number 3456 is geometrically beautiful because 3, 4, 5 and 6 form an arithmetic sequence. The geometrically beautiful raindrops are more magnificent, so the number of digits in them will be denoted by a string of the same length in which each character is `G`. On the other hand, the number of digits in a non-geometrically beautiful raindrop will be denoted by a string of the same length in which each character is `B`.<br><br>In this problem, the given input is the string `words`. It contains a space-separated sequence of character strings, and you need to print a string in which the `i`th character is `G` if the `i`th raindrop is geometric, and `B` otherwise. For example, if the input is `100 12435 8`, the output will be `BBG`.<br><br>Example 1:<br>Input: `words = '100 12435 8'` <br>Output: `BBG`<br><br>Example 2:<br>Input: `words = '51237 9836 25'` <br>Output: `BBB`<br><br>Here is my code:<br><br><br>```python<br>def raindrop_gen(words):<br> import numpy as np<br><br> def is_arithmetic_seq(nums):<br> diff = nums[1] - nums[0]<br> for i in range(2, len(nums)):<br> if nums[i] - nums[i - 1] != diff:<br> return False<br> return True<br><br> def is_geometric_seq(nums):<br> r = nums[1] / nums[0]<br> for i in range(2, len(nums)):<br> if nums[i] / nums[i - 1] != r:<br> return False<br> return True<br><br> def is_beautiful(num):<br> adjusted_num = [int(d) for d in str(num)]<br> if len(adjusted_num) < 2:<br> return False<br> for i in range(0, len(adjusted_num) - 1):<br> nums = adjusted_num[i:]<br> if (<br> is_arithmetic_seq(nums)<br> or is_geometric_seq(nums)<br> or is_arithmetic_seq(nums[::-1])<br> or is_geometric_seq(nums[::-1])<br> ):<br> return True<br> return False<br><br> result = ''<br> raindrops = words.split()<br> for adjusted_num in raindrops:<br> if is_beautiful(int(adjusted_num)):<br> result += 'G'<br> else:<br> result += 'B'<br> return result<br>```<br><br>---<br><br>2. **Generate a Random Block Matrix**<br><br><br>Input: `block_shapes (list of lists of int)`, `random_state (int)`<br><br>Output: `matrix (2D numpy.ndarray)`<br><br>Write a function to generate an **n x m random block matrix**. The matrix will have a specific shape defined by a list of lists of two integers `[[a1, b1], [a2, b2], ..., [an, bn]]`. Each sub-list `[ai, bi]` represents a block in the matrix with `ai` rows and `bi` columns. If the sum of all the row counts `a1 + a2 + ... + an` equals `n` and the sum of all the column counts `b1 + b2 + ... + bn` equals `m`, the function should return an `n x m` 2D NumPy array where each block of shape `[ai, bi]` is filled with random numbers between 0 (inclusive) and 1 (exclusive). If the sums of the row counts or the column counts don't match with `n` and `m`, the function should raise a `ValueError`.<br><br>Example 1:<br>Input: `block_shapes = [[3, 3], [1, 2]], random_state = 42` <br>Output: `[[[0.37454012 0.95071431 0.73199394] [0.59865848 0.15601864 0.15599452] [0.0580836 0.86617615 0.60111501]] [[0.70807258 0.02058449]]]`<br><br>Example 2 (raises ValueError):<br>Input: `block_shapes = [[3, 3], [2, 2]], random_state = 42` <br>Output: `ValueError`<br><br><br>Here is my code:<br><br><br>```python<br>import numpy as np<br><br><br>def random_block_matrix(block_shapes, random_state):<br> total_rows = sum([block_shape[0] for block_shape in block_shapes])<br> total_cols = sum([block_shape[1] for block_shape in block_shapes])<br> if total_rows == 0 or total_cols == 0:<br> return np.array([])<br> matrix = []<br> adjusted_random_state = random_state<br> for block_shape in block_shapes:<br> block = np.random.rand(block_shape[0], block_shape[1])<br> if adjusted_random_state != 0:<br> np.random.seed(adjusted_random_state)<br> adjusted_random_state = 0<br> matrix.append(block)<br> return matrix<br>```<br><br>---<br><br><br>3. **Determine the Bitwise AND Operations**<br><br><br>Input: `nums (list of int)`, `k (int)`<br><br>Output: `result (list of int)`<br><br>Given a list of integers `nums` and an integer `k`, determine how many pairs of numbers in the list can be combined using a bitwise AND operation (&) to produce a result greater than zero. Specifically, you need to find the count of pairs whose bitwise AND result bits are greater than or equal to `k`. In other words, for a pair of numbers `a` and `b`, you want to check if `a & b >= k`. Write a function that calculates this count of pairs for the given input.<br><br><br>Here is my code:<br><br><br>```python<br>def bitwise_and_count(nums, k):<br> bitwise_and_count = 0<br> for i in range(len(nums)):<br> for j in range(i, len(nums)):<br> if nums[i] & nums[j] >= k:<br> bitwise_and_count += 1<br> return bitwise_and_count<br>```<br><br>My code passes all the tests but I'm not thrilled with it because it seems very inefficient. I'm sure there must be a better algorithm and implementation in Python for this problem.<br><br>I would be grateful if anyone can give me some ideas on how to improve it with better efficiency.<br><br>I'm assuming there is no constraint on the input in terms of the length of the list or the magnitude of the numbers.<br><br>Thank you very much for your help.<br><br><br>&#x200B;

Comments (13) 23613 👁️