Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aggregation exercise: Test failure in exercism but works fine in ABAP editor #214

Open
ankitagarwal02 opened this issue Oct 4, 2022 · 0 comments

Comments

@ankitagarwal02
Copy link

ankitagarwal02 commented Oct 4, 2022

Exercise code -

CLASS zcl_itab_aggregation DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.
    TYPES group TYPE c LENGTH 1.
    TYPES: BEGIN OF initial_numbers_type,
             group  TYPE group,
             number TYPE i,
           END OF initial_numbers_type,
           initial_numbers TYPE STANDARD TABLE OF initial_numbers_type WITH EMPTY KEY.

    TYPES: BEGIN OF aggregated_data_type,
             group   TYPE group,
             count   TYPE i,
             sum     TYPE i,
             min     TYPE i,
             max     TYPE i,
             average TYPE f,
           END OF aggregated_data_type,
           aggregated_data TYPE STANDARD TABLE OF aggregated_data_type WITH EMPTY KEY.

    METHODS perform_aggregation
      IMPORTING
        initial_numbers        TYPE initial_numbers
      RETURNING
        VALUE(aggregated_data) TYPE aggregated_data.
  PROTECTED SECTION.
  PRIVATE SECTION.

ENDCLASS.

CLASS zcl_itab_aggregation IMPLEMENTATION.

  METHOD perform_aggregation.
    DATA: lv_group_average TYPE f.

    LOOP AT initial_numbers ASSIGNING FIELD-SYMBOL(<lfs_init_number>)
                                      GROUP BY ( key = <lfs_init_number>-group
                                                 count = GROUP SIZE )
                                      ASSIGNING FIELD-SYMBOL(<lfs_group>).

      DATA(lv_group_sum) = REDUCE i( INIT lv_sum TYPE i
                                    FOR ls_group_sum IN GROUP <lfs_group>
                                    NEXT lv_sum += ls_group_sum-number ).

      DATA(lv_group_min) = REDUCE i( INIT lv_min = '99999'
                                     FOR ls_group_min IN GROUP <lfs_group>
                                     NEXT lv_min = nmin( val1 = lv_min
                                                                        val2 = ls_group_min-number ) ).

      DATA(lv_group_max) = REDUCE i( INIT lv_max TYPE i
                                     FOR ls_group_max IN GROUP <lfs_group>
                                     NEXT lv_max = nmax( val1 = lv_max
                                                                         val2 = ls_group_max-number ) ).

      APPEND VALUE #( group = <lfs_group>-key
                                  count = <lfs_group>-count
                                  sum = lv_group_sum 
                                  min = lv_group_min
                                  max = lv_group_max
                                  average = ( lv_group_sum / <lfs_group>-count ) ) TO aggregated_data.
    ENDLOOP.

  ENDMETHOD.

ENDCLASS.

Not sure what is wrong here. Things look fine to me.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants