Computationally Minded
Various things computationally oriented. Tech stuff, too.

All Your Link Are Belong To Us!

If you came by way of a search engine and did not find exactly what you were looking for, try the




People who may think me ungrateful rather than incompetent















Smart people I ought to read more




People who can code rings around me



Writers, Fantasts




Those who have cared to comment






Well-connected





Can’t help myself






Unfiled for now




Self-linked... creepy, or crappy?







Subscribe to "Computationally Minded" in Radio UserLand.

Click to see the XML version of this web page.

Click here to send an email to the editor of this weblog.


Saturday 2 November 2002
 

I tried my hand at coding the Luhn algorithm in Smalltalk, as almost every other language seems to have had a shot at it. I am certain that are better implementations than this quick hack, but here it is. It works true for the example case that the accordian guy uses, but having no other test cases, this is what I have. Note that this is in the Squeak dialect, and it attaches to a base class.

Integer>>isValidLuhn

	| str accOdd accEven anEven |
	accOdd _ accEven _ 0.
	str _ ReadStream on: self asString reverse.
	[str atEnd]
		whileFalse: [accOdd _ accOdd + str next asString asNumber.
			anEven _ str next.
			anEven
				ifNotNil: [anEven _ anEven asString asNumber * 2.
					anEven > 9
						ifTrue: [anEven _ anEven - 9].
					accEven _ accEven + anEven]].
	^ accOdd + accEven \ 10 = 0

[update: Whoops. I was using a stripped image (the image I use for my Comanche Swiki), so all the variable names were lost, so I replaced them. The underscore is the keyboard key used for the assignment operator, which in Squeak images renders as a left-pointing arrow. The caret sign is an up arrow and returns from the function.

update: There must be a better implementation, probably using #inject:into:. The implementation above streams through the digits of the integer, accumulating the odd and even digits alternately.

update: I don’t know why the algorithm description insists on adding the digits of the doubled even-positioned digits; it seems to me that the digit-added property holds true even if you add the doubled digits together after, instead of at each step. That is, 35 + 11 and 8 + 2 both ultimately equal one. I don’t remember what this property is called. If that is the case, the implementation above could be simplified to something like, um, oh, wait, never mind. Sequence of actions is important. Never mind.

update: I originally pasted in the file-in version, but I thought if I presented as it is presented in Kent Beck, it would be less scary. Smalltalk really is a pretty language.

update: Whoops, more Smalltalk implementations just popped into my head: here and here.]
10:06:50 PM    comment []


An oldie, but Paul Graham writes for the ages.

Good design...

  • ...is simple
  • ...is timeless
  • ...solves the right problem
  • ...is suggestive
  • ...is often slightly funny
  • ...is hard
  • ...looks easy
  • ...uses symmetry
  • ...resembles nature
  • ...is redesign
  • ...can copy
  • ...is often strange
  • ...happens in chunks
  • ...is often daring

9:21:47 PM    comment []


Click here to visit the Radio UserLand website. © Copyright 2002 Richard Allan Baruz.
Last update: 12/5/02; 2:45:28 PM.
November 2002
Sun Mon Tue Wed Thu Fri Sat
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Oct   Dec