forked from rebordao/project-euler
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build_headers.R
99 lines (74 loc) · 2.64 KB
/
build_headers.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# This script writes the header of a typical R solution.
library(XML)
library(RCurl)
library(data.table)
library(dplyr)
#### User-defined problems ####
problems <- 11:11
language <- 'scala' # needs to be either R, python, julia or scala
#### Aux functions ####
# Fetches an url as parses its text/tree to extract the relevant info
get_info <- function (url) {
# Fetches text/tree
tree <- htmlTreeParse(readLines(url, warn = F), useInternalNodes = T)
# Parses text/tree
problem <- xpathSApply(tree, "//div[@id='problem_info']/h3", xmlValue)
title <- xpathSApply(tree, "//div[@id='content']/h2", xmlValue)
body <- xpathSApply(tree, "//div[@class='problem_content']", xmlValue)
list(problem = problem, title = title, body = gsub('\n', '', body))
}
# Creates the header based on the output of get_info()
create_header <- function(hd) {
# Defines comment symbol for the intended language
csym <- '#'
if (language == 'scala') {
csym <- '//'
}
# Builds body
url <- sprintf(
'https://projecteuler.net/problem=%s', strsplit(hd$problem, ' ')[[1]][2])
# Enforces body to be less than 80 chars and adds heading #s (comment setup)
body <- paste0(csym, ' ', strwrap(hd$body, width = 75), '\n', collapse = '')
sprintf("%s %s | %s\n%s\n%s %s\n%s\n%s%s",
csym, hd$problem, hd$title, csym, csym, hd$title, csym, body, csym, csym)
}
# Creates files with headers for the intended language (R, python, julia, scala)
create_file <- function(header = header, language = language) {
# Language needs to be supported
stopifnot(language %in% c('R', 'python', 'julia', 'scala'))
# Creates dir if it doesn't exist
if (!dir.exists(language)) {
dir.create(language)
}
file_extension <- switch(
language,
'R' = '.R',
'python' = '.py',
'julia' = '.jl',
'scala' = '.sc')
# Parses the problem number
str_splited <- unlist(strsplit(header, ' '))
problem_nr <- str_splited[which(str_splited == '|') - 1]
# Creates file with header (if file doesn't exist)
probl_nr_prefix <- switch(
language,
'R' = 'p',
'python' = '',
'scala' = 'Problem')
file_name <- paste0(language, '/', probl_nr_prefix, problem_nr, file_extension)
if (!file.exists(file_name)) {
conn <- file(file_name)
writeLines(text = header, con = conn)
close(conn)
}
}
#### Algorithm ####
# Builds urls to scrap
urls <- paste0(
rep('https://projecteuler.net/problem=', length(problems)), problems)
# Scraps urls and gets their info
info <- lapply(urls, get_info)
# Builds headers
headers <- sapply(info, create_header)
# Creates files
sapply(headers, create_file, language = language)