Archive for October, 2010

Wildcards in C

As I have been using the command line more recently, I have started to wonder many things. One of the most commonly used thing throughout command-line applications is wildcards. That is, replacing part of a search with a *, or deleting all the files that end with a .c. Since wildcards are so useful, I figured I would make my own wildcard program to see how easy it was to make.

My method of wildcard-checking is as follows: loop through the pattern until you see a * while checking regular characters with the other string. If I find a ‘*’, I find all of the characters until the end of string. I then check to see where the last occurrence of that pattern is in the regular string. If I find it, I go from there.  This is not an easy thing to understand in english, so take a look at the C code at my Git repository for it: BasicWildcards

[UPDATE]: One of our readers commented on this post with information on how command-line apps do it. There is a function called glob() (declared in glob.h) that filters files that match a simple pattern. Since I am sure you are wondering how this works, check out this little example:

#include <stdio.h>
#include <glob.h>

int main () {
    glob_t g;
    int i;
    glob("*.php", GLOB_ERR | GLOB_NOSORT, NULL, &g);
    for (i = 0; i < g.gl_matchc; i++) {
        printf("%s\n", g.gl_pathv[i]);
    }
}

Spam on WordPress Blogs?

This is a topic that I do not normally get into, but today I was reviewing the unapproved comments made on my blog and found quite a bit of spam. Most of the spam contains links to products, or information about prescription drugs. The rest is bogus info about insurance of different sorts. My question for you is, why do people spam wordpress blogs? And a follow-up question, how?

Do people write scripts that crawl the internet looking for wordpress php filenames and then try to post comments on them? I am not sure if this is a regular occurrence, or just some prank, but I am certainly interested in learning more. Do people with wordpress installed on their site allow comments at all? Should I turn comments off all together? I would be glad to hear any output that anyone might have.

Automator on Mac

Today I decided that it was time to make a macheads101 video.  Since I have made so many programming tutorials lately, I decided to make a video on something else.  As I tried to think of an idea, a though occurred to me: why not make a video on something that I don’t know yet?  I then went on to find that Automator has been sitting on my computer since I got it, and never once have I even considered using it.

It turns out that automator is one of the most useful programs that comes with your mac.  From scaling images to running automated sql queries, Automator is the app for you.  The workflow of automator is extremely strait-forward and self explanatory.  You can drag tons of different instructions into a linear string, creating a simple, step by step program.  Here is the basic set of commands that would be used to scale a folder full of images: Ask User for Text > Get Directory Contents > Copy Files > Scale Images.  Since pretty much every instruction returns a string or an array, and every instruction takes one, almost any combination of instructions is possible.

Of course, I had to make a youtube video on my wonderful discovery.  Check it out (I also go over some apple scripting) on youtube now!

Using Google Translator as a Proxy

Today in school my friend showed me a practical proxy that has been sitting in front of my face for quite some time. Google Translator has an option to translate any webpage, and it actually fetches the page for you to do that. So my idea was to make an app that uses Google Translator to translate base64 web pages to a different language with the same alphabet, thus causing nothing to change.

As I made a simple objective-C class library to do this, I noticed suddenly that my requests stopped going through properly. It turns out that google will block your IP for certain features if it thinks you are auto-fetching their site in some way. Even though my IP is blocked from translating websites, I will post my existing code for anyone to read (if they want to). I have no idea if it works perfectly since I cannot test it, but I am hoping for the best. The code: GoogleTranslateFetcher.zip.  Here is a screenshot of what happens to google translator after a few requests made by my program:

Why Do Math Homework?

A few days ago my friend showed me an extra credit assignment he was given for his algebra two course. The problem was supposed to be set up as a systems of equations (with three variables.) I quickly found it unreasonable that one of the three equations was not linear, but exponential. After spending many hours attempting to solve the equation on paper, I decided to write a program to find the answer.  The problem is as follows:

A park named Writer’s Rectangle recently opened in our town.  When asked about the dimensions of the rectangle, Jarvis Jameson, the city planner responded with these clues:

- The diagonals of the rectangular park plus its longer sides together measure seven times one of the shorter sides.
- The length of the diagonal is 250 meters longer than one of the shorter sides.

Find the area of the park.

After I realized that no one I knew could solve the problem, I wrote a simple python script to brute force all three variables, hence finding the answer (if all the variables turned out to be integers.) My script found an answer in under 2 minutes! All the program does is check every combination of variables and check them with all three equations.  Here is the code:

for d in range(1,1000):
	for l in range(1,1000):
		for s in range(1,1000):
			if d - s == 250:
				if 2*d + 2*l == 7*s:
					if d**2 == (l**2 + s**2):
						print "d: " + str(d)
						print "l: " + str(l)
						print "s: " + str(s)

Return top