+ Reply to Thread
Results 1 to 14 of 14
  1. #1
    You just got served THE CALLISTO SPECIAL
    SASSAGE KING OF DA WORLD
    cheap hawks gay

    Join Date
    Sep 2007
    Posts
    26,424
    BG Level
    10

    Beginner Programming Help Thread

    I've managed to stump myself on some pretty elementary shit so I'm sending up the help flare, but I figured if there's enough people with random coding questions/problems maybe this can be used like a random question-ish thread, if so I'll stick it later on...

    Currently working on the MIT OCW 6.00 Introduction to CompSci/Programming course, I know a few other people here on BG have taken it already as well, the class is in Python, for the record I have had almost zero coding experience prior to this minus working with Spellcast XMLs, so I'll probably be asking some dumb questions.

    The problem at hand: http://ocw.mit.edu/courses/electrica...ents/pset2.pdf
    Solving a Diophantine Equation

    Using this theorem, we can write an exhaustive search to find the largest number of McNuggets that cannot be bought in exact quantity. The format of the search should probably follow this outline:

    - Hypothesize possible instances of numbers of McNuggets that cannot be purchased exactly, starting with 1
    - For each possible instance, called n, Test if there exists non-negative integers a, b, and c, such that 6a+9b+20c = n. (This can be done by looking at all feasible combinations of a, b, and c)
    - If not, n cannot be bought in exact quantity, save n
    - When you have found six consecutive values of n that in fact pass the test of having an exact solution, the last answer that was saved (not the last value of n that had a solution) is the correct answer, since you know by the theorem that any amount larger can also be bought in exact quantity

    Problem 3.

    Write an iterative program that finds the largest number of McNuggets that cannot be bought in exact quantity. Your program should print the answer in the following format (where the correct number is provided in place of <n>): “Largest number of McNuggets that cannot be bought in exact quantity: <n>”

    Hint: your program should follow the outline above.

    Hint: think about what information you need to keep track of as you loop through possible ways of buying exactly n McNuggets. This will guide you in deciding what state variables you will need to utilize.
    Here's the code that I've come up with, which does not currently work:
    Code:
    consecFound = 0 # confirmed amounts which can be purchased
    lastFail = 0 # last confirmed impossible amount
    
    while consecFound < 6: # operation to run until 'consecFound' is 6
        for i in range(1, 50): # positive integers >= 50 already proven
            for a in range(0, i/9 + 1): # amounts of a > 8 will have value > 50
                for b in range(0, i/6 + 1): # amounts of b > 5 will have value > 50
                    for c in range(0, i/3 + 1): # amounts of c > 2 will have value > 50
                        n = (6 * a) + (9 * b) + (20 * c)
                        if n == i: # if i is solvable with given amounts,
                            consecFound += 1 # increase 'consecFound'
                        else: # otherwise, reset 'consecFound' to zero, and log last
                            consecFound = 0 # known impossible amount
                            lastFail = i
        
    print lastFail # output the last known impossible amount
    and here's an example of a working one from someone else:

    Code:
    foundInARow = 0
    maxThatCannotBePurchased = 0
    
    for i in range(0, 50):
        canPurchaseThisAmount = False
    
        for a in range(0, i / 6 + 1):
            for b in range(0, i / 9 + 1):
                for c in range(0, i / 20 + 1):
                    solution = (6 * a) + (9 * b) + (20 * c)
                    if solution == i:
                        canPurchaseThisAmount = True
    
        if canPurchaseThisAmount:
            foundInARow += 1
            if 6 == foundInARow:
                break;
        else:
            foundInARow = 0
            maxThatCannotBePurchased = i
    
    print "Largest number of McNuggets that cannot be bought in exact quantity: %d" % maxThatCannotBePurchased
    The for loops for a/b/c I did not come up with, those seemed to be widely used in the answers I found posted, and I'm not exactly sure how they're working/supposed to work. I was trying to come up with a working code largely on my own without just putting exactly what someone else who already figured it out did, but I'm having a really hard time understanding what's keeping mine from working while the other example is.

    Been trying to debug it for a few hours now, I figure maybe a fresh set of eyes would help. Any pointers in the write direction, or ideally an english explanation of those for loops would be appreciated.

  2. #2
    Chram
    Join Date
    Aug 2007
    Posts
    2,699
    BG Level
    7
    FFXIV Character
    Nours Sruon
    FFXIV Server
    Moogle
    FFXI Server
    Fenrir

    I think the issue is that you're resetting consecFound to 0 whenever n isn't i, thus it never breaks off the loop (while (consecFound < 6)).

    A for loop works like that

    for (counter; valuemax (in some languages you also specify if you increment/decrement it after each loop, I don't know python at all)

    for i in range(0, 50):
    This will loop until i is equal 50, i is incremented after each loop


    Anyway the issue in your code is the while loop, if you remove it you'll notice it works but gives 49 (while the other gives 43)

  3. #3
    You just got served THE CALLISTO SPECIAL
    SASSAGE KING OF DA WORLD
    cheap hawks gay

    Join Date
    Sep 2007
    Posts
    26,424
    BG Level
    10

    That does seem to be what's happening(the loop never breaking), but part of the instructions were to go until you find 6 consecutive integers which can be solved by some combination of 6a + 9b + 20c = n. I guess my main hangup is how to make sure that the loop only breaks after 6 consecutive, rather than just after 6 total.

    also for some context re: the class, I'm trying to keep the solutions using only what has been covered in the class up to the point of the assignment being given, he's just barely gotten into tuples/lists and string traversal/slicing. I tried turning the positive results into a tuple then ending the loop when the length of it reached 6 values but I ended up with the exact same problem I'm facing now.

  4. #4
    Chram
    Join Date
    Aug 2007
    Posts
    2,699
    BG Level
    7
    FFXIV Character
    Nours Sruon
    FFXIV Server
    Moogle
    FFXI Server
    Fenrir

    Check the other code
    if 6 == foundInARow:
    break;
    If foundInARow equals 6, break out of the loop

  5. #5
    You just got served THE CALLISTO SPECIAL
    SASSAGE KING OF DA WORLD
    cheap hawks gay

    Join Date
    Sep 2007
    Posts
    26,424
    BG Level
    10

    Yeah that solved that part...the rest seems to have been a nesting error, put a variable inside the loop instead of out of it and it works now, thanks. As I said before if anyone else has issues feel free to hijack this thread.

  6. #6
    Chram
    Join Date
    Nov 2007
    Posts
    2,668
    BG Level
    7
    FFXIV Character
    Avelle Asteria
    FFXIV Server
    Hyperion
    FFXI Server
    Phoenix
    WoW Realm
    Alleria

    Quote Originally Posted by Callisto View Post
    Yeah that solved that part...the rest seems to have been a nesting error, put a variable inside the loop instead of out of it and it works now, thanks. As I said before if anyone else has issues feel free to hijack this thread.
    If you're new to programming, scope is definitely one of those things to keep your eye on. Also pretty good idea for a thread. I like checking out tech every once in a while and looking at programming problems, though I think I already hate python after looking at it. Does python not use braces at all? Makes my eyes bleed. :<

  7. #7
    Chram
    Join Date
    Aug 2007
    Posts
    2,699
    BG Level
    7
    FFXIV Character
    Nours Sruon
    FFXIV Server
    Moogle
    FFXI Server
    Fenrir

    Quote Originally Posted by avelle View Post
    If you're new to programming, scope is definitely one of those things to keep your eye on. Also pretty good idea for a thread. I like checking out tech every once in a while and looking at programming problems, though I think I already hate python after looking at it. Does python not use braces at all? Makes my eyes bleed. :<
    Seriously lol, first thing I did was rewriting it.

  8. #8
    You just got served THE CALLISTO SPECIAL
    SASSAGE KING OF DA WORLD
    cheap hawks gay

    Join Date
    Sep 2007
    Posts
    26,424
    BG Level
    10

    Quote Originally Posted by avelle View Post
    If you're new to programming, scope is definitely one of those things to keep your eye on. Also pretty good idea for a thread. I like checking out tech every once in a while and looking at programming problems, though I think I already hate python after looking at it. Does python not use braces at all? Makes my eyes bleed. :<
    Yeah I've seen that complaint a lot, from what I can gather the main upside to python is just that it's syntactically much simpler/straightforward than other languages. I'm hoping to get a firm grip on the main concepts of programming with python then move onto something like C/C++.

  9. #9
    You just got served THE CALLISTO SPECIAL
    SASSAGE KING OF DA WORLD
    cheap hawks gay

    Join Date
    Sep 2007
    Posts
    26,424
    BG Level
    10

    Quote Originally Posted by sruon View Post
    Seriously lol, first thing I did was rewriting it.
    I guess on that note, do you still have your rewrite handy? I'd be curious to see how it looks in another language, I don't even know what a 'brace' is, lol...

  10. #10
    Chram
    Join Date
    Aug 2007
    Posts
    2,699
    BG Level
    7
    FFXIV Character
    Nours Sruon
    FFXIV Server
    Moogle
    FFXI Server
    Fenrir

    http://pastebin.com/QYvtWkkm

    Second code in C#, deleted yours already sorry!

  11. #11
    You just got served THE CALLISTO SPECIAL
    SASSAGE KING OF DA WORLD
    cheap hawks gay

    Join Date
    Sep 2007
    Posts
    26,424
    BG Level
    10

    Does the brace just indicate where the nesting takes place?

  12. #12
    Chram
    Join Date
    Aug 2007
    Posts
    2,699
    BG Level
    7
    FFXIV Character
    Nours Sruon
    FFXIV Server
    Moogle
    FFXI Server
    Fenrir

    Yes, I guess?

    Everything between braces is part of the element, the element can be part of yet another element...


  13. #13
    You just got served THE CALLISTO SPECIAL
    SASSAGE KING OF DA WORLD
    cheap hawks gay

    Join Date
    Sep 2007
    Posts
    26,424
    BG Level
    10

    Yeah pretty much then, in python it's just done with colon and a tab indentation.

  14. #14
    Chram
    Join Date
    Nov 2007
    Posts
    2,668
    BG Level
    7
    FFXIV Character
    Avelle Asteria
    FFXIV Server
    Hyperion
    FFXI Server
    Phoenix
    WoW Realm
    Alleria

    Quote Originally Posted by Callisto View Post
    Does the brace just indicate where the nesting takes place?
    I guess that's one way to put it, but it's also completely necessary in languages like C#, C++, Java etc.

    If you do something like
    Code:
    else:
            foundInARow = 0
            maxThatCannotBePurchased = i
    the else would execute "foundInARow=0," but the next line "maxThatCannotBePurchased = i" would execute regardless if else ever occurred. Braces, in addition to making the code look cleaner also help you visually determine the scope of a variable. If you declare a variable in an if statement and then leave that statement, the variable is said to go out of scope (I believe that's what they call it). There's probably more important reasons to use braces but those are a few for consideration. Also, some compilers may just flat out reject conditions without braces. Eclipse wouldn't let me do in Java for testing purposes.

    If you're planning on moving up to C++, I don't think it would be anymore difficult than Python. Maybe Python is good because it looks a lot like pseudocode but if you can read Sruon's code and see what's going on for the most part, I don't think you'd struggle with it. You might struggle with more advanced C++ concepts like pointers, but I don't think Python will prepare you for that either way, but then again I know exactly dick about Python so ya.

    edit: C# is also extremely easy to learn, but I started out in C++, and pretty much anything after C++ is a joke I've found.
    edit2: Used a better code example and fixed the error in my explanation.

Similar Threads

  1. Replies: 8
    Last Post: 2010-02-20, 01:21