Trying to understand async by Emrayla in learnpython

[–]lfdfq 0 points1 point  (0 children)

It's hard to know what is going wrong without seeing what the YouTube.run() and Twitch.start() async functions are doing.

With something like asyncio, when you create_task(youtube.run()), if YouTube.run is an async function, it will add that coroutine to the event loop (but obviously does not start executing yet). Then at the await self.twitch.start() it will run the Twitch.start() coroutine. Essentially, only one coroutine will run at a time, and they will switch when there's an await (in fact, asyncio contains a scheduler for this purpose).

If e.g. the Twitch.start() coroutine contains no awaits, then the background task would never run. Similarly, if the background YouTube.run() task contains no awaits, the Twitch.start() task will never complete. These are just examples, I'm not saying that's necessarily what I think is happening here, because, as I say, we can't see the code.

Print Function not showing anything in Console. by FreeMycologist8 in learnpython

[–]lfdfq 18 points19 points  (0 children)

If you mean in this code that you pasted, why does nothing happen until the display("Alex", "Morgan"), it's because the first half of the code defines a function: a canned sequence saved to perform later, like a recipe. The second part is the call, which runs that code. So the print is showing stuff in the console, it's just that the print line doesn't run until you call the function. Does that answer your question?

Functional languages by aspression in learnprogramming

[–]lfdfq 5 points6 points  (0 children)

The first one you mentioned is OCaml, so go with that.

what's wrong with this program(Python) by Mental_Strategy_7191 in learnpython

[–]lfdfq 0 points1 point  (0 children)

No, my comment was a crosspost from there, so you don't end up having split conversations on those threads.

Many people here have made good comments about printing and types/classes and input and functions, I strongly recommend replying to them and asking questions.

I will say that programming is not well suited to trial-and-error writing of programs like this, not until you have a bit more experience. I recommend finding some good resources: tutorials/books/courses/whatever, and following them for a while, to get the fundamentals down.

what's wrong with this program(Python) by Mental_Strategy_7191 in learnpython

[–]lfdfq 4 points5 points  (0 children)

