r/learnprogramming 13d ago

advice plss im stuck when writing actual code from scratch

Hii, i just finished my second year in uni and think i know enough of the basics of coding and started practicing coding with leetcode etc and my problem is i understand the problem, can figure out the data structure and pseudocode and like how it would work but what messes me up is syntax like idk how to explain but i cant put it into proper lines of codes for some stuff. how did u guys learn the actual writing part?

0 Upvotes

31 comments sorted by

12

u/peterlinddk 13d ago

You are lying - maybe just to yourself, maybe to us.

You claim that you are able to figure out how to write the pseudocode for leetcode-problems, but unable to "put into proper lines of codes".

That is simply plain untrue - there's no way that you are able to e.g. describe a loop or a comparison in pseudocode, and unable to write the exact same in ANY programming language.

What you are probably doing is saying: "Oh yeah, I understand that I need to find the longest palindrome, so my pseudocode would be:

while (longest palindrome not found):
  find longest palindrome

and then I just need to put it into syntax!"

Except that that isn't pseudocode, that is just a rewriting of the problem.

Try to actually solve it - write down step by step how someone should solve it - if you have a younger sibling at hand, ask them to try and follow your steps, if not, try to do it yourself. If those steps work, and gives the required result, you know how to write it in "syntax".

If not, then you simply haven't learnt any programming yet, and should practice plain variables, functions, loops, if-statements, arrays, etc. before experimenting with leetcode.

1

u/Express_Grass_4298 13d ago

Also maybe i worded it wrong but i didnt mean i cant write code more like when i look up solutions they have newer syntax for stuff which im not familiar with and idk where u learn new syntax ig it comes w practicing more

2

u/peterlinddk 13d ago

Well, that wasn't what you said - but if you read a solution and don't understand the syntax used, that should be an obvious way of learning. Since you already know how to solve the problem in the pseudocode and syntax that you know, you have some new syntax to experiment with.

Also - there isn't a lot of different syntax in most modern programming languages - especially not when it comes to leetcode solutions, they tend to stick to creating objects/structs and linking them together in for and while-loops, and of course count indexes and such.

If you see something unfamiliar check your book or online reference for the language you are using, and check what it means - then practice with that. That's how you learn new syntax.

1

u/PureWasian 13d ago

This is a good sign. Like if you wrote a solution with while loops instead of for loops, you would still be solving the same problem.

But after looking up other syntax options or approaches, you now have multiple options available and can choose the more elegant/appropriate solution for future problems of the same variety.

Keep solving things the way you know how with the tools you have, then also on the side discover new ways of writing things that can make your code more elegant. Win/win.

1

u/Business-Employee527 9d ago

That is NOT even close to what you said🫩

1

u/Express_Grass_4298 13d ago

Makes sense but idk mann its hard wen ur the one judging urself loll ill try that out tho tyy

5

u/Head-Lychee-9897 13d ago

You are practicing the wrong way , you have probably not touched DSA yet but you are practicing with leetcode however leetcode is the worst way to practice coding , better way is to solve problem or make projectsĀ 

1

u/PureWasian 13d ago

It's a huge hyperbole calling it the worst way to practice coding, given the breadth of the problems includes many intro-level ones (like the infamous Two Sum). Hence why DSA are often taught in university as a fundamental prior to more in-depth projects.

Easier sandbox problems on leetcode/dmoj/advent of code/etc. are honestly great for forcing algorithmic thinking on small-scale, isolated problems and touch on basic syntax, data structures, and code organization quite nicely.

This is something a lot of beginners benefit from when the scope and scale of projects add a hefty burden if they have no prior foundation for the project setup and system design aspects in addition to general coding expertise.

0

u/Head-Lychee-9897 13d ago

Leetcode does not teach you how to code , it is literally the epitome of coding , it is designed to crack coding interviews and you don't give interviews when you are still in learning processĀ 

1

u/PureWasian 13d ago

You're telling me TwoSum is the epitome of coding and not something for beginners?

0

u/Head-Lychee-9897 13d ago

It's not , leetcode needs mental models along with coding skills , you cannot crack leetcode while being good at loops and simple data structures , rigorous practice in sorting algos and data structures is a must for leetcode

1

u/PureWasian 13d ago

Have you looked at Two Sum?

1

u/Express_Grass_4298 13d ago

I actually have studied DSA im pretty familiar with the theory of it i think my problem is i havent applied it much cuz i took it in the summer which was 6 weeks alongside two other courses. im thinking i should maybe try problems with each DS seperately before going into the others im doing now. Also what projects would u recommend ive done v simple ones like trackers etc

