-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_problem.rb
executable file
·91 lines (68 loc) · 2.17 KB
/
setup_problem.rb
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
#!/usr/bin/ruby
# This script downloads a problem statement from projecteuler.net, sets
# up a new folder for the problem, and creates a solution.hs file containing the
# problem statement as a comment.
#
# I wrote this because I was tired of doing the process manually, and wanted to
# play with scraping web pages anyway.
require 'rubygems'
require 'nokogiri'
require 'open-uri'
require 'pathname'
# Represent a problem from Project Euler
class ProjectEulerProblem
def initialize(problem_number)
@problem_number = problem_number
end
# Download the problem webpage and parse out the problem statement
def statement
uri = "https://projecteuler.net/problem=" + @problem_number
page = Nokogiri::HTML(open(uri))
element = page.css('div.problem_content')
statement = ""
element.children.each do |child|
if child.instance_of?(Nokogiri::XML::Element)
# Use double-spacing to represent paragraphs
statement += child.text.strip + "\n\n"
end
end
return statement.strip
end
end
# Utility to wrap text that exeeds a max line length while keeping words intact.
# Also has an option to include a prefix on every post-wrap line.
class TextWrapper
def initialize(wrap, prefix="")
@prefix = prefix
@wrap = wrap
end
def wrap(text)
input_lines = text.split("\n")
output_lines = []
input_lines.each do |input_line|
words = input_line.split
loop do
output_line = @prefix
until words.empty? || output_line.length + words[0].length > @wrap
output_line += words.shift + " "
end
output_lines.push( output_line.strip )
break if words.empty?
end
end
return output_lines.join("\n")
end
end
problem_number = ARGV[0]
problem = ProjectEulerProblem.new(problem_number)
solution_dir = Pathname.new(problem_number)
solution_dir.mkdir
File.open(solution_dir + "solution.hs", "w") do |script|
script.puts "-- Solution to Project Euler problem " + problem_number
script.puts "-- By Trey Thomas"
script.puts "--"
script.puts TextWrapper.new(80, "-- ").wrap(problem.statement)
script.puts ""
script.puts ""
script.close
end