Skip to content

Commit 0b0b774

Browse files
authoredMay 27, 2017
Merge pull request freeCodeCamp#19 from Ethan-Arrowood/master
Generate challenges.json
2 parents ecf3895 + 35b44eb commit 0b0b774

33 files changed

+126
-48
lines changed
 

Diff for: ‎challenges.json

+1-29
Original file line numberDiff line numberDiff line change
@@ -1,29 +1 @@
1-
{
2-
"name": "Python Beginner Challenges",
3-
"challenges": [
4-
{
5-
"id": "a",
6-
"title": "Addition",
7-
"repl": "https://repl.it/student_embed/assignment/136138/81542db8f9c1157a5a8741c702aa95bc",
8-
"completed": false
9-
},
10-
{
11-
"id": "b",
12-
"title": "Subtraction",
13-
"repl": "https://repl.it/student_embed/assignment/136139/36ee9cc212ca5a2fe77d3acfb4748a1b",
14-
"completed": false
15-
},
16-
{
17-
"id": "c",
18-
"title": "Dummy Lesson",
19-
"repl": "https://repl.it/student_embed/assignment/136137/d860e0ad34862a709895f54706dcf9af",
20-
"completed": false
21-
},
22-
{
23-
"id": "d",
24-
"title": "Really Long Title with tons of white space in it",
25-
"repl": "https://repl.it/student_embed/assignment/106433/cd0e574d0bd1c6e03110fe501ace41be",
26-
"completed": false
27-
}
28-
]
29-
}
1+
{"name":"Python Beginner Challenges","last_edit":1495857853978,"challenges":[[],[],[],[],[{"id":"592893767a194ee412ae2e1f","title":"Addition Edit","repl":"https://repl.it/student_embed/assignment/136137/d860e0ad34862a709895f54706dcf9af","completed":false,"chapter":4,"lesson":1},{"id":"592895b87a194ee412ae2e20","title":"Subtraction","repl":"https://repl.it/student_embed/assignment/136137/d860e0ad34862a709895f54706dcf9af","completed":false,"chapter":4,"lesson":2},{"id":"592895ef7a194ee412ae2e21","title":"Multiplication","repl":"https://repl.it/student_embed/assignment/136137/d860e0ad34862a709895f54706dcf9af","completed":false,"chapter":4,"lesson":3},{"id":"5928961e7a194ee412ae2e22","title":"Float Division","repl":"https://repl.it/student_embed/assignment/136137/d860e0ad34862a709895f54706dcf9af","completed":false,"chapter":4,"lesson":4},{"id":"592896397a194ee412ae2e23","title":"Integer Division","repl":"https://repl.it/student_embed/assignment/136137/d860e0ad34862a709895f54706dcf9af","completed":false,"chapter":4,"lesson":5},{"id":"5928965a7a194ee412ae2e24","title":"Exponents","repl":"https://repl.it/student_embed/assignment/136137/d860e0ad34862a709895f54706dcf9af","completed":false,"chapter":4,"lesson":6},{"id":"592896717a194ee412ae2e25","title":"Remainder","repl":"https://repl.it/student_embed/assignment/136137/d860e0ad34862a709895f54706dcf9af","completed":false,"chapter":4,"lesson":7},{"id":"592898c97a194ee412ae2e26","title":"Divmod","repl":"https://repl.it/student_embed/assignment/136137/d860e0ad34862a709895f54706dcf9af","completed":false,"chapter":4,"lesson":8},{"id":"592898dc7a194ee412ae2e27","title":"Square Root","repl":"https://repl.it/student_embed/assignment/136137/d860e0ad34862a709895f54706dcf9af","completed":false,"chapter":4,"lesson":9},{"id":"592899f67a194ee412ae2e28","title":"Sum","repl":"https://repl.it/student_embed/assignment/136137/d860e0ad34862a709895f54706dcf9af","completed":false,"chapter":4,"lesson":10},{"id":"59289a1a7a194ee412ae2e29","title":"Rounding","repl":"https://repl.it/student_embed/assignment/136137/d860e0ad34862a709895f54706dcf9af","completed":false,"chapter":4,"lesson":11},{"id":"59289a2f7a194ee412ae2e2a","title":"Absolute Value","repl":"https://repl.it/student_embed/assignment/136137/d860e0ad34862a709895f54706dcf9af","completed":false,"chapter":4,"lesson":12},{"id":"59289a477a194ee412ae2e2b","title":"Min Value","repl":"https://repl.it/student_embed/assignment/136137/d860e0ad34862a709895f54706dcf9af","completed":false,"chapter":4,"lesson":13},{"id":"59289a5a7a194ee412ae2e2c","title":"Max Value","repl":"https://repl.it/student_embed/assignment/136137/d860e0ad34862a709895f54706dcf9af","completed":false,"chapter":4,"lesson":14}],[]]}

