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

Book store - if statement executing wrong code block in loop #315

Open
thadien665 opened this issue Jun 26, 2024 · 1 comment
Open

Book store - if statement executing wrong code block in loop #315

thadien665 opened this issue Jun 26, 2024 · 1 comment

Comments

@thadien665
Copy link

thadien665 commented Jun 26, 2024

Hello,

I'm facing error that I don't understand (in Eclipse - all tests OK).

My approach to this exercise is to process the same input with 3 different algoritms. Problem is caused by 3rd one (I've marked it with some comments) - one step is executed wrongly on Exercism (in Eclipse it is working correctly). I noticed that after checking this loop with console.log help.

CLASS zcl_book_store DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .
  PUBLIC SECTION.
    "! ID of book to buy from 1 to 5
    TYPES book_id TYPE i.
    TYPES basket_type TYPE SORTED TABLE OF book_id
      WITH NON-UNIQUE KEY table_line.
    TYPES total TYPE p LENGTH 3 DECIMALS 2.
    METHODS calculate_total
      IMPORTING basket       TYPE basket_type
      RETURNING VALUE(total) TYPE total.
  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.
CLASS zcl_book_store IMPLEMENTATION.
  METHOD calculate_total.
    " add solution here
  data summ type p LENGTH 3 DECIMALS 2.
  types summ type p LENGTH 3 DECIMALS 2.
  data summ_table type table of summ.
    data temp_table type table of string.
    data temp type string.
    data ind type i.
    data max_rows type table of i.
    append 11 to max_rows.
    append 8 to max_rows.
    data: quantity_1 type i,
          quantity_5 type i.
    loop at basket ASSIGNING FIELD-SYMBOL(<c>).
        case <c>.
            when 1.
                quantity_1 = quantity_1 + 1.
            when 5.
                quantity_5 = quantity_5 + 1.
        ENDCASE.
    endloop.
    data basket_p type table of book_id.
    basket_p = basket.
    if quantity_5 > quantity_1.
        sort basket_p DESCENDING.
    endif.
    loop at max_rows ASSIGNING FIELD-SYMBOL(<max_stack>).
      loop at basket_p ASSIGNING FIELD-SYMBOL(<a>).
        temp = <a>.
        if lines( temp_table ) = 0.
            append temp to temp_table.
            CONTINUE.
        else.
            loop at temp_table ASSIGNING FIELD-SYMBOL(<existing_row>).
                ind = sy-tabix.
                    if strlen( <existing_row> ) < <max_stack>.
                        if <existing_row> CS temp.
                            if ind = lines( temp_table ).
                                append temp to temp_table.
                                exit.
                            else.
                                CONTINUE.
                            endif.
                        else.
                            temp_table[ ind ] = temp_table[ ind ] && temp.
                            exit.
                        endif.
                     else.
                         if ind = lines( temp_table ).
                            append temp to temp_table.
                            exit.
                         else.
                            CONTINUE.
                         endif.
                     endif.
             ENDLOOP.
        endif.
      ENDLOOP.
      loop at temp_table ASSIGNING FIELD-SYMBOL(<b>).
        case strlen( <b> ).
            when 10.
                summ = summ + '30'.
            when 8.
                summ = summ + '25.6'.
            when 6.
                summ = summ + '21.6'.
            when 4.
                summ = summ + '15.2'.
            when 2.
                summ = summ + '8'.
        ENDCASE.
      endloop.
      append summ to summ_table.
      clear summ.
      clear temp_table.
  endloop.

" this loop is not working as it should

  loop at basket_p ASSIGNING <a>.
        temp = <a>.
        if lines( temp_table ) = 0.
            append temp to temp_table.
            CONTINUE.
        else.
            loop at temp_table ASSIGNING <existing_row>.
                ind = sy-tabix.
                    if temp NE basket_p[ 1 ]. " this step is executing block below it when temp = 1 and test = 1, when it should skip to else statement
                      if  ind NE lines( temp_table ).
                           if strlen( <existing_row> ) >= strlen( temp_table[ ind + 1 ] ).
                                CONTINUE.
                           else.
                               if <existing_row> CS temp.
                                  CONTINUE.
                               else.
                                  temp_table[ ind ] = temp_table[ ind ] && temp.
                                  exit.
                               endif.
                           endif.
                      else.
                          temp_table[ ind ] = temp_table[ ind ] && temp.
                          exit.
                       endif.
                    else.
                       append temp to temp_table. " this step should create a 2nd row with 1
                       exit.
                    endif.
             ENDLOOP.
        endif.
      ENDLOOP.
      loop at temp_table ASSIGNING <b>.
        case strlen( <b> ).
            when 10.
                summ = summ + '30'.
            when 8.
                summ = summ + '25.6'.
            when 6.
                summ = summ + '21.6'.
            when 4.
                summ = summ + '15.2'.
            when 2.
                summ = summ + '8'.
        ENDCASE.
      endloop.
      append summ to summ_table.
  sort summ_table ascending.
  total = summ_table[ 2 ]. " this index should be 1 instead of 2 - value 2 is set up to pass rest of tests - to leave only test 9 failing
  ENDMETHOD.
ENDCLASS.
@larshp
Copy link
Member

larshp commented Jun 26, 2024

thanks, I'll have a look

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