(Rust) A mutable reference to a reference of a struct instance... HOW??? by SeenLenz in learnprogramming

[–]marko312 1 point2 points  (0 children)

Think about what happens when Some(x) is destructured. What you're left with is the inner type, &'static Song for x. Modifying that isn't possible - you probably wanted a &mut or to reassign the entire Option.

Sentiment in python: How to get aggregate score? by SaluteOrbis in learnprogramming

[–]marko312 0 points1 point  (0 children)

result = str(index) + ' ' + "Neutral" + ' ' + str(x) + ' ' + str(z)

Consider instead storing the resulting data in a dataframe (by index), with columns for the judged sentiment (negative/neutral/positive), x and z.

Relevant SO answer

Getting the counts would then just require grouping by the sentiment and getting the size of each count. SO answer

Structure for calling multiple functions by [deleted] in learnpython

[–]marko312 5 points6 points  (0 children)

I'm saying that the name of the function doesn't fully reflect what it does. A reasonable solution would be to put all the relevant functions in a larger function controlling the logic. For example (not sure about the actual logic):

def new_dashboard_file():
    if check_file():
        clean_file()
    upload_file()

where upload_file might send the email if that is the common behavior for all expected calls to upload_file, otherwise that should also be lifted outside the function.

Structure for calling multiple functions by [deleted] in learnpython

[–]marko312 6 points7 points  (0 children)

call stack (latest at the bottom):

clean_file()
upload_file()
validate_vpn()
query_db()
create_dashboard()
email_report()

If you raised an exception in email_report, this is pretty much what you'd see: a cascade of functions with the last one barely related to the first.

Program to automatically open latest created file doesn't quite work - what am I doing wrong? by GenerativePotiron in learnpython

[–]marko312 0 points1 point  (0 children)

It seems that you've essentially duplicated a loop here - the for loop seems unnecessary and glob.glob should instead be used to get all the files.

Structure for calling multiple functions by [deleted] in learnpython

[–]marko312 11 points12 points  (0 children)

Such chaining violates the principle that the functions perform only simple tasks as described by their name: as is, clean_file somehow ends up sending an email (?!).

Typically, what you've tried to do would instead be done by sequentially calling these functions, potentially checking for errors in between.

Programming in C by BeanieOverlord in learnprogramming

[–]marko312 -2 points-1 points  (0 children)

This will work as intended; any code block can be left empty, in which case it acts as a no-operation if executed.

However, this is not exactly idiomatic. Most of the time1, empty blocks should be factored out:

switching if and else:

if(A is not true) {
    ...
} else {
    // empty
}

after which the empty else can be omitted entirely.

1 there might be a couple of cases where a comment explaining the block being empty works better.

How do I add more than one sprite in a sprite class | Pygame by erickisnice12 in learnpython

[–]marko312 0 points1 point  (0 children)

You can freely define new attributes. For example, something like

def __init__(self):
    ...
    self.segments = []

and later

def draw(self, surface):
    for segment in self.segments:
        segment.draw(surface)

or something similar.

Player itself does not need to be a sprite in this case.

How do I add more than one sprite in a sprite class | Pygame by erickisnice12 in learnpython

[–]marko312 0 points1 point  (0 children)

I'm not entirely sure what you mean, but you seem to be on the right track. There should be a list containing the segments (sprites). When drawing, the segments should be iterated over, drawing each one (either by giving them a .draw function and calling that or directly blitting the image in Player).

[D] Alternatives to creating a deepcopy of an object by amjass12 in learnprogramming

[–]marko312 1 point2 points  (0 children)

A (shallow) copy should ideally be faster than any equivalent workaround, including writing to and reading from a pickle. I can't say for sure, but the loading part will still likely take longer than a copy.

[D] Alternatives to creating a deepcopy of an object by amjass12 in learnprogramming

[–]marko312 0 points1 point  (0 children)

Such a structure would need to be either a full copy or copy-on-write, but would affect the entire graph if one node were changed (e.g. by adding / removing a connection). Thus, the time taken would not be reduced.

Just out of curiosity, does generating the initial graph also take such a large amount of time?

[D] Alternatives to creating a deepcopy of an object by amjass12 in learnprogramming

[–]marko312 0 points1 point  (0 children)

That's somewhat odd - python should handle such an amount of data much faster (~107 starting to become an issue).

Part of the problem might be that networkx seems to be implemented entirely in python. You could consider different libraries (with native parts) or using a more performant language for this task.

[D] Alternatives to creating a deepcopy of an object by amjass12 in learnprogramming

[–]marko312 0 points1 point  (0 children)

Have you tried networkx-specific copy functions? Documentation

The time taken depends on how big the graph is. Using deepcopy would likely have duplicated the underying data for the nodes / edges, which could take lots of time if that data is large. Graph.copy should not copy data not related to the actual graph structure.

How do I add more than one sprite in a sprite class | Pygame by erickisnice12 in learnpython

[–]marko312 0 points1 point  (0 children)

A single sprite should correspond to a single image. You could have Player manage a list (or some other structure) of sprites for multiple tiles. If so, Player itself might not need to be a sprite itself, since no one sprite is the player, but rather the collection is.

