Friday, December 08, 2006

Codegolf

Ive been working on this codegolf challenge recently which I found quite fun to do (codegolf == accomplish a challenge in code using least amt of lines )

The challenge in question:-

The game of REVERSE requires you to arrange a list of numbers in numerical order from left to right. To move, you tell the computer how many numbers (counting from the left) to reverse. For example, if the current list is 2 3 4 5 1 6 7 8 9 and you reverse 4, the result will be 5 4 3 2 1 6 7 8 9. Now if you reverse 5, you win.

What we're asking you to do is, given a list of numbers in a random order, produce the moves required to arrange them so they end up in numerical order.

(from codegolf.com)



Now Ive been working on this for a couple of hours and the best I can come up with is this ( My language of choice, python):-


[UPDATE #1]: File Size - 181kb

1) Changed the while loop condition

2)A different way of printing, by adding a comma after the last object one can suppress the new line that is automatically added by print in python

3)Removing unnecessary indentation helped on the file size

4)Didnt need the strip function

import re,sys
n = map(int,re.split(" ",sys.stdin.readline()))
l = len(n)
while l:
m = n.index(max(n[0:l])) + 1
n[:m] = n[m-1::-1]
n[:l] = n[l-1::-1]
print m,l,
l -= 1


[ORIGINAL]


import re,sys
n = map(int,re.split(" ",str(sys.stdin.readline()).strip()))
l = len(n)
while n != sorted(n):
m = n.index(max(n[0:l])) + 1
n[:m] = n[m-1::-1]
n[:l] = n[l-1::-1]
print m,"\n",l
l -= 1


the algorithm works in the following way:-

given a list, get its length

find the index of the maximum value within it, do a reverse from 0 to the index, so now the max is in front (and the move being the index + 1)

then do a reverse again on the length of the list , bringing the max to the end of the list (now the move after that being the length of the list)

decrease the length, so you iterate over a smaller list and repeat the same steps on the smaller section of the list


Anyone have anymore ideas to make this shorter ( either the program itself or the algorithm ) ?

Saturday, December 02, 2006

C-USA Champions


That was a fantastic game yesterday. Had a lot of fun ... definitely need more pictures

Thursday, October 05, 2006

Cant argue with this

"You’ve achieved success in your field when you don’t know whether what you’re doing is work or play." - Warren Beatty

Saturday, September 23, 2006

Learning Python

Just started to pick up on Python,

I am sure everyone who took COSC 2320 with Dr. Anderson remembers the word count program. Well after going through some of the tutorials of python here is the same program in python

Not only is it smaller, but its also easier to understand. Hopefully more to follow


import re
import string

#dictionary to store words and their counts
word_count = {}

#read in text document line by line
for line in open("trial.txt").readlines():

#remove leading and trailing whitespace
line = string.strip(line)

#split the string into words
#based on whitespace, punctuation, digits
for word in re.split("["+string.whitespace+string.punctuation+string.digits+"]",line):

#make the word lower case
word = string.lower(word)

#check if it is actually a word
if re.match("^["+string.lowercase+"]+$",word):

#increment count if true
if word_count.has_key(word):
word_count[word]+=1

#else add entry
else:
word_count[word] = 1

for w in word_count:
print w, ":" ,word_count[w]



Edit: Some more playing around


import re
import string

word_count = {}

text = open("trial.txt").read();

#list of words delimited by whitespace, punctuation and digits
#iterate by words in returned list from split
#lower case all the words in the text
words = re.split("["+string.whitespace+string.punctuation+string.digits+"]",string.lower(text))

#go through the list
for i in range(0,len(words)-1):

#as long as the word in the list is a word and is not already a key
if re.match("^["+string.lowercase+"]+$",words[i]) and not word_count.has_key(words[i]):

#add to the dictionary and get the count from the list
word_count[words[i]] = words.count(words[i])

for w in word_count:
print w,":",word_count[w]

Thursday, August 31, 2006

