Showing posts with label Adventures in CS. Show all posts
Showing posts with label Adventures in CS. Show all posts

Thursday, April 14, 2011

Thus Spake HAL

Dissociated press is a computer program that generates text based on another text. Here is an example of dissociated press based on the US presidential inaugural addresses:
Fellow - Citizens of the world. Abroad and at the same mistake. In a great river system, purified and made self. And there is a privilege which he has shown that our Government together and laugh together and save the lives of our civilization secure. In our time, a pleasing guaranty that the actual equality of rewards or possessions so long bled and suffered, we have fought not for themselves. Mindful of these States, was out of the Ohio or of rank have been distinguished by some from a fever of words

A computer would have apparently written The Book of Genesis thus:
In the day when they were both the daughters of Lot with him, at the first born; I bare the loss of it; for a possession of flocks, and she called his name Onan. And Hamor communed with him for righteousness. And her brother and her damsels, and possession of herds, and at night he shall be fruitful and multiply thee, then the other end thereof. And Terah took Abram his son with the sword? Wherefore didst thou flee away secretly, and where thou art yet alive,

I was curious how Gandhi would come out. The following text is based on a collection of Gandhi's writings at Project Gutenberg. I did not remove the Project Gutenberg copyright notice before feeding it to the computer, and the effects are pleasing:
Project Gutenberg is a grand doctrine of life. With them it is to weld India into one, he instanced Scotland's poverty of the Government whenever it needed their help. Their resistance consisted of disobedience of the copyright holder, your use and distribution of Project Gutenberg-tm eBooks are often created from several printed editions, all of them fellow cranks--if in similar circumstances I acted towards them differently from my own family. It were better to concentrate my effort upon the subject before us. I was hard put to it that the co-operative movement. " In his opinion

 

Wednesday, April 28, 2010

Binary Search Algorithm

Apparently, only 10 percent of programmers can correctly code the binary search algorithm. What follows is my attempt to prove once and for all that I belong to that select group of programmers! I implemented the algorithm in Excel VBA.
Option Explicit
'++++++++++++++++++++++++++++++++++++++++++++++++
' Author: Ram Limbu
' Date: 2010/04/28
' Description: Binary Search Algorithm in VBA
'++++++++++++++++++++++++++++++++++++++++++++++++

Sub BinaySearch()
Dim UpperLimit As Long
Dim LowerLimit As Long
Dim Mid As Long
Dim Val As Long
Dim NotFound As Boolean
Dim WS As Worksheet

On Error GoTo ErrRtn

'prompt user to enter the number to search
Val = InputBox("Please, enter the number to search")

'set WS to the first Worksheet
Set WS = ThisWorkbook.Worksheets(1)

NotFound = True
LowerLimit = 1

UpperLimit = WS.Cells(65536, 1).End(xlUp).Row

If UpperLimit = 1 And IsEmpty(WS.Cells(1, 1).Value) Then
MsgBox "The Column A on Worksheet1 is empty. Exiting ..."
Exit Sub
End If

While NotFound And LowerLimit <= UpperLimit
Mid = (LowerLimit + UpperLimit) \ 2 ' perform integer division
If Val = WS.Cells(Mid, 1) Then
NotFound = False
ElseIf Val < WS.Cells(Mid, 1) Then
UpperLimit = Mid - 1
Else
LowerLimit = Mid + 1
End If
Wend

If NotFound Then
MsgBox Val & " wasn't found"
Else
WS.Cells(Mid, 1).Select
MsgBox Val & " was found at row " & Mid
End If
Exit Sub
ErrRtn:
MsgBox Err.Description
End Sub

The assumptions are as follows: (1) The data set is pre-ordered and (2) located on Column A of the first Worksheet. To keep the code simple, no data validation is performed.