Dutch national flag problem: Difference between revisions

Content added Content deleted
mNo edit summary
mNo edit summary
Line 27: Line 27:
generate_unsorted_sequence
generate_unsorted_sequence
importing
importing
i_length type int4
lenght_of_sequence type int4
returning
returning
value(r_unsorted_sequence) type string,
value(unsorted_sequence) type string,


sort_sequence
sort_sequence
importing
changing
i_sequence type string
sequence_to_be_sorted type string,
returning
value(r_sorted_sequence) type string,


is_sorted
is_sorted
importing
importing
i_sequence type string
sequence_to_check type string
returning
returning
value(r_sorted) type abap_bool.
value(sorted) type abap_bool.
endinterface.
endinterface.


Line 53: Line 51:
constants:
constants:
begin of dutch_flag_colors,
begin of dutch_flag_colors,
red type char1 value `R`,
red type char1 value 'R',
white type char1 value `W`,
white type char1 value 'W',
blue type char1 value `B`,
blue type char1 value 'B',
end of dutch_flag_colors.
end of dutch_flag_colors.
endclass.
endclass.
Line 67: Line 65:
max = 2 ).
max = 2 ).


do lenght_of_sequence - 1 times.
data(random_sequence) = ``.

do i_length - 1 times.
data(random_int) = random_int_generator->get_next( ).
data(random_int) = random_int_generator->get_next( ).


Line 77: Line 73:
when random_int eq 2 then dutch_flag_colors-blue ).
when random_int eq 2 then dutch_flag_colors-blue ).


random_sequence = |{ random_sequence }{ next_color }|.
unsorted_sequence = |{ unsorted_sequence }{ next_color }|.
enddo.
enddo.


if strlen( random_sequence ) > 0.
if strlen( unsorted_sequence ) > 0.
random_int = random_int_generator->get_next( ).
random_int = random_int_generator->get_next( ).


Line 87: Line 83:
when random_int eq 1 then dutch_flag_colors-white ).
when random_int eq 1 then dutch_flag_colors-white ).


random_sequence = |{ random_sequence }{ next_color }|.
unsorted_sequence = |{ unsorted_sequence }{ next_color }|.
endif.
endif.

r_unsorted_sequence = random_sequence.
endmethod.
endmethod.




method sorting_problem~sort_sequence.
method sorting_problem~sort_sequence.
data(sequence) = i_sequence.
data(low_index) = 0.
data(low_index) = 0.
data(middle_index) = 0.
data(middle_index) = 0.
data(high_index) = strlen( sequence ) - 1.
data(high_index) = strlen( sequence_to_be_sorted ) - 1.


while middle_index <= high_index.
while middle_index <= high_index.
data(current_color) = sequence+middle_index(1).
data(current_color) = sequence_to_be_sorted+middle_index(1).


if current_color eq dutch_flag_colors-red.
if current_color eq dutch_flag_colors-red.
data(buffer) = sequence+low_index(1).
data(buffer) = sequence_to_be_sorted+low_index(1).


sequence = replace(
sequence_to_be_sorted = replace(
val = sequence
val = sequence_to_be_sorted
off = middle_index
off = middle_index
len = 1
len = 1
with = buffer ).
with = buffer ).


sequence = replace(
sequence_to_be_sorted = replace(
val = sequence
val = sequence_to_be_sorted
off = low_index
off = low_index
len = 1
len = 1
Line 122: Line 115:
middle_index = middle_index + 1.
middle_index = middle_index + 1.
elseif current_color eq dutch_flag_colors-blue.
elseif current_color eq dutch_flag_colors-blue.
buffer = sequence+high_index(1).
buffer = sequence_to_be_sorted+high_index(1).


sequence = replace(
sequence_to_be_sorted = replace(
val = sequence
val = sequence_to_be_sorted
off = middle_index
off = middle_index
len = 1
len = 1
with = buffer ).
with = buffer ).


sequence = replace(
sequence_to_be_sorted = replace(
val = sequence
val = sequence_to_be_sorted
off = high_index
off = high_index
len = 1
len = 1
Line 141: Line 134:
endif.
endif.
endwhile.
endwhile.

r_sorted_sequence = sequence.
endmethod.
endmethod.




method sorting_problem~is_sorted.
method sorting_problem~is_sorted.
r_sorted = abap_true.
sorted = abap_true.


do strlen( i_sequence ) - 1 times.
do strlen( sequence_to_check ) - 1 times.
data(current_character_index) = sy-index - 1.
data(current_character_index) = sy-index - 1.
data(current_color) = i_sequence+current_character_index(1).
data(current_color) = sequence_to_check+current_character_index(1).
data(next_color) = i_sequence+sy-index(1).
data(next_color) = sequence_to_check+sy-index(1).


r_sorted = cond abap_bool(
sorted = cond abap_bool(
when ( current_color eq dutch_flag_colors-red and
when ( current_color eq dutch_flag_colors-red and
( next_color eq current_color or
( next_color eq current_color or
Line 166: Line 157:
( current_color eq dutch_flag_colors-blue and
( current_color eq dutch_flag_colors-blue and
current_color eq next_color )
current_color eq next_color )
then r_sorted
then sorted
else abap_false ).
else abap_false ).


check r_sorted eq abap_false.
check sorted eq abap_false.
return.
return.
enddo.
enddo.
Line 181: Line 172:
dutch_national_flag_problem = new dutch_national_flag_problem( ).
dutch_national_flag_problem = new dutch_national_flag_problem( ).


data(unsorted_sequence) = dutch_national_flag_problem->generate_unsorted_sequence( 20 ).
data(sequence) = dutch_national_flag_problem->generate_unsorted_sequence( 20 ).


data(sorted_sequence) = dutch_national_flag_problem->sort_sequence( unsorted_sequence ).
write:|{ sequence }, is sorted? -> { dutch_national_flag_problem->is_sorted( sequence ) }|, /.


write:|{ unsorted_sequence }, is sorted? -> { dutch_national_flag_problem->is_sorted( unsorted_sequence ) }|, /.
dutch_national_flag_problem->sort_sequence( changing sequence_to_be_sorted = sequence ).


write:|{ sorted_sequence }, is sorted? -> { dutch_national_flag_problem->is_sorted( sorted_sequence ) }|, /.
write:|{ sequence }, is sorted? -> { dutch_national_flag_problem->is_sorted( sequence ) }|, /.
</lang>
</lang>