Please, try EDU on Codeforces! New educational section with videos, subtitles, texts, and problems. ×

By duckladydinh, history, 2 months ago, ,

Hi,

[Note: I am a Java developer who loves Python, okay? I just want to learn something like this for Java/Python to use them more in CP]

I have a question buried deep in my head for all these years, but I don't think I can hold back anymore: Why would anyone use Python or Java in Codeforces?

Not everyone knows C++? I doubt so. Most coders would know at least 2 or 3 programming languages and C/C++ is one of the very classic one usually taught in universities.

More features? I doubt so. Except for big arithmetic problems, I doubt if there is any other feature in Java or Python for CP that C++ does not have. On the other hand, C++ does have more features than them such as speed, policy based data structures and direct memory access.

Shorter code? I seriously doubt so. At least compare to Java, C++ is shorter. Compared to Python, unless you want to couple everything into one unreadable line, then C++ should not be very longer.

I did try Python and Java for CP but only because I think Codeforces is a good place for me to learn those languages. What are your reasons?

Thanks

[Results]

1. Debugging. (actually CLion is quite good at debugging but not free and yeah, not excellent)

2. Shortness. (as kabuszki pointed out, I am not a Python natural user yet, so I didn t recognize)

• +83

 » 2 months ago, # | ← Rev. 2 →   -59 LoooLOOOLOLoloOL. Rofl. Lmao. You are stupid. If you truly believe in your words then that means you're kinda illiterate.1)Java forbids to make stupid mistakes. 2) TL;DR: Python's functional paradigm is cool Compared to Python, unless you want to couple everything into one unreadable line, then C++ should not be very long. Square matrices multiplicationin C++: matrix mul(matrix& a,matrix& b) { int n=a.size(); matrix c(n,row(n,0)); for (int i=0;i v(20); for (int i=0;i<20;i++) v[i]=i*2; In python: [2*i for i in range(20)] I hope that helps you to overcome your illiterateness.
•  » » 2 months ago, # ^ |   +3 For example? Come on, I want to know the details, otherwise I wouldn't ask the question. 2.1 The matrix multiplication in Python is indeed 'quite' readable (by declaring 2 functions, okay) but is it really more readable than the C++ version?2.2 Indeed the Python version is shorter and simpler, but as I say the C++ version is not very longer. And how will it look with more dimensions?Not very convincing, isn't it.
•  » » » 2 months ago, # ^ | ← Rev. 2 →   +7 using undeclared values, returning nothing from a non-void function, etc
•  » » » » 2 months ago, # ^ |   0 A better example would be to point him/her/them to actually learn Java to a good extent and learn the difference as to what makes Java so special in keeping the programmer away from mistakes.@duckladydinh In C++, it's very easy to get RTEs. In Java, it's far from easy. C++ has it's fair bit of security concerns and flaws, you should probably google about it, and doing things like out-of-bounds access, endless recursion, signed integer overflows, etc are very easy to occur (until your CPU decides it's had enough of your nonsense and decides to terminate the program). In Java, you can't this can't happen or doesn't usually happen to be the case.
•  » » » » 2 months ago, # ^ |   0 okay, those are valid points. Note: for "returning nothing from a non-void function", even if no error is shown but actually there is warning.
•  » » » 2 months ago, # ^ |   -25 2.2 Some people really enjoy this shortness.
•  » » » » 2 months ago, # ^ |   +155 That's what she said
•  » » » » » 2 months ago, # ^ |   +2
•  » » » 2 months ago, # ^ |   +9 1) If I remember correctly, for example such function // Reads and return an integer from input int read_input() { int n; cin >> n; } will compile in C++, but an equivalent one in Java won't. Now you probably will say "but kabuszki, you can set proper compilation flags in C++, so it will warn you about such stupid mistakes" and you will be right. But in general Java is more robust to idiot mistakes.2) I guess this is just about taste. Some people eulogise over french language, for other it just sounds like gibberish. You don't need to be a fan of python and its useful, 'cute' perks. But from my experience, python is great for solving not too complicated problems, because then it is easier for me to translate my thinking process to python code, than to c++ code.Generally, not everyone is here to get as high rating as possible. So why would they use c++, if they enjoy coding in python or java? Why would they switch to c++ if they use other languages in their work? You made some faulty assumptions about people in here and that led you to this confusion.
•  » » » » 2 months ago, # ^ |   0 you remember correctly :) and actually in C++ those mistakes have warnings even before you compile (at least in CLion :v). actually I am not confused. Rather because I want to know if there are some special reasons (especially when I noticed Petr submissions recently, I learn Java from his code). I used all these 3 and I come back to C++ because I did not notice the advantages (in CP only). So I just want to ask if there are some good stuff that I didn't know.
•  » » » » » 2 months ago, # ^ |   +15 You used them != you learned how to use them.This is from your python code: a = [] for e in map(int, input().split()): if e > 0: a += [e] What it should be: a = [e for e in map(int, input().split()) if e > 0] If you use python, but you write code as if you were writing in C++, then it has no added value.
•  » » » » » » 2 months ago, # ^ |   0 I didn't use it efficiently that time doesn't mean I never didMuch Older Codethat submission was in the middle of C++ only submissions so it would feel strange if I wasn't influenced after not using for long :v.
•  » » 2 months ago, # ^ | ← Rev. 2 →   +19 Is it really necessary to call the author "stupid" or "illiterate"? I agree with what you're saying but personal attacks aren't necessary (and can have different connotations depending on your culture, background).
•  » » 2 months ago, # ^ |   +1 illeterateness isn't a word. What you were looking for is illiteracy. Probably shouldn't call people illiterate when your grasp of literacy is wanting.
 » 2 months ago, # |   +136 Usually "Not everyone knows C++" is the reason.
