Nov 24

I've seen several different approaches to dictionary generators over time, and this was one method that struck me that it didn't seem like anyone else had used. If someone else has used this method, I apologize, you deserve full credit, but I came up with this particular version completely independently. I first wrote it in JavaScript (if you want that version, feel free to leave me a comment, but it was slow as fuck), then I ported it to Python for better speed. I even wrote comments for once, so that you can understand what I did (it's not very hard to understand, I just need to get into the habit). Enjoy it :)

def generate(size):
	f = file(raw_input("Output file: "), "w") # Set the output file
	y = [] # Create an empty list
	z = True # Set the toggle boolean to "True" (use the x list)
	charset = "0123456789abcdefghijklmnopqrstuvwxyz" # Charset for generating dictionary
	charlen = len(charset) # Length of charset
	x = list(charset) # Make a list with all the characters in the charset string
	for i in xrange(charlen): f.write(x[i] + "\\n") # Write the character set to the output file
	for i in xrange(1,size+1): # Loop until you have generated to the length specified
		if z == False: # If the toggle boolean is "False", append to y
			xlen = len(x) # Length of list x
			for i in xrange(0,charlen): # Loop through all the characters in charset
				for c in xrange(0,xlen): # Loop through all the items in x and prefix them with the appropriate character
					y.append(charset[i] + x[c]) # Append results to y
					f.write(charset[i] + x[c] + "\\n") # Write the results to file
			x = [] # Clear out list x

		else: # If the toggle boolean is "True", append to x
			ylen = len(y) # Length of list y
			for i in xrange(0,charlen): # Loop through all the characters in charset
				for c in xrange(0,ylen): # Loop through all the items in y and prefix them with the appropriate character
					x.append(charset[i] + y[c]) # Append results to x
					f.write(charset[i] + y[c] + "\\n") # Write the results to file
			y = [] # Clear out list y
		z = z == False # Flip the toggle boolean
	f.close() # Close the output file
generate(4) # Call with largest character length you want to generate
  • Digg
  • StumbleUpon
  • del.icio.us
  • Reddit
Nov 18

I had a project I was working on yesterday and I needed to sort out an array in order of most to least repeated, so I came up with this:

$array = array_count_values($array);
array_multisort($array, SORT_DESC);

If you don't want the frequency that they occur in as the value, you can do this:

$array = array_count_values($array);
array_multisort($array, SORT_DESC);
$array = array_keys($array);

Hope this helps someone. :)

  • Digg
  • StumbleUpon
  • del.icio.us
  • Reddit
Nov 12

Here was my problem: I needed to reverse a boolean value throughout a loop. True, False, True, False, etc. In most languages, let's say PHP, that looks like this:

$var = !$var;

Python doesn't like the ! when used in this context. However, I got to thinking, and this is what I came up with.

var = var == False

or

var = var != True

Hope this is useful to someone.

  • Digg
  • StumbleUpon
  • del.icio.us
  • Reddit
Nov 11

I stumbled upon this post which lists the parts of 10 episodes of an interesting documentary, The Day The Universe Changed. I soon realized that I didn't really want to open up 50 windows to watch them all in, so I wrote this. It's rather simplistic, and could certainly be improved, but for all intents and purposes, it works. Simply enter the links to the videos you want to watch, and hit the "Next" button when you're ready to move on to the next video.

  • Digg
  • StumbleUpon
  • del.icio.us
  • Reddit
Nov 9

Sen. Patrick Leahy (D-VT) and Sen. John Cornyn (R-TX) yesterday introduced the Intellectual Property Enforcement Act (PDF), with Leahy saying, "The PIRATE Act has passed the Senate on three separate occasions; this should be the Congress in which it becomes law." Like previous incarnations of the PIRATE Act, this one tries to force the Department of Justice to bring suits against individual file-swappers, something that could save the recording industry plenty of money and could also displace some of the "bad guy" stigma that the labels have acquired after suing people like Jammie Thomas. The bill would give the Department of Justice authority to bring civil (not just criminal) cases against infringers, though it does limit penalties to those that could be imposed in criminal proceedings. The Attorney General can also bring such civil suits only when the act in question constitutes a crime (such civil suits can be easier to win).

Read the rest here.

Once again, we have the age old debate: does innovation survive if it doesn't produce direct income? Well, once again, instead of thinking, our government has decided that they know the answer and are going to try to fix the "problem". What is this problem? The problem is that record executives don't have as much gold lining on their pockets (so they claim). Don't kid yourself - artists make obscene sums of money, they don't need to make any more. If they claim they do, they're lying. I promise. And if they're not making enough money, it's for one of two reasons. They're either being screwed by the record executives, or they're simply not good.
Read the rest of this entry »

  • Digg
  • StumbleUpon
  • del.icio.us
  • Reddit

« Previous Entries Next Entries »