hate these ads?, log in or register to hide them
Page 13 of 129 FirstFirst ... 3101112131415162363113 ... LastLast
Results 241 to 260 of 2564

Thread: <xml><thread isTerrible="true" isUnderstandable="false" /></xml> - Coding help thread

  1. #241
    NoirAvlaa's Avatar
    Join Date
    April 12, 2011
    Location
    Liverpool, laaaa
    Posts
    5,063
    Be terrible and fix it by subtracting the output from 11111111 and using that as the new input :P

  2. #242
    Super Moderator DonorGlobal Moderator
    Join Date
    April 9, 2011
    Posts
    3,701
    I am really hungover now, but won't iterating the for loop the other way around fix that?

  3. #243
    Tarminic's Avatar
    Join Date
    April 9, 2011
    Location
    Nashville, TN
    Posts
    2,998
    Quote Originally Posted by Hast View Post
    I am really hungover now, but won't iterating the for loop the other way around fix that?
    I think so. Change it to this:
    Code:
    for (int j = 6; j >0; j--){
    	if (smallstr[j] == '1')
    		placeholder += pow(2.0,j);
    
    }
    Might need to be >= instead of >.

    Status of Babby: 100% Formed

  4. #244

    Join Date
    April 9, 2011
    Posts
    345
    The most significant bit corresponds to the highest power of 2, not the lowest.


    It doesn't matter which way you iterate, the result is exactly the same. You just can't use the index for the power.

    Edit:
    With problems like this it helps to first do it manually on paper, and then work out your algorithm from there. That way you immediately see where this is going wrong and how to fix it.

    Edit 2:
    On top of being algorithmically wrong, Tarminic's solution also gives you a memory access violation.
    Ok, it doesn't but it is still questionable to use length for index.
    Last edited by MortyM; June 21 2013 at 04:43:33 PM.

  5. #245
    Daneel Trevize's Avatar
    Join Date
    April 10, 2011
    Location
    T L A
    Posts
    12,495
    Quote Originally Posted by I Legionnaire View Post
    Code:
    int count = 0;
    for (int j = 5; j >=0; j--){
    	if (smallstr[j] == '1')
    		placeholder += pow(2.0,count);
    	count ++;
    	}
    Would be neater something like:
    Code:
    int chunk_size = 6;
    for (int j = chunk_size-1; j >=0; j--){
    	if (smallstr[j] == '1')
    		placeholder += pow(2.0, chunk_size - (j+1));
    	}
    Or
    Code:
    int chunk_size = 6;
    for (int j = chunk_size; j>0; j--){
    	if (smallstr[j-1] == '1')
    		placeholder += pow(2.0, chunk_size-j);
    	}
    With the first line being able to move way up the code & variable be used elsewhere, to have less magic numbers floating about to fuck up missing editing one. Though why are you going 6 bits at a time??
    Last edited by Daneel Trevize; June 22 2013 at 08:59:35 AM. Reason: (j+1)

  6. #246
    Qwert's Avatar
    Join Date
    April 10, 2011
    Location
    Elsewhere
    Posts
    1,827
    Quote Originally Posted by I Legionnaire View Post
    Code:
    int count = 0;
    for (int j = 5; j >=0; j--){
    	if (smallstr[j] == '1')
    		placeholder += pow(2.0,count);
    count ++;
    			}
    @MortyM
    Thanks, it took me a bit to realize that the index didn't correspond to what power I needed to be using. Using a count was a pretty easy fix.
    This is, by the way, the difference between big and little endianness. Quite a bit of existing code and hardware uses little endian (what your code did), even though humans use big endian for actually writing the number out.

  7. #247
    Tarminic's Avatar
    Join Date
    April 9, 2011
    Location
    Nashville, TN
    Posts
    2,998
    So, I'm trying to work out how to do something, and for some reason the solution has evaded me when I feel like it should be a relatively simple, straightforward process.

    I have two points with an (X,Y) coordinate pair and a cardinal vector V (0, 90, 180, 270), and I know these points are neighbors in some form. For example, if the first point is (3,3) the second point must be (3,2), (3,4), (2,3), or (4,3).

    I need to figure out if and how these two points are moving in relation to each other. Specifically:

    1. Whether they're colliding (3,3 moving down and 3,4 moving up)
    2. Whether they're moving parallel to each other (3,3 moving left and 3,4 moving left/right)
    3. Whether they're moving in the same direction (3,3 moving up and 3,4 moving up)
    4. Whether they're moving apart (3,3 moving up and 3,4 moving down/left/right)

    I feel like this should be easy, but for the life of me I've been at this for 3+ hours and I couldn't quite get it right. I thought I could use the difference between the two vectors and the distance between the two points (after moving them in the direction of those vectors), but I so far I haven't gotten it right yet. I feel like there should be a simple way to do this but it's escaped me up to this point. Halp!

    Status of Babby: 100% Formed

  8. #248
    Super Moderator Global Moderator QuackBot's Avatar
    Join Date
    March 7, 2012
    Posts
    21,326
    Quote Originally Posted by Daneel Trevize View Post
    IDK, having to be so verbose about everything really drags. Python 3 lets you do really clear things with slices, zips & list comprehensions, and yet still ensures your explicit about trying to combine e.g. strings and ints.
    Code:
    >>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
    >>> letters
    ['a', 'b', 'c', 'd', 'e', 'f', 'g']
    >>> # replace some values
    >>> letters[2:5] = ['C', 'D', 'E']
    >>> letters
    ['a', 'b', 'C', 'D', 'E', 'f', 'g']
    Code:
    >>> x = [1, 2, 3]
    >>> y = [4, 5, 6]
    >>> zipped = zip(x, y)
    >>> list(zipped)
    [(1, 4), (2, 5), (3, 6)]
    >>> x2, y2 = zip(*zip(x, y)) # unzipping
    >>> x == list(x2) and y == list(y2)
    True
    Code:
    >>> [str(round(pi, i)) for i in range(1, 6)]
    ['3.1', '3.14', '3.142', '3.1416', '3.14159']
    Ah, so much win from Haskell.
    Idk about that, and i'm fucking stupid.

  9. #249
    Daneel Trevize's Avatar
    Join Date
    April 10, 2011
    Location
    T L A
    Posts
    12,495
    You lose, Quackbot! You lose. Sigged.
    Quote Originally Posted by QuackBot View Post
    Idk about that, and i'm fucking stupid.

  10. #250
    Tarminic's Avatar
    Join Date
    April 9, 2011
    Location
    Nashville, TN
    Posts
    2,998
    Quote Originally Posted by I Legionnaire View Post
    1.) If (ptA.X == ptB.X || ptA.Y == ptB.Y) collision = True

    2.) If(ptA.degrees == ptB.degrees || ptA.degrees == ptB.degrees -180) parallel = True

    3.) if (ptA.degrees == ptB.degrees ) samedirection = True

    4.) If you're strictly talking about moving apart you can (assuming part 1 is not true) check the Xs and Ys against each other after the move is made. If the difference between either is >1 then they are moving apart. Being able to check the points post-move might take a bit of work though.


    you can normailze the degrees using this function

    Code:
    double constrainAngle(double x){
        x = fmod(x,360);
        if (x < 0)
            x += 360;
        return x;
    }
    Not nearly that easy, unfortunately. For the first case, (3,3) and (3,4) are only colliding if (3,3)'s vector is down or (4,4)'s vector is up, for example.

    I'm trying to figure out a way to do this simply but I'm thinking I'm just gonna end up with a ton of if statements to handle every case.

    Status of Babby: 100% Formed

  11. #251
    Donor halbarad's Avatar
    Join Date
    April 9, 2011
    Posts
    5,005
    Any good books for learning Python? Coming from a Java background and I've got a reasonable grasp of it.

  12. #252
    NoirAvlaa's Avatar
    Join Date
    April 12, 2011
    Location
    Liverpool, laaaa
    Posts
    5,063
    Quote Originally Posted by halbarad View Post
    Any good books for learning Python? Coming from a Java background and I've got a reasonable grasp of it.
    About to start a "Beginning Python on Raspberry Pi" book, will see if it's any good over the next few weeks/months. Picking raspi because it's also teaching python for i/o stuff like buttons and LEDs which is cool.

  13. #253
    Tarminic's Avatar
    Join Date
    April 9, 2011
    Location
    Nashville, TN
    Posts
    2,998
    Quote Originally Posted by I Legionnaire View Post
    Not nearly that easy, unfortunately. For the first case, (3,3) and (3,4) are only colliding if (3,3)'s vector is down or (4,4)'s vector is up, for example.
    It is if you treat your "cars" as structs. Pass each object the start position, and direction. Then compute what their second position is, this should only take eight switches , four per car.

    Ex:: CarA.degrees = 0
    case (0) CarA.secposY = CarA.firstposY ++;
    case (90) CarA.secposX = CarA.firstposX ++;
    etc etc...

    Then you can compare their second positions against each other. In both part one and four. Part two and three are only dependent on direction.
    That was my first strategy, actually: Essentially move each the two coords along their specified direction and compare the distance between the two afterwards, but for whatever reason I couldn't get that to work. I was able to get it to work properly, but only after writing some fairly ugly code.

    Warning: 120 lines of javascript with 4 switch statements
      Spoiler:

    Code:
        object.determineType = function() {
            var type = null;
    
            if(isConvergent(pixel1, pixel2)) {
                type = Fault.TYPE.CONVERGENT;
            } else if(isTransform(pixel1, pixel2)) {
                type = Fault.TYPE.TRANSFORM;
            } else if(isDivergent(pixel1, pixel2)) {
                type = Fault.TYPE.DIVERGENT;
            } else if(isInactive(pixel1, pixel2)) {
                type = Fault.TYPE.INACTIVE;
            } else {
                throw new PlanetSimError('Unable to determine fault type', [pixel1, pixel2]);
            }
            
            faultType = type;
            
            return type;
        }
        
        function isConvergent(pixel1, pixel2) {
            var convergent = false;
            var vectorDiff = Math.abs(pixel1.getPlate().getVector() - pixel2.getPlate().getVector());
            if(vectorDiff != 0) {
                convergent = isMovingTowards(pixel1, pixel2) || isMovingTowards(pixel2, pixel1);
            }
            return convergent;
        }
        
        function isMovingTowards(pixel1, pixel2) {
            var isMovingTowards = false;
            var sameX = pixel1.getX() == pixel2.getX();
            var sameY = pixel1.getY() == pixel2.getY();
            var vector = pixel1.getPlate().getVector();
            switch(vector) {
                case 0:   //east
                    isMovingTowards = sameY && (pixel1.getX() - pixel2.getX() < 0);
                    break;
                case 90:  //south
                    isMovingTowards = sameX && (pixel1.getY() - pixel2.getY() < 0);
                    break;
                case 180: //west
                    isMovingTowards = sameY && (pixel1.getX() - pixel2.getX() > 0);
                    break;
                case 270: //north
                    isMovingTowards = sameX && (pixel1.getY() - pixel2.getY() > 0);
                    break;
                default:
                    throw new PlanetSimError('Cannot determine if pixel1 is moving toward pixel2: only cardinal vectors are supported', vector);
            }
            return isMovingTowards;
        }
    
        function isTransform(pixel1, pixel2) {
            var transform = false;
            var sameX = pixel1.getX() == pixel2.getX();
            var sameY = pixel1.getY() == pixel2.getY();
            var vector = pixel1.getPlate().getVector();
            switch(vector % 180) {
                case 0: //east or west
                    transform = sameX;
                    break;
                case 90: //south or north
                    transform = sameY; 
                    break;
                default:
                    throw new PlanetSimError('Cannot determine if pixel1 is moving parallel to pixel2: only cardinal vectors are supported', vector);
            }
            return transform;
        }
        
        function isDivergent(pixel1, pixel2) {
            var divergent = false;
            var vectorDiff = Math.abs(pixel1.getPlate().getVector() - pixel2.getPlate().getVector());
            if(vectorDiff > 0) {
                switch(pixel1.getPlate().getVector()) {
                    case 0:   //east
                        divergent = ((pixel1.getX() - pixel2.getX()) == 1);
                        break;
                    case 90:  //south
                        divergent = ((pixel1.getY() - pixel2.getY()) == 1);
                        break;
                    case 180: //west
                        divergent = (pixel1.getX() - pixel2.getX() == -1);
                        break;
                    case 270: //north
                        divergent = (pixel1.getY() - pixel2.getY() == -1);
                        break;
                }
            }
            return divergent;
        }
    
        function isInactive(pixel1, pixel2) {
            var inactive = false;
            if(pixel1.getPlate().getVector() == pixel2.getPlate().getVector()) {
                var sameX = pixel1.getX() == pixel2.getX();
                var sameY = pixel1.getY() == pixel2.getY();
                var vector = pixel1.getPlate().getVector();
                switch(vector % 180) {
                    case 0:  //east or west
                        inactive = sameY;
                        break;
                    case 90: //south or north
                        inactive = sameX;
                        break;
                    default:
                        throw new PlanetSimError('Cannot determine if pixel1 is moving parallel to pixel2: only cardinal vectors are supported', vector);
                }
            }
            return inactive;
        }


    I need to refactor this so it sucks less, but in the interest of making progress I did it in a way that made it as hard as possible to fuck up. At least it passes the two-dozen unit tests I have running on it.

    Status of Babby: 100% Formed

  14. #254
    Donor illusionalsgcty's Avatar
    Join Date
    April 14, 2011
    Posts
    400

    Chris' teacher is a bad and noone understands his classes - Coding help thread

    Quote Originally Posted by halbarad View Post
    Any good books for learning Python? Coming from a Java background and I've got a reasonable grasp of it.
    There's a ton of free online resources if you're not set on hard copies of programming books, the official Python docs are pretty good: http://docs.python.org/3/tutorial/

    Here's a list of some other resources if you feel the docs aren't your style: http://docs.python-guide.org/en/late.../learning.html

    Definitely start with python 3, much better than 2 imo.

  15. #255
    Donor Rami's Avatar
    Join Date
    April 10, 2011
    Location
    London/Snuffbox
    Posts
    1,374
    Flogging a dead horse here, but the crypto challenges are ideal to pick up a new language. Learning a language from docs alone is dicks, learn it whilst doing fun stuff of some sort.

  16. #256
    Donor halbarad's Avatar
    Join Date
    April 9, 2011
    Posts
    5,005
    Quote Originally Posted by Rami View Post
    Flogging a dead horse here, but the crypto challenges are ideal to pick up a new language. Learning a language from docs alone is dicks, learn it whilst doing fun stuff of some sort.
    That's my plan. I've been doing them in Java but I want to learn python for a uni assignment be next year and figure I might as well start now as I've got a bunch of "free" time coming up with summer holidays starting soon and not much going on at work during the time.

    I'll work through the docs to get the basics and then look at some of the challenges I've done so far and redo them in python, probably do some of the project euler stuff too as that was good when I did it a age back.

  17. #257
    מלך יהודים Zeekar's Avatar
    Join Date
    April 10, 2011
    Posts
    15,378
    They are good fun and doing the first couple of examples in python is easy enough even for a newbie. I should really get back to them but most of my time is taken by my diploma


    

  18. #258
    Lana Torrin's Avatar
    Join Date
    April 13, 2011
    Location
    Bonding around
    Posts
    19,664
    Quote Originally Posted by I Legionnaire View Post
    Its kinda win really.. As long as you arent running windows.

    I assume you can do this in a browser driveby with the appropriate browser hack?

    Oh also because this is educational.. http://technet.microsoft.com/en-us/s...letin/MS10-092

    Non-Affected Software
    Operating System
    Windows XP Service Pack 3
    Windows XP Professional x64 Edition Service Pack 2
    Windows Server 2003 Service Pack 2
    Windows Server 2003 x64 Edition Service Pack 2
    Windows Server 2003 with SP2 for Itanium-based Systems
    And people laughed at us for not upgrading anything.
    Last edited by Lana Torrin; June 26 2013 at 02:38:20 AM.
    Quote Originally Posted by lubica
    And her name was Limul Azgoden, a lowly peasant girl.

  19. #259
    Lana Torrin's Avatar
    Join Date
    April 13, 2011
    Location
    Bonding around
    Posts
    19,664
    Quote Originally Posted by I Legionnaire View Post
    It's part of the source dump for a worm that was selling for $40k USD. People on reddit and HN are pretty surprised by how poorly it's written considering how effective it is/was at evading AV, and propagating. Shit's waaay beyond my level.
    Most viruses are horribly coded. Most antivirus software is worse.

    Tapaderpin v4
    Quote Originally Posted by lubica
    And her name was Limul Azgoden, a lowly peasant girl.

  20. #260
    Donor
    Join Date
    April 11, 2011
    Location
    The Netherlands
    Posts
    1,027
    Given that maintainability of virus code isn't exactly needed, it's about the only field in which code quality isn't a huge deal.

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •