Già Thiên Ký

Cài đặt

Kích thước chữ

Kiểu chữ

Chiều cao dòng

Xem trước

Đây là bản xem trước trải nghiệm đọc của bạn. Điều chỉnh các cài đặt ở trên.

Già Thiên Ký

Chương 1102:  0.第一千一百零三章識海本源與神識本源

To determine which notes can be played based on the given rules, we need to keep track of the last one or two successfully played notes. A `deque` (double-ended queue) of maximum length 2 is an efficient way to do this. Here's the logic: 1. Initialize an empty list `played_notes` to store the sequence of notes that are successfully played. 2. Initialize a `collections.deque` with `maxlen=2` called `played_history` to keep track of the last two successfully played notes. 3. Iterate through each `note` in the `generated_notes` sequence. 4. For each `note`, check the rules in order: * **Rule 1 (0, 1, 2):** If `note` is 0, 1, or 2, it can always be played. * **Rule 2 (3, 4, 5, 6):** If `note` is 3, 4, 5, or 6, it can be played *only if* there's a `prev_note` in `played_history` and `prev_note` is equal to `note - 3`. * **Rule 3 (7):** If `note` is 7, it can be played *only if* there are at least two notes in `played_history`, and the `prev_note` is 6, and the `prev_prev_note` is 5. 5. If a `note` meets any of the conditions, add it to `played_notes` and update `played_history` by appending the `note`. The `deque`'s `maxlen` will automatically handle removing the oldest note if it exceeds 2 elements. 6. If a `note` does not meet any conditions, it is skipped, and `played_notes` and `played_history` remain unchanged. Let's apply this logic to the given `generated_notes`: `[0, 1, 3, 4, 0, 3, 5, 6, 7]` * **`note = 0`**: Rule 1. Play 0. * `played_notes = [0]` * `played_history = deque([0])` * **`note = 1`**: Rule 1. Play 1. * `played_notes = [0, 1]` * `played_history = deque([0, 1])` * **`note = 3`**: Rule 2 (requires `prev_note == 0`). `played_history` is `[0, 1]`, so `prev_note` is 1. `1 != 0`. Skip 3. * `played_notes = [0, 1]` * `played_history = deque([0, 1])` * **`note = 4`**: Rule 2 (requires `prev_note == 1`). `played_history` is `[0, 1]`, so `prev_note` is 1. `1 == 1`. Play 4. * `played_notes = [0, 1, 4]` * `played_history = deque([1, 4])` (0 is popped due to `maxlen=2`) * **`note = 0`**: Rule 1. Play 0. * `played_notes = [0, 1, 4, 0]` * `played_history = deque([4, 0])` (1 is popped) * **`note = 3`**: Rule 2 (requires `prev_note == 0`). `played_history` is `[4, 0]`, so `prev_note` is 0. `0 == 0`. Play 3. * `played_notes = [0, 1, 4, 0, 3]` * `played_history = deque([0, 3])` (4 is popped) * **`note = 5`**: Rule 2 (requires `prev_note == 2`). `played_history` is `[0, 3]`, so `prev_note` is 3. `3 != 2`. Skip 5. * `played_notes = [0, 1, 4, 0, 3]` * `played_history = deque([0, 3])` * **`note = 6`**: Rule 2 (requires `prev_note == 3`). `played_history` is `[0, 3]`, so `prev_note` is 3. `3 == 3`. Play 6. * `played_notes = [0, 1, 4, 0, 3, 6]` * `played_history = deque([3, 6])` (0 is popped) * **`note = 7`**: Rule 3 (requires `prev_note == 6` and `prev_prev_note == 5`). `played_history` is `[3, 6]`. `prev_note` is 6 (matches), but `prev_prev_note` is 3 (`3 != 5`). Skip 7. * `played_notes = [0, 1, 4, 0, 3, 6]` * `played_history = deque([3, 6])` The final sequence of notes that can be played is `[0, 1, 4, 0, 3, 6]`. ```python from collections import deque def get_playable_notes(generated_notes): """ Determines which notes from a generated sequence can be played based on specific rules. Args: generated_notes: A list of integers representing the sequence of notes to evaluate. Returns: A list of integers representing the playable notes. """ played_notes = [] # played_history stores the last two *successfully played* notes # deque(maxlen=2) automatically handles removing the oldest element played_history = deque(maxlen=2) for note in generated_notes: is_playable = False # Rule 1: Notes 0, 1, or 2 can always be played if note in {0, 1, 2}: is_playable = True # Rule 2: Notes 3, 4, 5, or 6 require the previous note to be 'note - 3' elif note in {3, 4, 5, 6}: if len(played_history) >= 1: prev_note = played_history[-1] if prev_note == note - 3: is_playable = True # Rule 3: Note 7 requires the previous two notes to be 5 and 6 (in that order) elif note == 7: if len(played_history) >= 2: prev_note = played_history[-1] prev_prev_note = played_history[-2] if prev_prev_note == 5 and prev_note == 6: is_playable = True # If the note is playable, add it to our sequence and update history if is_playable: played_notes.append(note) played_history.append(note) return played_notes # Example usage: generated_notes = [0, 1, 3, 4, 0, 3, 5, 6, 7] playable_sequence = get_playable_notes(generated_notes) print(playable_sequence) # Additional test cases: # No notes playable initially generated_notes_2 = [3, 4, 5, 6, 7] print(f"Test case 2: {get_playable_notes(generated_notes_2)}") # Expected: [] # A sequence that plays 7 generated_notes_3 = [0, 1, 2, 5, 6, 7] print(f"Test case 3: {get_playable_notes(generated_notes_3)}") # Expected: [0, 1, 2, 5, 6, 7] # A sequence where 7 fails but 6 would pass generated_notes_4 = [0, 1, 2, 3, 6, 7] print(f"Test case 4: {get_playable_notes(generated_notes_4)}") # Expected: [0, 1, 2, 3, 6] (3-3=0, so if 0 was prev, 3 passes. Here, 2 -> 3 fails; 5->6 passes) # Let's re-evaluate generated_notes_4: # 0 -> [0], hist=[0] # 1 -> [0,1], hist=[0,1] # 2 -> [0,1,2], hist=[1,2] # 3 (req 0). prev=2. Fails. [0,1,2], hist=[1,2] # 6 (req 3). prev=2. Fails. [0,1,2], hist=[1,2] # 7 (req 5,6). prev=2, prev_prev=1. Fails. [0,1,2], hist=[1,2] # So, Test case 4 should be [0,1,2] # Let's try one where 7 passes: generated_notes_5 = [0, 1, 2, 5, 6, 7] # 0 -> played_notes=[0], played_history=[0] # 1 -> played_notes=[0,1], played_history=[0,1] # 2 -> played_notes=[0,1,2], played_history=[1,2] # 5 (req 2). prev=2. Pass. played_notes=[0,1,2,5], played_history=[2,5] # 6 (req 3). prev=5. Pass. played_notes=[0,1,2,5,6], played_history=[5,6] # 7 (req 5,6). prev_prev=5, prev=6. Pass. played_notes=[0,1,2,5,6,7], played_history=[6,7] print(f"Test case 5: {get_playable_notes(generated_notes_5)}") # Expected: [0,1,2,5,6,7] # Let's provide the final answer based on the initial example. ```

Đăng Nhập

Đăng Ký Tài Khoản

Quên mật khẩu?

Nhập email của bạn và chúng tôi sẽ gửi hướng dẫn để đặt lại mật khẩu