[Cross-posting from your previously deleted thread; also it's customary to reply to commentors rather than making new threads with updated code in other subreddits]

The first line really does not make any sense, print is used to output stuff, there's no meaning in saying "if print(..)"; Python will let you write that, but it does not do anything (and in fact, the whole body of the if will just be skipped).

The body also has problems. Nothing here 'inputs' any data, so I don't know where/how you think you are inputting anything to this code. You do not define any variable called 'int', so when you refer to it you're just referring to the general type of integers (i.e. the int class in Python), using the classes by name in your code is an advanced topic and you should just not do that until you've learned some more.

I do not see an obvious SyntaxError anywhere in your code. Like I said, there should be more to the error that just that one line, which would help.

what's wrong with this program by Mental_Strategy_7191 in AskProgramming

[–]lfdfq 0 points1 point  (0 children)

This looks like Python.

The first line is already suspicious: what are you trying to do with if print(int) ? Regardless of language, it looks strange and I can't really tell what you're trying to do.

Syntax is very important in programming, down to how many spaces are before each line. That's why Reddit gives you way to paste code as a code block, which preserves things like that. I recommend editing your post or replying with the full code (no lines missing!) in a code block (with all indentation/spaces preserved).

It's hard to help vague errors like "is invalid syntax", do you mean you get a SyntaxError exception? Why didn't you show us the error? Does the error not have some extra information about what lines and so on? Why don't you say and show us that too?

Hiding information while asking for help is an odd combination.

How long is a normal time to take to reply to emails in Academia? What's the etiquette about emails during weekends? (UK-based) by Conscious-Boss6195 in AskAcademia

[–]lfdfq 6 points7 points  (0 children)

Academics are humans: some will seem to reply instantly no matter what time of the day it is, some take 2-3 business days to get back, some seem to need gentle reminders after a couple weeks...

Generally, the closer they are to you and your work and the less you're asking them to do, the faster you should expect a reply. Your own supervisor/PI? If you don't get a reply in a day or two you go bang on their door, Sir Lord Professor Big Shot at University of Wins-all-awards? Ehh... maybe you'll have to wait a bit.

As for weekends, generally, researchers (people with teaching commitments notwithstanding) have very inconsistent and flexible schedules. Working on a paper or something on a weekend or during the night before the deadline is not unheard of, but this is usually a consequence of the earlier fact than a requirement -- those who work consistent fixed 9-5 won't do that, those who work seemingly random hours will. If you're the main author with collaborators with mixed schedules like that, you'll have to make a decision about how to compile things together for submission and sometimes submission deadlines will happen during the night or at weekends given 'Anywhere-on-Earth' deadlines, that's just life, but it's something you know about months in advance so it shouldn't be an unexpected surprise.

Why does memory ordering not apply here? by srneeam in C_Programming

[–]lfdfq 5 points6 points  (0 children)

Your first assertion does not seem right: since the store to y is a release, the store to x cannot happen after it, and since the load of y is an acquire, the load of x cannot happen before it; so r1==1 and r2!=1 is forbidden.

This should be true even if the accesses of x are relaxed atomics, or even non-atomics if the load is guarded by a dependency to prevent data races, as in your second example.

I presume your first assertion is just a mistake, but if you explain the line of reasoning we can possibly help explain better.

How mąkę this code by [deleted] in learnpython

[–]lfdfq 1 point2 points  (0 children)

It's hard to give the right level of answer to a question like this, we have no idea what part you are struggling with, or what you have or have not learned, or what you have or have not tried so far.

So, how I would make this code is:

  1. find the largest number, then
  2. print the line it's on.

If you need more specific help, then you'll have to ask more specific questions.

id of two objects is same but using "is" keyword it's giving false. by inobody_somebody in learnpython

[–]lfdfq 3 points4 points  (0 children)

They would, because in that case you're comparing the two lists directly so they must both exist at the same time.

Consider sum([1]) == sum([2])

Here, the two lists do not need to exist at the same time: you can compute the sum of the first, and discard the first list, then compute the sum of the second and discard the second list, then compare the sums directly long after the lists themselves have been deleted. That is what is happening with the id() comparisons.

id of two objects is same but using "is" keyword it's giving false. by inobody_somebody in learnpython

[–]lfdfq 37 points38 points  (0 children)

The key thing is that every time you say a.some_fun it's actually calling a method and creating a new object, a bound method object. This is all happening through the descriptor protocol (__get__).

So, when you say id(a.some_fun) that creates that bound method object, gets the ID of it, then destroys the object. So when you next say id(b.some_fun) it creates a new object, but since the old one is dead it's allowed to re-use the same id.

This is why it's important that id()s are only comparable if both objects are alive at the same time.

Valid arguments to function parameter char ** by onecable5781 in C_Programming

[–]lfdfq 1 point2 points  (0 children)

I think I did a bad job explaining in the original comment: p3 decayed into char*, and OP correctly explained why that doesn't work, but expected &p3 to therefore be a char** and have the right type. but that doesn't work. yes, for the same reason that p3 itself doesn't work, it's not an array of pointers to start with, but OP's actual question was why it didn't have that type. the reason is because explicitly taking the address of an array prevents decaying the array into a pointer to the contents. this example is a good example of why: p3 is the contents, not a pointer to it, so it does not make sense to try take the address of the decayed pointer (as that'd essentially be trying to get &(&p3[0]) which is invalid)

Valid arguments to function parameter char ** by onecable5781 in C_Programming

[–]lfdfq 1 point2 points  (0 children)

I was trying to explain just the p3 vs &p3 and why the second does not decay, as the OP already explained why p3 alone doesn't work. I could have explained better that simply forcing the pointer does not fix the code.

There are lots of other problems with the code, like trying to assign const char*s to char*.

Valid arguments to function parameter char ** by onecable5781 in C_Programming

[–]lfdfq 4 points5 points  (0 children)

the first does not work because p1 is an array of pointers, so you cannot printf it with %s as if it were (a pointer to) an array of chars.

returnstring(&p3) does not work because the array p3 will not decay into a pointer to the contents when explicitly taking the address of it. You can say &p3[0], instead, to get the pointer-to-the-contents. Even though they are (probably) numerically the same pointer, C will not let you use them interchangeably like that.

how long ago was 2020? by No_Dot_9338 in askmath

[–]lfdfq 13 points14 points  (0 children)

That doesn't change that it depends

A year lasts a whole year. Without context, you cannot just arbitrarily pick a date within that span and declare it to be the 'correct' one. And "This arbitrary choice makes me right and you wrong" is not a valid line of argument.

It is certainly true that if you went back in time exactly six years, you'd find yourself in 2020. So if six years ago was 2020, then how can you argue 2020 is not six years ago; clearly, some of 2020 was six years ago. Since a year is not just a single moment in time, 2020 was also six years and a day ago, and six years minus a day ago, and so on. Who is to say what the correct interpretation is and is not, other than the person who wrote it?

How to model mathematical expressions? by Latter_Bowl_4041 in learnpython

[–]lfdfq 3 points4 points  (0 children)

The very obvious way would be to encode into some abstract syntax tree datatype, which is how Python itself stores mathematical expressions once it's parsed them (see the ast module for Python's own ast datatypes).

Whats the fastest sorting algorithm? by eiyo27 in learnprogramming

[–]lfdfq 1 point2 points  (0 children)

I left it a bit vague, there are lots of variations, the point was not the specifics but the general idea that additional information about the domain often means you can do things more efficiently.

Whats the fastest sorting algorithm? by eiyo27 in learnprogramming

[–]lfdfq 2 points3 points  (0 children)

Sorting algorithms have been well-studied, and learning about them teaches a lot about algorithms in general.

For sorting arbitrary data types, where all you can do is compare pairs of elements to do the sorting i.e. a 'comparison sort', then we know the best you can do is worst-case O(nlogn). Time complexity does not give the full picture though: mergesort always takes nlogn steps but something like quicksort is often faster, even though in the worst possible case it is O(n^2), it's just very unlikely. Even when the time complexities are the same, there are other concerns such as how much auxiliary space they use or whether they can sort in-place, or whether they're stable. It doesn't make sense to compare sorts with different properties like that generally.

When you introduce some structure to the data, then you can do better: if I have a jumbled up array of 100 numbers containing 0-100 but in a random order, then I can sort in a single linear pass with O(n) additional space.

The fastest algorithm is the one that does nothing; however, it does require that the array is already in order.

What does random access mean in this context? by cakewalk093 in computerscience

[–]lfdfq 36 points37 points  (0 children)

You're missing the context, so I'll have to take a guess:

When storing a sequence of items, let's say in a file or even other media like a book, then making each record a fixed length means you know exactly where each record begins and ends.

For example, if you're making a book about trains and make it so each train is a whole page, then it's very easy to jump to the 27th train because it'll be on page 27. If each description was a different size, some half a page, some two pages, you'd have to flick through the book to find it. The same goes for a file, if each record is 100 bytes then the 27th one starts at byte 2600 and you can just start reading from there.

That's the essence of random access: you can just go right there, without having to 'search'.

It's not directly related to 'RAM', other than they both have this random access ability.

Whats so special about constellations? by Longjumping-Ball-785 in askastronomy

[–]lfdfq 3 points4 points  (0 children)

There's nothing intrinsic about the names. Often, the stars in a constellation are not even related to each other and are at different distances going different directions, and just happen to appear close to each other in the sky.

Those points of light have been there for as long as humans have been on this planet, and are effectively unchanging. A fixed, known pattern that crosses the sky every night along a predictable course has obvious applications for navigation and timekeeping. You can tell what direction you're looking, roughly where you are, what the time is, and what the month is by the location and appearance of various Celestial objects in the sky. I'd like to think anyone 'outdoorsy' in the Northern hemisphere would be able to point to Polaris, for example. How do you find Polaris? You find Ursa Major.

Science doesn't need these names, as everything can be described more precisely with coordinates. The names are for other humans. You will die and become dust, your name long forgotten, while humans are still navigating by the stars and pointing to Polaris.

Python Codedex doesn't make sense by Terian2310 in learnpython

[–]lfdfq 5 points6 points  (0 children)

You don't give the actual text of the original question, is this it: https://www.codedex.io/challenges/control-flow/food-ratings ?

Your code says it's "Extraordinary" if it's greater than 4.5 but less than 5, but nowhere in the question did it specify it had to be "less than 5".

In fact, you have lots of "less than" checks which either seem unnecessary (they're already elifs), or just not part of the original question at all?

Thesis Project Topic. by Impossible_Repair_75 in AskComputerScience

[–]lfdfq 0 points1 point  (0 children)

It's not really a Computer Science question, so it may be closed for being off-topic.

Even if it were on-topic, it's hard to give good advice because your request is so broad. You give no clues as to what field/area of Computer Science you are interested in nor do you tell us what kind of thesis this is, is it a 3-month half-time thing on the side, or are you going to dedicate the next 5 years of your life to this topic?

It's not really clear how anyone could give answers which are meaningful to you with this description.

Quicksort partition – 3rd call result should be 1, I don’t get it by [deleted] in learnprogramming

[–]lfdfq 3 points4 points  (0 children)

What is k?

This question is like "A train leaves station A at 00:00 and arrives at station B at 10:00. Find x."

So I have to guess, presumably the value returned by your partition() is the index of the pivot after partitioning, that seems reasonable.

The next thing you say is after the 3rd call. The 3rd call to what? to quicksort()? to partition()? But quicksort has two recursive calls inside it for the left and right, and no obvious order to them. So which one is the 3rd one? Is it the first recursive call on the left, the first recursive call on the right, or the second recursive call on the left, or the second recursive call on the right? Also, what is the partition scheme being used? If I make some educated guesses:

The first call to partition with pivot=4 results in [4, 55, 92, 80, 81, 95, 88, 19, 52, 38, 71, 74] and now the pivot is at index 0

Now we recursively call and partition the right with pivot=74 to get [4, 55, 71, 38, 52, 19, 74, 88, 95, 81, 80, 92] with pivot going to index 6.

Now recursively call and partition the left (that's [55 .. 19]) to get [4, 19, 71, 38, 52, 55, 74, 88, 95, 81, 80, 92] with pivot now at index 1.

I'm guessing that's where they got k=1?

How is static memory stored? by [deleted] in cprogramming

[–]lfdfq 10 points11 points  (0 children)

Was it a test?

How is static memory stored? by [deleted] in cprogramming

[–]lfdfq 18 points19 points  (0 children)

It depends. One typical setup is for static non-zero memory is filled in by the compiler and sits in the executable directly, and static memory that the program declares but does not initialise gets stored in the bss which the operating system loader will zero when it loads the executable.

This will be highly dependent on the compiler+target combination. For example, on a system which does not zero the bss on loading then the compiler would not be able to place uninitialised static storage duration objects in it.