?

Log in

No account? Create an account
 
 
18 October 2013 @ 06:10 pm
Programming Class Week 1  
I'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.
 
 
Current Mood: accomplishedaccomplished
Current Music: Erang - The Forester's Grandson
 
 
 
ccamfieldccamfield on October 19th, 2013 01:15 am (UTC)
Go Dorchadas!
Hey,

First, I want to say congrats on making a first foray into computer programming!

Second, as far as the -2 mod 5 = 3 thing. That stems, I believe, from a particular element of math I remember from university. Basically if you are working with a certain range of numbers, say 0, 1, 2, 3, 4, then you can define addition operations using those with a modulo of the size of your range. You always have to get back a value in that range. Thus "4+1=0" because it goes beyond your range and wraps around. i.e. (4+1) mod 5 = 0.

When you're working with those, then -2 is "the same as" or technically speaking "congruent to" 3. You can never get back a negative number from a modulo operation. You *have* to get back one of your numbers in the range 0-4.

("Congruent to": two numbers are congruent to each other if you take their difference and the result is evenly divisible by whatever you're using for mod. 3 - (-2) mod 5 = 5 mod 5 = 0, so they are congruent to each other.)

http://mathworld.wolfram.com/Congruence.html

Edited at 2013-10-19 01:16 am (UTC)
dorchadasdorchadas on October 19th, 2013 06:04 pm (UTC)
Re: Go Dorchadas!
Aha! Thank you!

Though this page seems to indicate it depends on the language, but I'll file that away for later since this is a Python class. (^_^)