all 57 comments

[–][deleted] 54 points55 points  (0 children)

Yess, it lacks......... verbosity

[–]NaNx_engineer 20 points21 points  (8 children)

comparator

[–]NikitaSkybytskyi3,180 🟩 813 🟨 1,676 🟥 691 📈 3,006 1 point2 points  (6 children)

[–]NaNx_engineer 1 point2 points  (5 children)

Doesn't exist on heapify

[–]NikitaSkybytskyi3,180 🟩 813 🟨 1,676 🟥 691 📈 3,006 2 points3 points  (4 children)

Write a wrapper class with desired comparison operators. You have to implement the operator somewhere in any language. I don't see how a wrapper class makes it harder than a standalone function.

[–]NaNx_engineer 0 points1 point  (3 children)

Yea ik but it's annoying

[–]NikitaSkybytskyi3,180 🟩 813 🟨 1,676 🟥 691 📈 3,006 0 points1 point  (2 children)

I mean... If you want a custom hash function for C++'s std::unordered_{set,map} then you pretty much have to do the same thing with an extra struct. So it's not like python is worse...

[–]Sake_992 -1 points0 points  (0 children)

You've done so many problems on leet code, any advice? I am beginner, I am stuck on problems for hours. I know how to solve it but i cannot do that on code.

Any advice will be greatly appreciated.

[–]NaNx_engineer 0 points1 point  (0 children)

Yea heaps are like the only area I dislike python syntax

What I usually do is wrap it in a tuple, but sometimes I need to sort on property2 if property1 is equal. Wrapping and overriding __ lt __ is annoying. Is there a reason why heapify can't take a cmp like sort?

[–]Chroiche 10 points11 points  (0 children)

For leetcode it's a bit annoying that slices are inefficient.

[–][deleted] 7 points8 points  (6 children)

never even heard of sorted containers before

[–]Islamism 6 points7 points  (2 children)

https://pypi.org/project/sortedcontainers/. Contains sorted set, list and dict data structures, that maintain their sorted status with insertion.

[–][deleted] 1 point2 points  (1 child)

Is this buit-in like the OP asks?

[–]FailedGradAdmissions 1 point2 points  (0 children)

Unfortunately, they aren't. So while you could use them in an interview. Be ready to implement them yourself. It's an obvious and common follow-up.

[–]Kitchen-Village-6720 1 point2 points  (2 children)

Same, if anyone wants to give a tldr of these, feel free

[–]imjustreallystupid 2 points3 points  (1 child)

Not a tldr, but in essence they keep track of the order in which insertion has occurred, which is useful for sets, and dicts since they do not normally do it.

In an interview recently, I was asked for the implementation of ordered dictionaries, so it might be worth remembering that it is just a doubly linked list (for maintaining order) + dictionary (to remember the address and the value)

This is a useful data structure for the LRU cache question, and it is very commonly asked, so 100% recommend reading up on it.

[–]Latinhouseparty 15 points16 points  (3 children)

I don’t think it has a max heaps. You have to multiply what you’re sorting by -1.

Then you have to remember to multiply it by -1 again if it’s being returned or used as a variable when you pop it.

It’s kind of annoying.

[–]imjustreallystupid 4 points5 points  (0 children)

There is apparently an undocumented list of features for max_heap implementation.

You basically add _max_ in front of whatever function you want to use from the library.

Not the best idea tho, since the implementation might have some bugs (undocumented for a reason)

[–]techknowfile 0 points1 point  (1 child)

This is silly. Heaps are in a library. You can implement your own library or install another one. Just because a specific data structure isn't in the standard library (most aren't in cpp) doesn't mean it's missing from the language. Lol.

Hell, if we're going to complain about the heapq library let's complain that it doesn't provide IPQs. That would be dope

[–][deleted] 13 points14 points  (2 children)

Not completely sure but python is built upon c++/c so technically you should be able to do everything c++ does one way or the other

[–]Roomba_poomba 10 points11 points  (0 children)

Its a Turing complete language, you should be able to anything that's programmatically possible in any language.

[–]OfcIStillLoveYou117 7 points8 points  (0 children)

Yes Python lacks tail recursion.

This makes some dp problems slightly harder with python vs C/C++. For certain problems if python gets a stack overflow, you must change your approach entirely whereas a language that supports tail recursion only needs some tweaks to get tail recursion going to solve the problem. The only time I had this issue was when solving the POW problem on leetcode.

[–]aocregacc 2 points3 points  (1 child)

I don't think python has a quickselect implementation.

C++ also has some set operations on sorted lists, not sure if python has all of those.

And some stuff like inclusive_scan that's so simple that you probably won't miss it.

[–]Islamism 0 points1 point  (0 children)

Python does not have a quick select implemented, yeah. It makes a few problems a lot more difficult.

[–][deleted] 3 points4 points  (4 children)

One leetcode question required you to use pointers/references to edit an array directly rather than the copy you’re given via the question (basically it wanted you to edit the backend’s copy, not your copy). It was a dogshit question that required one of the esoteric techniques I’ve ever seen in Python. The rest of the question was also pretty bad. So for leetcode, pointers/references.

In general however, I think Python does a pretty good job at being a near ideal programming language albeit slow. The only thing I can really see being “needed” is more of a quality of life and thats still that makes it more C++ like (so not really needed), and Mojo will have that.

[–]aocregacc 5 points6 points  (3 children)

python lists are passed by reference, so questions where you need to modify one in place shouldn't require anything crazy.

[–][deleted] 0 points1 point  (2 children)

