Stage 2 summary at 50 accepted trajectories
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
70
tasks_gen/train/G1113/reference/don_sum.func.abap
Normal file
70
tasks_gen/train/G1113/reference/don_sum.func.abap
Normal file
@@ -0,0 +1,70 @@
|
||||
FUNCTION {{P}}DON_SUM
|
||||
IMPORTING
|
||||
iv_donations TYPE string
|
||||
EXPORTING
|
||||
ev_donor_count TYPE i
|
||||
CHANGING
|
||||
cv_totals TYPE string
|
||||
EXCEPTIONS
|
||||
invalid_units.
|
||||
|
||||
TYPES:
|
||||
BEGIN OF ty_total,
|
||||
donor_id TYPE string,
|
||||
group_code TYPE string,
|
||||
total_units TYPE i,
|
||||
END OF ty_total.
|
||||
|
||||
DATA lt_lines TYPE STANDARD TABLE OF string WITH EMPTY KEY.
|
||||
DATA lt_totals TYPE SORTED TABLE OF ty_total
|
||||
WITH UNIQUE KEY donor_id group_code.
|
||||
DATA lv_line TYPE string.
|
||||
DATA lv_donor TYPE string.
|
||||
DATA lv_group TYPE string.
|
||||
DATA lv_units TYPE string.
|
||||
DATA lv_units_int TYPE i.
|
||||
DATA lv_result TYPE string.
|
||||
DATA lv_prev TYPE string.
|
||||
|
||||
IF iv_donations IS NOT INITIAL.
|
||||
SPLIT iv_donations AT cl_abap_char_utilities=>newline INTO TABLE lt_lines.
|
||||
ENDIF.
|
||||
|
||||
LOOP AT lt_lines INTO lv_line.
|
||||
SPLIT lv_line AT ';' INTO lv_donor lv_group lv_units.
|
||||
|
||||
lv_units_int = lv_units.
|
||||
IF lv_units_int < 1.
|
||||
RAISE invalid_units.
|
||||
ENDIF.
|
||||
|
||||
lv_group = to_upper( lv_group ).
|
||||
|
||||
READ TABLE lt_totals ASSIGNING FIELD-SYMBOL(<ls_total>)
|
||||
WITH TABLE KEY donor_id = lv_donor group_code = lv_group.
|
||||
IF sy-subrc <> 0.
|
||||
INSERT VALUE #( donor_id = lv_donor
|
||||
group_code = lv_group
|
||||
total_units = 0 )
|
||||
INTO TABLE lt_totals ASSIGNING <ls_total>.
|
||||
ENDIF.
|
||||
<ls_total>-total_units = <ls_total>-total_units + lv_units_int.
|
||||
ENDLOOP.
|
||||
|
||||
LOOP AT lt_totals INTO DATA(ls_total).
|
||||
IF ls_total-donor_id <> lv_prev.
|
||||
ev_donor_count = ev_donor_count + 1.
|
||||
lv_prev = ls_total-donor_id.
|
||||
ENDIF.
|
||||
|
||||
DATA(lv_out) = |{ ls_total-donor_id };{ ls_total-group_code };{ ls_total-total_units }|.
|
||||
IF lv_result IS INITIAL.
|
||||
lv_result = lv_out.
|
||||
ELSE.
|
||||
lv_result = lv_result && cl_abap_char_utilities=>newline && lv_out.
|
||||
ENDIF.
|
||||
ENDLOOP.
|
||||
|
||||
cv_totals = lv_result.
|
||||
|
||||
ENDFUNCTION.
|
||||
120
tasks_gen/train/G1113/reference/t14_test.clas.abap
Normal file
120
tasks_gen/train/G1113/reference/t14_test.clas.abap
Normal file
@@ -0,0 +1,120 @@
|
||||
CLASS {{P}}T14_TEST DEFINITION PUBLIC FINAL CREATE PUBLIC
|
||||
FOR TESTING DURATION SHORT RISK LEVEL HARMLESS.
|
||||
PRIVATE SECTION.
|
||||
METHODS line_for
|
||||
IMPORTING iv_donor TYPE string
|
||||
iv_group TYPE string
|
||||
iv_units TYPE i
|
||||
RETURNING VALUE(rv_line) TYPE string.
|
||||
|
||||
METHODS totals_for
|
||||
IMPORTING iv_donations TYPE string
|
||||
EXPORTING ev_totals TYPE string
|
||||
ev_count TYPE i.
|
||||
|
||||
METHODS groups_donations FOR TESTING.
|
||||
METHODS counts_donors FOR TESTING.
|
||||
METHODS rejects_zero_units FOR TESTING.
|
||||
METHODS empty_input FOR TESTING.
|
||||
ENDCLASS.
|
||||
|
||||
|
||||
CLASS {{P}}T14_TEST IMPLEMENTATION.
|
||||
|
||||
METHOD line_for.
|
||||
rv_line = iv_donor && ';' && iv_group && ';' && |{ iv_units }|.
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD totals_for.
|
||||
DATA lv_totals TYPE string.
|
||||
|
||||
CALL FUNCTION '{{P}}DON_SUM'
|
||||
EXPORTING
|
||||
iv_donations = iv_donations
|
||||
IMPORTING
|
||||
ev_donor_count = ev_count
|
||||
CHANGING
|
||||
cv_totals = lv_totals
|
||||
EXCEPTIONS
|
||||
invalid_units = 1
|
||||
OTHERS = 2.
|
||||
|
||||
cl_abap_unit_assert=>assert_subrc( exp = 0 ).
|
||||
ev_totals = lv_totals.
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD groups_donations.
|
||||
DATA lv_totals TYPE string.
|
||||
DATA lv_count TYPE i.
|
||||
|
||||
DATA(lv_input) = line_for( iv_donor = 'D1' iv_group = 'A+' iv_units = 2 )
|
||||
&& cl_abap_char_utilities=>newline
|
||||
&& line_for( iv_donor = 'D1' iv_group = 'a+' iv_units = 3 )
|
||||
&& cl_abap_char_utilities=>newline
|
||||
&& line_for( iv_donor = 'D2' iv_group = 'O-' iv_units = 1 ).
|
||||
|
||||
totals_for( EXPORTING iv_donations = lv_input
|
||||
IMPORTING ev_totals = lv_totals
|
||||
ev_count = lv_count ).
|
||||
|
||||
DATA(lv_expected) = line_for( iv_donor = 'D1' iv_group = 'A+' iv_units = 5 )
|
||||
&& cl_abap_char_utilities=>newline
|
||||
&& line_for( iv_donor = 'D2' iv_group = 'O-' iv_units = 1 ).
|
||||
|
||||
cl_abap_unit_assert=>assert_equals( exp = lv_expected act = lv_totals ).
|
||||
cl_abap_unit_assert=>assert_equals( exp = 2 act = lv_count ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD counts_donors.
|
||||
DATA lv_totals TYPE string.
|
||||
DATA lv_count TYPE i.
|
||||
|
||||
DATA(lv_input) = line_for( iv_donor = 'D1' iv_group = 'A+' iv_units = 1 )
|
||||
&& cl_abap_char_utilities=>newline
|
||||
&& line_for( iv_donor = 'D1' iv_group = 'O-' iv_units = 1 )
|
||||
&& cl_abap_char_utilities=>newline
|
||||
&& line_for( iv_donor = 'D2' iv_group = 'A+' iv_units = 1 ).
|
||||
|
||||
totals_for( EXPORTING iv_donations = lv_input
|
||||
IMPORTING ev_totals = lv_totals
|
||||
ev_count = lv_count ).
|
||||
|
||||
cl_abap_unit_assert=>assert_equals( exp = 2 act = lv_count ).
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = 3
|
||||
act = strlen( lv_totals ) - strlen( replace( val = lv_totals sub = cl_abap_char_utilities=>newline with = '' occ = 0 ) ) + 1 ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD rejects_zero_units.
|
||||
DATA lv_totals TYPE string.
|
||||
DATA lv_count TYPE i.
|
||||
|
||||
DATA(lv_input) = line_for( iv_donor = 'D1' iv_group = 'A+' iv_units = 0 ).
|
||||
|
||||
CALL FUNCTION '{{P}}DON_SUM'
|
||||
EXPORTING
|
||||
iv_donations = lv_input
|
||||
IMPORTING
|
||||
ev_donor_count = lv_count
|
||||
CHANGING
|
||||
cv_totals = lv_totals
|
||||
EXCEPTIONS
|
||||
invalid_units = 1
|
||||
OTHERS = 2.
|
||||
|
||||
cl_abap_unit_assert=>assert_equals( exp = 1 act = sy-subrc ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD empty_input.
|
||||
DATA lv_totals TYPE string.
|
||||
DATA lv_count TYPE i.
|
||||
|
||||
totals_for( EXPORTING iv_donations = ''
|
||||
IMPORTING ev_totals = lv_totals
|
||||
ev_count = lv_count ).
|
||||
|
||||
cl_abap_unit_assert=>assert_equals( exp = 0 act = lv_count ).
|
||||
cl_abap_unit_assert=>assert_equals( exp = '' act = lv_totals ).
|
||||
ENDMETHOD.
|
||||
|
||||
ENDCLASS.
|
||||
Reference in New Issue
Block a user