fix(challenges): H problems

This commit is contained in:
Kris Koishigawa
2019-03-05 18:37:06 +09:00
committed by mrugesh
parent 537d1ed642
commit a3fd1aa284
8 changed files with 108 additions and 92 deletions

View File

@ -6,21 +6,27 @@ challengeType: 5
## Description
<section id='description'>
<p>The Hailstone sequence of numbers can be generated from a starting positive integer, n by:</p>
If n is 1 then the sequence ends.
If n is even then the next n of the sequence <code> = n/2 </code>
If n is odd then the next n of the sequence <code> = (3 * n) + 1 </code><p>The (unproven) <a href="https://en.wikipedia.org/wiki/Collatz conjecture" title="wp: Collatz conjecture">Collatz conjecture</a> is that the hailstone sequence for any starting number always terminates.</p>
<p>The hailstone sequence is also known as hailstone numbers (because the values are usually subject to multiple descents and ascents like hailstones in a cloud), or as the Collatz sequence.</p>
Task:
Create a routine to generate the hailstone sequence for a number.
Use the routine to show that the hailstone sequence for the number 27 has 112 elements starting with <code>27, 82, 41, 124</code> and ending with <code>8, 4, 2, 1</code>
Show the number less than 100,000 which has the longest hailstone sequence together with that sequence's length. (But don't show the actual sequence!)See also:
<a href="http://xkcd.com/710" title="link: http://xkcd.com/710">xkcd</a> (humourous).
The Hailstone sequence of numbers can be generated from a starting positive integer, n by:
<ul>
<li>If n is <b>1</b> then the sequence ends.</li>
<li>If n is <b>even</b> then the next n of the sequence <code>= n/2</code></li>
<li>If n is <b>odd</b> then the next n of the sequence <code>= (3 * n) + 1</code></li>
</ul>
The (unproven) <a href="https://en.wikipedia.org/wiki/Collatz conjecture" title="wp: Collatz conjecture" target="_blank">Collatz conjecture</a> is that the hailstone sequence for any starting number always terminates.
The hailstone sequence is also known as hailstone numbers (because the values are usually subject to multiple descents and ascents like hailstones in a cloud), or as the Collatz sequence.
</section>
## Instructions
<section id='instructions'>
<ol>
<li>Create a routine to generate the hailstone sequence for a number.</li>
<li>Use the routine to show that the hailstone sequence for the number 27 has 112 elements starting with <code>27, 82, 41, 124</code> and ending with <code>8, 4, 2, 1</code></li>
<li>Show the number less than 100,000 which has the longest hailstone sequence together with that sequence's length. (But don't show the actual sequence!)</li>
</ol>
<b>See also:</b>
<ul>
<li><a href="http://xkcd.com/710" title="link: http://xkcd.com/710" target="_blank">xkcd</a> (humourous).</li>
</ul>
</section>
## Tests
@ -44,7 +50,7 @@ tests:
```js
// noprotect
function hailstoneSequence () {
function hailstoneSequence() {
const res = [];
// Good luck!

View File

@ -6,14 +6,13 @@ challengeType: 5
## Description
<section id='description'>
<p>A happy number is defined by the following process:</p>
<p>Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers, while those that do not end in 1 are unhappy numbers.</p>
<p>Implement a function that returns true if the number is happy, or false if not.</p>
A <a href="https://en.wikipedia.org/wiki/Happy_number" target="_blank">happy number</a> is defined by the following process:
Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals <b>1</b> (where it will stay), or it loops endlessly in a cycle which does not include <b>1</b>. Those numbers for which this process ends in <b>1</b> are happy numbers, while those that do not end in <b>1</b> are unhappy numbers.
</section>
## Instructions
<section id='instructions'>
Implement a function that returns true if the number is happy, or false if not.
</section>
## Tests
@ -58,7 +57,7 @@ tests:
<div id='js-seed'>
```js
function happy (number) {
function happy(number) {
// Good luck!
}
```

View File

@ -6,15 +6,15 @@ challengeType: 5
## Description
<section id='description'>
<p>The <a href="http://mathworld.wolfram.com/HarshadNumber.html" title="link: http://mathworld.wolfram.com/HarshadNumber.html">Harshad</a> or Niven numbers are positive integers ≥ 1 that are divisible by the sum of their digits.</p><p>For example, 42 is a <a href="http://rosettacode.org/wiki/oeis:A005349" title="oeis:A005349">Harshad number</a> as 42 is divisible by (4 + 2) without remainder.</p>
The <a href="http://mathworld.wolfram.com/HarshadNumber.html" title="link: http://mathworld.wolfram.com/HarshadNumber.html" target="_blank">Harshad</a> or Niven numbers are positive integers ≥ 1 that are divisible by the sum of their digits.
For example, <b>42</b> is a <a href="http://rosettacode.org/wiki/oeis:A005349" title="oeis:A005349" target="_blank">Harshad number</a> as <b>42</b> is divisible by <b>(4 + 2)</b> without remainder.
Assume that the series is defined as the numbers in increasing order.
Task:
<p>Implement a function to generate successive members of the Harshad sequence.</p><p>Use it to list the first twenty members of the sequence and list the first Harshad number greater than 1000.</p>
</section>
## Instructions
<section id='instructions'>
Implement a function to generate successive members of the Harshad sequence.
Use it to list the first twenty members of the sequence and list the first Harshad number greater than 1000.
</section>
## Tests
@ -37,7 +37,7 @@ tests:
<div id='js-seed'>
```js
function isHarshadOrNiven () {
function isHarshadOrNiven() {
const res = {
firstTwenty: [],
firstOver1000: undefined

View File

@ -6,10 +6,11 @@ challengeType: 5
## Description
<section id='description'>
Task:
<p>Using two Arrays of equal length, create a Hash object where the elements from one array (the keys) are linked to the elements of the other (the values)</p>
Related task:
<a href="http://rosettacode.org/wiki/Associative arrays/Creation" title="Associative arrays/Creation">Associative arrays/Creation</a>
Using two Arrays of equal length, create a Hash object where the elements from one array (the keys) are linked to the elements of the other (the values).
<b>Related task:</b>
<ul>
<li><a href="http://rosettacode.org/wiki/Associative arrays/Creation" title="Associative arrays/Creation" target="_blank">Associative arrays/Creation</a></li>
</ul>
</section>
## Instructions

View File

@ -6,30 +6,37 @@ challengeType: 5
## Description
<section id='description'>
<p>An <a href="https://en.wikipedia.org/wiki/Join_(SQL)#Inner_join" title="wp: Join_(SQL)#Inner_join">inner join</a> is an operation that combines two data tables into one table, based on matching column values. The simplest way of implementing this operation is the <a href="https://en.wikipedia.org/wiki/Nested loop join" title="wp: Nested loop join">nested loop join</a> algorithm, but a more scalable alternative is the <a href="https://en.wikipedia.org/wiki/hash join" title="wp: hash join">hash join</a> algorithm.</p>
<p>Implement the "hash join" algorithm, and demonstrate that it passes the test-case listed below.</p><p>You should represent the tables as data structures that feel natural in your programming language.</p>
<p>The "hash join" algorithm consists of two steps:</p>
Hash phase: Create a <a href="https://en.wikipedia.org/wiki/Multimap" title="wp: Multimap">multimap</a> from one of the two tables, mapping from each join column value to all the rows that contain it.
The multimap must support hash-based lookup which scales better than a simple linear search, because that's the whole point of this algorithm.
Ideally we should create the multimap for the smaller table, thus minimizing its creation time and memory size.
Join phase: Scan the other table, and find matching rows by looking in the multimap created before.
<p>In pseudo-code, the algorithm could be expressed as follows:</p>
An <a href="https://en.wikipedia.org/wiki/Join_(SQL)#Inner_join" title="wp: Join_(SQL)#Inner_join" target="_blank">inner join</a> is an operation that combines two data tables into one table, based on matching column values. The simplest way of implementing this operation is the <a href="https://en.wikipedia.org/wiki/Nested loop join" title="wp: Nested loop join">nested loop join</a> algorithm, but a more scalable alternative is the <a href="https://en.wikipedia.org/wiki/hash join" title="wp: hash join" target="_blank">hash join</a> algorithm.
The "hash join" algorithm consists of two steps:
<ol>
<li><b>Hash phase:</b> Create a <a href="https://en.wikipedia.org/wiki/Multimap" title="wp: Multimap" target="_blank">multimap</a> from one of the two tables, mapping from each join column value to all the rows that contain it.</li>
<ul>
<li>The multimap must support hash-based lookup which scales better than a simple linear search, because that's the whole point of this algorithm.</li>
<li>Ideally we should create the multimap for the smaller table, thus minimizing its creation time and memory size.</li>
</ul>
<li><b>Join phase:</b> Scan the other table, and find matching rows by looking in the multimap created before.</li>
</ol>
In pseudo-code, the algorithm could be expressed as follows:
<pre>
let A = the first input table (or ideally, the larger one)
let B = the second input table (or ideally, the smaller one)
let j<sub>A</sub> = the join column ID of table A
let j<sub>B</sub> = the join column ID of table B
let M<sub>B</sub> = a multimap for mapping from single values to multiple rows of table B (starts out empty)
let C = the output table (starts out empty)
for each row b in table B:
place b in multimap M<sub>B</sub> under key b(j<sub>B</sub>)
for each row a in table A:
for each row b in multimap M<sub>B</sub> under key a(j<sub>A</sub>):
let c = the concatenation of row a and row b
place row c in table C</p>
<b>let</b> <i>A</i> = the first input table (or ideally, the larger one)
<b>let</b> <i>B</i> = the second input table (or ideally, the smaller one)
<b>let</b> <i>j<sub>A</sub></i> = the join column ID of table <i>A</i>
<b>let</b> <i>j<sub>B</sub></i> = the join column ID of table <i>B</i>
<b>let</b> <i>M<sub>B</sub></i> = a multimap for mapping from single values to multiple rows of table <i>B</i> (starts out empty)
<b>let</b> <i>C</i> = the output table (starts out empty)
<b>for each</b> row <i>b</i> in table <i>B</i>:
<b>place</b> <i>b</i> in multimap <i>M<sub>B</sub></i> under key <i>b(j<sub>B</sub>)</i>
<b>for each</b> row <i>a</i> in table <i>A</i>:
<b>for each</b> row <i>b</i> in multimap <i>M<sub>B</sub></i> under key <i>a(j<sub>A</sub>)</i>:
<b>let</b> <i>c</i> = the concatenation of row <i>a</i> and row <i>b</i>
<b>place</b> row <i>c</i> in table <i>C</i>
</pre>
Test-case
<p>Input</p>
</section>
## Instructions
<section id='instructions'>
Implement the "hash join" algorithm as a function and demonstrate that it passes the test-case listed below. The function should accept two arrays of objects and return an array of combined objects.
<h4><b>Input</b></h4>
<table>
<tr>
<td style="padding: 4px; margin: 5px;">
@ -107,13 +114,13 @@ Test-case
</td>
<td style="padding: 4px; margin: 5px;">
</td></tr></table>
<p>Output</p>
<h4><b>Output</b></h4>
<table>
<tr>
<th style="padding: 4px; margin: 5px;"> A.Age </th>
<th style="padding: 4px; margin: 5px;"> A.Name </th>
<th style="padding: 4px; margin: 5px;"> B.Character </th>
<th style="padding: 4px; margin: 5px;"> B.Nemesis
<th style="padding: 4px; margin: 5px;"> A_age </th>
<th style="padding: 4px; margin: 5px;"> A_name </th>
<th style="padding: 4px; margin: 5px;"> B_character </th>
<th style="padding: 4px; margin: 5px;"> B_nemesis
</th></tr>
<tr>
<td style="padding: 4px; margin: 5px;"> 27 </td>
@ -157,13 +164,7 @@ Test-case
<td style="padding: 4px; margin: 5px;"> Alan </td>
<td style="padding: 4px; margin: 5px;"> Zombies
</td></tr></table>
<p></p><p></p><p>The order of the rows in the output table is not significant.</p>
<p>If you're using numerically indexed arrays to represent table rows (rather than referring to columns by name), you could represent the output rows in the form <code style="white-space:nowrap">[[27, "Jonah"], ["Jonah", "Whales"]]</code>.</p><hr>
</section>
## Instructions
<section id='instructions'>
The order of the rows in the output table is not significant.
</section>
## Tests
@ -186,7 +187,7 @@ tests:
<div id='js-seed'>
```js
function hashJoin (hash1, hash2) {
function hashJoin(hash1, hash2) {
// Good luck!
return [];
}
@ -239,7 +240,7 @@ const bench2 = [{ friend: 'o8b', num: 8 }, { friend: 'ye', num: 2 }, { friend: '
```js
function hashJoin (hash1, hash2) {
function hashJoin(hash1, hash2) {
const hJoin = (tblA, tblB, strJoin) => {
const [jA, jB] = strJoin.split('=');
const M = tblB.reduce((a, x) => {

View File

@ -6,18 +6,21 @@ challengeType: 5
## Description
<section id='description'>
<p><a href="https://en.wikipedia.org/wiki/Heron's formula" title="wp: Heron's formula">Hero's formula</a> for the area of a triangle given the length of its three sides <big> a,</big> <big>b,</big> and <big>c</big> is given by:</p><p><big>$$A = \sqrt{s(s-a)(s-b)(s-c)},$$</big></p><p>where <big>s</big> is half the perimeter of the triangle; that is,</p><p><big>$$s=\frac{a+b+c}{2}.$$</big></p>
<p><a href="http://www.had2know.com/academics/heronian-triangles-generator-calculator.html" title="link: http://www.had2know.com/academics/heronian-triangles-generator-calculator.html">Heronian triangles</a> are triangles whose sides and area are all integers.</p>
<p> An example is the triangle with sides 3, 4, 5 whose area is 6 (and whose perimeter is 12). </p>
<p>Note that any triangle whose sides are all an integer multiple of 3, 4, 5; such as 6, 8, 10, will also be a Heronian triangle.</p><p>Define a Primitive Heronian triangle as a Heronian triangle where the greatest common divisor</p>
<p>of all three sides is 1 (unity).</p><p>This will exclude, for example, triangle 6, 8, 10.</p>
Task:
<p>Implement a function based on Hero's formula that returns the first <code>n<sub>th</sub></code> ordered triangles in an array of arrays.</p>
<a href="https://en.wikipedia.org/wiki/Heron's formula" title="wp: Heron's formula" target="_blank">Hero's formula</a> for the area of a triangle given the length of its three sides <big> a,</big> <big>b,</big> and <big>c</big> is given by:
<span style="margin-left: 2em;"><big>$A = \sqrt{s(s-a)(s-b)(s-c)},$</big></span>
where <big>s</big> is half the perimeter of the triangle; that is,
<span style="margin-left: 2em;"><big>$s=\frac{a+b+c}{2}.$</big></span>
<a href="http://www.had2know.com/academics/heronian-triangles-generator-calculator.html" title="link: http://www.had2know.com/academics/heronian-triangles-generator-calculator.html" target="_blank">Heronian triangles</a> are triangles whose sides and area are all integers.
An example is the triangle with sides <b>3, 4, 5</b> whose area is <b>6</b> (and whose perimeter is <b>12</b>).
Note that any triangle whose sides are all an integer multiple of <b>3, 4, 5</b>; such as <b>6, 8, 10,</b> will also be a Heronian triangle.
Define a Primitive Heronian triangle as a Heronian triangle where the greatest common divisor
of all three sides is <b>1</b> (unity).
This will exclude, for example, triangle <b>6, 8, 10.</b>
</section>
## Instructions
<section id='instructions'>
Implement a function based on Hero's formula that returns the first <code>n<sub>th</sub></code> ordered triangles in an array of arrays.
</section>
## Tests
@ -47,7 +50,7 @@ tests:
```js
// noprotect
function heronianTriangle (n) {
function heronianTriangle(n) {
// Good luck!
return [];
@ -81,7 +84,7 @@ const res = [
```js
// noprotect
function heronianTriangle (n) {
function heronianTriangle(n) {
const list = [];
const result = [];

View File

@ -6,23 +6,31 @@ challengeType: 5
## Description
<section id='description'>
<p>These two sequences of positive integers are defined as:</p>
<p><big>$$R(1)=1\ ;\ S(1)=2 \\R(n)=R(n-1)+S(n-1), \quad n>1.$$</big></p>
<p>The sequence <big>$S(n)$</big> is further defined as the sequence of positive integers not present in <big>$R(n)$</big>.</p><p>Sequence <big>$R$</big> starts:</p>
<p>1, 3, 7, 12, 18, ...</p>
<p>Sequence <big>$S$</big> starts:</p>
<p>2, 4, 5, 6, 8, ...</p>
Task:
Create two functions named ffr and ffs that when given n return R(n) or S(n) respectively.(Note that R(1) = 1 and S(1) = 2 to avoid off-by-one errors).
No maximum value for n should be assumed.
Sloane's <a href="http://oeis.org/A005228" title="link: http://oeis.org/A005228">A005228</a> and <a href="http://oeis.org/A030124" title="link: http://oeis.org/A030124">A030124</a>.
<a href="http://mathworld.wolfram.com/HofstadterFigure-FigureSequence.html" title="link: http://mathworld.wolfram.com/HofstadterFigure-FigureSequence.html">Wolfram MathWorld</a>
Wikipedia: <a href="https://en.wikipedia.org/wiki/Hofstadter_sequence#Hofstadter_Figure-Figure_sequences" title="wp: Hofstadter_sequence#Hofstadter_Figure-Figure_sequences">Hofstadter Figure-Figure sequences</a>.
These two sequences of positive integers are defined as:
<span style="margin-left: 2em;"><big>$R(1)=1\ ;\ S(1)=2 \\R(n)=R(n-1)+S(n-1), \quad n>1.$</big></span>
The sequence <big>$S(n)$</big> is further defined as the sequence of positive integers not present in <big>$R(n)$</big>.
Sequence <big>$R$</big> starts:
<pre>1, 3, 7, 12, 18, ...</pre>
Sequence <big>$S$</big> starts:
<pre>2, 4, 5, 6, 8, ...</pre>
</section>
## Instructions
<section id='instructions'>
Create two functions named <b>ffr</b> and <b>ffs</b> that when given <b>n</b> return <b>R(n)</b> or <b>S(n)</b> respectively. (Note that R(1) = 1 and S(1) = 2 to avoid off-by-one errors).
No maximum value for <b>n</b> should be assumed.
<b>References</b>
<ul>
<li>
Sloane's <a href="http://oeis.org/A005228" title="link: http://oeis.org/A005228" target="_blank">A005228</a> and <a href="http://oeis.org/A030124" title="link: http://oeis.org/A030124" target="_blank">A030124</a>.
</li>
<li>
<a href="http://mathworld.wolfram.com/HofstadterFigure-FigureSequence.html" title="link: http://mathworld.wolfram.com/HofstadterFigure-FigureSequence.html" target="_blank">Wolfram MathWorld</a>
</li>
<li>
Wikipedia: <a href="https://en.wikipedia.org/wiki/Hofstadter_sequence#Hofstadter_Figure-Figure_sequences" title="wp: Hofstadter_sequence#Hofstadter_Figure-Figure_sequences" target="_blank">Hofstadter Figure-Figure sequences</a>.
</li>
</ul>
</section>
## Tests

View File

@ -6,16 +6,14 @@ challengeType: 5
## Description
<section id='description'>
<p>The <a href="https://en.wikipedia.org/wiki/Hofstadter_sequence#Hofstadter_Q_sequence" title="wp: Hofstadter_sequence#Hofstadter_Q_sequence">Hofstadter Q sequence</a> is defined as:</p>
<p>$Q(1)=Q(2)=1, \\ Q(n)=Q\big(n-Q(n-1)\big)+Q\big(n-Q(n-2)), \quad n>2.$</p>
<p>It is defined like the <a href="http://rosettacode.org/wiki/Fibonacci sequence" title="Fibonacci sequence">Fibonacci sequence</a>, but whereas the next term in the Fibonacci sequence is the sum of the previous two terms, in the Q sequence the previous two terms tell you how far to go back in the Q sequence to find the two numbers to sum to make the next term of the sequence.</p>
Task:
Implement the Hofstadter Q Sequence equation into JavaScript
The <a href="https://en.wikipedia.org/wiki/Hofstadter_sequence#Hofstadter_Q_sequence" title="wp: Hofstadter_sequence#Hofstadter_Q_sequence" target="_blank">Hofstadter Q sequence</a> is defined as:
<span style="left-margin: 2em;">$Q(1)=Q(2)=1, \\ Q(n)=Q\big(n-Q(n-1)\big)+Q\big(n-Q(n-2)), \quad n>2.$</span>
It is defined like the <a href="http://rosettacode.org/wiki/Fibonacci sequence" title="Fibonacci sequence" target="_blank">Fibonacci sequence</a>, but whereas the next term in the Fibonacci sequence is the sum of the previous two terms, in the Q sequence the previous two terms tell you how far to go back in the Q sequence to find the two numbers to sum to make the next term of the sequence.
</section>
## Instructions
<section id='instructions'>
Implement the Hofstadter Q Sequence equation as a function. The function should accept number, <code>n</code>, and return an integer.
</section>
## Tests
@ -46,7 +44,7 @@ tests:
<div id='js-seed'>
```js
function hofstadterQ (n) {
function hofstadterQ(n) {
// Good luck!
return n;
}