Diff for: ‎challenges/4.1.Addition/lesson_settings.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
2-
"lesson_title": "Addition",
2+
"lesson_title": "Addition Edit",
33
"chapter_number": 4,
44
"lesson_number": 1,
5-
"id":"592893767a194ee412ae2e1f"
5+
"id":"592893767a194ee412ae2e1f",
6+
"repl": ""
67
}

Diff for: ‎challenges/4.2.Subtraction/lesson_settings.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"lesson_title": "Subtraction",
33
"chapter_number": 4,
44
"lesson_number": 2,
5-
"id":"592895b87a194ee412ae2e20"
5+
"id":"592895b87a194ee412ae2e20",
6+
"repl": ""
67
}

Diff for: ‎challenges/4.3.Multiplication/lesson_settings.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"lesson_title": "Multiplication",
33
"chapter_number": 4,
44
"lesson_number": 3,
5-
"id":"592895ef7a194ee412ae2e21"
5+
"id":"592895ef7a194ee412ae2e21",
6+
"repl": ""
67
}

Diff for: ‎challenges/4.4.Float_Division/lesson_settings.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"lesson_title": "Float Division",
33
"chapter_number": 4,
44
"lesson_number": 4,
5-
"id":"5928961e7a194ee412ae2e22"
5+
"id":"5928961e7a194ee412ae2e22",
6+
"repl": ""
67
}

Diff for: ‎challenges/4.5.Integer_Division/lesson_settings.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"lesson_title": "Integer Division",
33
"chapter_number": 4,
44
"lesson_number": 5,
5-
"id":"592896397a194ee412ae2e23"
5+
"id":"592896397a194ee412ae2e23",
6+
"repl": ""
67
}

Diff for: ‎challenges/4.6.Exponents/lesson_settings.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"lesson_title": "Exponents",
33
"chapter_number": 4,
44
"lesson_number": 6,
5-
"id":"5928965a7a194ee412ae2e24"
5+
"id":"5928965a7a194ee412ae2e24",
6+
"repl": ""
67
}

Diff for: ‎challenges/4.7.Remainder/lesson_settings.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"lesson_title": "Remainder",
33
"chapter_number": 4,
44
"lesson_number": 7,
5-
"id":"592896717a194ee412ae2e25"
5+
"id":"592896717a194ee412ae2e25",
6+
"repl": ""
67
}

Diff for: ‎challenges/4.8.Divmod/lesson_settings.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"lesson_title": "Divmod",
33
"chapter_number": 4,
44
"lesson_number": 8,
5-
"id":"592898c97a194ee412ae2e26"
5+
"id":"592898c97a194ee412ae2e26",
6+
"repl": ""
67
}

Diff for: ‎challenges/4.9.Square_Root/lesson_settings.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"lesson_title": "Square Root",
33
"chapter_number": 4,
44
"lesson_number": 9,
5-
"id":"592898dc7a194ee412ae2e27"
5+
"id":"592898dc7a194ee412ae2e27",
6+
"repl": ""
67
}
File renamed without changes.
File renamed without changes.

Diff for: ‎challenges/4.10.Sum/lesson_settings.json renamed to ‎challenges/4.A.Sum/lesson_settings.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"lesson_title": "Sum",
33
"chapter_number": 4,
44
"lesson_number": 10,
5-
"id":"592899f67a194ee412ae2e28"
5+
"id":"592899f67a194ee412ae2e28",
6+
"repl": ""
67
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

Diff for: ‎challenges/4.11.Rounding/lesson_settings.json renamed to ‎challenges/4.B.Rounding/lesson_settings.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"lesson_title": "Rounding",
33
"chapter_number": 4,
44
"lesson_number": 11,
5-
"id":"59289a1a7a194ee412ae2e29"
5+
"id":"59289a1a7a194ee412ae2e29",
6+
"repl": ""
67
}
File renamed without changes.
File renamed without changes.

Diff for: ‎challenges/4.12.Absolute_Value/lesson_settings.json renamed to ‎challenges/4.C.Absolute_Value/lesson_settings.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"lesson_title": "Absolute Value",
33
"chapter_number": 4,
44
"lesson_number": 12,
5-
"id":"59289a2f7a194ee412ae2e2a"
5+
"id":"59289a2f7a194ee412ae2e2a",
6+
"repl": ""
67
}
File renamed without changes.
File renamed without changes.

Diff for: ‎challenges/4.13.Min_Value/lesson_settings.json renamed to ‎challenges/4.D.Min_Value/lesson_settings.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"lesson_title": "Min Value",
33
"chapter_number": 4,
44
"lesson_number": 13,
5-
"id":"59289a477a194ee412ae2e2b"
5+
"id":"59289a477a194ee412ae2e2b",
6+
"repl": ""
67
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

