Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abbreviations and functions in preparation for a programming contest [closed]

Tags:

r

I am participating in a big programming competition tomorrow where I use R.

Time is the main factor (only 2 hours for 7 coding problems). The problems are very mathematics related.

  1. I would like to write "f" instead of "function" when I define a function. This can be done and I had the code to do so, but I lost it and cannot find it.

  2. Where do I find sin() functions for degrees input, not radian?

  3. (optional) Is there any algorithm specific task view or libraries.

  4. Any tip for a programming contest?

I prepared the following cheat sheet for the contest: http://pastebin.com/h5xDLhvg

======== EDIT: ==========

So I finally have time to write down my lessons learned.

The programming contest was a lot of fun, but unfortunately I did not score very well. I was in the top 50%, but my aim was to be in the top 25%.

The main problem was that there was very little time to program, just 2 hours in total. But I had to read the problem descriptions and also I needed some time to paste the results in the web form, etc., so it was more like 90 Minutes of programming.

Hopefully the next contest in December will have extended time, like 3-4 hours. The organizers said that perhaps will be the case.

Also, there was no Internet access at the contest, and my mobile reception was not really working.

The main lesson for me is that you have to use a language you daily use in order to have a real chance. Especially, if there is only about 90 Minutes time to program. Since I use haskell more than R in my daily work, I think R was not the best choice. During the contest I mixed up haskell and R function definitions, and I made too many small typos to program fast enough.

What was great about the contest was, that there was about 20 000 bucks prize money in total for the about 80 participants. So the top 25% participants got from 500 to 1500 bucks each. Further, I think the top 15% get a job right away from one of the sponsor IT firms.

So it's a win-win situation. It's fun, plus you can get prize money. Further the IT firms are more than happy, because they have access to the top programmers.

I used the chance to speak to IT decision makers. One of them was from a larger bank. I boldly suggested that they consider switching to Scala for their development (switchung from Java). And also to consider using R and Haskell. It was fun, and they even said they already looked into Scala!

What was interesting to note was, that one of my best friends scored very good at the competition. He is only 19 years old, but he was well in the top 20% and got 500 bucks prize money. He beat me plus 6 of my colleges, who all have a respectable computer science degree. My friend programs more like hacker style, but he was very fast.

People in the top 10 used: 1) Java 2) C# and 3) C++ (No other programming language in the top 10!). The only other programming language that scored reasonably well was Ruby, I think.

For the next contest the programming language of choice will probably be haskell. For one reason, it's just easier to find 2 team mates for haskell than for R programming. And up to 3 persons can form a team.

My ideal scenario would be a very light weight framework, where I could use multiple programming languages at once for the contest. That way, the main code can be written in haskell (which all team mates can program in). And some specific functions may be programmed in R, or in Mathematica, or even some other programming language (like python/sage).

This sounds a little bit overkill. But I think it would be very usefull. Like a function that has a matrix as a parameter and returns a matrix. Then this framework work generate automatically a RESTful service from the R code, so I could call the R function from any programming language. The matrix is just passed around as JSON data (or some other serialization). Okay, but this is off topic...

So finally some lessons learned as a bullet list:

  • don't bring food. you don't have time to eat, and there is a rich buffet afterwards
  • time is the limiting factor!
  • if you don't program R for a living, don't use R
  • look for contests where there is more time (3-4 hourss minimum!)
  • all in all, the concept of the contest is superb! Both for the participants, but also for the sponsors.

BIG THANKS to the help of 'Iterator' for his post!!

like image 332
mrsteve Avatar asked Oct 21 '11 00:10

mrsteve


3 Answers

I'm going to answer a related, but different question. No offense, but your original suggestions don't seem very wise for a programming competition. Much of the time spent in such contexts is in devising an answer and in debugging (or, better, avoiding the need to debug).

Instead, I will answer this question: "What are the key resources in R that are useful for rapid prototyping, with a focus on being able to find resources quickly, being able to debug quickly, and being able to investigate data quickly? If I need to use numerical optimization methods and algebra systems, what should I investigate?"