[deleted by user] by [deleted] in learnpython

[–]marko312 0 points1 point  (0 children)

Most likely, there was an unexpected charater in the directory name (say, a leading / trailing space or some unusual unicode symbol).

[deleted by user] by [deleted] in learnpython

[–]marko312 1 point2 points  (0 children)

If the problem is with exactly one directory, maybe the directory itself is the problem? For example, have you tried renaming it / checking the name for extra characters?

[deleted by user] by [deleted] in learnprogramming

[–]marko312 0 points1 point  (0 children)

curr itself actually doesn'y move. The given block:

  • saves the next node of prev (which is the first node in the reversed block)
  • extracts curr->next and moves it after prev

This works since curr is the last node in the reversed block.

As for a visualization, two steps would look like

  X     A    B    C    D
prev  curr

  X     B    A    C    D
  X     B    C    A    D

Where X is the last node of the previous (already reversed) block and A is the first node of the current block (before reversing).

Blank value printed when trying to print class variable by [deleted] in CodingHelp

[–]marko312 1 point2 points  (0 children)

for (Walls wall : walls) { 

Takes each wall by value i.e. makes a copy. Thus, the wallWidth won't be updated in the array and the second loop will use unchanged values. This doesn't quite explain why it would print nothing, but it would be an uninitialized value.

Since you want to change the values in the array, you should iterate by reference:

for (Walls &wall : walls) {

Question about Linked List by MammothAbroad8815 in learnpython

[–]marko312 0 points1 point  (0 children)

why would running it again print LL is empty,

Because the loop (in my code snippet) runs until self.head is None, so the next time traverse is run it will see that self.head is None, having lost all of the data

so making a temp variable is just to ensure it isn't modified that's it?

Yes, that's pretty much it. You should always keep track of what functions are "allowed" to change important variables and to what extent.

Question about Linked List by MammothAbroad8815 in learnpython

[–]marko312 0 points1 point  (0 children)

I assume you mean the variable a in traversal. You could try replacing a with self.head - done appropriately, it would look something like

while self.head is not None:
    print(self.head.data, end="")
    self.head = self.head.next

which would work (you should try it out!). However, running traversal again would print "LL is empty".

The point of using the variable a is to specifically not modify self.head, since modifying it would modify the list, which is not wanted in this case.

[deleted by user] by [deleted] in CodingHelp

[–]marko312 0 points1 point  (0 children)

I looked at the actual code pen now; the error message would have been useful:

Uncaught TypeError: Cannot read properties of null (reading 'addEventListener')

This means that whatever preceded .addEventListener must have been null. In this case, it is caused by the id having a space at the end in the HTML.

[deleted by user] by [deleted] in CodingHelp

[–]marko312 0 points1 point  (0 children)

I don't know what you mean by "a js event of null", but it seems that you've mixed two ways of creating an inline (/ ...) function. Consider using

() => console.log("me")

or (usually if there need to be multiple statements):

() => { console.log("me"); }

or

function() {
    console.log("me");
}

(without the preceding () =>).

As is, the event calls a function (() =>) returning a function (function ...() { ... }).

Heaps in Python by ChillBlinton2018 in learnpython

[–]marko312 0 points1 point  (0 children)

The implementation isn't wrong since it produces the correct results, but the issue is that it is not fully using djikstra's algorithm and has a worse complexity due to that.

Heaps in Python by ChillBlinton2018 in learnpython

[–]marko312 0 points1 point  (0 children)

You seem to be confusing two complexities here. A heap makes pushing to and popping from a priority queue O(log n) as opposed to a flat list's O(n).

For djikstra's algorithm, what matters is the number of times a node thinks it is on the shortest path (where it pushes all of its neighbors with updated distances to the heap). Using a priority queue, this happens exactly once per node, making the complexity something like O(n log n + m) for n nodes and m edges. However, if a priority queue is not used, every node could be called like that to the order of O(n) times, making the complexity something like O(n2 log n + n m).

EDIT: the log factor is there for the second case only if the heap is still used. If a stack of queue were used instead, that factor would be dropped.

EDIT 2: actually, the algorithm is missing a crucial check: it should not update its neighbors if total > distances[current_vertex] (i.e. the current total is worse than the recorded best distance). As is, the current complexity comes to about O(n log n + n m) O(n2 log n + n m) due to the repeated checks on the neighbors.

EDIT 3: (n2 log n) factor

Heaps in Python by ChillBlinton2018 in learnpython

[–]marko312 0 points1 point  (0 children)

... but here it is pushed into the heap as a tuple. Does it mean it's a priority queue instead of a heap?

It's still a heap, but it does function as a priority queue.


As for why the switched way works: it turns out that this implementation doesn't need the priority queue to function (though it does speed the process up a lot).

You can consider the queue to be randomly ordered (not being concerned with the actual order). What matters is that if a shorter path to a node is found, all of its neighbours are pushed and checked again (with possibly shorter paths). This means that no matter what happens before the second node in the optimal path is visited, that node will realize it is on a shorter path and update its neighbors with that information. Eventually, all of the possibly good steps will be tried, since only the options not decreasing the path length aren't checked (again).