ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

YAML Sequence Reversal

YAML

Goal -- WPM

Ready
Exercise Algorithm Area
1package com.example.yamlparser
2
3import java.util.List;
4import java.util.ArrayList;
5import java.util.Collections;
6
7public class YamlSequenceProcessor {
8
9/**
10* Reverses the order of elements in a given list, simulating a YAML sequence.
11* This method modifies the list in-place.
12*
13* @param sequence The list of elements to reverse.
14*/
15public static void reverseSequence(List<String> sequence) {
16// Edge case: If the sequence is null or has 0 or 1 element, it's already reversed.
17if (sequence == null || sequence.size() <= 1) {
18return;
19}
20
21int left = 0;
22int right = sequence.size() - 1;
23
24// Iterate from both ends towards the middle, swapping elements.
25while (left < right) {
26// Swap the elements at the left and right pointers.
27String temp = sequence.get(left);
28sequence.set(left, sequence.get(right));
29sequence.set(right, temp);
30
31// Move the pointers towards the center.
32left++;
33right--;
34}
35}
36
37/**
38* A helper method to demonstrate reversing a sequence.
39* It creates a new list and then reverses it.
40*
41* @param originalSequence The original list of elements.
42* @return A new list with elements in reversed order.
43*/
44public static List<String> createAndReverseSequence(List<String> originalSequence) {
45// Handle null input gracefully.
46if (originalSequence == null) {
47return new ArrayList<>();
48}
49
50// Create a mutable copy to avoid modifying the original list if it's immutable.
51List<String> mutableSequence = new ArrayList<>(originalSequence);
52
53// Use the in-place reversal method.
54reverseSequence(mutableSequence);
55
56return mutableSequence;
57}
58
59public static void main(String[] args) {
60// Example usage:
61List<String> mySequence = new ArrayList<>();
62mySequence.add("apple");
63mySequence.add("banana");
64mySequence.add("cherry");
65mySequence.add("date");
66
67System.out.println("Original sequence: " + mySequence);
68List<String> reversed = createAndReverseSequence(mySequence);
69System.out.println("Reversed sequence: " + reversed);
70
71// Test edge cases:
72List<String> emptySequence = new ArrayList<>();
73System.out.println("Original empty sequence: " + emptySequence);
74List<String> reversedEmpty = createAndReverseSequence(emptySequence);
75System.out.println("Reversed empty sequence: " + reversedEmpty);
76
77List<String> singleElementSequence = new ArrayList<>();
78singleElementSequence.add("grape");
79System.out.println("Original single element sequence: " + singleElementSequence);
80List<String> reversedSingle = createAndReverseSequence(singleElementSequence);
81System.out.println("Reversed single element sequence: " + reversedSingle);
82
83System.out.println("Reversing null sequence: " + createAndReverseSequence(null));
84}
85}
Algorithm description viewbox

YAML Sequence Reversal

Algorithm description:

This Java code provides functionality to reverse a list of strings, mimicking the reversal of a YAML sequence. The `reverseSequence` method performs an in-place reversal using a two-pointer approach. A helper method, `createAndReverseSequence`, demonstrates how to use the reversal function by creating a copy of an input list and then reversing it. This is useful in scenarios where the order of items in a YAML list needs to be manipulated, such as for testing or specific data processing tasks.

Algorithm explanation:

The `reverseSequence` method reverses a list of strings in-place. It initializes two pointers, `left` at the beginning and `right` at the end of the list. The `while` loop continues as long as `left` is less than `right`. Inside the loop, it swaps the elements at the `left` and `right` indices using a temporary variable. Then, `left` is incremented and `right` is decremented, moving the pointers towards the center. The loop terminates when the pointers meet or cross, indicating the entire list has been reversed. The time complexity is O(N/2), which simplifies to O(N), where N is the number of elements in the sequence, as each element is swapped at most once. The space complexity is O(1) because the reversal is done in-place, using only a constant amount of extra space for the temporary variable. Edge cases handled include null lists, empty lists, and lists with a single element, all of which are correctly identified as already reversed and require no action.

Pseudocode:

function reverseSequence(sequence):
  if sequence is null or size of sequence <= 1:
    return
  left = 0
  right = size of sequence - 1
  while left < right:
    swap sequence[left] and sequence[right]
    increment left
    decrement right

function createAndReverseSequence(originalSequence):
  if originalSequence is null:
    return empty list
  mutableSequence = copy of originalSequence
  reverseSequence(mutableSequence)
  return mutableSequence