Advertisement
depth1

Scrabble

Oct 1st, 2024
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ABAP 0.98 KB | None | 0 0
  1. CLASS zcl_scrabble_score DEFINITION PUBLIC .
  2.  
  3.   PUBLIC SECTION.
  4.     METHODS score
  5.       IMPORTING
  6.         input         TYPE string OPTIONAL
  7.       RETURNING
  8.         VALUE(result) TYPE i.
  9.  
  10.     METHODS letter_score
  11.       IMPORTING
  12.         input         TYPE c
  13.       RETURNING
  14.         VALUE(result) TYPE i.
  15.   PROTECTED SECTION.
  16.   PRIVATE SECTION.
  17.      
  18. ENDCLASS.
  19.  
  20.  
  21. CLASS zcl_scrabble_score IMPLEMENTATION.
  22.   METHOD score.
  23.     " add solution here
  24.     DATA(i) = strlen( input ).
  25.     WHILE i >= 0.
  26.       result += letter_score( input+i(1) ).
  27.       i -= 1.
  28.     ENDWHILE.
  29.   ENDMETHOD.
  30.  
  31.  METHOD letter_score.
  32.   DATA(input_upper) = to_upper( input ).
  33.   result = COND #(
  34.     WHEN input_upper CA 'AEIOULNRST' THEN 1
  35.     WHEN input_upper CA 'DG' THEN 2
  36.     WHEN input_upper CA 'BCMP' THEN 3
  37.     WHEN input_upper CA 'FHVWY' THEN 4
  38.     WHEN input_upper CA 'K' THEN 5
  39.     WHEN input_upper CA 'JX' THEN 8
  40.     WHEN input_upper CA 'QZ' THEN 10
  41.     ELSE 0 ).
  42.  ENDMETHOD.
  43. ENDCLASS.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement