Archive for Programming

Web Design Woes

I’ll tell you right off the bat–this is not an “article”, just a post. Now that we’re clear:

I take back a lot of what I said about HTML, CSS and the W3C. It’s all a good idea, and I now realize that. That said, I’m still a bit touchy about the Validator. Want proof? This page was validated in 2008, but running it through the Validator again brings up four errors and two warnings. I’m sure they have their reasons, but I still feel that my anger is justified.

All of that said, I’ve been redoing my website to:

  • Use HTML for page logic.
  • Use CSS for page looks (a challenge, considering that as of this morning I didn’t know CSS at ALL.)
  • Make the little navbar thingy towards the top a lot easier to maintain.
  • Pass the Validator (OK, OK, maybe Tidy HTML can help a bit.)

I also took advantage of the shuffle to rewrite the content a bit.

So far, I’ve finished one page, which was based on this one, but will now double as my index too.

A few notes:

  • All the CSS on it was whatever I could hack in a few hours using Google, part of this, scientific experiments with simple pages and Tidy HTML and logic. I have no idea how ‘nice’ it is (in terms of coding style) but it appeases my eyes, the Validator, and the end product, so I’m not complaining. I do not recommend this method of learning for the faint of heart. (Yes, I am going to do a proper tutorial this summer, I just wanted results NOW.)
  • I hacked together some Javascript for my navbar. This was accomplished by: Looking at sample code, and writing like a Pythoneer while using the Java syntax I’m learning in school.
  • So far, the Validator is happy!

Some of you might be wondering what happened to my colors. Answer: I’m colorblind, and as such have absolutely no sense for what looks good on a page. I began to think that all the blue might be a bit overwhelming, so I’m going old-school with the black-on-white. If you have any opinion on this, please relate it in the comments. I would really appreciate it.

I’m a bit miffed that this means absolutely nothing to people looking at my site with polite browsers, but I guess this is what they would call a Black Triangle. It makes me feel really proud of myself though, which is one of the reasons I love coding.

Goal: Finish the rest of the pages by the end of this weekend, and move everything from my ‘experimental’* subdomain to the real deal.

*That thing is great for organization.

Leave a Comment

Python and C…Wow…

I’ve come to the realization that as nice as Python is, I really ought to learn a lower-level language. Even though I own two C++ books, I’m only reading them to a)Gain a rudimentary knowledge of the C++ syntax, such that I can hack it if I have to b)Improve my conceputal knowledge c)Well, they are interesting.

Instead, I’m learning C, because it just seems like more of my cup of tea. And kernel hacking might be fun :)

So one of the sample programs in the tutorial I’m using is that which will list all the numbers from 1-2500, and say if they’re prime or not. I faithfully wrote and compiled it in C, ran it, and WOW! that was fast. I could not detect any sort of delay, or processing or anything, it was just:

<Enter><A screenfull of numbers, ending in “2500 is not prime” appears>

I was so impressed by this, I converted the code to Python. I even removed a function that listed the version number and time compiled. When I ran it, I could see numbers whizzing by the screen for half a second or so, and then it was done.

I know it’s old news that compiled languages are faster than interpreted ones, and that low-level languages are faster than high-level ones, but it’s just impressive to tangibly see it.

Comments (4)

Playing Python, Programming Pianos

New programmers are never sure what it “takes” to learn a programming language. This is not helped by the quantity of “Learn x language in 24 hours/3 days/etc.!” books that are out there, and by the overabundance of technical information available.

So what I propose is an analogy.