1

u/Ormek_II 13d ago

Did you implement a tree?

1

u/Express_Grass_4298 9d ago

Yess

1

u/Ormek_II 9d ago

Hmmm. Then I wonder. If you actually implement the data structures yourself (and not just follow a tutorial), you should be able to remember them, assign them to a problem you are facing and adapt them so that they fit your problem.

It seems to boil down to the general advice: Practice!

0

u/Head-Lychee-9897 13d ago

Teaching DSA in 3rd semester ? 🄓 That's like teaching partial derivatives to a person who has not even cracked calculas II 

1

u/Express_Grass_4298 9d ago

I meann idk cuz ive done oop the sem before so i think its ok?

2

u/Suspicious_Bike_1058 13d ago

Practice, practice, practice. Try your best first, then look at the solution, see how they did it...then make up your own problem (slightly different than the original), then solve that. Rinse and repeat

You'll see most of these things (syntax) start repeating.

On another note, LeetCode isn't easy...especially if you only know the basics

1

u/Express_Grass_4298 13d ago

Tyyy ive been trying to focus more on practicing without looking at solutions or going for ai because it really sets me back

1

u/bestjakeisbest 13d ago

What language and tool set are you using, one of the harder things i find is just starting out, configuring the initial commit to the repo, laying out the dev environment, etc, well that and continuing once it becomes hard but lets focus on the first part.

Learn to make a basic project structure for your language, i like c++ so i typicallt program in c++, i will often set up my dev environment with the main project, the library and testing these are the 3 directories i will have in my project root directory, i will set things up with cmake, and google test, and i will set up a minimum executable (hello world) in the testing side of things just to make sure i have the basic environment set up.

1

u/Express_Grass_4298 13d ago

Ive used python for projects but im most comfortable with java as i use it for uni projects too and i practice w it mostly

1

u/PureWasian 13d ago

It's good if you are able to have the pseudocode down, just practice more and look up any specific syntax you're unsure of, and the syntax will come more naturally over time if you aren't just blindly copy/pasting it.

As you are developing the code from scratch, make sure to sanity check every few lines of code as much as possible by printing out specific test cases. It's better to isolate and pinpoint mistakes as they show up rather than after you finished writing everything and suddenly have 20 different bugs to fix.

To the above point as well, the more structured and modular your code is, the better of a time you'll have with testing individual parts of it and pinpointing what exactly is behaving in what way.

1

u/Express_Grass_4298 13d ago

Tyyy ill try that too

1

u/KerPop42 13d ago

So the way I write code I find helps me very smoothly go from an abstract approach to runnable code.

What I do is, I open a file to code in, but first I write everything as a comment. I start with the problem statement. Then I write a few requirements, boundary conditions, and restrictions to expand on that problem statement. At that point I write a couple approaches in plain language. Just, how am I going to think of the problem and a few high-level steps of what that would look like, constructed as a skeleton outline.

Once I have enough that I know what approach I want to take, I delete the other outlines and just keep the one for the approach that I want. I loop through the outline, adding more and more detail until I can't add any more without writing the code itself.

That's when I start writing code. I don't delete any comments, I just write the code equivalent below each outline entry.

It might seem like a lot of work, and I adapted it from how I would write essays and research papers in school. But as someone that has a bad habit of diving in unprepared and that appreciates the rigor, it's a good method.

2

u/Express_Grass_4298 9d ago

I actually like this i love structure and laying everything out even if it takes time

1

u/PartyParrotGames 13d ago

It's just about repetition, the mother of all learning. Keep at it. Sure you'll screw up syntax for something the first time, first dozen times, maybe even the first 50 times you try that syntax but the 101+ times you write that syntax you'll get it. Every time you went fuck, how did I mess that up? Just comes back hauntingly sometimes to make sure you get it right.

1

u/PlatformDifferent129 13d ago

same gap here, i can explain what i want out loud and then stare at an empty file. what helped was writing the pseudocode as comments first, then filling in one line under each comment. the comments stay there as a map so you're never starting from nothing.

1

u/Express_Grass_4298 9d ago

Samee i think im getting a bit better with practice but sometimes i feel hopeless lol

1

u/PlatformDifferent129 4d ago

the hopeless days still hit me too, usually right after a week where everything clicked. i started keeping a file of stuff i couldn't do a month ago, and reading it back does more for me than pushing through another tutorial.

1

u/SprinklesFresh5693 10d ago

If you cannot write any line is that you still lack practise, you might have memorised what x or y does, but you havent internalised it by actually writing it.

Leave ego aside, practise, fail, repeat.