Re: how can I improve this
Posted by
hernanmd on
Sep 14, 2020; 8:19pm
URL: https://forum.world.st/how-can-I-improve-this-tp5121828p5121835.html
Hi Roelof,
Maybe something like this:
String>>hammingDistanceTo: aString
" Answer the amount of substitutions between the receiver and aString. Both must be of equal length "
^ (1 to: self size) count: [ : i | (self at: i) ~= (aString at: i) ]
El lun., 14 sept. 2020 a las 16:15, Roelof Wobben via Pharo-users (<
[hidden email]>) escribió:
Hello,
I have solved the hamming challenge of exercism where I have to find in
how many places two strings are different.
my solution is :
distanceStrand1: aString strand2: aString2
aString size == aString2 size
ifFalse: [ DomainError signal: (self messageFor: aString
strand2: aString2) ].
^ (1 to: aString size)
sumNumbers: [ :index |
(aString at: index) == (aString2 at: index)
ifTrue: [ 0 ]
ifFalse: [ 1 ] ]
messageFor: aString strand2: aString2
aString notEmpty & aString2 notEmpty
ifTrue: [ ^ 'left and right strands must be of equal length' ].
aString ifEmpty: [ ^ 'left strand must not be empty' ].
^ 'right strand must not be empty'
Can this be improved?
Roelof