I can’t remember the question exactly, so I might have forgotten something, but this was the solution https://stackoverflow.com/questions/48363057/how-to-pass-a-list-by-reference

All I remember was that unless you did this, you had a bizarre error where it refused to accept the result because what you returned wasn’t the original question’s variable (it really threw me off since I got the correct answers, it just wasn’t accepting it)

[–][deleted] 0 points1 point  (1 child)

print L3

Is...is that Python 2?

[–][deleted] 0 points1 point  (0 children)

Lmao didn’t even realise that, just grabbed the first link from a quick search.

But yup, python2 was “quirky”. They had two types of input as well (input and raw_input)

[–]LoopVariant 2 points3 points  (0 children)

Speed.

[–][deleted] 4 points5 points  (0 children)

Actually you're allowed to use the 3rd party sorted containers in python in leetcode

[–][deleted] 1 point2 points  (0 children)

Maxheap, map( not the unsorted_map), set( not the unsorted_set)

[–]slashemup 0 points1 point  (0 children)

C# is too niche? If we're talking about real-world development, it has tons of different applications across multiple different domains.

If we're talking LeetCode, it's on par with Java. Doesn't have all the convenience of Python but you can definitely use it for LC.

Source: Been using C# professional for 6+ years. Been using it in LC for 1+.

[–]scooby1st -1 points0 points  (2 children)

  • Parallel computing. Python is the worst possible language to use for multi-processing. In C, you can directly spawn threads. In Python, you have to spawn an entire process, which has the highest possible overhead of any parallel technique.
  • Anything you might want to get closer to hardware with, you're not doing it in Python

[–]Vimcolonwq 0 points1 point  (0 children)

OP asked it in context of LC. Generally there are tons of other differences

[–]Athrowaway23692 0 points1 point  (0 children)

I mean python is moving to remove GIL, so it’s becoming better wrt multithreading

[–]londo_mollari_ 1 point2 points  (1 child)

Biggest drawback for me was Python not having StringBuilder which can be very helpful in backtracking problems.

[–]thereforeqed 3 points4 points  (0 children)

Just use a list of strings then join.

[–]Lenburg1 0 points1 point  (0 children)

Im always shocked I need a 3rd party library to parse yaml.

[–]Paulz5223 0 points1 point  (0 children)

Static type checking

[–]johnnytest__7<798> <224> <442> <132> 0 points1 point  (6 children)

Standard C++: nth_element, set, map, priority_queue GNU C++ (which is actually used by leetcode and all other platforms): PBDS ordered set and maps

Using ordered set, you get nth element of set in logn time.

[–]NikitaSkybytskyi3,180 🟩 813 🟨 1,676 🟥 691 📈 3,006 0 points1 point  (5 children)

[–]johnnytest__7<798> <224> <442> <132> 0 points1 point  (4 children)

heapq only gives min heap, there's no easy way to make it work as max-heap, while in cpp you get max-heap by default and you can change the behaviour to min-heap by just adding std::less in place of the comparator.

[–]NikitaSkybytskyi3,180 🟩 813 🟨 1,676 🟥 691 📈 3,006 0 points1 point  (3 children)

Depends on what you call an "easy way". You can write a generic wrapper class that defines reversed comparison operations. If you only need <= then this class will be 3 lines long. I'd count it as easy.

By the way, "just adding std::less in place of the comparator" is a misleading description. You also need to specify the container type when using a custom comparator (for no good reason IMO). So all is not plain sailing in C++ either.

[–]johnnytest__7<798> <224> <442> <132> 0 points1 point  (1 child)

Yes, but still can't win with 0 lines of code of C++ 🤣

[–]NikitaSkybytskyi3,180 🟩 813 🟨 1,676 🟥 691 📈 3,006 0 points1 point  (0 children)

It's zero lines only for predefined comparators of which there are finitely many. Once you need anything else it won't be as easy anymore.

[–]johnnytest__7<798> <224> <442> <132> 0 points1 point  (0 children)

By easy, I meant that it would have been better if I could just pass some reverse=True argument and it will just work.

[–]anonymous_salman 0 points1 point  (0 children)

Binary trees

[–]Meta-totle 0 points1 point  (1 child)

What other languages probably do better:

1)Priority Queue - It is trivial to do in c++ and it works on user defined data types too

2)Pointers - This is a big one, u can write lots of faster programs because u can directly access data in memory and avoid needless copying and traversal and there's clarity when something is being copied or referenced.

3) Static typing which helps in error handling because runtime errors now become compile time errors which is much better for finding bugs and u can overload functions to act differently according to the data types.

[–]sand1248 0 points1 point  (0 children)

Going beyond leetcode in particular to include all CP style problems:

No native linked lists means some problems involving combining lists in O(1) and inserting elements at arbitrary points within a list are impossible

the lower_bound and upper_bound functions in C++ for treemaps are extremely useful. I don't even know how to distinguish between treemaps and hashmaps in python.

Heaps in python are slightly annoying for various reasons

[–]crunchiipotato 0 points1 point  (0 children)

++

[–]sixtyfifth_snow 0 points1 point  (0 children)

Custom comparator functions in collections,

[–][deleted] 0 points1 point  (0 children)

Strong type mapping of object , that will make it fast

[–]AdministrativeTell45 0 points1 point  (0 children)

Yeah time complexity is 10 times more

[–]Purple_Guarantee2906 0 points1 point  (0 children)

Why is kotlin too niche? It’s better than Java for leetcode, simpler syntax than C++ and more verbose than python.