A curious case of Chat GPT and ABAP
source link: https://blogs.sap.com/2023/01/07/a-curious-case-of-chat-gpt-and-abap/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Technical Articles
A curious case of Chat GPT and ABAP
Introduction
I recently came across an interesting and excellent blog series written by Kallol Chakraborty , https://blogs.sap.com/2022/12/20/easy-way-to-write-algorithms-in-abap-series-01/ wherein he is discussing about writing algorithms in ABAP.
Now that Chat GPT is out, we have already seen a lot of use cases where many developers have used Chat GPT to generate code from one language to another. I thought, why not use Chat GPT and see how efficiently it could convert a piece of code to ABAP.
The Prompt
I logged in to leetcode and searched for an algorithm to test this out.
I took the example of grouping anagrams as follows and then, gave the following, as input to Chat GPT for converting it to ABAP
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
if (strs.length == 0) return new ArrayList();
Map<String, List> ans = new HashMap<String, List>();
int[] count = new int[26];
for (String s : strs) {
Arrays.fill(count, 0);
for (char c : s.toCharArray()) count[c - 'a']++;
StringBuilder sb = new StringBuilder("");
for (int i = 0; i < 26; i++) {
sb.append('#');
sb.append(count[i]);
}
String key = sb.toString();
if (!ans.containsKey(key)) ans.put(key, new ArrayList());
ans.get(key).add(s);
}
return new ArrayList(ans.values());
}
}
The following is the output in ABAP, that I got as a response from Chat GPT.
You can find the video of the prompt I gave, given below.
Now, the generated output is not that far from being accurate with some minor syntax errors which can be fixed by a skilled developer. But what surprised me is the fact that it was able to identify similar syntaxes in ABAP write that down.
Conclusion
This opens a huge avenue of opportunities where code can be swapped across multiple languages. The possibilities are endless, and I am excited for what the future holds for the ABAP and the developer community as a whole.
This is the code from the screenshot above, so that ABAP developers can check by themselves:
CLASS solution DEFINITION.
PUBLIC SECTION.
METHODS: group_anagrams IMPORTING strs TYPE string_table
RETURNING VALUE(rv_anagrams) TYPE t_t_string_table.
ENDCLASS.
CLASS solution IMPLEMENTATION.
METHOD group_anagrams.
DATA: ans TYPE t_t_string_table,
ca TYPE string,
key TYPE string.
IF lines( strs ) = 0.
rv_anagrams = VALUE #( ).
RETURN.
ENDIF.
CREATE DATA ans TYPE HASHED TABLE OF t_string_table
WITH UNIQUE KEY string.
LOOP AT strs INTO ca.
SORT ca BY character.
key = ca.
IF NOT ans IS INITIAL.
INSERT VALUE #( ca ) INTO TABLE ans-values INDEX key.
ELSE.
INSERT VALUE #( ( ca ) ) INTO TABLE ans INDEX key.
ENDIF.
ENDLOOP.
rv_anagrams = ans-values.
ENDMETHOD.
ENDCLASS.
That's the test class:
CLASS ltc_main DEFINITION FOR TESTING DURATION SHORT RISK LEVEL HARMLESS.
PRIVATE SECTION.
METHODS test FOR TESTING.
ENDCLASS.
CLASS ltc_main IMPLEMENTATION.
METHOD test.
DATA(words) = VALUE string_table(
( `are` ) ( `bat` ) ( `ear` ) ( `code` ) ( `tab` ) ( `era` ) ).
DATA(answer) = NEW solution( )->group_anagrams( words ).
cl_abap_unit_assert=>assert_equals( act = answer exp = VALUE solution=>t_t_string_table(
( VALUE #( ( `are` ) ( `ear` ) ( `era` ) ) )
( VALUE #( ( `bat` ) ( `tab` ) ) )
( VALUE #( ( `code` ) ) ) ) ).
ENDMETHOD.
ENDCLASS.
- Like 1
Below what I would expect from a developer who would convert the java algorithm line by line. That's a code which works, each line is more or less equivalent to the java code:
CLASS solution DEFINITION.
PUBLIC SECTION.
TYPES: t_t_string_table TYPE STANDARD TABLE OF string_table WITH DEFAULT KEY.
METHODS: group_anagrams IMPORTING strs TYPE string_table
RETURNING VALUE(rv_anagrams) TYPE t_t_string_table.
ENDCLASS.
CLASS solution IMPLEMENTATION.
METHOD group_anagrams.
TYPES: ty_count TYPE STANDARD TABLE OF i WITH EMPTY KEY,
BEGIN OF ty_ans_line,
mapkey TYPE string,
mapvalue TYPE string_table,
END OF ty_ans_line,
ty_ans TYPE HASHED TABLE OF ty_ans_line WITH UNIQUE KEY mapkey.
IF lines( strs ) = 0. rv_anagrams = VALUE #( ). RETURN. ENDIF.
DATA(ans) = VALUE ty_ans( ).
DATA(count) = VALUE ty_count( FOR x = 0 WHILE x < 26 ( ) ).
LOOP AT strs INTO DATA(s).
DO lines( count ) TIMES. count[ sy-index ] = 0. ENDDO.
LOOP AT VALUE string_table( FOR off = 0 WHILE off < strlen( s ) ( substring( val = s off = off len = 1 ) ) ) INTO DATA(c).
DATA(index) = cl_abap_conv_out_ce=>uccpi( c ) - cl_abap_conv_out_ce=>uccpi( 'a' ) + 1.
count[ index ] = count[ index ] + 1.
ENDLOOP.
DATA(sb) = VALUE string_table( ).
DO 26 TIMES.
APPEND `#` TO sb.
APPEND |{ count[ sy-index ] }| TO sb.
ENDDO.
DATA(key) = concat_lines_of( table = sb ).
IF NOT line_exists( ans[ mapkey = key ] ). INSERT VALUE #( mapkey = key ) INTO TABLE ans. ENDIF.
ASSIGN ans[ mapkey = key ]-mapvalue TO FIELD-SYMBOL(<mapvalue>).
INSERT s INTO TABLE <mapvalue>.
ENDLOOP.
rv_anagrams = VALUE #( FOR <ans_line> IN ans ( <ans_line>-mapvalue ) ).
ENDMETHOD.
ENDCLASS.
- Like 1
Hi Sandra,
Thank you for this. The human touch is clearly evident here.
Best Regards,
Prasanth
- Like 1
Thanks a lot Prasanth Padmanabhan Menon for the encouragement and Discussion.
- Like 1
I think you're a little too bit optimistic when you say "the generated output is not that far from being accurate with some minor syntax errors which can be fixed by a skilled developer."
Here is the version of ChatGPT corrected by me with the fewest changes possible:
CLASS solution DEFINITION.
PUBLIC SECTION.
TYPES: t_t_string_table TYPE STANDARD TABLE OF string_table WITH DEFAULT KEY.
METHODS: group_anagrams IMPORTING strs TYPE string_table
RETURNING VALUE(rv_anagrams) TYPE t_t_string_table.
ENDCLASS.
CLASS solution IMPLEMENTATION.
METHOD group_anagrams.
TYPES: BEGIN OF t_string_table,
string TYPE string,
mapvalue TYPE string_table,
END OF t_string_table,
t_string_table_2 TYPE HASHED TABLE OF t_string_table WITH UNIQUE KEY string.
DATA: ans TYPE REF TO t_string_table_2,
key TYPE string.
IF lines( strs ) = 0.
rv_anagrams = VALUE #( ).
RETURN.
ENDIF.
CREATE DATA ans TYPE HASHED TABLE OF t_string_table
WITH UNIQUE KEY string.
LOOP AT strs INTO DATA(s).
DATA(ca) = VALUE string_table( FOR off = 0 WHILE off < strlen( s ) ( substring( val = s off = off len = 1 ) ) ).
SORT ca BY table_line.
key = concat_lines_of( table = ca ).
IF NOT line_exists( ans->*[ string = key ] ).
INSERT VALUE #( string = key ) INTO TABLE ans->*.
ENDIF.
ASSIGN ans->*[ string = key ]-mapvalue TO FIELD-SYMBOL(<mapvalue>).
INSERT s INTO TABLE <mapvalue>.
ENDLOOP.
rv_anagrams = VALUE #( FOR <ans_line> IN ans->* ( <ans_line>-mapvalue ) ).
ENDMETHOD.
ENDCLASS.
What the differences are:
To do these fixes, I had to understand the algorithm (from the java code for instance). It was impossible for me to FIRST understand the crazy code generated by ChatGPT.
After that, I had to understand which parts of ChatGPT code correspond to which part of the algorithm, and I could find the minimal changes.
Conclusion: it took me 3x more time to find how to fix the program with minimal changes rather than writing the algorithm from scratch.
But I guess ChatGPT could be trained with ABAP code equivalent to java code and maybe that would result in more reusable code in the future...
- Like 1
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK