Waving the Stream
Description:
Waving the Stream
The goal here is to practice a very simple form of data stream processing. Given a simple stream of comparable elements, we want to produce and output stream that is modified in some way that changes the order of the elements to match a criteria.
Wiggle sort
In this case, we want to "wiggle sort" the stream. A wiggle sort stirves to ensure that the sequence goes up and then back down again in alternating patterns. This can only be done on a sequence of elements that can be compared with some form of >, <, or = operators to define what up and down mean.
Lets assume we have a sequence with elements A,B,C:
A B C
This sequence is wiggle sorted if we don't have A > B > C or A < B < C
This must hold for any two consecutive pairs of elements. Effectively it must alternate < and >, with = counting as either one.
Some examples:
1 3 2 4 3 5 is wiggle sorted
1 2 3 5 4 3 2 1 is not wiggle sorted
1 1 1 2 2 3 3 is wiggle sorted (remember that it only applies to consecutive pairs)
Simulated Streams
Your stream proccessor will be handed two objects, one is a source stream and the other is the output stream.
The source stream object has one method, .read(), which will return the next element of the stream. In this situation, we will only be using the characters '123456789' for our testing to keep things simple. If the stream is exhausted, it will return a falsy value.
The output stream has two methods, .write(item) and .close(). Write puts an element into the output, which should be wiggled. Close signals that you are done writing your output stream, and for your convenience it will also return a string representation of the output you constructed.
Time pressure
In order to simulate time pressure for potentially large streams, the Reader and Writer are actually keeping track of your reads and writes, and if you read too many elements without writing any, it will throw an error. In our testing, the number of reads can only exceed the number of writes by 5.
Testing
For testing purposes, you can create a stream testing object using the supplied makeStream() function. It takes a string as an argument and uses that string as the sequence to supply with .read(), one character at a time.
The stream object will throw errors if you write un-wiggled streams to it. For your convenience, the writer object has a .isWiggled() method, which will return true if the stream writen to it is wiggled.
Goal
Your goal is to write an output stream to the Writer that contains all of the elements from the Reader in a wiggle sorted order! :)
Similar Kata:
Stats:
Created | Feb 28, 2014 |
Published | Mar 13, 2014 |
Warriors Trained | 241 |
Total Skips | 110 |
Total Code Submissions | 147 |
Total Times Completed | 16 |
JavaScript Completions | 15 |
Total Stars | 1 |
% of votes with a positive feedback rating | 80% of 5 |
Total "Very Satisfied" Votes | 4 |
Total "Somewhat Satisfied" Votes | 0 |
Total "Not Satisfied" Votes | 1 |
Total Rank Assessments | 7 |
Average Assessed Rank | 5 kyu |
Highest Assessed Rank | 5 kyu |
Lowest Assessed Rank | 6 kyu |