Sort the letters of string in alphabetical order: Difference between revisions
Content added Content deleted
Line 382: | Line 382: | ||
=={{header|Eiffel}}== |
=={{header|Eiffel}}== |
||
<lang eiffel> |
|||
class |
|||
SORT_STRING_LETTERS |
|||
feature -- Basic Ops |
|||
sort_string (s: STRING): STRING |
|||
-- Perform `sort_string' on `s' such that |
|||
-- each letter is in ascending alphabetical order. |
|||
note |
|||
deviation: "[ |
|||
This Eiffel example deviates from the task requirement for this |
|||
Rosetta Code task in that we reuse Eiffel Base library code for |
|||
the {SORTED_TWO_WAY_LIST [G]}. We do this for two very good reasons: |
|||
1. Reuse is king. Never code what is already coded and tested. |
|||
2. The library code is readily available for examination |
|||
(i.e. the library code is not hidden and unaccessible). |
|||
Based on #1 and #2 above, examine the code in: {SORTED_TWO_WAY_LIST}.make_from_iterable |
|||
Specifically, look at the `extend' routine and the routines it calls (i.e. `search_after', |
|||
`put_left', and `back'). These routines will tell you the story of how sorting can |
|||
be coded in Eiffel. There is no need to rehash that code here. |
|||
]" |
|||
local |
|||
l_list: SORTED_TWO_WAY_LIST [CHARACTER] |
|||
do |
|||
create l_list.make_from_iterable (s) -- Add & Auto-sort string by chars |
|||
create Result.make_empty -- Create the Result STRING |
|||
⟳ c:l_list ¦ Result.append_character (c) ⟲ -- Populate it with the sorted chars |
|||
Result.adjust -- Remove the leading white space |
|||
end |
|||
end |
|||
</lang> |
|||
And the Test Code to operate it. |
|||
<lang eiffel> |
|||
class |
|||
RC_SORT_STRING_LETTERS_TEST_SET |
|||
inherit |
|||
TEST_SET_SUPPORT |
|||
feature -- Test routines |
|||
rc_sort_string_letters_test |
|||
-- Test {SORT_STRING_LETTERS}. |
|||
note |
|||
testing: |
|||
"covers/{SORT_STRING_LETTERS}", |
|||
"execution/isolated", |
|||
"execution/serial" |
|||
do |
|||
assert_strings_equal ("sorted", now_is_string, item.sort_string ("Now is the time for all good men to come to the aid of their country.")) |
|||
end |
|||
feature {NONE} -- Test Support |
|||
now_is_string: STRING = "[ |
|||
.Naaccddeeeeeeffghhhiiiillmmmnnooooooooorrrstttttttuwy |
|||
]" |
|||
item: SORT_STRING_LETTERS |
|||
-- An `item' for testing. |
|||
once |
|||
create Result |
|||
end |
|||
end |
|||
</lang> |
|||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |