Author Topic: Programming Megathread  (Read 115094 times)

Sorry, I'm writing it through a putty console to my school's linux server, or else I would have posted the whole thing right then. Give me one moment though.
Code: [Select]
import java.util.Scanner;
public class StackTester
{
CharStack testStack = new CharStack();
Scanner scan = new Scanner(System.in);
System.out.println("Please enter a string:");
String str = scan.next();

for (int i = 1; i < str.length(); i++)
if (str.charAt[i] == '(')
testStack.push(str.charAt[i]);
}//main
First of all, wouldn't you want int i = 0; instead? charAt begins at index 0.

As for your problem, here's working code:
Code: [Select]
import java.util.Scanner;
import java.util.Stack;
class StackTester
{
    public void main()
    {
        Stack<Character> testStack = new Stack<Character>();
        Scanner scan = new Scanner(System.in);
        System.out.println("Please enter a string:");
        String str = scan.next();
   
        for (int i = 1; i < str.length(); i++)
        {
           if (str.charAt(i) == '(')
           {
               testStack.push(str.charAt(i));
           }
        }
    }
}//main
You had three problems.

1) You used brackets "[]" instead of parentheses after charAt.
2) You did not import Java's stack class.
3) The stack was declared incorrectly.

Hope this helps!

Name 10 out of your 100 reasons as to why we should still be capped at 80 width.

1. if you have a line of code that needs to longer then generally your code is getting too complicated to the point where you're jamming many parts of an expression onto a single line just because you can (or you're writing NSAPI code forget that)

if you have a function which has so many arguments that it makes callsite lines that long then either 1) don't use so many positional arguments for a function or 2) put arguments on different lines with clear indentation from the first

Thanks for the help! We are making a stack from scratch using arrays so that's why the stack was weird.

1. if you have a line of code that needs to longer then generally your code is getting too complicated to the point where you're jamming many parts of an expression onto a single line just because you can (or you're writing NSAPI code forget that)

if you have a function which has so many arguments that it makes callsite lines that long then either 1) don't use so many positional arguments for a function or 2) put arguments on different lines with clear indentation from the first
Usually when ever I go over 80 width, it's generally when I'm writing comments or strings to output. I agree that you shouldn't have like a bazillion things in your function call, but 80 width is still rather short for this time and age.

Thanks for the help! We are making a stack from scratch using arrays so that's why the stack was weird.
Oh, ok. That makes more sense. ;)

ok, so... I'm trying to make a function (in python, but will be converted to js later) that picks a "random" number, but it needs to be weighted
the arguments should be a minimum, maximum, number to be biased towards, and the magnitude with which it should be biased (that still needs some defining)
I've found a lot of examples but almost all of them use squares or square roots which force you to bias towards the top or bottom, and I want it to be variable
and now I have two ideas, mostly inspired by what other people have said:

create a list of numbers from the min to the max, then go back through it and add more numbers, depending on the bias arguments
like, with min=1 and max=5, biased towards 3, the end result list might be [1, 2, 2, 3, 3, 3, 4, 4, 5]
or the same min and max biased towards 1 might be [1, 1, 1, 2, 2, 3, 4, 5]
see what I mean? then I pick a random item from that list. the magnitude argument would determine how many of each number is added
also not sure how to determine how quickly the number of numbers added should drop off as it gets further from the number it should be biased towards. reducing by one for each number seems too slow

I thought I had another idea but actually no I don't. plz help

number to be biased towards, and the magnitude with which it should be biased
Based off these descriptions, it sounds like your looking for a normal distribution. In which case, the two terms you're describing would be 'mean' and 'standard deviation'

A normal distribution doesn't have any actual upper and lower limit, though, just an increasing improbability as you get further from the mean. So you could combine it with a clamp method if you need absolute insurance of a range.

I don't have any code for you, but knowing the name of what you're looking for should help immensely

