Hacking – playing with things you’re not supposed to! (descramble words)

Posted: 24th February, 2013 in Software
Tags: , , , , , , , , , ,

I love to learn about new things and challenge myself, but crossword puzzles are so old school!  I have found something far more interesting, legal sites aimed at training people on essential hacking skills! – Hack this site and hack this are firm favourites.

This blog post contains the perl script I used to solve the basic programming level 1 challenge on Hack this site!  The mission is simple, descramble a selection of words within the allotted time.  To solve this problem I implemented a perl script, this is given below, read the comments to see how I approached the problem.

#!/usr/bin/perl -w

# Written by Craig Barry 24/2/13
# A program designed to descramble words
# Call the perl script and '>' to point the output to a txt file for ease of use in
# programming basic 1 on www.hackthissite.org/missions/prog/1/
# eg. bash$: perl HTS_sortwordlist.pl > descrambled_words.txt

# The script first parses the dictionary file (wordlist.txt) and hashes the sorted characters
# against a primary key, (the dictionary word).
# It then accepts the scrambled words that need to be identified.
# These are stored as sorted characters, eg. handle --> adehln
# We then iterate through all of the sorted dictionary words,
# When we find a match for a hash value (sorted dictionary word) we look up its corresponding
# key, which is the actual dictionary word, this is then piped to our output file, '>'

use strict;
my ($word,$sorted_word,@chars, %list_hash,$sorted_dict);
my $dictionary_file = "wordlist.txt"; # sub with <> for user input!
my $scrambled_file = "scrambled.txt"; # sub with <> for user input!

open(my $dict_FH, '<', $dictionary_file); #$dict_FH is my file handle for dictionary file
open(my $desc_FH, '<', $scrambled_file);  #$desc_FH is my file handle for descramble words

print STDERR "$_";
@chars = split //,$_;
@chars = sort @chars;
$sorted_word = join '',@chars;
$sorted_word =~ s/\n//g; #replace all new lines
$sorted_dict = $_; # this is the value of the key for the hash
$list_hash{$_} = $sorted_word; #the key is the actual word, sorted is the hash value

print STDERR "\nHash table in RAM, ready to start finding scrambled words...\n\n";

@chars       = undef;
$sorted_word = undef;
$word        = undef; #empty the old bits...
print STDERR "The scrambled words were...\n";
@chars = split //,$_;
@chars = sort @chars;
$sorted_word = join '',@chars; #this takes the scrambled word, arranges in alpha order

while( ($word, $sorted_dict) = each %list_hash ) #the key of the list hash is a real word!
$word =~ s/\W//g; #Remove all the other invisible formatting
$sorted_dict =~ s/\W//g;
$sorted_word =~ s/\W//g;

if ($sorted_word eq $sorted_dict)
print "$word, "; # We matched a scrambled word with a dictionary word
print STDERR "$_";


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s