MaxWelll's blog

By MaxWelll, history, 7 weeks ago, In English

Some days ago I completed all stages of the CodeCrafters "Build your own HTTP server" challenge in C++. My code was made to pass each stage of the challenge, but I took some extra time to refactor it into something that I could use in the future.

Check out my code on GitHub and feel free to share your feedback!

These are some of the issues I know exist or questions and would like to address:

  • I have some issues with posting files:

    • posting images, for example, doesn't work. I dont get the same file as the one I posted
    • I also just noticed that when posting text files it doesn't write the end lines
  • My Dispatcher takes a std::function<HttpResponse(HttpRequest&, std::vector<std::string>)> and then uses it to get a HttpResponse. But in some cases, (by now just when the response is built with .stream) I'm taking a reference to a stream that is built inside the function, when the function end it goes out of scope. Any elegant workaround for this? In my case I just declared the stream outside of my dispatcher build, but that's just a hack.

  • The base code given for the socket connection seems like it uses a C-style API. Is there any C++ way of using sockets?

Full text and comments »

  • Vote: I like it
  • +18
  • Vote: I do not like it

By MaxWelll, history, 7 weeks ago, In English

In the last contest (Codeforces Round 933 (Div. 3)), while solving the problem 1941F - Рудольф и дисбаланс, I got WA on my first attempt. On debugging, I found this bit of code was problematic

const int INF = 1 << 30; // this is equal to 1073741824

I've always used this as my infinite value for integers, but it was not large enough in this case.

Note 1: Be sure your infinity is actually an upper bound for your problem

Well, I made INF large enough and submitted it again 250809384. WA. Can you spot the issue in my code? I was pretty sure that I didn't need to make every integer 64 bits so that the problem passed. But I did it and it passed.

Note 2: If doubtful, go ahead and make every int a long long int. It's ok for CP; avoid the unsuccessful submission penalty

The bug in my code was pointed out to me by Codeforces when I played a bit more after the contest 250815419. And it's so silly because I introduced that bug by rewriting the condition in a way I thought could be more efficient obscuring what I was trying to check. The gain in efficiency there would be that I could compute a0 + a1 only once and that the compiler probably makes 2 * fd really fast with a left shift. But why!? That would have had a negligible effect on the program as a whole.

As Donald Knuth said

premature optimization is the root of all evil...

In my case, it was not only premature but meaningless.

Note 3: Don't bother cutting edges unless it's actually useful and, more importantly, needed

I guess I'll be making posts like this whenever I do something that I regret in a contest. If you have any advice feel free to share it!

Full text and comments »

  • Vote: I like it
  • +10
  • Vote: I do not like it

By MaxWelll, history, 2 months ago, In English

Just a piece of advice:

Read well the problem statements.

During Codeforces Round 932 (Div. 2), I was trying to solve a problem that was completely different from what the problem 1935D - Exam in MAC was. I thought x and y had to be in the given set, and had no idea what magic to do to make it not at least n^2 (maybe that should have given me a hint that I was misreading).

Another piece of advice:

Go through the example test cases

If I had gone through the example I would have noticed.

(this is mostly a note to myself)

Full text and comments »

  • Vote: I like it
  • +29
  • Vote: I do not like it

By MaxWelll, history, 2 months ago, In English

It has happened several times to me that I can't solve a problem in the contest but right after it finishes I come up with the solution.

I'm not worried about being unable to solve all the problems in Div.2 contest, not even some Div.3's, I'm aware I'm simply not good enough (yet?). But not being able to solve in-contest some problems that you can solve with a little more time hits differently.

It's another skill issue, just the fact that it takes me too much time to solve the other problems I manage during the contest.

Any advice on how to stop this from happening?

Full text and comments »

  • Vote: I like it
  • +2
  • Vote: I do not like it

By MaxWelll, history, 3 months ago, In English

Codeforces has this option to "Offer to publish great rating rises", but when I hit the notification I don't see any social media share button or even one to share in a blog here in Codeforces.

Am I just not seeing it? What is the real purpose of it?

Full text and comments »

  • Vote: I like it
  • +14
  • Vote: I do not like it

By MaxWelll, history, 7 years ago, In English

Can anybody tell me about C++ multimaps. As far as I know you can assign many values to a key but... how do I access to any of theese values?

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it