•  » » 2 months ago, # ^ |   +8 I think this is by far the most reasonable answer. I started competitive programming in Java because I knew Java, duh. Not everyone has been to uni to learn 2 to 3 programming languages, and people want to practice the language most useful to them or they know best.
•  » » » 2 months ago, # ^ |   0 When I was at school, Pascal was the default language. Needless to say that most of O(NlogN) solutions were practically impossible to code in realtime, even quick sort. This is my excuse why I sucked at school :)
 » 2 months ago, # |   -45 -1Downvote me if that's what it takes to get the message through but your questions are stupid, your examples are stupid, your blog is stupid.Why would anyone use Python or Java in Codeforces?Choosing a programming language is the choice of the programmer. Some people like efficiency, C/C++/Assembly. Some people like correctness, Java. etc etc etc. Every language exists because there's probably something unique to it. Different people like to have different options. If I like to have my code free from any and all runtime errors on the very first go, I'd choose Java. It will not let me make mistakes unlike C++ assuming that I'm always right. I could go on and on and on ranting about this for different languages, but people who usually pose such questions are stupid and giving enough examples won't do any good. You need to understand that C++ isn't C++ because of its speed, pbds or direct memory access or any other feature. Same goes for Python, Java, Lisp, Pearl, Mathlab, C, Javascript, Assembly, Scala, C#, etc. If features were to be the defining factor of a language, I bet there'd already be one incorporating all programming paradigms by now, but there isn't, not that I know of, and so you must respect programmers of all languages and the languages as well and leave their choice to be theirs.It's like saying "Yes, the british almost ruled over the entire world, so everyone should know how to speak english". Or more like, "Everyone should speak language $X$ because it has the most letters, most number of meaningful words and in general, it's considered the best because many people like to use it. So, you should stfu and speak in this language because your language doesn't have some features like ours".
•  » » 2 months ago, # ^ |   +2 I asked 'why they used it'. I didn't ask them to stop using it. What's the point?
•  » » » 2 months ago, # ^ |   0 My point is that this blog's stupid. It's been repeated several times over the years, I believe. It's usually asked by people without much experience with other languages. The answers remain the same though. I believe I've made myself clear to your question: "Why would anyone use Python or Java in Codeforces?" in the first paragraph. The rest continued on as my rant (which is usually the case when people ask such questions :p)
•  » » » » 2 months ago, # ^ |   0 first no question is stupid no matter how many time it is repeated.second, talking about repeated stupid 'statement' Downvote me if ...
•  » » » » » 2 months ago, # ^ |   0 Don't listen to these troglodytes who call you or your questions stupid. I'd much rather work with people like you who ask questions than people who try to put you down for asking a question they deem to be stupid. I'm very new here, just a few hours, but I've already noticed from this one blog that some people here and immature and rude, and others seem to actually give meaningful and well intentioned answers. Cheers.
 » 2 months ago, # |   0
