From 1478f4c1ed40f20beb8a7250b1d14344726cc0c7 Mon Sep 17 00:00:00 2001 From: Bhanu Pratap Singh Rathore Date: Fri, 1 Jun 2018 19:00:40 +0530 Subject: [PATCH] feat(interview-prep): Fractran (#17328) --- .../rosetta-code.json | 79 ++++++++++++++++++- 1 file changed, 78 insertions(+), 1 deletion(-) diff --git a/seed/challenges/08-coding-interview-prep/rosetta-code.json b/seed/challenges/08-coding-interview-prep/rosetta-code.json index 42e310a224..a559187025 100644 --- a/seed/challenges/08-coding-interview-prep/rosetta-code.json +++ b/seed/challenges/08-coding-interview-prep/rosetta-code.json @@ -3588,6 +3588,83 @@ } } }, + { + "title": "Fractran", + "description": [ + "

FRACTRAN is a Turing-complete esoteric programming language invented by the mathematician John Horton Conway.


A FRACTRAN program is an ordered list of positive fractions $P = (f_1, f_2, \\ldots, f_m)$, together with an initial positive integer input $n$.

", + "

The program is run by updating the integer $n$ as follows:


", + "
", + "

Conway gave a program for primes in FRACTRAN:


$17/91$, $78/85$, $19/51$, $23/38$, $29/33$, $77/29$, $95/23$, $77/19$, $1/17$, $11/13$, $13/11$, $15/14$, $15/2$, $55/1$


Starting with $n=2$, this FRACTRAN program will change $n$ to $15=2\\times (15/2)$, then $825=15\\times (55/1)$, generating the following sequence of integers:


$2$, $15$, $825$, $725$, $1925$, $2275$, $425$, $390$, $330$, $290$, $770$, $\\ldots$


After 2, this sequence contains the following powers of 2:


$2^2=4$, $2^3=8$, $2^5=32$, $2^7=128$, $2^{11}=2048$, $2^{13}=8192$, $2^{17}=131072$, $2^{19}=524288$, $\\ldots$


which are the prime powers of 2.

", + "
Task:
", + "

Write a function that takes a fractran program as a string parameter and returns the first 10 numbers of the program as an array. If the result does not have 10 numbers then return the numbers as is.

" + ], + "solutions": [ + "function fractran(progStr){\n var num = new Array();\n var den = new Array();\n var val ;\n var out=\"\";\n function compile(prog){\n var regex = /\\s*(\\d*)\\s*\\/\\s*(\\d*)\\s*(.*)/m;\n while(regex.test(prog)){\n num.push(regex.exec(prog)[1]);\n den.push(regex.exec(prog)[2]);\n prog = regex.exec(prog)[3];\n }\n }\n\n function step(val){\n var i=0;\n while(ifractran should be a function.", + "testString": "assert(typeof fractran=='function','fractran should be a function.');" + }, + { + "text": "fractran(\"'+tests[0]+'\") should return an array.", + "testString": "assert(Array.isArray(fractran(tests[0])),'fractran(\"'+tests[0]+'\") should return an array.');" + }, + { + "text": "fractran(\"'+tests[0]+'\") should return '+JSON.stringify(results[0])+'.", + "testString": "assert.deepEqual(fractran(tests[0]),results[0],'fractran(\"'+tests[0]+'\") should return '+JSON.stringify(results[0])+'.');" + }, + { + "text": "fractran(\"'+tests[1]+'\") should return '+JSON.stringify(results[1])+'.", + "testString": "assert.deepEqual(fractran(tests[1]),results[1],'fractran(\"'+tests[1]+'\") should return '+JSON.stringify(results[1])+'.');" + }, + { + "text": "fractran(\"'+tests[2]+'\") should return '+JSON.stringify(results[2])+'.", + "testString": "assert.deepEqual(fractran(tests[2]),results[2],'fractran(\"'+tests[2]+'\") should return '+JSON.stringify(results[2])+'.');" + }, + { + "text": "fractran(\"'+tests[3]+'\") should return '+JSON.stringify(results[3])+'.", + "testString": "assert.deepEqual(fractran(tests[3]),results[3],'fractran(\"'+tests[3]+'\") should return '+JSON.stringify(results[3])+'.');" + }, + { + "text": "fractran(\"'+tests[4]+'\") should return '+JSON.stringify(results[4])+'.", + "testString": "assert.deepEqual(fractran(tests[4]),results[4],'fractran(\"'+tests[4]+'\") should return '+JSON.stringify(results[4])+'.');" + } + ], + "id": "5a7dad05be01840e1778a0d1", + "challengeType": "3", + "releasedOn": "May 31, 2018", + "files": { + "indexjs": { + "key": "indexjs", + "ext": "js", + "name": "index", + "contents": [ + "function fractran (progStr) {", + " // Good luck!", + "}" + ], + "head": [], + "tail": [ + "let tests=[", + " '3/2,1/3',", + " '3/2,5/3,1/5',", + " '3/2,6/3',", + " '2/7,7/2',", + " '17/91 78/85 19/51 23/38 29/33 77/29 95/23 77/19 1/17 11/13 13/11 15/14 15/2 55/1'", + "]", + "let results=[", + " [ 2, 3, 1 ],", + " [ 2, 3, 5, 1 ],", + " [ 2, 3, 6, 9, 18, 27, 54, 81, 162, 243 ],", + " [ 2, 7, 2, 7, 2, 7, 2, 7, 2, 7 ],", + " [ 2, 15, 825, 725, 1925, 2275, 425, 390, 330, 290 ]", + "]" + ] + } + } + }, { "title": "Hailstone sequence", "description": [ @@ -5657,4 +5734,4 @@ } } ] -} \ No newline at end of file +}