marmalade 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +96 -18
- data/bin/jam +14 -0
- data/examples/reverse-words/B-small-practice.in +6 -0
- data/examples/reverse-words/README.md +4 -0
- data/examples/reverse-words/reverse-words.rb +14 -0
- data/examples/rope-intranet/A-large-practice.in +12614 -0
- data/examples/rope-intranet/A-small-practice.in +43 -0
- data/examples/rope-intranet/README.md +3 -0
- data/examples/rope-intranet/rope-intranet.rb +34 -0
- data/lib/marmalade/project_builder.rb +28 -0
- data/lib/marmalade/puzzle.rb +21 -3
- data/lib/marmalade/version.rb +1 -1
- data/lib/marmalade.rb +16 -4
- data/spec/marmalade/marmalade_spec.rb +7 -0
- data/spec/marmalade/project_builder_spec.rb +47 -0
- data/spec/marmalade/puzzle_spec.rb +59 -16
- data/templates/Rakefile +27 -0
- data/templates/main.rb +14 -0
- data/templates/main_spec.rb +5 -0
- metadata +72 -75
data/README.md
CHANGED
@@ -19,40 +19,118 @@ Install via RubyGems:
|
|
19
19
|
|
20
20
|
$ gem install marmalade
|
21
21
|
|
22
|
-
|
22
|
+
At the moment, the requirements for using it are:
|
23
23
|
|
24
|
-
|
24
|
+
- A *nix environment
|
25
|
+
- Ruby 1.9
|
25
26
|
|
26
|
-
|
27
|
+
## Basic Usage
|
27
28
|
|
28
|
-
|
29
|
+
Once the gem is installed, use the generator to create a project file for you:
|
30
|
+
|
31
|
+
$ jam example
|
29
32
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
33
|
+
This command will create a directory called "example" under the current directory and will create a file in the directory called "example.rb", which is an executable Ruby file set up for using Marmalade to do the dirty work for you. Running this file with the `--help` option lets you know about a few things it can do out of the box:
|
34
|
+
|
35
|
+
$ cd example
|
36
|
+
$ ./example.rb --help
|
37
|
+
Options:
|
38
|
+
--file, -f <s>: Input file to read
|
39
|
+
--debug, -d: Debug mode
|
40
|
+
--step, -s: Step through each case
|
41
|
+
--case, -c <i>: Only run the given case
|
42
|
+
--help, -h: Show this message
|
43
|
+
|
44
|
+
Say we have an input file like the one described above in the introduction. The first line of the file declares how many test cases the file contains. The first number in each test case is called `k` and the second is `n`, which is the number of `lines` to read in for each puzzle. Assuming we have a method called `solve_case` that returns the result we're looking for, we can edit example.rb and instruct Marmalade to read that for us like so:
|
34
45
|
|
35
|
-
Marmalade.jam
|
36
|
-
|
46
|
+
Marmalade.jam do
|
47
|
+
read_num_cases
|
37
48
|
test_cases do
|
38
49
|
read [:k, :n], :type => :int
|
39
50
|
read :lines, :count => @n
|
40
|
-
|
51
|
+
puts solve_case(@k, @lines)
|
52
|
+
end
|
41
53
|
end
|
42
54
|
|
43
|
-
|
55
|
+
Assuming our input file is called `sample.txt`, we can now run the puzzle like this:
|
56
|
+
|
57
|
+
$ ./example -f sample.txt
|
58
|
+
|
59
|
+
Marmalade reads in each line and will assign the values to the instance variables that you specified (in this case, `@k`, `@n` and `@lines`). When the result is ready to be printed, Marmalade overrides `puts` to use the proper output format that Code Jam usually requires, much like:
|
44
60
|
|
45
61
|
Case #1: 12
|
46
62
|
Case #2: 55
|
47
63
|
# and so on...
|
48
64
|
|
49
|
-
|
65
|
+
## Reading the number of test cases
|
66
|
+
|
67
|
+
As shown in the example about the call to `read_num_cases` reads the next line from the file, interpreting it as an integer and assigning it to the `@num_cases` instance variable. It is functionally equivalent to the following call:
|
68
|
+
|
69
|
+
read :num_cases, :type => :int
|
70
|
+
|
71
|
+
`@num_cases` is important because it lets the `test_cases` block know how many cases to run. If `@num_cases` has not been assigned by the time `test_cases` is called, then Marmalade will throw an error.
|
72
|
+
|
73
|
+
## Reading strings
|
74
|
+
|
75
|
+
The `read` call will interpret lines and values as strings by default. So, to read strings you simply want to omit the `:type => :int` declaration on `read`.
|
76
|
+
|
77
|
+
## Splitting lines
|
78
|
+
|
79
|
+
Many times, Code Jam puzzles have lines with multiple values that you want to process as an array. For instance if you have a line like the following:
|
80
|
+
|
81
|
+
A B C D E
|
82
|
+
|
83
|
+
And you want to process that as an array, you can use the `:split` argument to `read` like so:
|
84
|
+
|
85
|
+
read :letters, :split => true
|
86
|
+
|
87
|
+
The result would be an instance variable `@letters` which will be the array `['A', 'B', 'C', 'D', 'E']`. Similarly, if the line were a series of number like this:
|
88
|
+
|
89
|
+
1 2 3 4 5 6 7
|
90
|
+
|
91
|
+
You can combine `:split` with `:type`:
|
92
|
+
|
93
|
+
read :numbers, :split => true, :type => :int
|
94
|
+
|
95
|
+
The `@numbers` instance variable would then contain the array `[1, 2, 3, 4, 5, 6, 7]`.
|
96
|
+
|
97
|
+
Finally, here is how you'd read multiple lines of integer arrays:
|
98
|
+
|
99
|
+
read :matrix, :count => 3, :split => true, :type => :int
|
100
|
+
|
101
|
+
If you had an input file that looks like this:
|
102
|
+
|
103
|
+
1 2 3
|
104
|
+
4 5 6
|
105
|
+
7 8 9
|
106
|
+
|
107
|
+
Then the `@matrix` instance variable will be an array of arrays: `[[1, 2, 3], [4, 5, 6], [7, 8, 9]]`.
|
108
|
+
|
109
|
+
## Debugging
|
110
|
+
|
111
|
+
Marmalade gives you a few ways to help debug as you solve puzzles. Firstly, you can pass `--debug` as a command line argument, which enables debug mode. As you go along, you can use `puts_dbg` to write debug messages to the console only if debug mode is enabled. This comes in handy when you want to do your final run to generate your submitted output file.
|
112
|
+
|
113
|
+
Passing the `--step` command line argument will cause Marmalade to pause for user input between every test case. This is particularly handy when you're validating your solution to a puzzle and want to follow your debug output as the program runs through test cases.
|
114
|
+
|
115
|
+
Sometimes you hit a tricky test case and want to work on just that case. Passing `--case X` will cause it to only process case X and skip all other cases. However, in order to enable this functionality, you must tell Marmalade which code in your test cases is used to process (rather than read) test data. Taking the example from above, we can enable the use of `--case` like this:
|
116
|
+
|
117
|
+
Marmalade.jam do
|
118
|
+
read_num_cases
|
119
|
+
test_cases do
|
120
|
+
read [:k, :n], :type => :int
|
121
|
+
read :lines, :count => @n
|
122
|
+
# run_case tells Marmalade that the following code is test case processing code
|
123
|
+
run_case do
|
124
|
+
puts solve_case(@k, @lines)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
By placing the call to `solve_case` in a `run_case` block, Marmalade knows to skip this code if it's looking for a particular case to run. For example, this command:
|
130
|
+
|
131
|
+
$ ./example.rb -f sample.txt --case 5
|
50
132
|
|
51
|
-
|
52
|
-
_ --case
|
53
|
-
- --step and run_case
|
54
|
-
- reading with :split
|
55
|
-
- reading strings
|
133
|
+
Will run `solve_case` only for test case 5.
|
56
134
|
|
57
135
|
## Contributing
|
58
136
|
|
data/bin/jam
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# TODO: Make this compatible on windows somehow
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'marmalade'
|
6
|
+
|
7
|
+
project_name = ARGV[0]
|
8
|
+
if project_name == '' || project_name.nil?
|
9
|
+
puts "Must supply a project name"
|
10
|
+
exit 1
|
11
|
+
end
|
12
|
+
|
13
|
+
project_builder = ProjectBuilder.new
|
14
|
+
project_builder.create(project_name)
|