MKS_Rajput's blog

By MKS_Rajput, 8 days ago, In English

I had been participating in contests for a long time, but something strange kept happening to me.

In some specific questions (particularly those involving hash maps or dictionaries), I always take contests in Python.
When I submitted solutions for questions involving hash maps, some of them would get accepted, but for others, I would get a TLE (Time Limit Exceeded).

Sometimes these questions would pass the pretests but then fail during system testing or higher test cases with a TLE. This same incident happened to my friend as well. When this first happened to me, I thought maybe my logic was incorrect or that I had solved it wrong. But then, when I checked solutions on streams like those of Shyaan or Aryancc, I found that my logic was correct. So, what was the issue?

One of my friends then suggested that I try solving the problem in C++.** So, I converted the same code to C++** and submitted it, and it worked for the exact same question that had given a TLE in Python.

In my last contest([contest:Hello 2025]), while solving Question B(2057B - Gorilla and the Exam) (another question involving hash maps), I submitted it in Python(299618966), and it passed the pretests. Then, suddenly, I remembered my previous experience with this issue. To be safe, I converted the same code into C++ and submitted it(299648513).

The contest results showed that my C++ submission for Question B was accepted. Out of curiosity, I submitted the same Python code afterward, and it gave a TLE on Test Case 17(299728582). That’s when I felt relieved, knowing I had done the right thing by submitting the C++ version during the contest.

In conclusion, I say this: Never trust Python dictionaries—they can betray you. So, can anyone help me with this? Because I really like submitting code in Python. Also, has this kind of incident ever happened to you as well?

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

»
8 days ago, # |
  Vote: I like it 0 Vote: I do not like it
»
4 days ago, # |
  Vote: I like it 0 Vote: I do not like it

I got the same issue with c++. unordered_map which is considered to be faster gave TLE(299715651). The same with map, which is ordered and has a O(log(n)) overall complexity passed the verdict(299715692)

  • »
    »
    3 days ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    map is faster than unordered_map

»
4 days ago, # |
  Vote: I like it -8 Vote: I do not like it

in CP Python is trash language

  • »
    »
    3 days ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Nah Python is competitively viable. Unless if it's USACO, but that's mostly because their Python judge doesn't use pypy.

    Proof Python can work on CodeForces:

    • »
      »
      »
      3 days ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      I click the link -> A Mountain of TLE -> Python gives much TLE -> Python is trash language

      welcome to #pythonsucks this is the beginning of #pythonsucks

      • »
        »
        »
        »
        3 days ago, # ^ |
          Vote: I like it +9 Vote: I do not like it

        At least I actually have more than 5 submissions on my account, also in about 99% of cases TLE is a skill issue and not a language issue

      • »
        »
        »
        »
        3 days ago, # ^ |
        Rev. 2   Vote: I like it 0 Vote: I do not like it

        It is not too bad.

»
3 days ago, # |
  Vote: I like it +11 Vote: I do not like it

https://mirror.codeforces.com/contest/2057/submission/299628938

If it's anything, I still sometimes forget about Python dicts sometimes being like this. If it's anything, C++ also can have a similar issue with unordered_map https://mirror.codeforces.com/blog/entry/62393.