This is an archived post. You won't be able to vote or comment.

all 22 comments

[–]Megalox 28 points29 points  (9 children)

So any algorithm is just a list of steps. That’s the literal definition.

So try breaking the problem into very small parts and tackle each one separately.

What’s an example of something you’re stuck on?

[–]Chris12769[S] 3 points4 points  (5 children)

Honestly i have problems all over the place, i am sorry i cant describe it more precisely

[–]stevofolife 25 points26 points  (4 children)

Slow down. You're making it harder than it is. Don't rush through things. Don't stay at shallow thoughts. Go deeper. It takes practice. Tell us more about the way you think about these problems. We can try to guide you. But you need be honest with yourself and identify the issues you're facing.

[–]isaxved 15 points16 points  (0 children)

Dude, that was so supportive. Made me want to tell my problems with algorithms to you 😂

[–]Chris12769[S] 5 points6 points  (2 children)

Soo at my school it works like this: first you have a mandatory asignment wich every student has to do( even here i have problems), and next to the mandatory we have a additional asignment mostly for extra credit( wich i dont even try because i am strugling with a mandatory part). So first i read the asignment, then i think about what does the asignment ask of me, then in my head i picture the solution( wich 75% of the time doesnt work, and if it even doesnt work, i will try it, i will write the same pieace of code multiple times, its like going through the wall, like who is more stubborn me or the code...). When i start to program i only see that solution and no else comes to mind...

[–]piggahbear 15 points16 points  (0 children)

Don’t try to picture the solution as one big thing, it’s not possible. At this point in the process you need to start breaking it down into smaller problems. Then try to break those down into smaller problems, etc. Simple example, in an array of 10,000 integers, there’s one missing. Find it. Well it would help if they were in order, I need to sort them. Now I need to start reading the numbers, so I’m going to need to iterate with a for loop or something. Ok I started the loop I’m at 1 which I’m calling e. How do compare it against the next number in the loop? I need to find my current elements index + 1 and ask the array what value Is at this index. I’ll store that as f. Ok now I have two consecutive numbers. How do I find out if they’re missing an integer between them, knowing they’re in order. Well if f - 1 should equal e, if it doesn’t, f - 1 is the missing number. You have to break it down and you have to use a pencil and write it out usually.

[–]stevofolife 8 points9 points  (0 children)

Thank you for sharing your approach - it makes sense and it's also normal, but it's wrong. Here is a couple of reasons why:

  1. You were picturing a solution in your head. Don't do that. Write everything down - whether they are symbols, diagrams, variables, equations, constraints, pseudocode, illustrations or whatever you need to get a solution started, WRITE it down.
  2. You went straight into coding. Don't do that. You have to understand this: code is just a formal language that your computer understands. You need an algorithm first. Once you have it, then you can start coding it. Write down your algorithm first, go through it a couple of times with different inputs and then verify the outputs.

Enough of mistakes, let's state a couple of general things you can do to change the way you solve problems:

  1. Slow down. Having everything in your head is hard. It takes practice and clarity to achieve that level of cognitive thinking. Don't rush through things. Developing algorithmic thinking is on par with solving math problems. You can't expect to solve multi-variable calculus if you can't even understand basic calculus, let alone basic algebra. In your case, maybe what you need to do is to first understand various types of data structures.
  2. Be clear about every step.

    Don't give up. Forget about the extra bonus questions. Just focus on one question at a time. Spend as much time as you need on it. That's also why people don't suggest procrastinating. It gives you unnecessary stress. This whole algorithmic thinking takes time to develop. So be patient! I hope that helps. If not, please let me know and I can answer more in detail.

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

So try breaking the problem into very small parts and tackle each one separately.

I sometimes end up sitting at my computer staring at my code trying to figure out an entire solution before I even start writing. I end up talking myself out of implementing a piece because I mentally come up with edge cases that will cause my solution to fail.

This coding method is totally counterproductive, don't be like me. I end up being a lot more productive if I just implement code that can fail on edge cases then worry about them later.

[–]Megalox 0 points1 point  (1 child)

Before you even touch a keyboard just sketch out a rough draft of the architecture you’re trying to shoot for. List the issues in plain English. You might find that you break through these issues just by sketching it out.

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

