isIsogram

 

                | i |

               

                i := 0.

               

                self asLowercase do: [ :char |

                                | val |

                                val := (char asInteger - 96).

                                (val between: 1 and: 26) ifFalse: [ ^ false ].

                                (i bitAt: val ) == 1 ifTrue: [ ^ false ].

                                i bitAt: val put: 1

                ].

 

                ^ true

 

 

An interesting observation here is that if #asLowercase is moved to  each character instead ���val := (char asLowercase  asInteger - 96).��� Then it leads to a 4-5x performance loss .

 

 

 

 

From: Pharo-users [mailto:pharo-users-bounces@lists.pharo.org] On Behalf Of Damien Pollet
Sent: Friday, November 11, 2016 4:14 PM
To: Any question about pharo is welcome <pharo-users@lists.pharo.org>
Subject: Re: [Pharo-users] Little challenges for a friday evening

 

On 11 November 2016 at 12:02, stepharo <stepharo@free.fr> wrote:

String>>#isIsogram
     | letters |
     letters := Dictionary new.
     self do: [ :x | letters at: x ifAbsent: [] ifPresent: [ ^false ].

Yes I did that one too and I was surprised because it was as slow as the bag implementation.

 

Anyone tried replacing the dictionary with an array of 26 booleans and indexing based on ascii, or even a SmallInteger and bit shifting?