S problems
This commit is contained in:
@ -6,50 +6,24 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
<p>
|
||||
<a href="https://en.wikipedia.org/wiki/S-Expression" title="wp: S-Expression">S-Expressions</a> are one convenient way to parse and store data.
|
||||
</p>
|
||||
Task:
|
||||
<p>
|
||||
Write a simple reader/parser for S-Expressions that handles quoted and unquoted strings, integers and floats.
|
||||
</p>
|
||||
<p>
|
||||
The function should read a single but nested S-Expression from a string and
|
||||
return it as a (nested) array.
|
||||
</p>
|
||||
<p>
|
||||
Newlines and other whitespace may be ignored unless contained within a quoted string.
|
||||
</p>
|
||||
<p>“<tt>()</tt>” inside quoted strings are not interpreted, but treated as part of the string.
|
||||
</p>
|
||||
<p>
|
||||
Handling escaped quotes inside a string is optional; thus “<tt>(foo"bar)</tt>” maybe treated as a string “<tt>foo"bar</tt>”, or as an error.
|
||||
</p>
|
||||
<p>
|
||||
For this, the reader need not recognize “<tt>\</tt>” for escaping, but should, in addition, recognize numbers if the language has appropriate datatypes.
|
||||
</p>
|
||||
<p>
|
||||
Note that with the exception of “<tt>()"</tt>” (“<tt>\</tt>” if escaping is supported) and whitespace there are no special characters. Anything else is allowed without quotes.
|
||||
</p>
|
||||
<p>The reader should be able to read the following input</p>
|
||||
<p>
|
||||
<pre>
|
||||
((data "quoted data" 123 4.5)
|
||||
(data (!@# (4.5) "(more" "data)")))
|
||||
</pre>
|
||||
</p>
|
||||
<p>
|
||||
and turn it into a native datastructure. (see the
|
||||
<a href="http://rosettacode.org/wiki/#Pike" title="#Pike">Pike</a>,
|
||||
<a href="http://rosettacode.org/wiki/#Python" title="#Python">Python</a> and
|
||||
<a href="http://rosettacode.org/wiki/#Ruby" title="#Ruby">Ruby</a> implementations
|
||||
for examples of native data structures.)
|
||||
</p>
|
||||
<a href="https://en.wikipedia.org/wiki/S-Expression" title="wp: S-Expression" target="_blank">S-Expressions</a> are one convenient way to parse and store data.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
Write a simple reader/parser for S-Expressions that handles quoted and unquoted strings, integers and floats.
|
||||
The function should read a single but nested S-Expression from a string and return it as a (nested) array.
|
||||
Newlines and other whitespace may be ignored unless contained within a quoted string.
|
||||
“<tt>()</tt>” inside quoted strings are not interpreted, but treated as part of the string.
|
||||
Handling escaped quotes inside a string is optional; thus “<tt>(foo"bar)</tt>” maybe treated as a string “<tt>foo"bar</tt>”, or as an error.
|
||||
For this, the reader need not recognize “<tt>\</tt>” for escaping, but should, in addition, recognize numbers if the language has appropriate datatypes.
|
||||
Note that with the exception of “<tt>()"</tt>” (“<tt>\</tt>” if escaping is supported) and whitespace there are no special characters. Anything else is allowed without quotes.
|
||||
The reader should be able to read the following input
|
||||
<pre>
|
||||
((data "quoted data" 123 4.5)
|
||||
(data (!@# (4.5) "(more" "data)")))
|
||||
</pre>
|
||||
and turn it into a native datastructure. (see the <a href="https://rosettacode.org/wiki/S-Expressions#Pike" title="#Pike" target="_blank">Pike</a>, <a href="https://rosettacode.org/wiki/S-Expressions#Python" title="#Python" target="_blank">Python</a> and <a href="https://rosettacode.org/wiki/S-Expressions#Ruby" title="#Ruby" target="_blank">Ruby</a> implementations for examples of native data structures.)
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
|
@ -6,69 +6,22 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
<p>
|
||||
Five sailors are shipwrecked on an island and
|
||||
collect a large pile of coconuts during the day.
|
||||
</p>
|
||||
<p>That night the first sailor wakes up and decides
|
||||
to take his first share early so tries to divide the pile of coconuts equally
|
||||
into five piles but finds that there is one coconut left over, so he tosses it
|
||||
to a monkey and then hides "his" one of the five equally sized piles of
|
||||
coconuts and pushes the other four piles together to form a single visible pile
|
||||
of coconuts again and goes to bed.
|
||||
</p>
|
||||
<p>
|
||||
To cut a long story short, each of the sailors in
|
||||
turn gets up once during the night and performs the same actions of dividing
|
||||
the coconut pile into five, finding that one coconut is left over and giving
|
||||
that single remainder coconut to the monkey.
|
||||
</p>
|
||||
<p>
|
||||
In the morning (after the surreptitious and
|
||||
separate action of each of the five sailors during the night), the remaining
|
||||
coconuts are divided into five equal piles for each of the sailors, whereupon
|
||||
it is found that the pile of coconuts divides equally amongst the sailors with
|
||||
no remainder. (Nothing for the monkey in the morning.)
|
||||
</p>
|
||||
|
||||
The task:
|
||||
|
||||
|
||||
Create a function that returns the
|
||||
the minimum possible size
|
||||
of the initial pile of coconuts collected during the day for N
|
||||
sailors.
|
||||
|
||||
|
||||
Note:
|
||||
|
||||
|
||||
Of course the tale is told in a
|
||||
world where the collection of any amount of coconuts in a day and multiple
|
||||
divisions of the pile, etc can occur in time fitting the story line, so as
|
||||
not to affect the mathematics.
|
||||
|
||||
|
||||
|
||||
C.f:
|
||||
|
||||
<a
|
||||
href="https://www.youtube.com/watch?v=U9qU20VmvaU" title="link: https://www.youtube.com/watch?v=U9qU20VmvaU">
|
||||
Monkeys and Coconuts - Numberphile</a> (Video) Analytical solution.
|
||||
|
||||
|
||||
<a
|
||||
href="http://oeis.org/A002021" title="link: http://oeis.org/A002021">A002021 Pile of coconuts problem</a> The On-Line
|
||||
Encyclopedia of Integer Sequences. (Although some of its references may use
|
||||
the alternate form of the tale).
|
||||
|
||||
|
||||
|
||||
Five sailors are shipwrecked on an island and collect a large pile of coconuts during the day.
|
||||
That night the first sailor wakes up and decides to take his first share early so tries to divide the pile of coconuts equally into five piles but finds that there is one coconut left over, so he tosses it to a monkey and then hides "his" one of the five equally sized piles of coconuts and pushes the other four piles together to form a single visible pile of coconuts again and goes to bed.
|
||||
To cut a long story short, each of the sailors in turn gets up once during the night and performs the same actions of dividing the coconut pile into five, finding that one coconut is left over and giving that single remainder coconut to the monkey.
|
||||
In the morning (after the surreptitious and separate action of each of the five sailors during the night), the remaining coconuts are divided into five equal piles for each of the sailors, whereupon it is found that the pile of coconuts divides equally amongst the sailors with no remainder. (Nothing for the monkey in the morning.)
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
Create a function that returns the minimum possible size of the initial pile of coconuts collected during the day for <code>N</code> sailors.
|
||||
<b>Note:</b>
|
||||
Of course the tale is told in a world where the collection of any amount of coconuts in a day and multiple divisions of the pile, etc. can occur in time fitting the story line, so as not to affect the mathematics.
|
||||
<b>C.f:</b>
|
||||
<ul>
|
||||
<li><a href="https://www.youtube.com/watch?v=U9qU20VmvaU" title="link: https://www.youtube.com/watch?v=U9qU20VmvaU" target="_blank"> Monkeys and Coconuts - Numberphile</a> (Video) Analytical solution.</li>
|
||||
<li><a href="http://oeis.org/A002021" title="link: http://oeis.org/A002021" target="_blank">A002021 Pile of coconuts problem</a> The On-Line Encyclopedia of Integer Sequences. (Although some of its references may use the alternate form of the tale).</li>
|
||||
</ul>
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
|
@ -6,19 +6,8 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Task:
|
||||
|
||||
<p>
|
||||
For each number list of 6-digit
|
||||
<a href="https://en.wikipedia.org/wiki/SEDOL" title="wp: SEDOL">SEDOL</a>s,
|
||||
calculate and append the checksum digit.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
That is, given the input string on the left, your function should return the
|
||||
corresponding string on the right:
|
||||
</p>
|
||||
|
||||
For each number list of 6-digit <a href="https://en.wikipedia.org/wiki/SEDOL" title="wp: SEDOL" target="_blank">SEDOL</a>s, calculate and append the checksum digit.
|
||||
That is, given the input string on the left, your function should return the corresponding string on the right:
|
||||
<pre>
|
||||
710889 => 7108899
|
||||
B0YBKJ => B0YBKJ7
|
||||
@ -32,12 +21,7 @@ challengeType: 5
|
||||
B0YBKT => B0YBKT7
|
||||
B00030 => B000300
|
||||
</pre>
|
||||
|
||||
<p>
|
||||
Check also that each input is correctly formed, especially
|
||||
with respect to valid characters allowed in a SEDOL string. Your function
|
||||
should return <code>null</code> on invalid input.
|
||||
</p>
|
||||
Check that each input is correctly formed, especially with respect to valid characters allowed in a SEDOL string. Your function should return <code>null</code> on an invalid input.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
|
@ -6,20 +6,22 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
When sorting records in a table by a particular column or field, a <a href="https://en.wikipedia.org/wiki/Stable_sort#Stability">stable sort</a> will always retain the relative order of records that have the same key.
|
||||
When sorting records in a table by a particular column or field, a <a href="https://en.wikipedia.org/wiki/Stable_sort#Stability" target="_blank">stable sort</a> will always retain the relative order of records that have the same key.
|
||||
For example, in this table of countries and cities, a stable sort on the <b>second</b> column, the cities, would keep the US Birmingham above the UK Birmingham. (Although an unstable sort <i>might</i>, in this case, place the US Birmingham above the UK Birmingham, a stable sort routine would <i>guarantee</i> it).
|
||||
<pre>UK London
|
||||
<pre>
|
||||
UK London
|
||||
US New York
|
||||
US Birmingham
|
||||
UK Birmingham</pre>
|
||||
UK Birmingham
|
||||
</pre>
|
||||
Similarly, stable sorting on just the first column would generate “UK London” as the first item and “US Birmingham” as the last item (since the order of the elements having the same first word – “UK” or “US” – would be maintained).
|
||||
Write a function that takes a 2D array as a parameter. Each element has 2 elements similar to the above example. The function should sort the array as mentioned previously and return the sorted array.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
Write a function that takes a 2D array as a parameter. Each element has 2 elements similar to the above example. The function should sort the array as mentioned previously and return the sorted array.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
@ -27,8 +29,8 @@ Write a function that takes a 2D array as a parameter. Each element has 2 elemen
|
||||
tests:
|
||||
- text: <code>stableSort</code> should be a function.
|
||||
testString: assert(typeof stableSort == 'function', '<code>stableSort</code> should be a function.');
|
||||
- text: <code>stableSort([["UK", "London"], ["US", "New York"], ["US", "Birmingham"], ["UK", "Birmingham"]])</code> should return a array.
|
||||
testString: assert(Array.isArray(stableSort([["UK", "London"], ["US", "New York"], ["US", "Birmingham"], ["UK", "Birmingham"]])), '<code>stableSort([["UK", "London"], ["US", "New York"], ["US", "Birmingham"], ["UK", "Birmingham"]])</code> should return a array.');
|
||||
- text: <code>stableSort([["UK", "London"], ["US", "New York"], ["US", "Birmingham"], ["UK", "Birmingham"]])</code> should return an array.
|
||||
testString: assert(Array.isArray(stableSort([["UK", "London"], ["US", "New York"], ["US", "Birmingham"], ["UK", "Birmingham"]])), '<code>stableSort([["UK", "London"], ["US", "New York"], ["US", "Birmingham"], ["UK", "Birmingham"]])</code> should return an array.');
|
||||
- text: <code>stableSort([["UK", "London"], ["US", "New York"], ["US", "Birmingham"], ["UK", "Birmingham"]])</code> should return <code>[["US", "Birmingham"], ["UK", "Birmingham"], ["UK", "London"], ["US", "New York"]]</code>.
|
||||
testString: assert.deepEqual(stableSort([["UK", "London"], ["US", "New York"], ["US", "Birmingham"], ["UK", "Birmingham"]]), [["US", "Birmingham"], ["UK", "Birmingham"], ["UK", "London"], ["US", "New York"]], '<code>stableSort([["UK", "London"], ["US", "New York"], ["US", "Birmingham"], ["UK", "Birmingham"]])</code> should return <code>[["US", "Birmingham"], ["UK", "Birmingham"], ["UK", "London"], ["US", "New York"]]</code>.');
|
||||
- text: <code>stableSort([[2, 2], [1, 2], [1, 4], [1, 5]])</code> should return <code>[[2, 2], [1, 2], [1, 4], [1, 5]]</code>.
|
||||
|
Reference in New Issue
Block a user