Programming Class Week 1
2013-Oct-18, Friday 18:10I'm pretty sure this is the week that a lot of people flee in terror when they realize that programming is just math with more words. And not only that, the words are things like "random.randrange()" and "print" and "def" and "if/elif/else" and not actual sentences.
Fucking random.randrange().
Let me explain. In Python, there are two main ways to get a random integer (well, there's probably more, but there are two I know of). There's random.randint(), which returns everything from X to Y inclusive, and random.randrange, which returns everything from X to (Y-1). Forgetting that distinction let me to some problems in my program that I had to go back and fix because the program wasn't returning the full range of possible answers.
The assignment this week was to make a program that plays Rock-paper-scissors-lizard-Spock, has the computer make a random guess, and determine the winner. We haven't learned how to implement dynamic input yet, or how to implement a GUI, so it just takes hardcoded player "guesses" and then returns a range of responses, and it's up to us to show that those responses are properly random and the program interprets who wins and who loses correctly. This takes math.
The hard way is to implement a giant if/elif/else statement using Booleans that takes into account every possible combination of throws, which is really long and annoying, and the easy way is to convert the throws into numbers and use math, since it's possible to plot the throws on a wheel--as in the wiki article linked above--and assign them numbers. Subtract, interpret the number to determine winner or loser, and boom. It didn't take all that long, fortunately, but I do need to go back and look into doing modulo operations with negative numbers. 3 mod 5 is 3, because there's 3 left over after the 0 times 3 is divisible by 5 in integer division, but -2 mod 5 is also 3 because ??? Because of floor division and the difference between 2 and 5? Doing other mod operations makes it seem like that's the case, but I don't want to have developed a rule-of-thumb that turns out to be inaccurate later.
That's the thing--the computer only does what you tell it to, so you need to have at least some understanding of the underlying math or you won't be able to tell when the answer seems wrong. In this week's quiz, I had to build programs to calculate inputs into these formulas: ¼ n s2 / tan(π / n) and f(x) = -5x5 + 69x2 - 47 . Those are the kind of cases where knowing what the answer should be, even as a rough ballpark, is really handy.
Fucking random.randrange().
Let me explain. In Python, there are two main ways to get a random integer (well, there's probably more, but there are two I know of). There's random.randint(), which returns everything from X to Y inclusive, and random.randrange, which returns everything from X to (Y-1). Forgetting that distinction let me to some problems in my program that I had to go back and fix because the program wasn't returning the full range of possible answers.
The assignment this week was to make a program that plays Rock-paper-scissors-lizard-Spock, has the computer make a random guess, and determine the winner. We haven't learned how to implement dynamic input yet, or how to implement a GUI, so it just takes hardcoded player "guesses" and then returns a range of responses, and it's up to us to show that those responses are properly random and the program interprets who wins and who loses correctly. This takes math.
The hard way is to implement a giant if/elif/else statement using Booleans that takes into account every possible combination of throws, which is really long and annoying, and the easy way is to convert the throws into numbers and use math, since it's possible to plot the throws on a wheel--as in the wiki article linked above--and assign them numbers. Subtract, interpret the number to determine winner or loser, and boom. It didn't take all that long, fortunately, but I do need to go back and look into doing modulo operations with negative numbers. 3 mod 5 is 3, because there's 3 left over after the 0 times 3 is divisible by 5 in integer division, but -2 mod 5 is also 3 because ??? Because of floor division and the difference between 2 and 5? Doing other mod operations makes it seem like that's the case, but I don't want to have developed a rule-of-thumb that turns out to be inaccurate later.
That's the thing--the computer only does what you tell it to, so you need to have at least some understanding of the underlying math or you won't be able to tell when the answer seems wrong. In this week's quiz, I had to build programs to calculate inputs into these formulas: ¼ n s2 / tan(π / n) and f(x) = -5x5 + 69x2 - 47 . Those are the kind of cases where knowing what the answer should be, even as a rough ballpark, is really handy.