You might think that the analogy I would make would compare learning a programming language to learning a foreign language (e. g. Spanish.) But after much deliberation, I don’t think that’s a valid for several reasons:

  • Foreign languages are almost always more complex than programming languages. Something like French has evolved for the past two millenia, while even ‘old’ programming languages have only been around for 50 years. Furthermore, French has the task of concisely communicating any conceivable expression to another human, while C/Java/Delphi only has to boss around some chunks of metal and silicon.
  • You must memorize every aspect of a foreign language, while with programming languages you have the all-powerful documentation to help you with some of the less common things.
  • Even if you know that “Hello” is the most common greeting in English, you’ll sound dumb if you don’t use “Hi”, “Hey”, and “What’s up?” in the right circumstances. Meanwhile, BASIC will always think “10 PRINT “HELLO, WORLD!” is the best way of doing things.
  • Similar to the first one, foreign languages have a MUCH bigger vocabulary to learn than programming languages.

So I really don’t think it’s a valid comparison.

What I consider more vaild is comparing programming languages to musical instruments. First, let’s think about a story about little Billy.

Little Billy decided one morning that, since he really like music, he wanted to learn to play a musical instrument, so he could make his own music. He went to the music store, and was initially awed by the sheer quantity of different types of instruments for sale. After much inner turmoil, he decided to use the piano, since it was common, rationalizing his choice by thinking, “Since so many people use it, it must be one of the best choices. There’ll also be more people who can help me. Finally, it sounds good.” While he was in the music store, he also picked up a tutorial on learning how to play the piano.

He went home, and did a chapter of the book per day, being sure to actually play the piano as much as possible. By the time he finished working through the book, he was a decent piano player, but still had a lot to learn. He kept playing, hired a teacher, and within a few years he was downright adept.

Now consider little Billy’s brother–Tom.

Tom decided one morning that, since he really like computers, he wanted to learn to program, so he could make his own programs. He went online, and was initially awed by the sheer quantity of different types of programming languages available. After much inner turmoil, he decided to use Python, since it was common, rationalizing his choice by thinking, “Since so many people use it, it must be one of the best choices. There’ll also be more people who can help me. Finally, the syntax looks good.” While he was online, he also picked up a tutorial on programming in Python.

He went home, and did a chapter of the tutorial per day, being sure to actually do the programs as much as possible. By the time he finished working through the tutorial, he was a decent programmer, but still had a lot to learn. He kept programming, started working on projects so he could learn from the project chief, and within a few years he was downright adept.

Now that you know where I’m going with this, I can fill in some details:

They both give you ‘instant success.’ Anybody, and I mean anybody, can either make his computer say “Hello World!”, or play a few notes on an instrument within ten minutes.

They both have specific technique. Java programmers know that using a variable without declaring it first is a big no-no. Trumpet players will fail if they just move their fingers–and don’t blow.  Another way of saying this is that they both teach through failure–be it a bad sound or a segfault.

They both provide gratification. People get really excited when they play a tune well, or solve a particularly sticky programming problem.

If you do something again, you’ll do it better When an elementary school band plays “The Star-Spangled Banner”, it is very apparent that it’s not the New York Philharmonic playing “The Star-Spanled Banner.” A first-year programmer will use a very different method for solving a problem when compared to an old-hand.

So if you’ve ever learned how to play an instrument, you’re primed for programming. And if you’ve ever programmed, you know what it takes to play an instrument.

——-

I’m kind of interested in the reception this is going to get–please let me know what you think!

Comments (1)

Really concise explanation of random integers in Python

I wanted to use Python to generate some random integers, and a few minutes of Googling was not very helpful. I figured it out in the end, but I feel like something this basic should be easy.

First you need to import the random module, so:

import random

Then, to get a random integer from 1 to x, use

random.randrange(x)

It’s as easy as that.

(OK, OK, sorry. I know this probably isn’t of general interest, and it’s probably of minimal help, but I was just irritated that it wasn’t really easy to find online.)

Leave a Comment

TI-BASIC (Round Three) (Control Flow: If-Then-End)

Consider the following code:

1->A
If A = 1
Disp "A=1"

Let’s study it a bit. The first line just stores 1 as A. Standard stuff. The second line is freaky, and the third line just displays “A=1″. Standard stuff. Let’s go back to the freaky part. It introduces one of the more powerful commands in TI-BASIC: If. What it says, is that if A equals 1, to do something. The syntax isn’t nearly as remarkable as the code, just If [value][thingy][value]. Thingy can be anything from the ‘Test’ menu. =, <, >, etc.

