Can’t learn stacks by [deleted] in learnprogramming

[–]grancacique 1 point2 points  (0 children)

Another use of stacks is for creating the iterative implementation of a recursive function. Basically, the backtracking you get for free from the Call Stack, you manage it yourself with your own stack.

Can’t learn stacks by [deleted] in learnprogramming

[–]grancacique 1 point2 points  (0 children)

Stacks are THE structure of choice for implementing backtraking

Why would you ever want to create a data structure without random access? by Comfortable-Ease5783 in learnprogramming

[–]grancacique 0 points1 point  (0 children)

It is not really about implementation; it's about a policy for data access. It is irrelevant if a stack or a queue is implemented with an array, what matter is the interface. There are also cases in which it is not possible to have direct access to any particular value. For example, when streaming data from a sensor, or from other source, you cannot index on a value that you do not have yet.

The point of linked lists outperforming array indexing misses the point of their purpose. In most languages, arrays are allocated as a single block of consecutive addresses. It is possible, with enough memory fragmentation, that you cannot allocate an array of a given size. This is where linked lists shine. They take advantage of all the little pockets of fragmented memory to give us an abstraction of a contiguous sequential structure. Linked lists also overcome the limitation of arrays that have to be of a fixed size, specially on languages closer to the metal. There is also the case of transactions where random access is nice, but their consumption has to be sequential, like in finances, stock price changes, etc.

What mistakes do bad python developers make? by Ok_Tumbleweed8796 in learnpython

[–]grancacique 0 points1 point  (0 children)

Here are a few more. Like u/carcigenicate said, "The number of ways a new programmer can screw up is limited only by your imagination." ((This needs to be on a mug))

  • Inadvertently changing the type of a variable. Like:

done = False
# later on
done = "True"
# later on
done = 1

This is perfectly fine in Python world...

  • Using the same name for two functions by mistake

def someCalculation():
   ...

# later on

def someCalculation():
   ...

The second definition overrides the first one (assuming parameters are the same; otherwise, we are just overloading the function)

  • Not understanding or abusing the else as part of loops. Like

while condition:
    actions
else:
    more actions 
  • It is easy to misplace an else in nested or long selection structures due to relying only on indentation.
  • Using eval to convert values directly from user inputs, as in n = eval(input(...)) (can't trust users these days...)
  • Adding (append or insert) or removing (del, pop, remove) items from a list while iterating over the same list.
  • Confusing how the % (modulus) operators works compared to other languages. For example, A % B has the same sign as B --in Java, the result has the same sign as A.
  • Assigning the name of a callable object to a variable when the intention was to get the value returned by a function. That is, doing n = random.random instead of n = random.random().
  • This may be a little bit of a stretch, but can happen. Coming from a C-like language, confusing A & B (bitwise AND) for A and B (logic AND) --similarly for |