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

Word Count #244

Open
mickrig opened this issue Oct 28, 2022 · 3 comments
Open

Word Count #244

mickrig opened this issue Oct 28, 2022 · 3 comments
Assignees

Comments

@mickrig
Copy link

mickrig commented Oct 28, 2022

Hi guys,
I have an issue with exercise word count. It works in ABAP but not on Exercism.
It looks like that the plattform is not able to work with adding 1 to the reference which is marked red.
All tests with a word count greater than 1 fail.
Please have a look on the screenshot.
Thanks in advance.
Michael
image

@larshp
Copy link
Member

larshp commented Oct 28, 2022

@mickrig
Copy link
Author

mickrig commented Oct 29, 2022

@larshp Yes, sure, please find the code below

CLASS zcl_word_count DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.
    TYPES:
      BEGIN OF return_structure,
        word  TYPE string,
        count TYPE i,
      END OF return_structure,
      return_table TYPE STANDARD TABLE OF return_structure WITH KEY word.
    METHODS count_words
      IMPORTING
        !phrase       TYPE string
      RETURNING
        VALUE(result) TYPE return_table .
  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.


CLASS zcl_word_count IMPLEMENTATION.

  METHOD count_words.
    DATA(string) = replace( val = to_lower( phrase ) sub = `'` with = `` occ = 0 ).
    string = replace( val = string sub = `\t` with = ` ` occ = 0 ).
    string = replace( val = string sub = `\n` with = ` ` occ = 0 ).
    string = condense( replace( val = string regex = `[^a-z0-9]` with = ` ` occ = 0 ) ).

    DO.
      TRY.
          DATA(substring) = segment( val = string
                            index = sy-index
                            sep = ` ` ).
          TRY.
              DATA(single_count) = REF #( result[ word = substring ]-count ).
              single_count->* += 1.
            CATCH cx_sy_itab_line_not_found.
              APPEND VALUE #( word = substring  count = 1 ) TO result.
          ENDTRY.

        CATCH cx_sy_strg_par_val.
          EXIT.
      ENDTRY.
    ENDDO.

  ENDMETHOD.
ENDCLASS.

@larshp
Copy link
Member

larshp commented Oct 29, 2022

downport problem, the reference is lost,

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

3 participants