Here are my answers:

  1. Install RStudio or possibly Revolution Analytics' R, depending on which interface seems more appropriate to you. Both are good. The former has a very smooth GUI, the latter has a more intense interface, with more capabilities for managing code. Both have some nice properties over the "community" R regarding being able to look up information and navigate the help libraries quickly.
  2. Get acquainted with example(), identify where to get vignettes and tutorials (from packages' pages on CRAN), and take a brief look at demo().
  3. Use the sos library, and master findFn.
  4. Look at the Task Views on CRAN - be sure you know about the tools for high performance computing (if that is going to be related) and the tools for optimization - it's quite common to need to use some kind of solver, and there's a task view for that.
  5. If your code is running slowly during the prototyping or competition, you'll need to run Rprof(). Take that for a spin first. You may also benefit from using the compiler package if your code involves much iteration. In short: You do not want to wait on the computer. You might also look at foreach and doSMP or doMC if you can parcel the job to different cores. To aggregate results, become familiar with plyr and methods like ldply, as well as standard *apply functions, like lapply and apply; another good one to know is rapply. (If you have lots of stuff to process and it takes some time, look at mclapply or the .parallel argument for the plyr functions.)
  6. On Stack Overflow: browse JD Long's questions - much of what you will discover that you do not know will have been asked by him before you thought to ask it. And there's an answer already there.
  7. Create a number of little code templates for yourself. Master functions so that you don't need to learn these in a rush. Learn how to debug and step through these, using debug() and browser().
  8. If you have to count things, learn how to use the hash package (akin to Perl and Python hash tables) and learn to use digest for keys that are too long to be used for hash (see this question for references)
  9. If you are going to need to plot things, get some basic example plots prepared, using either plot or ggplot2, along with hist, boxplot, and some others. If you don't know ggplot2 already, then postpone, but you should become familiar with it. If you happen to use a lot of data, then be sure you know hexbin. If you will have to interact with data, then get to know iplots and the interesting tools there, such as iplot, ihist, and parallel coordinate plots (ipcp).
  10. Be sure you know how to use lists, data frames, and matrices, including subscripting, lookups of entries based on (row, column) indices. (Again, be sure to investigate plyr for transforming and operating on some of these objects.)
  11. Get acquainted with data.table() - it's exceptionally efficient for a lot of things you might do with data frames and matrices.
  12. If you need to do symbolic mathematics, be sure you know the packages for that or else get another standalone tool for symbolic math. Ryacas is one package that appears to be useful.
  13. Get the PDF of the R in a Nutshell, so that you can rapidly search through it for useful methods. Else, get the book itself. Various other books, such as Venables & Ripley, the R Cookbook, and others may be useful, depending on your experience.
  14. If you've already mastered a good editor (e.g. emacs) or IDE (e.g. Eclipse), stick with it and look for bindings to R. Otherwise, a simple one you can begin using right away is Notepad++. Being able to do block selection is a very useful property in an editor. Being able to search through an entire directory hierarchy of code examples is another useful capability.
  15. If you need to do anything involving database data, you may want to know RSQLite and sqldf, though these may not be relevant to a math competition.
  16. Open a bunch of R instances so that you can try things out. :) [This is actually serious: by having multiple instances running, you can somewhat avoid latency associated with sequentially trying things out, waiting for results, and then debugging the results.]
like image 155
Iterator Avatar answered Sep 22 '22 15:09

Iterator


For (1), you can do something like

f <- function(..., body)
{
    dots <- substitute(...)
    body <- substitute(body)
    f <- function()
    formals(f) <- dots
    body(f) <- body
    environment(f) <- parent.env(environment())
    f
}

which lets you write, eg, g <- f(x, y, body=x+y) but I'm not sure how far that gets you.

like image 20
Hong Ooi Avatar answered Sep 19 '22 15:09

Hong Ooi


For (2), you could just do:

sindeg <- function(x) sin(x*pi/180)
like image 40
Karl Avatar answered Sep 19 '22 15:09

Karl