|
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. |
On December 15 2015 12:12 WarSame wrote:If you had to create a weighted chooser in as few lines of code as possible, while still being easily changeable, how would you do so? For example, i=randint() if (i<0.1): blah elseif (i<0.2): blah2 etc.
is out because if you change any weight you have to change them all. What're your ideas? Well, seems to me your option is either that or define some relationship between all the variables so you can tell how one should change when the others do, neither of which is going to be easy. I'd probably go with the if statements unless it was really necessary.
|
Maybe. I was thinking of using an array that is as long as the sum of the weights, and filling it with weight*element of each element, and selected randomly inside it, but that seems a bit silly to do this, is it not?
|
Your element 0 will fuck you in most languages.
Are there other constraints? (weights add to 1.0, stepsize is 0.01, ...) Or do we just know the percentages?
|
If you're happy with the weights being passed in as an array, you could do something like:
total = sum(weights) roll = random.random() * total i = 0 accum = weights[0] while accum < roll: i += 1 accum += weights[i] // Here, i will be the index of the randomly selected weight, and you can do whatever you want based on that.
Basically, a generalized version of the earlier code. This is O(# of weights) in time, but better in space than the weight*elements solution (and works fine with arbitrary weights, not just integers), whereas the other is better in time since you just do an array lookup once you have the random number, but worse in space.
|
Thanks, that's a much improved version!
|
On December 15 2015 12:12 WarSame wrote:If you had to create a weighted chooser in as few lines of code as possible, while still being easily changeable, how would you do so? For example, i=randint() if (i<0.1): blah elseif (i<0.2): blah2 etc.
is out because if you change any weight you have to change them all. What're your ideas?
Depending on the language and context, I like to use associative arrays/maps with lambdas:
//PSEUDOCODE
weightMap = { 0.1: function() { do_stuff(); }, 0.2: function() { do_other_stuff(); }, 1.0: function() { do_last_stuff(); } };
i = random(); foreach (weight in keys weightMap) { if (i <= weight) { weightMap[weight](); break; } }
It's not the most concise and arguably not the most optimized, but it allows for very easy modification since the information is in a single place and not intermixed with the program logic (if/else tree). You just have to remember that not all languages preserve the ordering of the keys.
|
Is there a noteworthy alternative to AndroidStudio for developing an app for android which doesn’t suck as much as AndroidStudio?
|
Zurich15313 Posts
On December 15 2015 17:24 waffelz wrote: Is there a noteworthy alternative to AndroidStudio for developing an app for android which doesn’t suck as much as AndroidStudio? There is ADT, which sucks even more.
Really though, Studio is pretty nice once you get the hang of it.
|
On December 15 2015 17:52 zatic wrote: There is ADT, which sucks even more.
Really though, Studio is pretty nice once you get the hang of it.
On December 15 2015 17:52 zatic wrote: There is ADT, which sucks even more.
Really though, Studio is pretty nice once you get the hang of it.
"Pretty nice" - Like not updating the layout unless you also changed something in the actual code? (for which now I have a function called fuckAndoridStudio() in which I just add another line of Log.v (“MyApp”,”Fuck Android Studio”); whenever I just change something in the layout.
I work with an older version though(1.4) since in the lab they don’t have the actual one and since it isn’t backwards compatible… I also freely admit that I am new to AndroidStudio and that we never got a useful introduction besides “This is java, we work with AndoridStudio which isn’t exactly java but fuck it. We also know that you probably already know java and that there a ton of resources to learn java and therefore it would make more sense to show you how to use AndoridStudio – but we won’t since this is the first time we use AndroidStudio in this course and I couldn’t be asked to look into it. So… do you want to see some java example that would have been useful for previous year? Wanna read in some strings?”
Thanks nonetheless. If someone can recommend a really good AndroidStudio tutorial which takes focus on the appdevelopment/usage of AndroidStudio and not on java itself it would be greatly appreciated. I got a serious problem with my mindset when it comes to knowing my functions but being too incompetent with the tools… Like a fucking painter that suddenly can’t paint a wall anymore because he got a different brush. I get frustrated a lot and then I get stuck.
|
I've been doing some work on the number of different ways to make change, and I've almost got it working properly and running extremely fast, but got a strange bug - for a coin denomination it will run it with the max of that coin, then skip the second most possible and go straight to the third and on. I have no idea why, and can't seem to bug hunt it.
The code is here.
coinDenoms is a global variable.
This outputs + Show Spoiler +There are 30 different solutions. 2 0 0 0 0 5 0 0 0 3 4 0 0 3 2 10 0 3 1 15 0 3 0 20 0 2 6 0 0 2 4 10 0 2 3 15 0 2 2 20 0 2 1 25 0 2 0 30 0 1 8 0 0 1 6 10 0 1 5 15 0 1 4 20 0 1 3 25 0 1 2 30 0 1 1 35 0 1 0 40 0 0 10 0 0 0 8 10 0 0 7 15 0 0 6 20 0 0 5 25 0 0 4 30 0 0 3 35 0 0 2 40 0 0 1 45 0 0 0 50 As you can see, it goes straight from 2 quarters to 0 - and I have no idea why. If you can point it out to me I'd appreciate it.
|
On December 15 2015 17:00 Morfildur wrote:Show nested quote +On December 15 2015 12:12 WarSame wrote:If you had to create a weighted chooser in as few lines of code as possible, while still being easily changeable, how would you do so? For example, i=randint() if (i<0.1): blah elseif (i<0.2): blah2 etc.
is out because if you change any weight you have to change them all. What're your ideas? Depending on the language and context, I like to use associative arrays/maps with lambdas: //PSEUDOCODE
weightMap = { 0.1: function() { do_stuff(); }, 0.2: function() { do_other_stuff(); }, 1.0: function() { do_last_stuff(); } };
i = random(); foreach (weight in keys weightMap) { if (i <= weight) { weightMap[weight](); break; } } It's not the most concise and arguably not the most optimized, but it allows for very easy modification since the information is in a single place and not intermixed with the program logic (if/else tree). You just have to remember that not all languages preserve the ordering of the keys.
The problem with this approach is, that adding elements if fairly hard because every new element reduces the probability of all the elements after it but not of the ones before it. So you would need to do some serious calculation each time you add a new element.
Here is what I came up with in a few minutes: Not the prettiest code I ever wrote though :D
class Hash def total_weight @total_weight ||= values.reduce :+ end def sample sum = 0 keys.each do |key| probability = self[key] if rand(total_weight) >= total_weight - (probability + sum) return key else sum += probability end end end end
random_stuff = {"A" => 1, "B" => 5, "C" => 59, "D" => 120}
picks = { "A" => 0, "B" => 0, "C" => 0, "D" => 0}
10000.times do picks[random_stuff.sample] += 1 end
p picks # Sample output: {"A"=>57, "B"=>333, "C"=>3351, "D"=>6259}
You just have to create a Hash/Map/Dictionary/WhateverItIsCalledInYourLanguage with your random thing as key and its probability as value and call the "sample" method on it.  This only works with integer probabilities though.
|
Hey everyone,
I got a question.. I have a folder with around 11000 files (roms actually).. Im looking for a quick/easy way to select all files which have [i] in their file name. These are the roms that i need. Does anyone know of a way to select all those files so i can copy them to a seperate folder?
Just pm me or reply in this thread if u think u can be of any help.
Thanks in advance ^^
|
On December 16 2015 05:00 Lazorstorm wrote: Hey everyone,
I got a question.. I have a folder with around 11000 files (roms actually).. Im looking for a quick/easy way to select all files which have [i] in their file name. These are the roms that i need. Does anyone know of a way to select all those files so i can copy them to a seperate folder?
Just pm me or reply in this thread if u think u can be of any help.
Thanks in advance ^^ Since you're posting in the programming thread, how about you just write a small program that loops through all these files, checks their name, and copies them to another folder? If you have Visual Studio installed, that's like 5-10 lines of C#.
|
On December 16 2015 05:00 Lazorstorm wrote: Hey everyone,
I got a question.. I have a folder with around 11000 files (roms actually).. Im looking for a quick/easy way to select all files which have [i] in their file name. These are the roms that i need. Does anyone know of a way to select all those files so i can copy them to a seperate folder?
Just pm me or reply in this thread if u think u can be of any help.
Thanks in advance ^^ Which system are you on? If Linux(and maybe Unix) check out Bash scripting. You could simply "*[i]*" and pipe that into whatever you want to do.
|
@spineshealth :
That sounds like a good idea. I'm indeed into programming but i got 0 experience with C#, so it might be difficult for me. Thanks for the reply, i'll certainly give it a shot
@WarSame : I got a windows pc and a raspberry pi runnig Raspbian. I dont have a lot of experience with 'Bash Scripting' either. I tried googling what i want to do, but so far i havent found anything that actually pointed me in the right direction.
Any chance u guys got a link to some tutorial or example that could help me figure this stuff out?
Thanks in advance again! :D
|
|
Thanks!!! I'll try to figure this out ^^
|
On December 16 2015 05:46 Lazorstorm wrote: @spineshealth :
That sounds like a good idea. I'm indeed into programming but i got 0 experience with C#, so it might be difficult for me. Thanks for the reply, i'll certainly give it a shot
@WarSame : I got a windows pc and a raspberry pi runnig Raspbian. I dont have a lot of experience with 'Bash Scripting' either. I tried googling what i want to do, but so far i havent found anything that actually pointed me in the right direction.
Any chance u guys got a link to some tutorial or example that could help me figure this stuff out?
Thanks in advance again! :D Are these files on the Pi or the PC? If on the Pi you can quickly write something up for this in Python(here's some pseudocode):
import os, shutil fileList=os.listdir() for file in fileList: if file.contains("[i]"): newLocation=file.replace("[i]","") shutil.move(os.getcwd()+file, newLocation)#Make sure you run this script in the folder you want to change
I can't guarantee this works properly - test it before you run it on your main folder. In particular, you may have a problem with slashes - these types of functions handle them weird and I can't remember which functions do it which way. So test it on small sets of files beforehand.
|
Hyrule18969 Posts
If it's on the PC you can do what you want in Batch
|
On December 16 2015 02:07 WarSame wrote:I've been doing some work on the number of different ways to make change, and I've almost got it working properly and running extremely fast, but got a strange bug - for a coin denomination it will run it with the max of that coin, then skip the second most possible and go straight to the third and on. I have no idea why, and can't seem to bug hunt it. The code is here. coinDenoms is a global variable. This outputs + Show Spoiler +There are 30 different solutions. 2 0 0 0 0 5 0 0 0 3 4 0 0 3 2 10 0 3 1 15 0 3 0 20 0 2 6 0 0 2 4 10 0 2 3 15 0 2 2 20 0 2 1 25 0 2 0 30 0 1 8 0 0 1 6 10 0 1 5 15 0 1 4 20 0 1 3 25 0 1 2 30 0 1 1 35 0 1 0 40 0 0 10 0 0 0 8 10 0 0 7 15 0 0 6 20 0 0 5 25 0 0 4 30 0 0 3 35 0 0 2 40 0 0 1 45 0 0 0 50 As you can see, it goes straight from 2 quarters to 0 - and I have no idea why. If you can point it out to me I'd appreciate it. Do you have the full code, so we can just paste it into an editor and it it works, without having to piece things together? That makes it a lot easier to help. =]
Also, I asked this before (but not in a post that directly quoted you) and it's a very important difference: Is the question how many ways there are to make change, or which ways there are? This is important because if you take common coin denominations (1, 2, 5, 10, 25, 50, 100, 200), there are over 5 000 000 ways to change 500 cents and over 260 000 000 ways to change 1000 cents. While it's extremely easy to calculate how many ways there are, even for values much larger, it quickly become infeasible and slightly later impossible to determine which ways there are.
|
|
|
|