"""Returns True if n is prime."""
def isprime(n):
if n <= 3 and n > 1:
return True
if n % 2 == 0 or n % 3 == 0:
return False
i = 5
w = 2
while i * i <= n:
if n % i == 0:
return False
i += w
w = 6 - w
return True
The Big Programming Thread - Page 995
| Forum Index > General Forum |
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. | ||
|
Manit0u
Poland17450 Posts
| ||
|
Acrofales
Spain18132 Posts
| ||
|
mahrgell
Germany3943 Posts
In fact, why not be cool and impress anyone who later reads this code, by using the regexp ^.?$|^(..+?)\1+$ as prime checker? I'm sure they will all bow to your skills! (and hopefully never have to run the code) | ||
|
solidbebe
Netherlands4921 Posts
On February 09 2019 16:59 Manit0u wrote: If you need a list of primes you can always generate them yourself... + Show Spoiler +
thats a yikes | ||
|
Deleted User 3420
24492 Posts
| ||
|
SC-Shield
Bulgaria832 Posts
On February 09 2019 19:09 mahrgell wrote: Because coders pride. Why use an existing list of primes, when you can write (a highly inefficient) algorithm to make one? In fact, why not be cool and impress anyone who later reads this code, by using the regexp ^.?$|^(..+?)\1+$ as prime checker? I'm sure they will all bow to your skills! (and hopefully never have to run the code) It's probably human mentality. Why pay a handyman when you can do it yourself even if it's done really bad? Easily noticeable if you rent a property and you deal with landlord directly. :D | ||
|
graNite
Germany4434 Posts
i want to get a set of all points that have the maximum height. max_height = max(self.height_at(p) for p in self._points) | ||
|
Frolossus
United States4779 Posts
On February 11 2019 08:38 graNite wrote: can i simplify this somehow? i feel like this should be able to be done with only one height_at call. i want to get a set of all points that have the maximum height. max_height = max(self.height_at(p) for p in self._points) do 1 loop and bind self.height(p) to a temp variable? | ||
|
graNite
Germany4434 Posts
current_max = 0 i dont know if this is faster...i would like to use a set comprehension ![]() | ||
|
Acrofales
Spain18132 Posts
On February 11 2019 08:57 Frolossus wrote: do 1 loop and bind self.height(p) to a temp variable? E: nvm, misunderstood the question. You could do it in a single loop if you wanted, but it would be more complex, not simpler. You could make a map from heights to lists of points. That way you only loop once, but I doubt it's much faster (you have to construct the dict). | ||
|
Artesimo
Germany564 Posts
The amount of files make it hard to do this by hand. I know that there are ways to batchprocess files with audacity but here comes another problem: We aren’t sure if all the original and additional .wavs are similar in terms of volume which got me thinking. The base volume has to be encoded in some way right? Therefore there should be a way to read out this base volume? I am looking for a way to just check how "loud" the basevolume of all the files is and deduct from that how much the added sounds need to be amplified with maybe some individual adjustment rather than adjusting every sound by hand. Does anyone has experience with this or can share some insight how the encoding of the volume actually works? | ||
|
Deleted User 3420
24492 Posts
On February 11 2019 08:38 graNite wrote: can i simplify this somehow? i feel like this should be able to be done with only one height_at call. i want to get a set of all points that have the maximum height. max_height = max(self.height_at(p) for p in self._points) its impossible to do it in a comprehension because a comprehension only does one loop through but you can't know what the max is until you've gone through all elements, and there is no way to go back and change what you have already added to the comprehension well, I mean you can, but then you end up asking what the max height is for every single element, and you obviously don't want to do that On February 11 2019 09:22 graNite wrote: do you mean like this? current_max = 0 i dont know if this is faster...i would like to use a set comprehension ![]() this is probably faster what would be even better is to have a Max variable for whatever this object is | ||
|
graNite
Germany4434 Posts
The object is the height map the sc2 api provides. This is for the Python-sc2 library. | ||
|
Deleted User 3420
24492 Posts
I've used python-sc2 a little, but not much can I ask what are you trying to do? you are editing the api? | ||
|
graNite
Germany4434 Posts
And the 'lower' property which is the same but with minimum. | ||
|
Deleted User 3420
24492 Posts
| ||
|
emperorchampion
Canada9496 Posts
On February 11 2019 09:46 Artesimo wrote: I am trying to help a friend with a project regarding audio files and volume, regarding .wav files to be precise. He wants to make a soundmod for a game but the problem is that the original and the added files have different base volumes. Generally speaking I know how to normalize / amplify sounds using audacity, however this has some problems. The amount of files make it hard to do this by hand. I know that there are ways to batchprocess files with audacity but here comes another problem: We aren’t sure if all the original and additional .wavs are similar in terms of volume which got me thinking. The base volume has to be encoded in some way right? Therefore there should be a way to read out this base volume? I am looking for a way to just check how "loud" the basevolume of all the files is and deduct from that how much the added sounds need to be amplified with maybe some individual adjustment rather than adjusting every sound by hand. Does anyone has experience with this or can share some insight how the encoding of the volume actually works? I don't know anything about audio, but I would start by reading up on this: https://stackoverflow.com/questions/984729/how-can-i-determine-how-loud-a-wav-file-will-sound Any calculations can be easily done with a for loop in Python once you have the procedure down. | ||
|
graNite
Germany4434 Posts
On February 11 2019 11:14 travis wrote: yeah you won't be able to make it faster in one line of code I think, a comprehension will call a function on every iteration you are right i use the one loop solution above now, it is almost twice as fast. | ||
|
enigmaticcam
United States280 Posts
On February 09 2019 03:39 travis wrote: edit: nevermind found what I wanted. which was a list of primes. I found the first 50 million, geeze. probably morethan I need You can generate all prime numbers up to 10^9 in less than 30 seconds on an average work station using sieve of eratosthenes: https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes. It's about 50.8 million primes. public class PrimeSieve { | ||
|
enigmaticcam
United States280 Posts
| ||
| ||