•  » » 2 months ago, # ^ | ← Rev. 2 →   0 And? You mean if he uses C++ or Java, he won't become grandmaster? What I'd like to know more is why he likes Python.
•  » » » 2 months ago, # ^ |   0 Obviously he cant became first Python Grandmaster using C++/Java.
•  » » » » 2 months ago, # ^ |   +1 :) he did mix a small portion of C++ in his submissions :)
•  » » » » » 2 months ago, # ^ |   +30 I don't think I've used C++ in a rated contest in forever. Maybe I've done it a couple of times some years back, can't remember now. The C++ submissions I've done recently are to test out the performance difference between C++ and Python, and not because I've been solving problems in C++.There is one simple reason why I use Python, and that is because I enjoy coding in it. I would say Python is a lot more viable than what people give it credit for, but it requires knowing/learning what runs slow or not. I have been training cp by solving randomly picked 2200 — 2300 rated problems on cf and so far I have been able to solve everything in Python. So using Python to get to red is definitely not impossible.That said, I do not think Python is a good tool for cp. It simply is far slower than C++.
•  » » » » » » 2 months ago, # ^ |   +1 Sorry, I didn't mean to include you in this. I just tried to troll his answer :V .And yes, definitely you can get red with only Python since the mind is all it takes to get red. (like math guys running into coding and getting bloody red all the times)
 » 2 months ago, # |   +6 Petr also switched from Java to C++.
 » 2 months ago, # | ← Rev. 2 →   +24 The factor you leave out that I think is most important is how easy it is to debug. I spend most of my time in contest debugging errors in my solution. With C++ it takes a lot of experience to avoid common mistakes, and a lot of time to track down even simple typos. Both Java and Python have a major advantage over C++ in this area. I've wasted many minutes in contest dealing with a segmentation fault.The only real advantage C++ has over Java and Python is that the solutions execute faster. But if your solution is fast enough anyways, why not use Java/Python if they are your favorite language. You only really need C++ for solution where efficiency is really important.That said, for me, Java takes way too long to type simple things, and Python is too slow. I've been thinking of switching to Kotlin from C++ after the last Kotlin Heroes though because debugging was way easier, some other syntax was more natural, and it has better support for a more functional style.
•  » » 2 months ago, # ^ |   0 Okay this is the second most meaningful answer to the question. Thanks.
 » 2 months ago, # | ← Rev. 2 →   +67 Not sure why all people in the comments seem to have taken offense at this blog, seemed like a fairly reasonable question to me. Something I've wondered at certain times, too.Python is much more convenient than C++ for many things, but it's often way too slow. For real-life purposes it is often a great choice because the abundance of amazing libraries (many of them implemented in C, so fast) along with a neat syntax make it really easy to get stuff working quickly. For competitive programming, however, I personally find it impossible to overcome the speed difference, and you can't use public libraries either. However, many people start out by learning Python, so they naturally try to use that.Java, as mentioned by other people, has the great side of preventing many common mistakes and also making debugging much easier — for example accessing out of the bounds of an array will throw a runtime exception and tell you where exactly its thrown from, rather than just crashing as it would in C++. The speed compared to Python is much better, but still noticeably slower than C++. Additionally, the memory consumption can be much higher if you use the Java-way of doing things (i.e. structures of boxed primitives). My opinion on Java is that if one is very comfortable with it, it is a much more viable option for competitive programming than Python.So in summary, you seem to hold a view that these languages are somehow inferior to C++, which is why I suspect people are reacting harshly. While I personally agree (and statistically the community at large does too) that C++ is best for competitive programming, these languages have various advantages that some people deem worthy enough to sacrifice speed.To be honest if you're completely comfortable with all three languages, then picking the right one for each problem would be much more efficient than writing everything in C++.
