mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-01-10 09:05:55 -05:00
fix(curriculum): make Rosetta Farey solution/tests match definition (#49167)
* fix - make farey solution/tests match definition * Update curriculum/challenges/english/10-coding-interview-prep/rosetta-code/farey-sequence.md Co-authored-by: Krzysztof G. <60067306+gikf@users.noreply.github.com> * fix typo --------- Co-authored-by: Krzysztof G. <60067306+gikf@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
71c4a55bf4
commit
1ae24311ce
@@ -47,32 +47,33 @@ assert(typeof farey === 'function');
|
||||
assert(Array.isArray(farey(3)));
|
||||
```
|
||||
|
||||
`farey(3)` should return `["1/3","1/2","2/3"]`
|
||||
`farey(3)` should return `['0/1','1/3','1/2','2/3','1/1']`
|
||||
|
||||
```js
|
||||
assert.deepEqual(farey(3), ['1/3', '1/2', '2/3']);
|
||||
assert.deepEqual(farey(3),['0/1', '1/3', '1/2', '2/3', '1/1']);
|
||||
```
|
||||
|
||||
`farey(4)` should return `["1/4","1/3","1/2","2/4","2/3","3/4"]`
|
||||
`farey(4)` should return `['0/1','1/4','1/3','1/2','2/3','3/4','1/1']`
|
||||
|
||||
```js
|
||||
assert.deepEqual(farey(4), ['1/4', '1/3', '1/2', '2/4', '2/3', '3/4']);
|
||||
assert.deepEqual(farey(4), ['0/1', '1/4', '1/3', '1/2', '2/3', '3/4', '1/1']);
|
||||
```
|
||||
|
||||
`farey(5)` should return `["1/5","1/4","1/3","2/5","1/2","2/4","3/5","2/3","3/4","4/5"]`
|
||||
`farey(5)` should return `['0/1','1/5','1/4','1/3','2/5','1/2','3/5','2/3','3/4','4/5','1/1']`
|
||||
|
||||
```js
|
||||
assert.deepEqual(farey(5), [
|
||||
'0/1',
|
||||
'1/5',
|
||||
'1/4',
|
||||
'1/3',
|
||||
'2/5',
|
||||
'1/2',
|
||||
'2/4',
|
||||
'3/5',
|
||||
'2/3',
|
||||
'3/4',
|
||||
'4/5'
|
||||
'4/5',
|
||||
'1/1'
|
||||
]);
|
||||
```
|
||||
|
||||
@@ -89,21 +90,17 @@ function farey(n) {
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function farey(n){
|
||||
let farSeq=[];
|
||||
for(let den = 1; den <= n; den++){
|
||||
for(let num = 1; num < den; num++){
|
||||
farSeq.push({
|
||||
str:num+"/"+den,
|
||||
val:num/den});
|
||||
}
|
||||
function farey(n) {
|
||||
const sequence = [{ string: "0/1", float: 0.0 }];
|
||||
for (let i = 1; i < n; i++) {
|
||||
for (let j = n; j >= i; j--) {
|
||||
if (i === 1 || j % i > 0) {
|
||||
sequence.push({ string: `${i}/${j}`, float: i / j });
|
||||
}
|
||||
}
|
||||
farSeq.sort(function(a,b){
|
||||
return a.val-b.val;
|
||||
});
|
||||
farSeq=farSeq.map(function(a){
|
||||
return a.str;
|
||||
});
|
||||
return farSeq;
|
||||
}
|
||||
return sequence
|
||||
.sort((a, b) => a.float - b.float)
|
||||
.map(e => e.string)
|
||||
}
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user