Diff for: ‎challenges/4.14.Max_Value/lesson_settings.json renamed to ‎challenges/4.E.Max_Value/lesson_settings.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"lesson_title": "Max Value",
33
"chapter_number": 4,
44
"lesson_number": 14,
5-
"id":"59289a5a7a194ee412ae2e2c"
5+
"id":"59289a5a7a194ee412ae2e2c",
6+
"repl": ""
67
}
File renamed without changes.

Diff for: ‎generate-challenge-settings.js

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
var fs = require('file-system');
2+
3+
function printList(list) {
4+
list.forEach((v) => console.log(v));
5+
}
6+
7+
let dir_list = fs.readdirSync('challenges');
8+
9+
//keep directories only
10+
dir_list = dir_list.filter((val) => {
11+
return fs.statSync(`challenges/${val}`).isDirectory();
12+
});
13+
14+
// get the current challenge object, parse it, save the challenge list
15+
const challenges_string = fs.readFileSync('./challenges.json', 'utf-8');
16+
let challenges_json = JSON.parse(challenges_string);
17+
18+
let challenges_list = challenges_json.challenges;
19+
20+
// create a dictionary where challenges ids are keys
21+
// and their index in the challenges_list is the val
22+
let challenges_id_list = [];
23+
challenges_list.forEach((chapter) => {
24+
chapter.forEach((c) => {
25+
challenges_id_list.push(c.id);
26+
});
27+
});
28+
29+
// loop through dir_list
30+
dir_list.forEach((dir) => {
31+
const path = `challenges/${dir}/lesson_settings.json`;
32+
let data = fs.readFileSync(path, 'utf-8');
33+
34+
if (data.length === 0) return null;
35+
36+
const challenge = JSON.parse(data);
37+
const {
38+
id,
39+
lesson_title,
40+
repl,
41+
chapter_number,
42+
lesson_number
43+
} = challenge;
44+
45+
const default_repl = "https://repl.it/student_embed/assignment/136137/d860e0ad34862a709895f54706dcf9af";
46+
47+
const lesson_obj = {
48+
id,
49+
"title": lesson_title,
50+
"repl": repl.length === 0 ? default_repl : repl,
51+
"completed": false,
52+
"chapter": chapter_number,
53+
"lesson": lesson_number
54+
};
55+
56+
if (!challenges_id_list.includes(id)) {
57+
let chapter = challenges_list[chapter_number];
58+
let a = chapter.slice(0, lesson_number);
59+
let b = chapter.slice(lesson_number + 1, chapter.length);
60+
a.push(lesson_obj);
61+
challenges_list[chapter_number] = a.concat(b);
62+
challenges_id_list.push(id);
63+
} else {
64+
challenges_list[chapter_number][lesson_number - 1] = lesson_obj;
65+
}
66+
});
67+
challenges_json.challenges = challenges_list;
68+
challenges_json.last_edit = Date.now();
69+
fs.writeFile('./challenges.json', JSON.stringify(challenges_json), (err) => {
70+
if (err) throw err;
71+
console.log('The file has been saved!');
72+
printList(challenges_list);
73+
});

Diff for: ‎load-challenges.js

+19-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
/*global $*/
2+
function flatten(list) {
3+
return list.reduce((a, b) => a.concat(b), []);
4+
}
25
function getChallengeList() {
36
return JSON.parse(localStorage.getItem('fcc-python-challenges'));
47
}
@@ -110,14 +113,27 @@ function loadChallengeFromMap(id) {
110113
}
111114
}
112115
}
113-
116+
function checkChallengeAuthenticity() {
117+
const date = getChallengeList().last_edit;
118+
let isAuthentic = false;
119+
120+
$.getJSON("./challenges.json")
121+
.done(({last_edit}) => {
122+
isAuthentic = date === last_edit;
123+
})
124+
.fail((jqxhr, textStatus, error ) => {
125+
console.log(`Req failed: ${textStatus}, ${error}`);
126+
});
127+
128+
return isAuthentic;
129+
}
114130
(() => {
115-
if ( localStorage.getItem('fcc-python-challenges') ) {
131+
if ( localStorage.getItem('fcc-python-challenges') && checkChallengeAuthenticity()) {
116132
startLoadChallenge();
117133
} else {
118134
$.getJSON("./challenges.json")
119135
.done(({challenges}) => {
120-
setChallengeList(challenges);
136+
setChallengeList(flatten(challenges));
121137
startLoadChallenge();
122138
})
123139
.fail((jqxhr, textStatus, error ) => {

Diff for: ‎package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,8 @@
2222
"bugs": {
2323
"url": "https://github.com/FreeCodeCamp/python-coding-challenges/issues"
2424
},
25-
"homepage": "https://github.com/FreeCodeCamp/python-coding-challenges#readme"
25+
"homepage": "https://github.com/FreeCodeCamp/python-coding-challenges#readme",
26+
"dependencies": {
27+
"file-system": "^2.2.2"
28+
}
2629
}

0 commit comments

Comments
 (0)
Please sign in to comment.