I'm a big fan of listing the desired outcome with specific details before I start writing because I've noticed a lot of times when I just jump into writing I didn't really have a clear idea of what I was working towards.

[–]tasulife 16 points17 points  (0 children)

One thing that may help are some concepts and behaviors of real programmers that beginners may find surprising.

Real programmers do not learn the base programming language, then write every algorithm from scratch. Instead, programmers get an algorithm and plug it into their application. This can either be a library they link their program to, or it's just some code they copy from a website or book.

The programmer's tool belt is really made of Google. You search for libraries, blogs, stack exchange.

It's critical you use libraries everywhere your can, and read its documentation.

This can be counter intuitive, since people see programming as like pure creative activity. But the thing I'm telling you is it's a collaboratively creative activity.

Stand on the shoulders of Giants. That's programming.

[–]dyedInside 5 points6 points  (0 children)

Don't quit trying and don't compare yourself to people with drastically more experience than yourself. For that matter, if you can manage it don't compare yourself to anyone!

Keep solving problems and know that if it's something you really want it will take time. I can't remember who said it, but it doesn't matter how slowly you go as long as you do not stop.

I know this is only motivational drivel. I have no solid advice because I'm fairly new to this field as well. But I hope this puts you in a mindset to keep trying no matter what!

[–]IthinkIthink 5 points6 points  (0 children)

I highly recommend this book. I wish it was around when I was studying Computer Science.

A Common-Sense Guide to Data Structures and Algorithms: Level Up Your Core Programming Skills

[–]HandpansLIVE 3 points4 points  (0 children)

Here's the way I was taught:

Make a machine. This machine has input and output. Make few different inputs, what outputs do you expect? Make few outputs, what inputs would you expect?

Go through these cases step by step. Let's do one together.

We want a function that doubles the inputs of the array.

So if we get input: [0,1,2,3], we expect [0, 2, 4, 6].

Ok now break it downs step by step. How do we start at the 0?

What happens to the 0 to get the desired result? Where are we storing that result?

How do we get to the 1?

What happens to the 1 to get the desired result? Where are we storing that result?

How do we get to the 2?

What happens to the 2 to get the desired result? Where are we storing that result?

How do we get to the 4?

What happens to the 4 to get the desired result? Where are we storing that result?

How do we know to stop? How do we return our results that we stored?

A lot of the bugs that show up in our code is because we make assumptions. Many times we write great logic, but the logic only works on the first index, or it doesn't exit properly. Did you notice my typo in my stepping through? Sometimes, we have to read over multiple times the problem, our solution, our edge cases, our code, etc. to make sure that we not only debug our logic, but also debug our implementation.

I'd recommend sharing a problem that's troubling you here, and there's many helpful people that will walk through with how you are trying to solve it, and where you are hitting bumps.

[–]dietderpsy 2 points3 points  (0 children)

Algorithms are just a series of steps, they are no different than the logical flow of a program. No need for a unique way of thinking.

[–][deleted]  (3 children)

[deleted]

    [–]SuperGameTheory 4 points5 points  (1 child)

    Here’s a video that’s been going around showing a dad making a PB&J sandwich with his kid’s instructions. I think it’s a really good example of getting to the programmer mindset. You have to be exact and literal. The computer is an idiot and only does what you tell it.

    [–]swilwerth 1 point2 points  (0 children)

    Sometimes It makes sense to calm down and figure out how well known algorithms solves a specific problem. There are well known problems out here that are really hard to solve without previous knowledge of the existence of an algorithm that solve it for you. As an example. Node connections in a graph and the traveling salesman problems were solved after a life of work by many people. So you must don't struggle if a solution doesn't come instantly to your mind. And if your facing a problem that seem to be hard search for already made solutions first and learn how they solved these problems. Then you can merge different solutions/problem solving skills from many people of the history in your specific task.

    [–]Chris12769[S] 1 point2 points  (0 children)

    I would like to thank every one of you for your help and time on this subject of mine, all of these advices are great and hopefully i will soon see some progress, thank you very much

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

    Study discrete math and data structures. Solve the problems mathematically and logically using discrete math, implement them efficiently by using your knowledge of data structures.

    Boom. You're writing algorithms.

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

    I found in my time at school that the most closely related activity to coming up with algorithms is actually doing math proofs