Note: Due to the constraints of this keyboard, the less-than-or-equal-to sign will be written as <=, and the greater-than-or-equal-to sign will be written as >=. Also, the not-equal-to sign will be written as !=.

If you’re looking for ‘if’, it’s under ‘Ctl’, not ‘I/O’. And this is the first time we’re using ‘Ctl’…Yay! OK, digression over :)

If’s power comes from the fact that it introduces uncertainty into the program. This means the program can rocket off in a lot more directions, which means: Sweeeeeeet.

Since you handled that chunk of code so beautifully, let’s consider this one too.

Input A
If A=1
Disp "A=1"
Disp "Where is this?"

I hope you’re wondering if the ‘Where is this?’ line is part of the if statement or not. It’s not. The only command that’s conditional is the one immediately after the if line. Which is really useful for a code snippet like this:

If A=1
Disp "A is 1"
If A=2
Disp "A is 2"
If A>2
Disp "A is pretty big"
If A<1
Disp "A is pretty small"

That's a lot cleaner than the following method, which is also legal.
If A=1
Then
Disp "A is 1"
End
If A=2
Then
Disp "A is 2"
End
If A>2
Then
Disp "A is pretty big"
End
If A<1
Then
Disp "A is pretty small"
End

You’re probably wondering why that’s legal. It’s stinking huge! Which brings us back to our problem–if only lasts for a line. If-Then-End fixes that problem. Then means that all of the following instructions are only to happen if the statement is true, and End means that an end has been put to the conditional instructions. There’s no arguments required for Then or End (an argument is something that has to be put with it to make the syntax legal.) So let’s look at our ‘Guess!’ program again. Last time we’d gotten this far:

Disp "Guess!","","(GPL) 2008", "T. Macdonald"
#Note that going from the outline to the implementation there was a better idea--putting 'Guess!' and the GPL on separate lines.
Input "Guess:".G
#Oh no! We don't know how to make a random number!
#Oh no! We don't know how to say if the number is too big or too small!
#(That's why the tutorial's not done. Stay tuned.)

And now we do know how to say if a number is too big or too small!
So
#I'm not copying all the code, we'll pick it up here:
Input "Guess:", G
If G<R
Disp "Too small!"
If G>R
Disp "Too big!"
If G=R
Disp "You win!"

Unfortunately, that’s really all we can do with that, as of right now.
Next time we’ll talk about Else.

Assignment: Write a program that will ask the user for his age, and then will classify them as “Kid” (12 and under), “Teenager” (13-19) or “Adult” (20+).

Leave a Comment

TI-BASIC (Round Two-and-a-Half) (More with Input)

The somewhat astute among us have noticed that in Round *, there was the following line of code:

Input "Guess:",G

And that I haven’t explained how that works.

The pretty astute among us have also noticed that when they run a program such as the following:

Input G

All that’s shown is a question mark, and it’s not very visually appealing.

The quite astute among us have already realized that the way to get rid of the ugly question mark, and replace it with something more attractive is to put the attractive message in quotes, follow it with a comma, and then tack the variable name to the end of it.

Those who are not feeling very astute should be please that the astute have shared their thoughts, and we’re all back on a level playing field.

Finally, the EXTREMELY astute people are saying to themselves “It is such a drag when there’s like four variables that the user has to put in, and I have to make four Input statements. Four! Can you imagine? Surely there’s an easier way.”

Meet Input’s big brother ‘Prompt’. (TI-BASIC is very family-oriented. Output is Disp’s big brother, Input is Prompt’s little brother, Else is If’s big broth–oh, we haven’t gotten there yet. Round three is coming.)

Prompt is great for math. Take the Pythagorean Theorem:

