Feat: add new Markdown parser (#39800)
and change all the challenges to new `md` format.
This commit is contained in:
committed by
GitHub
parent
a07f84c8ec
commit
0bd52f8bd1
@ -5,16 +5,22 @@ challengeType: 5
|
||||
forumTopicId: 302328
|
||||
---
|
||||
|
||||
## Description
|
||||
# --description--
|
||||
|
||||
[Subleq](https://rosettacode.org/wiki/eso:Subleq) is an example of a [One-Instruction Set Computer (OISC)](https://en.wikipedia.org/wiki/One_instruction_set_computer).
|
||||
|
||||
It is named after its only instruction, which is **SU**btract and **B**ranch if **L**ess than or **EQ**ual
|
||||
|
||||
<section id='description'>
|
||||
<a href="https://rosettacode.org/wiki/eso:Subleq" target="_blank">Subleq</a> is an example of a <a href="https://en.wikipedia.org/wiki/One_instruction_set_computer" target="_blank">One-Instruction Set Computer (OISC)</a>.
|
||||
It is named after its only instruction, which is <b>SU</b>btract and <b>B</b>ranch if <b>L</b>ess than or <b>EQ</b>ual
|
||||
to zero.
|
||||
|
||||
Your task is to create an interpreter which emulates such a machine.
|
||||
|
||||
The machine's memory consists of an array of signed integers. Any reasonable word size is fine, but the memory must be
|
||||
|
||||
able to hold negative as well as positive numbers.
|
||||
|
||||
Execution begins with the instruction pointer aimed at the first word, which is address 0. It proceeds as follows:
|
||||
|
||||
<ol>
|
||||
<li>Let A, B, and C be the value stored in the three consecutive words in memory starting at the instruction pointer.</li>
|
||||
<li>Advance the instruction pointer 3 words to point at the address after the one containing C.</li>
|
||||
@ -27,17 +33,26 @@ Execution begins with the instruction pointer aimed at the first word, which is
|
||||
the result is zero or negative, the value C becomes the new instruction pointer.</li>
|
||||
<li>If the instruction pointer becomes negative, execution halts.</li>
|
||||
</ol>
|
||||
|
||||
Other negative addresses besides -1 may be treated as equivalent to -1, or generate an error, as you see fit.
|
||||
|
||||
Your solution should accept a program to execute on the machine, separately from the input fed to the program itself.
|
||||
|
||||
This program should be in raw subleq "machine code" - whitespace-separated decimal numbers, with no symbolic names or
|
||||
|
||||
other assembly-level extensions, to be loaded into memory starting at address 0. Show the output of your solution when
|
||||
|
||||
fed this "Hello, world!" program. (Note that the example assumes ASCII or a superset of it, such as any of the Latin-N
|
||||
|
||||
character sets or Unicode. You may translate it into another character set if your implementation is on a
|
||||
|
||||
non-ASCiI-compatible environment.)
|
||||
|
||||
<pre>15 17 -1 17 -1 -1 16 1 -1 16 3 -1 15 15 0 0 -1 72 101 108 108 111 44 32 119 111 114 108 100 33 10 0</pre>
|
||||
|
||||
Which corresponds to something like this in a hypothetical assembler language:
|
||||
<pre>
|
||||
start:
|
||||
|
||||
<pre>start:
|
||||
zero, message, -1
|
||||
message, -1, -1
|
||||
neg1, start+1, -1
|
||||
@ -47,35 +62,105 @@ zero: 0
|
||||
neg1: -1
|
||||
message: "Hello, world!\n\0"
|
||||
</pre>
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
# --instructions--
|
||||
|
||||
<section id='instructions'>
|
||||
Write a function that takes an array of integers as a parameter. This represents the memory elements. The function
|
||||
|
||||
should interpret the sequence and return the output string. For this task, assume that there is no standard input.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
# --hints--
|
||||
|
||||
<section id='tests'>
|
||||
`Subleq` should be a function.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>Subleq</code> should be a function.
|
||||
testString: assert(typeof Subleq == 'function');
|
||||
- text: <code>Subleq([15, 17, -1, 17, -1, -1, 16, 1, -1, 16, 3, -1, 15, 15, 0, 0, -1, 72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33, 0])</code> should return a string.
|
||||
testString: assert(typeof Subleq([15, 17, -1, 17, -1, -1, 16, 1, -1, 16, 3, -1, 15, 15, 0, 0, -1, 72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33, 0]) == 'string');
|
||||
- text: <code>Subleq([15, 17, -1, 17, -1, -1, 16, 1, -1, 16, 3, -1, 15, 15, 0, 0, -1, 72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33, 0])</code> should return <code>"Hello, world!"</code>.
|
||||
testString: assert.equal(Subleq([15, 17, -1, 17, -1, -1, 16, 1, -1, 16, 3, -1, 15, 15, 0, 0, -1, 72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33, 0]), "Hello, world!");
|
||||
```js
|
||||
assert(typeof Subleq == 'function');
|
||||
```
|
||||
|
||||
</section>
|
||||
`Subleq([15, 17, -1, 17, -1, -1, 16, 1, -1, 16, 3, -1, 15, 15, 0, 0, -1, 72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33, 0])` should return a string.
|
||||
|
||||
## Challenge Seed
|
||||
```js
|
||||
assert(
|
||||
typeof Subleq([
|
||||
15,
|
||||
17,
|
||||
-1,
|
||||
17,
|
||||
-1,
|
||||
-1,
|
||||
16,
|
||||
1,
|
||||
-1,
|
||||
16,
|
||||
3,
|
||||
-1,
|
||||
15,
|
||||
15,
|
||||
0,
|
||||
0,
|
||||
-1,
|
||||
72,
|
||||
101,
|
||||
108,
|
||||
108,
|
||||
111,
|
||||
44,
|
||||
32,
|
||||
119,
|
||||
111,
|
||||
114,
|
||||
108,
|
||||
100,
|
||||
33,
|
||||
0
|
||||
]) == 'string'
|
||||
);
|
||||
```
|
||||
|
||||
<section id='challengeSeed'>
|
||||
<div id='js-seed'>
|
||||
`Subleq([15, 17, -1, 17, -1, -1, 16, 1, -1, 16, 3, -1, 15, 15, 0, 0, -1, 72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33, 0])` should return `"Hello, world!"`.
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
Subleq([
|
||||
15,
|
||||
17,
|
||||
-1,
|
||||
17,
|
||||
-1,
|
||||
-1,
|
||||
16,
|
||||
1,
|
||||
-1,
|
||||
16,
|
||||
3,
|
||||
-1,
|
||||
15,
|
||||
15,
|
||||
0,
|
||||
0,
|
||||
-1,
|
||||
72,
|
||||
101,
|
||||
108,
|
||||
108,
|
||||
111,
|
||||
44,
|
||||
32,
|
||||
119,
|
||||
111,
|
||||
114,
|
||||
108,
|
||||
100,
|
||||
33,
|
||||
0
|
||||
]),
|
||||
'Hello, world!'
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function Subleq(mem) {
|
||||
@ -83,12 +168,7 @@ function Subleq(mem) {
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
|
||||
<section id='solution'>
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function Subleq(mem) {
|
||||
@ -113,5 +193,3 @@ function Subleq(mem) {
|
||||
return out;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
Reference in New Issue
Block a user