Edit: http://rosettacode.org/wiki/Statistics/Normal_distribution#Python
« Last Edit: January 29, 2016, 12:22:12 PM by Headcrab Zombie »

mmm thanks
that code example for python is unimportant for the most part, it just shows how to graph the results of the standard library function random.gauss()
which is what I'm actually looking for here. that function. javascript however does not seem to include such a function, but thankfully random.guass() is mostly pure python so I may be able to convert it to js myself

but knowing what I'm looking for does help a lot, ty ily

edit: I wish I was a math whiz. this kinda stuff is like the only reasons I have for wanting to go to college. feels like it'd be a waste to go through all of that just for the computer science and math classes though.......

double edit: successfully used some code from random.gauss() to make the function in python:

def roll(min_v=1, max_v=100, mean=None, sigma=None):
    if mean is not None and sigma is not None:
        x2pi = random.random() * (2.0 * math.pi)
        g2rad = math.sqrt(-2.0 * math.log(1.0 - random.random()))
        z = math.cos(x2pi) * g2rad
       
        result = mean + z*sigma
        if result < min_v: return min_v
        elif result > max_v: return max_v
        else: return int(round(result))
    else:
        return random.randint(min_v, max_v)


next up is turning that into javascript
(the min and max arguments have "_v" added because "_value" was too long, and min() and max() are built-in python functions which I didn't want to mess with)
one glaring issue is that if mean is near or equal to either the min or the max, that min or max number becomes probably significantly more likely to occur. I guess that is probably impossible to prevent? maybe? at least in any way that isn't convoluted, unless there are more crazy math tricks I don't know about

triple edit: that was pretty easy

function randint(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

function roll(min, max, mean, sigma) {
    if (typeof mean != 'undefined' && typeof sigma != 'undefined') {
        var x2pi = Math.random() * (2.0 * Math.PI);
        var g2rad = Math.sqrt(-2.0 * Math.log(1.0 - Math.random()));
        var z = Math.cos(x2pi) * g2rad;
       
        var result = mean + z*sigma;
        if (result < min)
            return min;
        else if (result > max)
            return max;
        else
            return Number(Math.round(result));
    }
    else {
        return randint(min, max);
    }
}


overedit: how can I make eclipse look better.............
it has a "dark" theme which is mostly good except all of the issues. the editor window looks great but in other menus there are a lot of occasional problems with dark-colored text on dark backgrounds. the close buttons on the tabs also look very odd
using the lighter themes is ok but not preferable
« Last Edit: January 29, 2016, 05:12:57 PM by Foxscotch »

For part of a project in one of my classes, I had to create a assembly program to multiply two numbers using only "add", "nor", and "beq". (And no just adding a number to itself x times, way too inefficient)
Was pretty interesting.

Anyone have any books they recommend for C and/or C#?  Preferably on Amazon kindle unlimited but open to suggestions, looking to read moar.

Anyone have any books they recommend for C and/or C#?  Preferably on Amazon kindle unlimited but open to suggestions, looking to read moar.

anything by tony gaddis

this man is a lord

I'm bored, I need someone to give me a (quick) programming challenge in Python or C++ that doesn't use external libraries.

I'm bored, I need someone to give me a (quick) programming challenge in Python or C++ that doesn't use external libraries.
make something that can convert back and forth between RGB tuples and CSS hex codes
like (244, 55, 66) and #F43742

make something that can convert back and forth between RGB tuples and CSS hex codes
like (244, 55, 66) and #F43742
:o Sounds neat.

make something that can convert back and forth between RGB tuples and CSS hex codes
like (244, 55, 66) and #F43742

printf("#%x", (0x10000*red) | (0x100*green) | blue);

printf("(%d, %d, %d)", (rgb & 0xFF0000)>>16, (rgb & 0x00FF00)>>8, rgb & 0x0000FF);
« Last Edit: February 03, 2016, 08:52:00 PM by $trinick »