S problems

This commit is contained in:
Kris Koishigawa
2019-03-07 23:15:29 +09:00
committed by mrugesh
parent df88daa482
commit 2053e99cd5
6 changed files with 55 additions and 142 deletions

View File

@ -6,50 +6,24 @@ challengeType: 5
## Description ## Description
<section id='description'> <section id='description'>
<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.
<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>
</section> </section>
## Instructions ## Instructions
<section id='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> </section>
## Tests ## Tests

View File

@ -6,69 +6,22 @@ challengeType: 5
## Description ## Description
<section id='description'> <section id='description'>
<p> Five sailors are shipwrecked on an island and collect a large pile of coconuts during the day.
Five sailors are shipwrecked on an island and 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.
collect a large pile of coconuts during the day. 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> 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>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).
</section> </section>
## Instructions ## Instructions
<section id='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> </section>
## Tests ## Tests

View File

@ -6,38 +6,22 @@ challengeType: 5
## Description ## Description
<section id='description'> <section id='description'>
Task: 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:
<p> <pre>
For each number list of 6-digit 710889 => 7108899
<a href="https://en.wikipedia.org/wiki/SEDOL" title="wp: SEDOL">SEDOL</a>s, B0YBKJ => B0YBKJ7
calculate and append the checksum digit. 406566 => 4065663
</p> B0YBLH => B0YBLH2
228276 => 2282765
<p> B0YBKL => B0YBKL9
That is, given the input string on the left, your function should return the 557910 => 5579107
corresponding string on the right: B0YBKR => B0YBKR5
</p> 585284 => 5852842
B0YBKT => B0YBKT7
<pre> B00030 => B000300
710889 => 7108899 </pre>
B0YBKJ => B0YBKJ7 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.
406566 => 4065663
B0YBLH => B0YBLH2
228276 => 2282765
B0YBKL => B0YBKL9
557910 => 5579107
B0YBKR => B0YBKR5
585284 => 5852842
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>
</section> </section>
## Instructions ## Instructions
@ -71,7 +55,7 @@ tests:
<div id='js-seed'> <div id='js-seed'>
```js ```js
function sedol (input) { function sedol(input) {
// Good luck! // Good luck!
return true; return true;
} }

View File

@ -40,7 +40,7 @@ tests:
<div id='js-seed'> <div id='js-seed'>
```js ```js
function sortByKey (arr) { function sortByKey(arr) {
// Good luck! // Good luck!
} }
``` ```

View File

@ -47,7 +47,7 @@ tests:
<div id='js-seed'> <div id='js-seed'>
```js ```js
function sortDisjoint (values, indices) { function sortDisjoint(values, indices) {
// Good luck! // Good luck!
} }
``` ```

View File

@ -6,20 +6,22 @@ challengeType: 5
## Description ## Description
<section id='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). 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 New York
US Birmingham 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). 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> </section>
## Instructions ## Instructions
<section id='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> </section>
## Tests ## Tests
<section id='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: tests:
- text: <code>stableSort</code> should be a function. - text: <code>stableSort</code> should be a function.
testString: assert(typeof stableSort == 'function', '<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. - 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 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 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>. - 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>.'); 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>. - text: <code>stableSort([[2, 2], [1, 2], [1, 4], [1, 5]])</code> should return <code>[[2, 2], [1, 2], [1, 4], [1, 5]]</code>.
@ -48,7 +50,7 @@ tests:
<div id='js-seed'> <div id='js-seed'>
```js ```js
function stableSort (arr) { function stableSort(arr) {
// Good luck! // Good luck!
} }
``` ```