Prompt A,B
A^2+B^2->D
sqrt(D)->C
Disp C

Which will display:

A=?
B=?
(The solution, which depends on the input)

So Prompt is just a quick and easy way of getting a bunch of variables inputted. Would I use it for games, or polished programs? No. Do I use it for quickly making a program that’ll do a mathematical formula? Always.

Leave a Comment

TI-BASIC (Round *) (Special Bonus Edition)

I’ve been thinking, and I realized it’d be nice to add another goal to the TI-BASIC series. So I’m adding a sample program.

About the program: It’s called ‘Guess!’ (I am really creative), and it’s been around since I was in the fifth grade, in one form or another. It consists of guessing a number from 1-100, and being told if it’s too big or too small, and then guessing again, and being told again, etcetera, until you guess the number.

Technical description: Before you make a program, it’s a really really really really really really really really good idea to know exactly what you want it to do. So here’s the ‘outline’ (remember that # and everything after it is a comment):

Guess! (GPL 2008 T. Macdonald) #Title and license (GPL is sort of like a Copyright)

Guess: #User guesses a number

Too big! #If the number’s too big

Too small! #If it’s too small

You win! #If the user guesses it

And that’s it. Believe it or not, the minute it took to write that saved a lot of wasted coding.

So let’s implement what we already know:

Disp "Guess!","","(GPL) 2008", "T. Macdonald"
#Note that going from the outline to the implementation there was a better idea--putting 'Guess!' and the GPL on separate lines.
Input "Guess:".G
#Oh no! We don't know how to make a random number!
#Oh no! We don't know how to say if the number is too big or too small!
#(That's why the tutorial's not done. Stay tuned.)

Leave a Comment

IDE Fight: Geany (ooh) vs. gedit (What!?) (Don’t worry–it’s close)

In my Python pursuits,I’ve used three IDEs to write the code. Geany, IDLE, and gedit (aka ‘Text Editor’ in Ubuntu) (OK, OK! I know gedit’s actually a text editor! But I’m using it as an IDE! Calm down!)

I’m kind of developing a love for the bullet form of review, since it keeps things concise. Also note that this is not very technical, it’s just what Timmy-the-end-user likes and dislikes.

  • I used IDLE for about five minutes. I didn’t like it. Don’t ask why, it’s the same feeling I had when I tried KDE. The gut-reaction, pseudo-phobia, I-can’t-deal-with-this moment. I’m sure it’s great, I just don’t like it. Case closed.
  • Geany is definitely…bigger than gedit, and it’s written with an eye to programming, not text editing. As such:
  • Geany: When you open a new document, it has a nifty GPL license already put in.
  • Geany: It has the all-powerful ‘Execute’ button, which is definitely faster than executing it with the Terminal.
  • Geany: I’m not quite sure how sound the ‘Execute’ button is, it seemed a bit buggy to me, but that may just be a hallucination.
  • Geany: When you type an if/while/for/etc., it’ll automatically indent. Which is kind of cool.
  • Geany: Speaking about those, it has the ability to ‘fold’ ifs/whiles/comments/fors. So you can just minimize them into one line to save screen real estate. Which is also kind of cool.
  • Geany: There’s variable auto-completion. So if you have variable user_input, just type in user, and a menu will show up asking if you want to autocomplete. It’s actually really intuitive.
  • Geany: Wikipedia calls it ‘cross-platform’. So it runs on every major OS except for Macs.
  • Both: They both have syntax highlighting.
  • Both: They both have tabs.
  • gedit: gedit’s lighter/faster/smaller.
  • gedit: gedit has plugins (read: Stinkin’ AWESOME!!. So with plugins (easily addable from Edit=>Preferences|Plugins), some of the highlights are that you can:
  • gedit: Have a Python interpreter chilling in the bottom of your screen. Which is HANDY.
  • gedit: Mass indent/unindent (Nice if you add or remove an extra level of control flow)
  • gedit: On the lame side, you need to have a Terminal running to execute your entire program. Though you should be executing less often with the interpreter.
  • gedit: Since we’re on the lame side: It only runs in ‘Unix-like systems’. Sorry, Windows people! (I wouldn’t be surprised if there’s some kind of workaround, though that’s outside of the scope of this post)

Conclusions:

Don’t use IDLE (haha, just kidding. But I really don’t have anything else to say about it).

Use Geany if you want to feel like the editor knows what you’re thinking (variable autocompletion, auto-indenting), or if you execute your program like crazy.

Use gedit (which I personally prefer) if you want speed, an interpreter, or handy tools (aka plugins).

Really though? They’re both great, I’ve used them both, and the easiest way of knowing is to code in both of them and make an executive decision.

Comments (9)

TI-BASIC (Round Two) (Basic BASIC Input…and Math)

If you have a program that consists solely of displaying things, and it’s not some kind of thrilling narrative or story, then you have problems.
What you need is input.
So here’s how to do it:

Input X
Disp X

Wasn’t that tricky? When you run it, you’ll get a question mark (as in, “Yo! What do you want to input?”) and when you type something in (numbers only, for now), it’ll name the input ‘X’. Then you just ask it to display X.

(Input is in the same menu as Disp, if you’re having trouble finding it. Typing in I then N then P…etcetera, won’t work)

Now let’s do something (somewhat) useful:

Input L #L for length
Input W #W for width
Disp L*W

(NOTE: Everything after a # is a comment. Do not type it into the actual program, it’s just me explaining why I wrote a particular line the way it is. So what you should type in is:)

Input L
Input W
Disp L*W

There’ll be a ‘?’. You type in a number (length of rectangle). There’ll be another ‘?’. You type in another number (width of rectangle). It displays the product of the two numbers (area of rectangle).

In terms of function that ends our lesson on Input. However, I find the ‘?’s pretty ugly. Especially when you have several. So here’s the syntax for pretty-fying them.

Input "Length:",L
Input "Width:",W
Disp "Area is:",L*W

Here’s the output:

Length:3
Width:2
Area is:
                6

One more thing: the ‘Input’ function only accepts numbers. This can be lame if you want the person to, say, type in their name. (What’s your name? 1337)

The TI-86 has a different function called “InpSt” (Input String), which lets the user input a string of letters, not numbers. However, I’m pretty sure that the TI-83/84/80 families do not have this. Darn.

That’s all you need to know about Input to succeed in life :)

Now let’s talk about math. (Don’t worry, it’s a bit more interesting than normal math.) We’ve already touched on it, and the best way of explaining it is giving a sample program.

(Note: I’m going to use ‘->’ for the Store key throughout this entire guide. The Store key has Sto> written on it (the > is filled in though) and is used for assigning values to variables.)(I’ll also use sqrt() for the square root sign)

This program will solve for the hypotenuse of a right triangle.

Input "A:",A
Input "B:",B
A^2 + B^2->D
Disp "C squared is:",D
sqrt(D)->C
Disp "Hypotenuse is:",C

So the moral of the story (erm, program, I mean) is that you can do math with variables in programs.

Leave a Comment

TI-BASIC (Round One-and-a-Half) (Fun with Disp)

We touched on the Disp command in Round One. I’ve since realized that in my quest for simplicity, I didn’t say very much about it. So here are a few things (Every other line is the Disp command, and the other lines are the output)
Disp “2+2″
2+2
Disp 2+2
4
Disp “2+2″,2+2
2+2 (aligned left) [new line] 4 (aligned right)

Disp 2+2,2+2,2+2,3+2

4 [new line]4 [new line]4 [new line]5 [new line]

Output(1,1,”Hello World!”)

(Note: This is not Disp. This is Disp’s big brother Output. Output let’s you ‘plot’ the output on the screen. So the ‘1,1′ means that it puts the output in the first column and the first row.

Hello World!

So now you should have a better handle on the syntax

Leave a Comment