in reply to Sorting characters within a string
1) Generate every possible string and its sorted version, storing them in a hash with the unsorted string as the key & the sorted string as the value. There's only, what, 45 possible strings? That's doable.
2) Split the string into characters, count the number of each character, then output the characters in order based on the counts. This is O(n) so it'd be a win if your strings were really long, but it's just overkill for these short strings.
If performance is a problem, a fairly painless thing to do is cache the sorted strings as you calculate them:
This is of course just a lazy variant on #1 above.if (!exists $sort_cache{$bases}) { $sort_cache{$bases} = join( '', sort split('', $bases)); } return $sort_cache{$bases};
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Sorting characters within a string
by jlongino (Parson) on Aug 24, 2001 at 05:05 UTC | |
by tilly (Archbishop) on Aug 24, 2001 at 06:02 UTC | |
by guillaume (Pilgrim) on Aug 24, 2001 at 05:45 UTC | |
by jlongino (Parson) on Aug 24, 2001 at 05:55 UTC | |
by jlongino (Parson) on Aug 24, 2001 at 05:33 UTC | |
|
Re: Re: Sorting characters within a string
by dga (Hermit) on Aug 24, 2001 at 23:42 UTC |