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 CHANGED
@@ -19,40 +19,118 @@ Install via RubyGems:
19
19
 
20
20
  $ gem install marmalade
21
21
 
22
- ## Usage
22
+ At the moment, the requirements for using it are:
23
23
 
24
- ### Basic Usage
24
+ - A *nix environment
25
+ - Ruby 1.9
25
26
 
26
- Once you create a Ruby file for solving a Code Jam puzzle, simply require the Marmalade gem:
27
+ ## Basic Usage
27
28
 
28
- #!/usr/bin/env ruby
29
+ Once the gem is installed, use the generator to create a project file for you:
30
+
31
+ $ jam example
29
32
 
30
- require 'rubygems'
31
- require 'marmalade'
32
-
33
- Make the file executable and Marmalade is ready to do the dirty work for you. Say we have an input file like the one described above. 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 instruct Marmalade to read that for us like so:
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(:file => 'input.txt') do
36
- read :num_cases, :type => :int
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
- puts_res solve_case(@k, @lines)
51
+ puts solve_case(@k, @lines)
52
+ end
41
53
  end
42
54
 
43
- Marmalade reads in each line and will assign the values to instance variables that you specify. When the result is ready to be printed, calling `puts_res` will output the proper format that Code Jam usually requires, much like:
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
- ### Other Stuff
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
- - --debug and puts_dbg
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)
@@ -0,0 +1,6 @@
1
+ 5
2
+ this is a test
3
+ foobar
4
+ all your base
5
+ class
6
+ pony along
@@ -0,0 +1,4 @@
1
+ # Reverse Words Example
2
+
3
+ A working example of the 'Reverse Words' puzzle at http://code.google.com/codejam/contest/351101/dashboard#s=p1
4
+
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'marmalade'
5
+
6
+ Marmalade.jam do
7
+ read_num_cases
8
+ test_cases do
9
+ read :words, :split => true
10
+ run_case do
11
+ puts @words.reverse.join(' ')
12
+ end
13
+ end
14
+ end