fix(curriculum): format challenge markdown as per linting rules (#36326)
* fix: Format challenge markdown according to linting rules * fix: Put spaces after section tags
This commit is contained in:
parent
c387873640
commit
7d4dc382b4
@ -7,6 +7,7 @@ videoUrl: 'https://scrimba.com/p/pVMPUv/cb6k8Cb'
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
You can nest links within other text elements.
|
||||
|
||||
```html
|
||||
@ -19,13 +20,14 @@ Let's break down the example:
|
||||
Normal text is wrapped in the <code>p</code> element:<br> <code><p> Here's a ... for you to follow. </p></code>
|
||||
Next is the <i>anchor</i> element <code><a></code> (which requires a closing tag <code></a></code>):<br> <code><a> ... </a></code>
|
||||
<code>target</code> is an anchor tag attribute that specifies where to open the link and the value <code>"_blank"</code> specifies to open the link in a new tab
|
||||
<code>href</code> is an anchor tag attribute that contains the URL address of the link:<br> <code><a href="http://freecodecamp.org"> ... </a></code>
|
||||
<code>href</code> is an anchor tag attribute that contains the URL address of the link:<br> `<a href="http://freecodecamp.org"> ... </a>`
|
||||
The text, <strong>"link to freecodecamp.org"</strong>, within the <code>a</code> element called <code>anchor text</code>, will display a link to click:<br> <code><a href=" ... ">link to freecodecamp.org</a></code>
|
||||
The final output of the example will look like this:<br><p>Here's a <a target="_blank" href="http://freecodecamp.org"> link to freecodecamp.org</a> for you to follow.</p>
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
Now nest the existing <code>a</code> element within a new <code>p</code> element (just after the existing <code>main</code> element). The new paragraph should have text that says "View more cat photos", where "cat photos" is a link, and the rest of the text is plain text.
|
||||
</section>
|
||||
|
||||
@ -86,9 +88,9 @@ tests:
|
||||
<h2>CatPhotoApp</h2>
|
||||
<main>
|
||||
<p>View more <a target="_blank" href="http://freecatphotoapp.com">cat photos</a></p>
|
||||
|
||||
|
||||
<img src="https://bit.ly/fcc-relaxing-cat" alt="A cute orange cat lying on its back.">
|
||||
|
||||
|
||||
<p>Kitty ipsum dolor sit amet, shed everywhere shed everywhere stretching attack your ankles chase the red dot, hairball run catnip eat the grass sniff.</p>
|
||||
<p>Purr jump eat the grass rip the couch scratched sunbathe, shed everywhere rip the couch sleep in the sink fluffy fur catnip scratched.</p>
|
||||
</main>
|
||||
|
@ -6,6 +6,7 @@ challengeType: 1
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
Sometimes you may need to iterate through all the keys within an object. This requires a specific syntax in JavaScript called a <dfn>for...in</dfn> statement. For our <code>users</code> object, this could look like:
|
||||
|
||||
```js
|
||||
@ -19,12 +20,14 @@ Jeff
|
||||
Sarah
|
||||
Ryan
|
||||
```
|
||||
|
||||
In this statement, we defined a variable <code>user</code>, and as you can see, this variable was reset during each iteration to each of the object's keys as the statement looped through the object, resulting in each user's name being printed to the console.
|
||||
<strong>NOTE:</strong> Objects do not maintain an ordering to stored keys like arrays do; thus a key's position on an object, or the relative order in which it appears, is irrelevant when referencing or accessing that key.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
We've defined a function <code>countOnline</code> which accepts one argument (a users object). Use a <dfn>for...in</dfn> statement within this function to loop through the users object passed into the function and return the number of users whose <code>online</code> property is set to <code>true</code>. An example of a users object which could be passed to <code>countOnline</code> is shown below. Each user will have an <code>online</code> property with either a <code>true</code> or <code>false</code> value.
|
||||
|
||||
|
||||
|
@ -6,6 +6,7 @@ challengeType: 1
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
In previous lessons, you learned that an object can inherit its behavior (methods) from another object by cloning its <code>prototype</code> object:
|
||||
|
||||
```js
|
||||
@ -38,14 +39,17 @@ Bird.prototype.eat = function() {
|
||||
```
|
||||
|
||||
If you have an instance <code>let duck = new Bird();</code> and you call <code>duck.eat()</code>, this is how JavaScript looks for the method on <code>duck’s</code> <code>prototype</code> chain:
|
||||
|
||||
1. duck => Is eat() defined here? No.
|
||||
2. Bird => Is eat() defined here? => Yes. Execute it and stop searching.
|
||||
3. Animal => eat() is also defined, but JavaScript stopped searching before reaching this level.
|
||||
4. Object => JavaScript stopped searching before reaching this level.
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
Override the <code>fly()</code> method for <code>Penguin</code> so that it returns "Alas, this is a flightless bird."
|
||||
</section>
|
||||
|
||||
|
@ -6,7 +6,9 @@ isRequired: false
|
||||
---
|
||||
|
||||
## Description
|
||||
|
||||
<section id='description'>
|
||||
|
||||
React components have several special methods that provide opportunities to perform actions at specific points in the lifecycle of a component. These are called lifecycle methods, or lifecycle hooks, and allow you to catch components at certain points in time. This can be before they are rendered, before they update, before they receive props, before they unmount, and so on. Here is a list of some of the main lifecycle methods:
|
||||
<code>componentWillMount()</code>
|
||||
<code>componentDidMount()</code>
|
||||
@ -14,12 +16,13 @@ React components have several special methods that provide opportunities to perf
|
||||
<code>componentDidUpdate()</code>
|
||||
<code>componentWillUnmount()</code>
|
||||
The next several lessons will cover some of the basic use cases for these lifecycle methods.
|
||||
|
||||
##### Note: The ``` componentWillMount ``` Lifecycle method will be deprecated in a future version of 16.X and removed in version 17. [(Source)](https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html)
|
||||
|
||||
<strong>Note:</strong> The `componentWillMount` Lifecycle method will be deprecated in a future version of 16.X and removed in version 17. [(Source)](https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html)
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
The <code>componentWillMount()</code> method is called before the <code>render()</code> method when a component is being mounted to the DOM. Log something to the console within <code>componentWillMount()</code> - you may want to have your browser console open to see the output.
|
||||
</section>
|
||||
|
||||
|
@ -6,6 +6,7 @@ challengeType: 0
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
The last challenge showed how the <code>@for</code> directive uses a starting and ending value to loop a certain number of times. Sass also offers the <code>@each</code> directive which loops over each item in a list or map.
|
||||
On each iteration, the variable gets assigned to the current value from the list or map.
|
||||
|
||||
@ -46,6 +47,7 @@ Both of the above code examples are converted into the following CSS:
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
Write an <code>@each</code> directive that goes through a list: <code>blue, black, red</code> and assigns each variable to a <code>.color-bg</code> class, where the "color" part changes for each item.
|
||||
Each class should set the <code>background-color</code> the respective color.
|
||||
</section>
|
||||
@ -99,6 +101,7 @@ tests:
|
||||
The solution requires using the $color variable twice: once for the class name and once for setting the background color. You can use either the list or map data type.
|
||||
|
||||
### List Data type
|
||||
|
||||
```html
|
||||
<style type='text/sass'>
|
||||
|
||||
@ -118,6 +121,7 @@ The solution requires using the $color variable twice: once for the class name a
|
||||
```
|
||||
|
||||
### Map Data type
|
||||
|
||||
```html
|
||||
<style type='text/sass'>
|
||||
|
||||
|
@ -6,14 +6,16 @@ challengeType: 2
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
In the good old days this was what you needed to do if you wanted to edit a document and be able to use it somehow e.g. sending it back in a server response. Mongoose has a dedicated updating method : <code>Model.update()</code>. It is bound to the low-level mongo driver. It can bulk edit many documents matching certain criteria, but it doesn’t send back the updated document, only a ‘status’ message. Furthermore it makes model validations difficult, because it just directly calls the mongo driver.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
Find a person by <code>_id</code> ( use any of the above methods ) with the parameter <code>personId</code> as search key. Add "hamburger" to the list of the person's <code>favoriteFoods</code> (you can use <code>Array.push()</code>). Then - inside the find callback - <code>save()</code> the updated <code>Person</code>.
|
||||
|
||||
<strong>Note:</strong> This may be tricky, if in your Schema, you declared <code>favoriteFoods</code> as an Array, without specifying the type (i.e. <code>[String]</code>). In that <code>casefavoriteFoods</code> defaults to Mixed type, and you have to manually mark it as edited using <code>document.markModified('edited-field')</code>. See Mongoose documentation at https://mongoosejs.com/docs/schematypes.html#Mixed
|
||||
<strong>Note:</strong> This may be tricky, if in your Schema, you declared <code>favoriteFoods</code> as an Array, without specifying the type (i.e. <code>[String]</code>). In that <code>casefavoriteFoods</code> defaults to Mixed type, and you have to manually mark it as edited using <code>document.markModified('edited-field')</code>. See [Mongoose documentation](https://mongoosejs.com/docs/schematypes.html#Mixed)
|
||||
|
||||
</section>
|
||||
|
||||
|
@ -6,15 +6,17 @@ challengeType: 2
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
As a reminder, this project is being built upon the following starter project on <a href='https://glitch.com/#!/import/github/freeCodeCamp/boilerplate-mochachai/'>Glitch</a>, or cloned from <a href='https://github.com/freeCodeCamp/boilerplate-mochachai/'>GitHub</a>.
|
||||
|
||||
<code>isOk()</code> will test for a truthy value and <code>isNotOk()</code> will test for a falsy value.
|
||||
Truthy reference: https://developer.mozilla.org/en-US/docs/Glossary/Truthy
|
||||
Falsy reference: https://developer.mozilla.org/en-US/docs/Glossary/Falsy
|
||||
[Truthy reference](https://developer.mozilla.org/en-US/docs/Glossary/Truthy)
|
||||
[Falsy reference](https://developer.mozilla.org/en-US/docs/Glossary/Falsy)
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
Use <code>assert.isOk()</code> or <code>assert.isNotOk()</code> to make the tests pass.
|
||||
</section>
|
||||
|
||||
|
@ -6,6 +6,7 @@ challengeType: 1
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
Now that we have an idea of the basics lets write a more complex method.
|
||||
In this challenge, we will create a method to add new values to our binary search tree. The method should be called <code>add</code> and it should accept an integer value to add to the tree. Take care to maintain the invariant of a binary search tree: the value in each left child should be less than or equal to the parent value, and the value in each right child should be greater than or equal to the parent value. Here, let's make it so our tree cannot hold duplicate values. If we try to add a value that already exists, the method should return <code>null</code>. Otherwise, if the addition is successful, <code>undefined</code> should be returned.
|
||||
Hint: trees are naturally recursive data structures!
|
||||
@ -30,10 +31,12 @@ tests:
|
||||
- text: Adding an element that already exists returns <code>null</code>
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.add !== 'function') { return false; }; test.add(4); return test.add(4) == null; })(), 'Adding an element that already exists returns <code>null</code>');
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
@ -49,6 +52,7 @@ function BinarySearchTree() {
|
||||
// change code above this line
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
### After Test
|
||||
@ -100,6 +104,7 @@ BinarySearchTree.prototype = {
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
</div>
|
||||
</section>
|
||||
|
||||
@ -145,4 +150,5 @@ function BinarySearchTree() {
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -6,6 +6,7 @@ challengeType: 1
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
Let's create a addAt(index,element) method that adds an element at a given index.
|
||||
Just like how we remove elements at a given index, we need to keep track of the currentIndex as we traverse the linked list. When the currentIndex matches the given index, we would need to reassign the previous node's next property to reference the new added node. And the new node should reference the next node in the currentIndex.
|
||||
Returning to the conga line example, a new person wants to join the line, but he wants to join in the middle. You are in the middle of the line, so you take your hands off of the person ahead of you. The new person walks over and puts his hands on the person you once had hands on, and you now have your hands on the new person.
|
||||
@ -13,6 +14,7 @@ Returning to the conga line example, a new person wants to join the line, but he
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
Create an <code>addAt(index,element)</code> method that adds an element at a given index. Return false if an element could not be added.
|
||||
<strong>Note:</strong> Remember to check if the given index is a negative or is longer than the length of the linked list.
|
||||
</section>
|
||||
@ -29,10 +31,12 @@ tests:
|
||||
- text: Your <code>addAt</code> method should return <code>false</code> if a node was unable to be added.
|
||||
testString: assert((function(){var test = new LinkedList(); test.add('cat'); test.add('dog'); return (test.addAt(4,'cat') === false); }()), 'Your <code>addAt</code> method should return <code>false</code> if a node was unable to be added.');
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
@ -74,6 +78,7 @@ function LinkedList() {
|
||||
// Only change code above this line
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
</section>
|
||||
|
||||
|
@ -6,6 +6,7 @@ challengeType: 1
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
Graphs can be represented in different ways. Here we describe one way, which is called an <dfn>adjacency list</dfn>.
|
||||
An adjacency list is essentially a bulleted list where the left side is the node and the right side lists all the other nodes it's connected to. Below is a representation of an adjacency list.
|
||||
<blockquote>Node1: Node2, Node3<br>Node2: Node1<br>Node3: Node1</blockquote>
|
||||
@ -34,6 +35,7 @@ var undirectedGArr = [
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
Create a social network as an undirected graph with 4 nodes/people named <code>James</code>, <code>Jill</code>, <code>Jenny</code>, and <code>Jeff</code>. There are edges/relationships between James and Jeff, Jill and Jenny, and Jeff and Jenny.
|
||||
</section>
|
||||
|
||||
@ -51,10 +53,12 @@ tests:
|
||||
- text: There should be an edge between <code>Jeff</code> and <code>Jenny</code>.
|
||||
testString: assert(undirectedAdjList.Jeff.indexOf("Jenny") !== -1 && undirectedAdjList.Jenny.indexOf("Jeff") !== -1, 'There should be an edge between <code>Jeff</code> and <code>Jenny</code>.');
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
|
@ -6,6 +6,7 @@ challengeType: 1
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
Another way to represent a graph is to put it in an <dfn>adjacency matrix</dfn>.
|
||||
An <dfn>adjacency matrix</dfn> is a two-dimensional (2D) array where each nested array has the same number of elements as the outer array. In other words, it is a matrix or grid of numbers, where the numbers represent the edges. Zeros mean there is no edge or relationship.
|
||||
<blockquote> 1 2 3<br> ------<br>1 | 0 1 1<br>2 | 1 0 0<br>3 | 1 0 0</blockquote>
|
||||
@ -36,6 +37,7 @@ Graphs can also have <dfn>weights</dfn> on their edges. So far, we have <dfn>unw
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
Create an adjacency matrix of an undirected graph with five nodes. This matrix should be in a multi-dimensional array. These five nodes have relationships between the first and fourth node, the first and third node, the third and fifth node, and the fourth and fifth node. All edge weights are one.
|
||||
</section>
|
||||
|
||||
@ -55,10 +57,12 @@ tests:
|
||||
- text: There should be an edge between the fourth and fifth node.
|
||||
testString: assert((adjMatUndirected[3][4] === 1) && (adjMatUndirected[4][3] === 1), 'There should be an edge between the fourth and fifth node.');
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
@ -80,4 +84,5 @@ var adjMatUndirected = [
|
||||
[0, 0, 1, 1, 0]
|
||||
];
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -6,12 +6,14 @@ challengeType: 1
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
Now that we have a general sense of what a binary search tree is let's talk about it in a little more detail. Binary search trees provide logarithmic time for the common operations of lookup, insertion, and deletion in the average case, and linear time in the worst case. Why is this? Each of those basic operations requires us to find an item in the tree (or in the case of insertion to find where it should go) and because of the tree structure at each parent node we are branching left or right and effectively excluding half the size of the remaining tree. This makes the search proportional to the logarithm of the number of nodes in the tree, which creates logarithmic time for these operations in the average case.
|
||||
Ok, but what about the worst case? Well, consider constructing a tree from the following values, adding them left to right: <code>10</code>, <code>12</code>, <code>17</code>, <code>25</code>. Following our rules for a binary search tree, we will add <code>12</code> to the right of <code>10</code>, <code>17</code> to the right of this, and <code>25</code> to the right of this. Now our tree resembles a linked list and traversing it to find <code>25</code> would require us to traverse all the items in linear fashion. Hence, linear time in the worst case. The problem here is that the tree is unbalanced. We'll look a little more into what this means in the following challenges.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
In this challenge, we will create a utility for our tree. Write a method <code>isPresent</code> which takes an integer value as input and returns a boolean value for the presence or absence of that value in the binary search tree.
|
||||
</section>
|
||||
|
||||
@ -29,6 +31,7 @@ tests:
|
||||
- text: <code>isPresent</code> handles cases where the tree is empty.
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.isPresent !== 'function') { return false; }; return test.isPresent(5) == false; })(), '<code>isPresent</code> handles cases where the tree is empty.');
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
|
@ -6,11 +6,12 @@ challengeType: 1
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
In this challenge you will be creating a Circular Queue. A circular queue is a queue that writes to the end of a collection then begins overwriting itself at the beginning of the collection. This type of data structure is useful in certain situations. For example, a circular queue can be used for streaming media. Once the queue is full, new media data will overwrite old data.
|
||||
|
||||
A good way to illustrate this concept is with an array of length `5`:
|
||||
|
||||
```output
|
||||
|
||||
```js
|
||||
[null, null, null, null, null]
|
||||
^Read @ 0
|
||||
^Write @ 0
|
||||
@ -18,21 +19,23 @@ A good way to illustrate this concept is with an array of length `5`:
|
||||
|
||||
Here the read and write are both at position `0`. Now the queue gets 3 new records `a`, `b`, and `c`. Our queue now looks like:
|
||||
|
||||
```output
|
||||
```js
|
||||
[a, b, c, null, null]
|
||||
^Read @ 0
|
||||
^Write @ 3
|
||||
```
|
||||
|
||||
As the read head reads, it can remove values or keep them:
|
||||
```output
|
||||
|
||||
```js
|
||||
[null, null, null, null, null]
|
||||
^Read @ 3
|
||||
^Write @ 3
|
||||
```
|
||||
|
||||
Now we write the values `d`, `e`, and `f` to the queue. Once the write reaches the end of the array it loops back to the beginning:
|
||||
```output
|
||||
|
||||
```js
|
||||
[f, null, null, d, e]
|
||||
^Read @ 3
|
||||
^Write @ 1
|
||||
@ -43,6 +46,7 @@ This approach requires a constant amount of memory but allows files of a much la
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
In this challenge we will implement a circular queue. The circular queue should provide `enqueue` and `dequeue` methods which allow you to read from and write to the queue. The class itself should also accept an integer argument which you can use to specify the size of the queue when created. We've written the starting version of this class for you in the code editor. When you enqueue items to the queue, the write pointer should advance forward and loop back to the beginning once it reaches the end of the queue. Likewise, the read pointer should advance forward as you dequeue items. The write pointer should not be allowed to move past the read pointer (our class won't let you overwrite data you haven't read yet) and the read pointer should not be able to advance past data you have written.
|
||||
In addition, the `enqueue` method should return the item you enqueued if it is successful; otherwise it will return `null`. Similarly, when you dequeue an item, that item should be returned and if you cannot dequeue an item you should return `null`.
|
||||
</section>
|
||||
@ -69,6 +73,7 @@ tests:
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
@ -159,4 +164,5 @@ class CircularQueue {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -6,11 +6,13 @@ challengeType: 1
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
The next few challenges will cover maps and hash tables. Maps are data structures that store key-value pairs. In JavaScript, these are available to us as objects. Maps provide rapid lookup of stored items based on key values and are very common and useful data structures.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
Let's get some practice creating our own map. Because JavaScript objects provide a much more efficient map structure than anything we could write here, this is intended primarily as a learning exercise. However, JavaScript objects only provide us with certain operations. What if we wanted to define custom operations?
|
||||
Use the <code>Map</code> object provided here as a wrapper around a JavaScript <code>object</code>. Create the following methods and operations on the Map object:
|
||||
<ul>
|
||||
@ -44,6 +46,7 @@ tests:
|
||||
- text: The clear method empties the map and the size method returns the number of items present in the map.
|
||||
testString: assert((function() { var test = false; if (typeof Map !== 'undefined') { test = new Map() }; test.add('b','b'); test.add('c','d'); test.remove('asdfas'); var init = test.size(); test.clear(); return (init == 2 && test.size() == 0)})(), 'The clear method empties the map and the size method returns the number of items present in the map.');
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
|
@ -6,7 +6,8 @@ challengeType: 1
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
In this exercise we are going to create a class named <code>Set</code> to emulate an abstract data structure called "set". A set is like an array, but it cannot contain duplicate values. The typical use for a set is to simply check for the presence of an item.
|
||||
|
||||
In this exercise we are going to create a class named <code>Set</code> to emulate an abstract data structure called "set". A set is like an array, but it cannot contain duplicate values. The typical use for a set is to simply check for the presence of an item.
|
||||
We can see how ES6 set object works in the example below-
|
||||
|
||||
```js
|
||||
@ -26,6 +27,7 @@ And finally, we will create a size method that returns the number of elements in
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
Create an <code>add</code> method that adds a unique value to the set collection and returns <code>true</code> if the value was successfully added and <code>false</code> otherwise.
|
||||
|
||||
Create a <code>remove</code> method that accepts a value and checks if it exists in the set. If it does, then this method should remove it from the set collection, and return <code>true</code>. Otherwise, it should return <code>false</code>.
|
||||
@ -63,6 +65,7 @@ tests:
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
@ -130,4 +133,5 @@ class Set {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -6,6 +6,7 @@ challengeType: 1
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
In the last section, we talked about what a stack is and how we can use an array to represent a stack. In this section, we will be creating our own stack class.
|
||||
Although you can use arrays to create stacks, sometimes it is best to limit the amount of control we have with our stacks.
|
||||
Apart from the <code>push</code> and <code>pop</code> method, stacks have other useful methods. Let's add a <code>peek</code>, <code>isEmpty</code>, and <code>clear</code> method to our stack class.
|
||||
@ -13,6 +14,7 @@ Apart from the <code>push</code> and <code>pop</code> method, stacks have other
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
Write a <code>push</code> method that pushes an element to the top of the stack, a <code>pop</code> method that removes the element on the top of the stack, a <code>peek</code> method that looks at the first element in the stack, an <code>isEmpty</code> method that checks if the stack is empty, and a <code>clear</code> method that removes all elements from the stack.
|
||||
Normally stacks don't have this, but we've added a <code>print</code> helper method that console logs the collection.
|
||||
</section>
|
||||
@ -41,10 +43,12 @@ tests:
|
||||
- text: The <code>clear</code> method should remove all element from the stack
|
||||
testString: assert((function(){var test = new Stack(); test.push('CS50'); test.clear(); return (test.isEmpty())}()), 'The <code>clear</code> method should remove all element from the stack');
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
@ -58,6 +62,7 @@ function Stack() {
|
||||
// Only change code above this line
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
</section>
|
||||
|
||||
|
@ -6,6 +6,7 @@ challengeType: 1
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
Here we will move on from binary search trees and take a look at another type of tree structure called a trie. A trie is an ordered search tree commonly used to hold strings, or more generically associative arrays or dynamic datasets in which the keys are strings. They are very good at storing sets of data when many keys will have overlapping prefixes, for example, all the words in a dictionary.
|
||||
Unlike a binary tree, nodes are not associated with actual values. Instead, the path to a node represents a specific key. For instance, if we wanted to store the string code in a trie, we would have four nodes, one for each letter: c — o — d — e. Following that path through all these nodes will then create code as a string — that path is the key we stored. Then, if we wanted to add the string coding, it would share the first three nodes of code before branching away after the d. In this way, large datasets can be stored very compactly. In addition, search can be very quick because it is effectively limited to the length of the string you are storing. Furthermore, unlike binary trees a node can store any number of child nodes.
|
||||
As you might have guessed from the above example, some metadata is commonly stored at nodes that hold the end of a key so that on later traversals that key can still be retrieved. For instance, if we added codes in our example above we would need some way to know that the e in code represents the end of a key that was previously entered. Otherwise, this information would effectively be lost when we add codes.
|
||||
@ -13,6 +14,7 @@ As you might have guessed from the above example, some metadata is commonly stor
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
Let's create a trie to store words. It will accept words through an <code>add</code> method and store these in a trie data structure. It will also allow us to query if a given string is a word with an <code>isWord</code> method, and retrieve all the words entered into the trie with a <code>print</code> method. <code>isWord</code> should return a boolean value and print should return an array of all these words as string values.
|
||||
In order for us to verify that this data structure is implemented correctly, we've provided a <code>Node</code> structure for each node in the tree. Each node will be an object with a <code>keys</code> property which is a JavaScript Map object. This will hold the individual letters that are valid keys of each node. We've also created an <code>end</code> property on the nodes that can be set to <code>true</code> if the node represents the termination of a word.
|
||||
</section>
|
||||
@ -38,6 +40,7 @@ tests:
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
@ -57,6 +60,7 @@ var Trie = function() {
|
||||
// change code above this line
|
||||
};
|
||||
```
|
||||
|
||||
</div>
|
||||
</section>
|
||||
|
||||
|
@ -6,6 +6,7 @@ challengeType: 1
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
The new version of JavaScript provides us with a built-in Map object which provides much of the functionality we wrote by hand in the last challenge. This Map object, although similar to regular JavaScript objects, provides some useful functionality that normal objects lack. For example, an ES6 Map tracks the insertion order of items that are added to it. Here is a more complete overview of its methods:
|
||||
<code>.has(key)</code> returns true or false based on the presence of a key
|
||||
<code>.get(key)</code> returns the value associated with a key
|
||||
@ -18,6 +19,7 @@ The new version of JavaScript provides us with a built-in Map object which provi
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
Define a JavaScript Map object and assign to it a variable called myMap. Add the key, value pair <code>freeCodeCamp</code>, <code>Awesome!</code> to it.
|
||||
</section>
|
||||
|
||||
@ -31,15 +33,18 @@ tests:
|
||||
- text: myMap contains the key value pair <code>freeCodeCamp</code>, <code>Awesome!</code>.
|
||||
testString: assert(myMap.get('freeCodeCamp') === 'Awesome!', 'myMap contains the key value pair <code>freeCodeCamp</code>, <code>Awesome!</code>.');
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
// change code below this line
|
||||
```
|
||||
|
||||
</div>
|
||||
</section>
|
||||
|
||||
|
@ -7,6 +7,7 @@ challengeType: 1
|
||||
## Description
|
||||
|
||||
<section id='description'>
|
||||
|
||||
This is the first of three challenges where we will implement a more difficult operation in binary search trees: deletion. Deletion is difficult because removing nodes breaks links in the tree. These links must be carefully reestablished to ensure the binary tree structure is maintained. For some deletions, this means the tree must be rearranged. In general, you will encounter one of three cases when trying to delete a node:
|
||||
Leaf Node: The target to delete has zero children.
|
||||
One Child: The target to delete only has one child.
|
||||
@ -17,6 +18,7 @@ Removing a leaf node is easy, we simply remove it. Deleting a node with one chil
|
||||
## Instructions
|
||||
|
||||
<section id='instructions'>
|
||||
|
||||
Create a method on our binary tree called <code>remove</code>. We'll build the logic for our deletion operation in here. First, you'll want to create a function within remove that finds the node we are trying to delete in the current tree. If the node is not present in the tree, <code>remove</code> should return <code>null</code>. Now, if the target node is a leaf node with no children, then the parent reference to it should be set to <code>null</code>. This effectively deletes the node from the tree. To do this, you will have to keep track of the parent of the node we are trying to delete as well. It will also be useful to create a way to track the number of children the target node has, as this will determine which case our deletion falls under.
|
||||
We will handle the second and third cases in the next challenges. Good luck!
|
||||
</section>
|
||||
@ -37,10 +39,12 @@ tests:
|
||||
- text: The <code>remove</code> method removes leaf nodes from the tree
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.remove !== 'function') { return false; }; test.add(5); test.add(3); test.add(7); test.add(6); test.add(10); test.add(12); test.remove(3); test.remove(12); test.remove(10); return (test.inorder().join('') == '567'); })(), 'The <code>remove</code> method removes leaf nodes from the tree');
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
|
@ -6,11 +6,13 @@ challengeType: 1
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
Now that we can delete leaf nodes let's move on to the second case: deleting a node with one child. For this case, say we have a tree with the following nodes 1 — 2 — 3 where 1 is the root. To delete 2, we simply need to make the right reference in 1 point to 3. More generally to delete a node with only one child, we make that node's parent reference the next node in the tree.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
We've provided some code in our <code>remove</code> method that accomplishes the tasks from the last challenge. We find the target to delete and its parent and define the number of children the target node has. Let's add the next case here for target nodes with only one child. Here, we'll have to determine if the single child is a left or right branch in the tree and then set the correct reference in the parent to point to this node. In addition, let's account for the case where the target is the root node (this means the parent node will be <code>null</code>). Feel free to replace all the starter code with your own as long as it passes the tests.
|
||||
</section>
|
||||
|
||||
@ -34,10 +36,12 @@ tests:
|
||||
- text: Removing the root in a tree with two nodes sets the second to be the root.
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.remove !== 'function') { return false; }; test.add(15); test.add(27); test.remove(15); return (test.inorder().join('') == '27'); })(), 'Removing the root in a tree with two nodes sets the second to be the root.');
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
|
@ -6,12 +6,14 @@ challengeType: 1
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
Removing nodes that have two children is the hardest case to implement. Removing a node like this produces two subtrees that are no longer connected to the original tree structure. How can we reconnect them? One method is to find the smallest value in the right subtree of the target node and replace the target node with this value. Selecting the replacement in this way ensures that it is greater than every node in the left subtree it becomes the new parent of but also less than every node in the right subtree it becomes the new parent of.
|
||||
Once this replacement is made the replacement node must be removed from the right subtree. Even this operation is tricky because the replacement may be a leaf or it may itself be the parent of a right subtree. If it is a leaf we must remove its parent's reference to it. Otherwise, it must be the right child of the target. In this case, we must replace the target value with the replacement value and make the target reference the replacement's right child.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
Let's finish our <code>remove</code> method by handling the third case. We've provided some code again for the first two cases. Add some code now to handle target nodes with two children. Any edge cases to be aware of? What if the tree has only three nodes? Once you are finished this will complete our deletion operation for binary search trees. Nice job, this is a pretty hard problem!
|
||||
</section>
|
||||
|
||||
@ -39,10 +41,12 @@ tests:
|
||||
- text: The root can be removed on a tree of three nodes.
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.remove !== 'function') { return false; }; test.add(100); test.add(50); test.add(300); test.remove(100); return (test.inorder().join('') == 50300); })(), 'The root can be removed on a tree of three nodes.');
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
|
@ -6,6 +6,7 @@ challengeType: 1
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
We know how to search a binary search tree for a specific value. But what if we just want to explore the entire tree? Or what if we don't have an ordered tree and we need to just search for a value? Here we will introduce some tree traversal methods which can be used to explore tree data structures. First up is depth-first search. In depth-first search, a given subtree is explored as deeply as possible before the search continues on to another subtree. There are three ways this can be done:
|
||||
In-order: Begin the search at the left-most node and end at the right-most node.
|
||||
Pre-order: Explore all the roots before the leaves.
|
||||
@ -15,6 +16,7 @@ As you may guess, you may choose different search methods depending on what type
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
Here we will create these three search methods on our binary search tree. Depth-first search is an inherently recursive operation which continues to explore further subtrees so long as child nodes are present. Once you understand this basic concept, you can simply rearrange the order in which you explore the nodes and subtrees to produce any of the three searches above. For example, in post-order search we would want to recurse all the way to a leaf node before we begin to return any of the nodes themselves, whereas in pre-order search we would want to return the nodes first, and then continue recursing down the tree.
|
||||
Define <code>inorder</code>, <code>preorder</code>, and <code>postorder</code> methods on our tree. Each of these methods should return an array of items which represent the tree traversal. Be sure to return the integer values at each node in the array, not the nodes themselves. Finally, return <code>null</code> if the tree is empty.
|
||||
</section>
|
||||
@ -45,6 +47,7 @@ tests:
|
||||
- text: The <code>postorder</code> method returns <code>null</code> for an empty tree.
|
||||
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; if (typeof test.postorder !== 'function') { return false; }; return (test.postorder() == null); })(), 'The <code>postorder</code> method returns <code>null</code> for an empty tree.');
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
|
@ -6,22 +6,11 @@ title: 'Problem 221: Alexandrian Integers'
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
We shall call a positive integer A an "Alexandrian integer", if there exist integers p, q, r such that:
|
||||
A = p · q · r and 1/A = 1/p + 1/q + 1/r
|
||||
<!-- TODO Use MathJax -->
|
||||
|
||||
|
||||
A = p · q · r and
|
||||
|
||||
|
||||
1A
|
||||
=
|
||||
|
||||
1p
|
||||
+
|
||||
|
||||
1q
|
||||
+
|
||||
|
||||
1r
|
||||
For example, 630 is an Alexandrian integer (p = 5, q = −7, r = −18).
|
||||
In fact, 630 is the 6th Alexandrian integer, the first 6 Alexandrian integers being: 6, 42, 120, 156, 420 and 630.
|
||||
|
||||
|
@ -6,16 +6,17 @@ title: 'Problem 228: Minkowski Sums'
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
<!-- TODO Use MathJax and re-write from projecteuler.net -->
|
||||
Let Sn be the regular n-sided polygon – or shape – whose vertices
|
||||
|
||||
vk (k = 1,2,…,n) have coordinates:
|
||||
|
||||
xk =
|
||||
cos( 2k-1/n ×180° )
|
||||
xk = cos( 2k-1/n ×180° )
|
||||
|
||||
yk =
|
||||
sin( 2k-1/n ×180° )
|
||||
Each Sn is to be interpreted as a filled shape consisting of all points on the perimeter and in the interior.
|
||||
yk = sin( 2k-1/n ×180° )
|
||||
|
||||
Each Sn is to be interpreted as a filled shape consisting of all points on the perimeter and in the interior.
|
||||
|
||||
The Minkowski sum, S+T, of two shapes S and T is the result of
|
||||
|
||||
|
@ -6,16 +6,17 @@ title: 'Problem 301: Nim'
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
Nim is a game played with heaps of stones, where two players take it in turn to remove any number of stones from any heap until no stones remain.
|
||||
|
||||
We'll consider the three-heap normal-play version of Nim, which works as follows:
|
||||
|
||||
- At the start of the game there are three heaps of stones.
|
||||
- On his turn the player removes any positive number of stones from any single heap.
|
||||
- The first player unable to move (because no stones remain) loses.
|
||||
|
||||
If (n1,n2,n3) indicates a Nim position consisting of heaps of size n1, n2 and n3 then there is a simple function X(n1,n2,n3) — that you may look up or attempt to deduce for yourself — that returns:
|
||||
zero if, with perfect strategy, the player about to move will eventually lose; or
|
||||
non-zero if, with perfect strategy, the player about to move will eventually win.For example X(1,2,3) = 0 because, no matter what the current player does, his opponent can respond with a move that leaves two heaps of equal size, at which point every move by the current player can be mirrored by his opponent until no stones remain; so the current player loses. To illustrate:
|
||||
If (n1,n2,n3) indicates a Nim position consisting of heaps of size n1, n2 and n3 then there is a simple function X(n1,n2,n3) — that you may look up or attempt to deduce for yourself — that returns: zero if, with perfect strategy, the player about to move will eventually lose; or non-zero if, with perfect strategy, the player about to move will eventually win. For example X(1,2,3) = 0 because, no matter what the current player does, his opponent can respond with a move that leaves two heaps of equal size, at which point every move by the current player can be mirrored by his opponent until no stones remain; so the current player loses. To illustrate:
|
||||
|
||||
- current player moves to (1,2,1)
|
||||
- opponent moves to (1,0,1)
|
||||
- current player moves to (0,0,1)
|
||||
|
@ -6,45 +6,24 @@ title: 'Problem 330: Euler''s Number'
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
An infinite sequence of real numbers a(n) is defined for all integers n as follows:
|
||||
|
||||
|
||||
For example,a(0) =
|
||||
11!
|
||||
+
|
||||
12!
|
||||
+
|
||||
13!
|
||||
+ ... = e − 1
|
||||
a(1) =
|
||||
e − 11!
|
||||
+
|
||||
12!
|
||||
+
|
||||
13!
|
||||
+ ... = 2e − 3
|
||||
a(2) =
|
||||
2e − 31!
|
||||
+
|
||||
e − 12!
|
||||
+
|
||||
13!
|
||||
+ ... =
|
||||
72
|
||||
e − 6
|
||||
<!-- TODO Use MathJax and re-write from projecteuler.net -->
|
||||
For example,a(0) = 11! + 12! + 13! + ... = e − 1
|
||||
a(1) = e − 11! + 12! + 13! + ... = 2e − 3
|
||||
a(2) = 2e − 31! + e − 12! + 13! + ... = 72 e − 6
|
||||
|
||||
with e = 2.7182818... being Euler's constant.
|
||||
|
||||
|
||||
It can be shown that a(n) is of the form
|
||||
|
||||
A(n) e + B(n)n!
|
||||
for integers A(n) and B(n).
|
||||
A(n) e + B(n)n!
|
||||
for integers A(n) and B(n).
|
||||
|
||||
For example a(10) =
|
||||
|
||||
328161643 e − 65269448610!
|
||||
.
|
||||
328161643 e − 65269448610!.
|
||||
|
||||
Find A(109) + B(109) and give your answer mod 77 777 777.
|
||||
</section>
|
||||
|
@ -6,6 +6,7 @@ title: 'Problem 334: Spilling the beans'
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
In Plato's heaven, there exist an infinite number of bowls in a straight line.
|
||||
Each bowl either contains some or none of a finite number of beans.
|
||||
A child plays a game, which allows only one kind of move: removing two beans from any bowl, and putting one in each of the two adjacent bowls. The game ends when each bowl contains either one or no beans.
|
||||
@ -13,49 +14,17 @@ A child plays a game, which allows only one kind of move: removing two beans fro
|
||||
For example, consider two adjacent bowls containing 2 and 3 beans respectively, all other bowls being empty. The following eight moves will finish the game:
|
||||
|
||||
|
||||
|
||||
<!-- TODO Use MathJax and re-write from projecteuler.net -->
|
||||
You are given the following sequences:
|
||||
t0 = 123456.
|
||||
|
||||
|
||||
ti =
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
ti-12
|
||||
|
||||
,
|
||||
|
||||
|
||||
|
||||
if ti-1 is even
|
||||
|
||||
|
||||
|
||||
ti-12
|
||||
|
||||
|
||||
|
||||
926252,
|
||||
|
||||
|
||||
if ti-1 is odd
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
where ⌊x⌋ is the floor function
|
||||
|
||||
|
||||
|
||||
|
||||
and is the bitwise XOR operator.
|
||||
|
||||
|
||||
bi = ( ti mod 211) + 1.
|
||||
t0 = 123456.
|
||||
ti = ti-12,
|
||||
if ti-1 is even
|
||||
ti-12
|
||||
926252,
|
||||
if ti-1 is odd
|
||||
where ⌊x⌋ is the floor function
|
||||
and is the bitwise XOR operator.
|
||||
bi = ( ti mod 211) + 1.
|
||||
|
||||
The first two terms of the last sequence are b1 = 289 and b2 = 145.
|
||||
If we start with b1 and b2 beans in two adjacent bowls, 3419100 moves would be required to finish the game.
|
||||
|
@ -6,13 +6,16 @@ title: 'Problem 385: Ellipses inside triangles'
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
For any triangle T in the plane, it can be shown that there is a unique ellipse with largest area that is completely inside T.
|
||||
|
||||
|
||||
|
||||
For a given n, consider triangles T such that:
|
||||
|
||||
- the vertices of T have integer coordinates with absolute value ≤ n, and
|
||||
- the foci1 of the largest-area ellipse inside T are (√13,0) and (-√13,0).
|
||||
|
||||
Let A(n) be the sum of the areas of all such triangles.
|
||||
|
||||
|
||||
|
@ -6,15 +6,16 @@ title: 'Problem 443: GCD sequence'
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
Let g(n) be a sequence defined as follows:
|
||||
g(4) = 13,
|
||||
g(n) = g(n-1) + gcd(n, g(n-1)) for n > 4.
|
||||
|
||||
The first few values are:
|
||||
|
||||
n4567891011121314151617181920...
|
||||
g(n)1314161718272829303132333451545560...
|
||||
|
||||
n 4567891011121314151617181920...
|
||||
g(n) 1314161718272829303132333451545560...
|
||||
<!-- TODO Use MathJax -->
|
||||
|
||||
You are given that g(1 000) = 2524 and g(1 000 000) = 2624152.
|
||||
|
||||
|
@ -6,14 +6,16 @@ title: 'Problem 449: Chocolate covered candy'
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
Phil the confectioner is making a new batch of chocolate covered candy. Each candy centre is shaped like an ellipsoid of revolution defined by the equation: b2x2 + b2y2 + a2z2 = a2b2.
|
||||
|
||||
|
||||
Phil wants to know how much chocolate is needed to cover one candy centre with a uniform coat of chocolate one millimeter thick.
|
||||
If a=1 mm and b=1 mm, the amount of chocolate required is
|
||||
|
||||
283
|
||||
π mm3
|
||||
283
|
||||
π mm3
|
||||
<!-- TODO Use MathJax -->
|
||||
|
||||
If a=2 mm and b=1 mm, the amount of chocolate required is approximately 60.35475635 mm3.
|
||||
|
||||
|
@ -6,13 +6,10 @@ title: 'Problem 454: Diophantine reciprocals III'
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
In the following equation x, y, and n are positive integers.
|
||||
|
||||
1x
|
||||
+
|
||||
1y
|
||||
=
|
||||
1n
|
||||
In the following equation x, y, and n are positive integers.
|
||||
1/x + 1/y= 1/n
|
||||
<!-- TODO Use MathJax -->
|
||||
|
||||
For a limit L we define F(L) as the number of solutions which satisfy x < y ≤ L.
|
||||
|
||||
|
@ -6,6 +6,8 @@ title: 'Problem 462: Permutation of 3-smooth numbers'
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
<!-- TODO use MathJax -->
|
||||
A 3-smooth number is an integer which has no prime factor larger than 3. For an integer N, we define S(N) as the set of 3-smooth numbers less than or equal to N . For example, S(20) = { 1, 2, 3, 4, 6, 8, 9, 12, 16, 18 }.
|
||||
|
||||
|
||||
@ -13,10 +15,12 @@ We define F(N) as the number of permutations of S(N) in which each element comes
|
||||
|
||||
|
||||
This is one of the possible permutations for N = 20.
|
||||
- 1, 2, 4, 3, 9, 8, 16, 6, 18, 12.
|
||||
This is not a valid permutation because 12 comes before its divisor 6.
|
||||
- 1, 2, 4, 3, 9, 8, 12, 16, 6, 18.
|
||||
|
||||
- 1, 2, 4, 3, 9, 8, 16, 6, 18, 12.
|
||||
|
||||
This is not a valid permutation because 12 comes before its divisor 6.
|
||||
|
||||
- 1, 2, 4, 3, 9, 8, 12, 16, 6, 18.
|
||||
|
||||
We can verify that F(6) = 5, F(8) = 9, F(20) = 450 and F(1000) ≈ 8.8521816557e21.
|
||||
Find F(1018). Give as your answer its scientific notation rounded to ten digits after the decimal point.
|
||||
|
@ -6,11 +6,13 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
Given a text file of many lines, where fields within a line are delineated by a single <code>$</code> character, write a program that aligns each column of fields by ensuring that words in each column are separated by at least one space. Further, allow for each word in a column to be either left justified, right justified, or center justified within its column.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
Use the following text to test your programs:
|
||||
<pre>
|
||||
Given$a$text$file$of$many$lines
|
||||
@ -130,6 +132,7 @@ const centerAligned = ' Given a text file of many
|
||||
'justified, right justified\n' +
|
||||
' or center justified within its column. ';
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
@ -6,6 +6,7 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
Write a function that takes an array of numbers as parameter and returns the <a href="https://en.wikipedia.org/wiki/Standard Deviation">standard deviation</a> of the series.
|
||||
</section>
|
||||
|
||||
@ -39,6 +40,7 @@ tests:
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
@ -68,4 +70,4 @@ function standardDeviation(arr) {
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
</section>
|
||||
|
@ -6,6 +6,7 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
A <b>CUSIP</b> is a nine-character alphanumeric code that identifies a North American financial security for the purposes of facilitating clearing and settlement of trades. The CUSIP was adopted as an American National Standard under Accredited Standards X9.6.
|
||||
</section>
|
||||
|
||||
@ -45,6 +46,7 @@ tests:
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
@ -88,4 +90,4 @@ function isCusip(s) {
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
</section>
|
||||
|
@ -6,6 +6,7 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
A given rectangle is made from <i>m</i> × <i>n</i> squares. If <i>m</i> and <i>n</i> are not both odd, then it is possible to cut a path through the rectangle along the square edges such that the rectangle splits into two connected pieces with the same shape (after rotating one of the pieces by 180°). All such paths for 2 × 2 and 4 × 3 rectangles are shown below.
|
||||
<div style="width: 100%; text-align: center;">
|
||||
<svg xmlns="https://www.w3.org/2000/svg" xmlns:xlink="https://www.w3.org/1999/xlink" width="520" height="170" aria-hidden="true" alt="Diagram showing the possible paths for 2 by 2 and 4 by 3 rectangles">
|
||||
@ -87,6 +88,7 @@ tests:
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
@ -156,4 +158,4 @@ function cutRectangle(w, h) {
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
</section>
|
||||
|
@ -6,6 +6,7 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
Create a function, to compute the <b><a href="https://en.wikipedia.org/wiki/Dot product">dot product</a></b>, also known as the <b>scalar product</b> of two vectors.
|
||||
</section>
|
||||
|
||||
@ -39,6 +40,7 @@ tests:
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
@ -62,4 +64,4 @@ function dotProduct(ary1, ary2) {
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
</section>
|
||||
|
@ -6,15 +6,17 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
A k-d tree (short for <i>k</i>-dimensional tree) is a space-partitioning data structure for organizing points in a k-dimensional space.
|
||||
k-d trees are a useful data structure for several applications, such as searches involving a multidimensional search key (e.g. range searches and nearest neighbor searches).
|
||||
|
||||
A k-d tree (short for <i>k</i>-dimensional tree) is a space-partitioning data structure for organizing points in a k-dimensional space.
|
||||
k-d trees are a useful data structure for several applications, such as searches involving a multidimensional search key (e.g. range searches and nearest neighbor searches).
|
||||
k-d trees are a special case of binary space partitioning trees. k-d trees are not suitable, however, for efficiently finding the nearest neighbor in high dimensional spaces. As a general rule, if the dimensionality is <i>k</i>, the number of points in the data, <i>N</i>, should be <i>N</i> ≫ 2<sup><i>k</i></sup>.
|
||||
Otherwise, when k-d trees are used with high-dimensional data, most of the points in the tree will be evaluated and the efficiency is no better than exhaustive search, and other methods such as approximate nearest-neighbor are used instead.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Write a function to perform a nearest neighbor search using k-d tree. The function takes two parameters: an array of k-dimensional points, and a single k-dimensional point whose nearest neighbor should be returned by the function. A k-dimensional point will be given as an array of k elements.
|
||||
|
||||
Write a function to perform a nearest neighbour search using k-d tree. The function takes two parameters: an array of k-dimensional points, and a single k-dimensional point whose nearest neighbour should be returned by the function. A k-dimensional point will be given as an array of k elements.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
@ -44,6 +46,7 @@ tests:
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
@ -350,4 +353,4 @@ function kdNN(fpoints, fpoint) {
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
</section>
|
||||
|
@ -6,6 +6,7 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
Given a set of positive integers, write a function to order the integers in such a way that the concatenation of the numbers forms the largest possible integer and return this integer.
|
||||
</section>
|
||||
|
||||
@ -39,6 +40,7 @@ tests:
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
@ -71,4 +73,4 @@ function maxCombine (xs) {
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
</section>
|
||||
|
@ -6,6 +6,7 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
Write a function that returns the date of the last Friday of a given month for a given year.
|
||||
</section>
|
||||
|
||||
@ -45,6 +46,7 @@ tests:
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
@ -73,4 +75,4 @@ function lastFriday (year, month) {
|
||||
};
|
||||
```
|
||||
|
||||
</section>
|
||||
</section>
|
||||
|
@ -6,6 +6,7 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
Determine whether a given year is a leap year in the Gregorian calendar.
|
||||
</section>
|
||||
|
||||
@ -41,6 +42,7 @@ tests:
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
@ -61,4 +63,4 @@ function isLeapYear (year) {
|
||||
};
|
||||
```
|
||||
|
||||
</section>
|
||||
</section>
|
||||
|
@ -6,6 +6,7 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
The least common multiple of 12 and 18 is 36, because 12 is a factor (12 × 3 = 36), and 18 is a factor (18 × 2 = 36), and there is no positive integer less than 36 that has both factors. As a special case, if either <i>m</i> or <i>n</i> is zero, then the least common multiple is zero.
|
||||
One way to calculate the least common multiple is to iterate all the multiples of <i>m</i>, until you find one that is also a multiple of <i>n</i>.
|
||||
If you already have <i>gcd</i> for <a href="https://rosettacode.org/wiki/greatest common divisor" target="_blank">greatest common divisor</a>, then this formula calculates <i>lcm</i>.
|
||||
@ -14,6 +15,7 @@ If you already have <i>gcd</i> for <a href="https://rosettacode.org/wiki/great
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
Compute the least common multiple of an array of intergers.
|
||||
Given <i>m</i> and <i>n</i>, the least common multiple is the smallest positive integer that has both <i>m</i> and <i>n</i> as factors.
|
||||
</section>
|
||||
@ -43,6 +45,7 @@ tests:
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
@ -69,4 +72,4 @@ function LCM (A) {
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
</section>
|
||||
|
@ -6,6 +6,7 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
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.
|
||||
@ -14,6 +15,7 @@ In the morning (after the surreptitious and separate action of each of the five
|
||||
|
||||
## 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.
|
||||
<strong>Note:</strong>
|
||||
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.
|
||||
|
@ -6,6 +6,7 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
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>
|
||||
|
@ -6,6 +6,7 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
Soundex is an algorithm for creating indices for words based on their pronunciation.
|
||||
The goal is for homophones to be encoded to the same representation so that they can be matched despite minor differences in spelling (from <a href="https://en.wikipedia.org/wiki/soundex" target="_blank">the WP article</a>).
|
||||
There is a major issue in many of the implementations concerning the separation of two consonants that have the same soundex code! According to the <a href="https://www.archives.gov/research/census/soundex.html" target="_blank">official Rules</a>. So check for instance if <b>Ashcraft</b> is coded to <b>A-261</b>.
|
||||
@ -55,6 +56,7 @@ tests:
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
@ -96,4 +98,4 @@ function soundex(s) {
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
</section>
|
||||
|
@ -6,6 +6,7 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
Produce a spiral array.
|
||||
A <i>spiral array</i> is a square arrangement of the first N<sup>2</sup> natural numbers, where the numbers increase sequentially as you go around the edges of the array spiraling inwards.
|
||||
For example, given <b>5</b>, produce this array:
|
||||
@ -44,6 +45,7 @@ tests:
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
@ -78,4 +80,4 @@ function spiralArray(n) {
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
</section>
|
||||
|
@ -6,6 +6,7 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
Split a (character) string into comma (plus a blank) delimited strings based on a change of character (left to right).
|
||||
Blanks should be treated as any other character (except they are problematic to display clearly). The same applies to commas.
|
||||
For instance, the string:
|
||||
@ -48,6 +49,7 @@ tests:
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
@ -97,4 +99,4 @@ function split(str) {
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
</section>
|
||||
|
@ -6,6 +6,7 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
This task is inspired by Mark Nelson's DDJ Column "Wordplay" and one of the weekly puzzle challenges from Will Shortz on NPR Weekend Edition <a href="https://www.npr.org/templates/story/story.php?storyId=9264290" target="_blank">[1]</a> and originally attributed to David Edelheit.
|
||||
The challenge was to take the names of two U.S. States, mix them all together, then rearrange the letters to form the names of two <i>different</i> U.S. States (so that all four state names differ from one another).
|
||||
What states are these?
|
||||
@ -15,6 +16,7 @@ A second challenge in the form of a set of fictitious new states was also presen
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
Write a function to solve the challenge for the given array of names of states. The function should return an array. Each element should be an object in this form: <code>{"from":[],"to":[]}</code>. The "from" array should contain the original names and the "to" array should contain the resultant names.
|
||||
</section>
|
||||
|
||||
@ -37,6 +39,7 @@ tests:
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
@ -98,4 +101,4 @@ function solve(input) {
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
</section>
|
||||
|
@ -6,7 +6,8 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Implement functions to encrypt and decrypt a message using the <a href="https://en.wikipedia.org/wiki/Straddling_checkerboard" target="_blank">straddling checkerboard</a> method. The functions will take a string and an array as parameters. The array has 3 strings representing the 3 rows of the checkerboard. The output will be a series of decimal digits.
|
||||
|
||||
Implement functions to encrypt and decrypt a message using the <a href="https://en.wikipedia.org/wiki/Straddling_checkerboard">straddling checkerboard</a> method. The functions will take a string and an array as parameters. The array has 3 strings representing the 3 rows of the checkerboard. The output will be a series of decimal digits.
|
||||
Numbers should be encrypted by inserting the escape character before each digit, then including the digit unencrypted. This should be reversed for decryption.
|
||||
</section>
|
||||
|
||||
@ -46,6 +47,7 @@ tests:
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
@ -105,4 +107,4 @@ function unstraddle(message, alphabet) {
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
</section>
|
||||
|
@ -6,6 +6,7 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
Write a function that takes multiple sorted arrays of items, and returns one array of sorted items.
|
||||
</section>
|
||||
|
||||
@ -39,6 +40,7 @@ tests:
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
@ -79,4 +81,4 @@ function mergeLists(lists) {
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
</section>
|
||||
|
@ -6,7 +6,9 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
The task is to strip control codes and extended characters from a string.
|
||||
|
||||
The task is to strip control codes and extended characters from a string. The solution should demonstrate how to achieve each of the following results:
|
||||
A string with control codes and extended characters stripped.
|
||||
In ASCII, the control codes have decimal codes 0 through to 31 and 127. On an ASCII based system, if the control codes are stripped, the resultant string would have all of its characters within the range of 32 to 126 decimal on the ASCII table.
|
||||
On a non-ASCII based system, we consider characters that do not have a corresponding glyph on the ASCII table (within the ASCII range of 32 to 126 decimal) to be an extended character for the purpose of this task.
|
||||
</section>
|
||||
@ -41,6 +43,7 @@ tests:
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
@ -65,4 +68,4 @@ function strip(s) {
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
</section>
|
||||
|
@ -6,6 +6,7 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
Write a function to solve a partially filled-in normal 9x9 <a href="https://en.wikipedia.org/wiki/Sudoku" target="_blank">Sudoku</a> grid and return the result. The blank fields are represented by 0s.
|
||||
<a href="https://en.wikipedia.org/wiki/Algorithmics_of_sudoku" target="_blank">Algorithmics of Sudoku</a> may help implement this.
|
||||
</section>
|
||||
@ -36,6 +37,7 @@ tests:
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
@ -233,4 +235,4 @@ function solveSudoku(puzzle) {
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
</section>
|
||||
|
@ -6,6 +6,7 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
The objective is to write a function that finds the sum of all positive multiples of 3 or 5 below <i>n</i>.
|
||||
</section>
|
||||
|
||||
@ -39,6 +40,7 @@ tests:
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
@ -63,4 +65,4 @@ function sumMults(n) {
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
</section>
|
||||
|
@ -6,6 +6,7 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
Compute the <b>n</b><sup>th</sup> term of a <a href="https://en.wikipedia.org/wiki/Series (mathematics)" target="_blank">series</a>, i.e. the sum of the <b>n</b> first terms of the corresponding <a href="https://en.wikipedia.org/wiki/sequence" target="_blank">sequence</a>.
|
||||
Informally this value, or its limit when <b>n</b> tends to infinity, is also called the <i>sum of the series</i>, thus the title of this task.
|
||||
For this task, use:
|
||||
@ -18,6 +19,7 @@ and compute $S_{1000}$
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
Write a function that take $a$ and $b$ as parameters and returns the sum of $a^{th}$ to $b^{th}$ members of the sequence.
|
||||
</section>
|
||||
|
||||
@ -46,6 +48,7 @@ tests:
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
@ -71,4 +74,4 @@ function sum(a, b) {
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
</section>
|
||||
|
@ -6,6 +6,7 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
Write a function to find the sum of squares of an array of integers.
|
||||
</section>
|
||||
|
||||
@ -39,6 +40,7 @@ tests:
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
@ -65,4 +67,4 @@ function sumsq(array) {
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
</section>
|
||||
|
@ -6,6 +6,7 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
The <a href="https://en.wikipedia.org/wiki/Sutherland-Hodgman clipping algorithm" target="_blank">Sutherland-Hodgman clipping algorithm</a> finds the polygon that is the intersection between an arbitrary polygon (the "subject polygon") and a convex polygon (the "clip polygon").
|
||||
It is used in computer graphics (especially 2D graphics) to reduce the complexity of a scene being displayed by eliminating parts of a polygon that do not need to be displayed.
|
||||
Take the closed polygon defined by the points:
|
||||
@ -16,6 +17,7 @@ and clip it by the rectangle defined by the points:
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
Write a function that takes 2 arrays as parameters. The first array contains the points of the subject polygon and the second array contains the points of the clipping polygon. The function should return an array containing the points of the clipped polygon. Each number should be rounded to 3 decimal places.
|
||||
</section>
|
||||
|
||||
@ -40,6 +42,7 @@ tests:
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
@ -93,4 +96,4 @@ function clip(subjectPolygon, clipPolygon) {
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
</section>
|
||||
|
@ -6,6 +6,7 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
Given two <a href="https://rosettacode.org/wiki/set" target="_blank">set</a>s <i>A</i> and <i>B</i>, compute $(A \setminus B) \cup (B \setminus A).$
|
||||
That is, enumerate the items that are in <i>A</i> or <i>B</i> but not both. This set is called the <a href="https://en.wikipedia.org/wiki/Symmetric difference" target="_blank">symmetric difference</a> of <i>A</i> and <i>B</i>.
|
||||
In other words: $(A \cup B) \setminus (A \cap B)$ (the set of items that are in at least one of <i>A</i> or <i>B</i> minus the set of items that are in both <i>A</i> and <i>B</i>).
|
||||
@ -13,6 +14,7 @@ In other words: $(A \cup B) \setminus (A \cap B)$ (the set of items that are in
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
Write a function that takes two arrays as parameters and returns the symmetric difference. Sort the resultant array before returning it.
|
||||
</section>
|
||||
|
||||
@ -41,6 +43,7 @@ tests:
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
@ -78,4 +81,4 @@ function symmetricDifference(A, B) {
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
</section>
|
||||
|
@ -6,12 +6,14 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
Given a mapping between items, and items they depend on, a <a href="https://en.wikipedia.org/wiki/Topological sorting" title="wp: Topological sorting" target="_blank">topological sort</a> orders items so that no item precedes an item it depends upon.
|
||||
The compiling of a library in the <a href="https://en.wikipedia.org/wiki/VHDL" title="wp: VHDL" target="_blank">VHDL</a> language has the constraint that a library must be compiled after any library it depends on.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
Write a function that will return a valid compile order of VHDL libraries from their dependencies.
|
||||
<ul>
|
||||
<li>Assume library names are single words.</li>
|
||||
|
@ -6,11 +6,13 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
A vector is defined as having three dimensions as being represented by an ordered collection of three numbers: (X, Y, Z).
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
Write a function that takes two vectors (arrays) as input and computes their cross product. Your function should return <code>null</code> on invalid inputs such as vectors of different lengths.
|
||||
</section>
|
||||
|
||||
|
@ -6,11 +6,13 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
A vector is defined as having three dimensions as being represented by an ordered collection of three numbers: (X, Y, Z).
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
Write a function that takes any numbers of vectors (arrays) as input and computes their dot product. Your function should return <code>null</code> on invalid inputs such as vectors of different lengths.
|
||||
</section>
|
||||
|
||||
|
@ -6,11 +6,13 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
Even today, with proportional fonts and complex layouts, there are still cases where you need to wrap text at a specified column. The basic task is to wrap a paragraph of text in a simple way.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
Write a function that can wrap this text to any number of characters. As an example, the text wrapped to 80 characters should look like the following:
|
||||
<pre>
|
||||
Wrap text using a more sophisticated algorithm such as the Knuth and Plass TeX
|
||||
|
@ -6,12 +6,14 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
In strict <a href="https://en.wikipedia.org/wiki/Functional programming" title="wp: functional programming" target="_blank">functional programming</a> and the <a href="https://en.wikipedia.org/wiki/lambda calculus" title="wp: lambda calculus" target="_blank">lambda calculus</a>, functions (lambda expressions) don't have state and are only allowed to refer to arguments of enclosing functions. This rules out the usual definition of a recursive function wherein a function is associated with the state of a variable and this variable's state is used in the body of the function.
|
||||
The <a href="https://mvanier.livejournal.com/2897.html" target="_blank">Y combinator</a> is itself a stateless function that, when applied to another stateless function, returns a recursive version of the function. The Y combinator is the simplest of the class of such functions, called <a href="https://en.wikipedia.org/wiki/Fixed-point combinator" title="wp: fixed-point combinator" target="_blank">fixed-point combinators</a>.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
Define the stateless Y combinator function and use it to compute <a href="https://en.wikipedia.org/wiki/Factorial" title="wp: factorial" target="_blank">factorial</a>. The <code>factorial(N)</code> function is already given to you.
|
||||
<strong>See also:</strong>
|
||||
<ul>
|
||||
|
@ -6,8 +6,11 @@ challengeType: 5
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
This is an algorithm used to thin a black and white i.e. one bit per pixel images.
|
||||
For example, with an input image of:
|
||||
<!-- TODO write fully in markdown>
|
||||
<!-- markdownlint-disable -->
|
||||
<pre>
|
||||
################# #############
|
||||
################## ################
|
||||
@ -50,17 +53,17 @@ It produces the thinned output:
|
||||
Assume black pixels are one and white pixels zero, and that the input image is a rectangular N by M array of ones and zeroes.
|
||||
The algorithm operates on all black pixels P1 that can have eight neighbours. The neighbours are, in order, arranged as:
|
||||
|
||||
<table border="3">
|
||||
<tr><td style="text-align: center;">P9</td><td style="text-align: center;">P2</td><td style="text-align: center;">P3</td></tr>
|
||||
<tr><td style="text-align: center;">P8</td><td style="text-align: center;"><strong>P1</strong></td><td style="text-align: center;">P4</td></tr>
|
||||
<tr><td style="text-align: center;">P7</td><td style="text-align: center;">P6</td><td style="text-align: center;">P5</td></tr>
|
||||
<table border="3">
|
||||
<tr><td style="text-align: center;">P9</td><td style="text-align: center;">P2</td><td style="text-align: center;">P3</td></tr>
|
||||
<tr><td style="text-align: center;">P8</td><td style="text-align: center;"><strong>P1</strong></td><td style="text-align: center;">P4</td></tr>
|
||||
<tr><td style="text-align: center;">P7</td><td style="text-align: center;">P6</td><td style="text-align: center;">P5</td></tr>
|
||||
</table>
|
||||
|
||||
Obviously the boundary pixels of the image cannot have the full eight neighbours.
|
||||
<ul>
|
||||
<li>Define $A(P1)$ = the number of transitions from white to black, (0 -> 1) in the sequence P2, P3, P4, P5, P6, P7, P8, P9, P2. (Note the extra P2 at the end - it is circular).</li>
|
||||
<li>Define $B(P1)$ = the number of black pixel neighbours of P1. ( = sum(P2 .. P9) )</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>Define $A(P1)$ = the number of transitions from white to black, (0 -> 1) in the sequence P2, P3, P4, P5, P6, P7, P8, P9, P2. (Note the extra P2 at the end - it is circular).</li>
|
||||
<li>Define $B(P1)$ = the number of black pixel neighbours of P1. ( = sum(P2 .. P9) )</li>
|
||||
</ul>
|
||||
|
||||
<h3>Step 1:</h3>
|
||||
All pixels are tested and pixels satisfying all the following conditions (simultaneously) are just noted at this stage.
|
||||
|
Loading…
x
Reference in New Issue
Block a user