Blogger Beta

Finally got into the beta, but where did the rest of the enteries go ? They are back

Friday, August 25, 2006

Time for another update .... dont know about what though ..... anyone able to switch over to the new blogger beta ?

Friday, June 16, 2006

A templating implementation for PHP that probably comes quite close to the one provided by struts :- Flexy http://pear.php.net/manual/en/package.html.html-template-flexy.php

Struts so far has been awesome

Tuesday, May 30, 2006

Lets see here

Well its kinda late for the highlights but here they are anyway:-

Michael awakens and finding himself in the most awkward position ever ( you have to hear the story from him for the full effect )

Awesome Career Fair

We all passed OS ( this one was too close )

PGH 547 is being remodelled ( and not the way we thought it would be, this one not so much of a highlist )

Completing the SE project 30 minutes before the demo

At the moment this is all that I remember , and good luck to everyone who will be starting their internship tomorrow.

Tuesday, May 23, 2006

Tuesday, April 04, 2006

Oldest Account

Just going through a list of accounts at places that I have ... my oldest one is my first email address that I created at hotmail.com .... I created that account 9 years ago !!!! I still use it but mainly as a junk mail collector

Whats your oldest account and do you still use it ?

OS

"In this a blow at the hardware level a realist"

I beat you to it abhi lol

Tuesday, March 14, 2006

Finally

Finally I completed the challenges on slyfx.com ..... today after Tom posted something about Java, i decided to have one more go at the final challenge, 20 minutes later I was done .... now hopefully the people on that site come back and write the new levels.

Sunday, March 12, 2006

Eclipse + PHP

If anyone is using Eclipse with the PHPeclipse plugin and you want to disable the at times frustating auto completing of quotes follow these steps:-

Windows -> Preferences -> PHPeclipse -> PHP, then select the Typing tab and uncheck the two options to enable closing of quotes

Wednesday, March 08, 2006

CodeIgniter

This PHP framework is probably one of the easiest frameworks to get your head around. It was released a couple of days ago and it has been real impressive. Though relatively new it has a lot of the standard features while offering new stuff like Image Manipulation, Email Class, Encryption, Benchmarking etc. The best part about it is the low learning curve. As part of an experiment I recoded the entire CougarCS library with it and it was quite a pleasant experience.

Replacing the library with this code is another issue, but it was done real quick

Have a go at it here ==> CodeIgniter

Monday, February 27, 2006

How people find this site

Here are some interesting keywords that people use while searching and then stumble on my blog:-

will kashif and i get into a big fight today? (Google) ==> this one had me laughing, is it you shey ?? lol j/k

"Kashif Deniro" (Google)

DirectShow Syllabus (AltaVista) ==> people still use AV ???

Thursday, February 23, 2006

Googlefasting

Chris Pirillo has been trying googlefasting, which is quite interesting since I have thought about and even tried in the past( and failed !!! ). My search engine of choice was Yahoo! ( no surprise there ) but it just didnt cut it. A feeling you always get is that maybe your missing out on something from the search results and then you end up going to google

Conclusion: You can stay away from their services like mail, maps, but when it comes to search it is very difficult to replace Google. Maybe if Yahoo improves on its mindset feature switching could be a little more easier.

Wednesday, February 22, 2006

CougarCS Career Fair

All the hard work paid off. It was a complete success !!!!

Shey where are the pics ??

Monday, February 06, 2006

was it just me or the superbowl ads this year were just terrible ?

Monday, January 23, 2006

Interesting Article

Claims college students are not ready for life


Twenty percent of U.S. college students graduating from 4-year colleges don't have the skills to handle tasks like estimating whether their car has enough gas, or calculating the total cost of ordering office supplies, a new study has found.

Thursday, January 19, 2006

Friday, January 06, 2006

More MSN 8.0 Invites

Its invites time again, I have 6 to spare, first five comments get them
I came back from Florida yesterday, here is one of many pics I took there :)