•  » » 2 months ago, # ^ |   0 Okay, I'll correct the post just to make sure
•  » » 2 months ago, # ^ |   +10 rather than just crashing as it would in C++ C++ only crashes if you're lucky :) To be honest if you're completely comfortable with all three languages, then picking the right one for each problem would be much more efficient than writing everything in C++. Honestly, I used to do this with Python/C++, and I've found context switching to not be worth it. There's too many differences between Python and C++ for me to easily switch between them without making silly mistakes.
 » 2 months ago, # |   0 Auto comment: topic has been updated by duckladydinh (previous revision, new revision, compare).
 » 2 months ago, # | ← Rev. 3 →   +29 First of, I don't use Python for competitive programming (mostly because of the huge speed difference). But I write Python for a living.Some of the advantages that you have with Python: shorter, easier, larger standard lib, safer, dynamic typingHere's one code comparison. std::vector v{1, 2, 5, 4, 3}; std::sort(v.begin(), v.end()); auto it = std::find(v.begin(), v.end(), 4); if (it != v.end()) { int idx = v.end() - it; } vs. v = [1, 2, 5, 4, 3] v.sort() if 4 in v: idx = v.index(4); Even though the actual task is very easy, the C++ code require understanding of quite a few hard concepts, like iterators, initializer_lists, templates or how the memory layout of vectors work. On the other hand the Python code is very easy and short, especially since lists are built in types and the many nice helper functions.In general the libraries of Python are a lot more powerful than C++'s. What to split a string by spaces: "a bc def".split() (I dare you to do that in a simple way in C++!) Want to iterate over all combination (without repeats): for u, v in combinations(lst, 2):. Want to speed up a function using memoization: just apply a decorator @lru_cache, Need big integers, high precision floats or fractions, they are in the standard lib. ...Compiler support is also a lot better (especially for inexperienced C++ user). And the language is safer. Ever had a multi-page error message in C++, because of you had a template parameter wrong, or because you forgot to implement the comparison operator. Python will tells you the problem concise. Ever had to hunt down a bug, because you accessed unallocated memory and C++ just gave you garbage values and didn't crash. With Python that will not happen.Also, not everybody learns C++ at university. E.g. at my university you only learn it in a computer science program, if you are doing an optional game programming course (however you also don't lean Python). And lots of teachers still teach very ancient C++98 or even older. Just look around at Codeforces and see thousands of complete ugly C++ submission.But all the nice stuff that comes with Python, it also has disadvantages. And the main one is speed. So I would still recommend C++ for anybody who wants to get seriously into competitive programming. But the most of the arguments that you make against Python are not true.
•  » » 2 months ago, # ^ |   +3 What you say is right but I dare you to do that in a simple way in C++! challenge accepted!  string a, b, c; istringstream s("a bc def"); s >> a >> b >> c; Well, I agree it is not as simple as Python, though.
•  » » » 2 months ago, # ^ |   0 what if one wants to split('X')?
•  » » » » 2 months ago, # ^ |   +4 a bit lengthier and yet Python is better in this case  string a, b, c; istringstream s("aXbcXdef"); getline(s, a, 'X'); getline(s, b, 'X'); getline(s, c, 'X'); 
 » 2 months ago, # |   0 Auto comment: topic has been updated by duckladydinh (previous revision, new revision, compare).
 » 2 months ago, # |   0 Auto comment: topic has been updated by duckladydinh (previous revision, new revision, compare).
 » 2 months ago, # |   +13 Why I use Python is because it's easier. That's it. It comes with some disadvantages as well, like being slow and recursion limit till 30000 or so. But at this stage it helps me in focusing on question and it's solution more than it's syntax and other things. I've used Java/C++ as well but right now I feel I'm most efficient while coding in Python.
 » 2 months ago, # |   0 My old blog on D language usage in competitive programming. Shows how the experience differs from C++ mostly, because C++ is the "default language" for competitive programming. Relevant because many of the grievances with C++ and joys with other languages, which make people choose the latter, are similar. Be it Python or Java or otherwise.
 » 2 months ago, # |   0 My reason for using java: Never undefined behaviour. C++ sucks at that.
 » 2 months ago, # | ← Rev. 2 →   +61 My reason to use Java is that I want TLE due to sorting.
 » 2 months ago, # |   0 I mostly use C++ but I switch to python sometimes when there are a lot of string manipulations involved. E.g the solution to 200D - Programming Language is relatively short in python 78036294 but seems like a nightmare to implement in C++. Also python can be useful for bigint problems but I haven't really encountered many.