Skip to Content
Event Information
Author's profile photo Thomas Jung

SAP Community Coding Challenge Series

It is with great pleasure that we announce a new community activity – the SAP Community Coding Challenge Series. The idea is that periodically SAP will post a coding challenge or puzzle for the community to solve. It will be something fun yet hopefully will teach something as well. It should be simple enough that most people could work on it over a lunch break but if you really want to solve the problem optimally might require some thoughtful research and experimentation.

For this first challenge, we will go to our technology roots in the SAP community and open with an ABAP coding challenge.  This challenge can be performed in the SAP Cloud Platform ABAP Environment Trial (this is where our sample solution was tested).

https://developers.sap.com/tutorials/abap-environment-trial-onboarding.html

Voting is now LIVE!

To vote on the winner, simply go to this Community question. The finalist are all listed as answers.  Upvote the answer you believe is best.

https://answers.sap.com/questions/13007129/sap-community-coding-challenge-march-2020.html

 

And if you want more details on the Special Award winners, Rich Heilman posted a blog about them here: https://blogs.sap.com/2020/03/17/sap-community-coding-challenge-series-march-2020-special-awards/

Midpoint Challenge Update:

Now that we are 1/2 way through the first challenge, we did a live stream to discuss the progress, judging and how to get started coding in the SAP Cloud Platform ABAP Environment Trial.

Challenge Directions:

Start with this line of code as the first line in your application. Requirement: make sure to cut and paste this line of code and NOT type it.

    DATA(sentence) = `ABАP  is excellent `.

Using the value in the variable sentence, programmatically determine the number of words in this variable and then count the number of UNIQUE letters in each word.

Your output should look like this:

The goal is to accomplish the task with as few lines of code as possible. Our sample implementation uses 9 lines including the data(sentence) and out->write lines. Can you do it in less?

Consider processing efficiency and ABAP best practices as well.  How clean is your code? https://github.com/SAP/styleguides/blob/master/clean-abap/CleanABAP.md  Usage of Hungarian notation will be severely ridiculed. ?

Finally you must also explain why ABАP correctly has 4 unique letters.

Submission and Judging

Once you have a solution you are happy with, please email your submissions along with a link to your public SAP Community Profile to the community@sap.com inbox.  Everyone has until March 15th, 23:59 Pacific Daylight Time (PDT) / UTC-7 to submit your coding solution.  The community and developer advocates team at SAP will then do the first round of judging, narrowing down to 5 finalist that have the cleanest, most elegant, or most creative solutions. We will then post these 5 finalist in a community Q&A entry so that everyone will have a chance to vote on the best overall solution.

In the spirit of the immortal Stan Lee, the fabulous prize for the winner of the Coding Challenge will be a No-Prize. The coveted No-Prize also comes with the unending adulation and admiration of the community of fellow developers.

 

 

Assigned Tags

      69 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Mohammad Saif
      Mohammad Saif

      Hi Thomas,

      Good initiative. Really appreciate it.  And also "No-Prize" Haha

      Author's profile photo Kesari Katakam
      Kesari Katakam

      Number of Unique characters in ABAP - 4 ??

      Author's profile photo Thomas Jung
      Thomas Jung
      Blog Post Author

      Yes – that’s part of the challenge to figure why that is the case. 😉

      Author's profile photo Kesari Katakam
      Kesari Katakam

      Sure some thing is 'special' in ABAP there 😉

      Author's profile photo Robin Wagner
      Robin Wagner

      This is why you should copy the "DATA(sentence)"-line 😉

      Author's profile photo Enric Castella Gonzalez
      Enric Castella Gonzalez

      Great challenge and magical award!

      Author's profile photo Vicky Khambhati
      Vicky Khambhati

      I stopped doing ABAP 7 years ago.. but this excites me. Specially, ABAP - 4 ?

       

      I think, I should give a try and see how much developer left in me?

      Author's profile photo Promise Aphana
      Promise Aphana

      Hi Thomas,

      Does the 9 lines include blank lines for neat formatting?

       

      Author's profile photo Thomas Jung
      Thomas Jung
      Blog Post Author

      No we are only counting lines of code.

      Author's profile photo Stephan Köster
      Stephan Köster

      Nice challenge. It's possible with 8 lines of Code.

      Author's profile photo Stephan Köster
      Stephan Köster

      Clean Code? Challenge accepted. 😉

      Author's profile photo Frederic Girod
      Frederic Girod

      Clean code does not means Clean writed. It means you follow all the rules

      https://github.com/SAP/styleguides/blob/master/clean-abap/CleanABAP.md

       

      Do one thing, do it well, do it only

      you do a loooot of things

      Author's profile photo Klaus Steinbach
      Klaus Steinbach

      Dear Thomas,

      as I love automated unit testing some questions to your requirement:

      1. Do we only talk about alphabetic letters (A-Za-z) which can make up a word or also numbers (0-9) and other letters (!"§...) but not separated by any space like characters?
      2. Shall it also work for other languages as like chinese, arabic, etc.?
      3. Do we talk about case-sensitive or case-insensitive?
      4. are we allowed to use helper classes like cl_demo_output or only out->write?

      Love the challenge!

      Cheers Klaus

      Author's profile photo Thomas Jung
      Thomas Jung
      Blog Post Author

      Characters not letters

      yes

      upper/lower would be different characters

      Your choice.  We didn’t specify how to produce the output.  The calculation itself is what we are after

      Author's profile photo Klaus Steinbach
      Klaus Steinbach

      Awesome challenge. Learned something new and would say it is possible in 6 statements. The ABAP case with 4 chars nice trick 🙂 Looking forward to your judgement.

      Author's profile photo Bärbel Winkler
      Bärbel Winkler

      I love a challenge but am wondering: is it solvable (without using brute force) on a NW 750 EHP 8 system or are newer ABAP syntax options needed to get it done in as few lines of code as possible?

      Cheers

      Bärbel

      Author's profile photo Thomas Jung
      Thomas Jung
      Blog Post Author

      I’m sure it’s solvable there but might require a few more lines of code.  Of course if you want to use the newest syntax, the SAP Cloud Platform ABAP Environment Trial is an easy solution.

      Author's profile photo Uwe Fetzer
      Uwe Fetzer

      I have a running code on 7.40 SP8 with just three lines of code -> so yes, it's possible on older releases

      Author's profile photo Peter Jaeckel
      Peter Jaeckel

      One line for the assignment, one for the split and one for the rest?

      Author's profile photo Uwe Fetzer
      Uwe Fetzer

      ?

      Author's profile photo Mahesh Palavalli
      Mahesh Palavalli

      wrapping of a single line to multiple lines is still considered a single line right? <Limitation of ABAP 255 chars in a single line>

      Author's profile photo Thomas Jung
      Thomas Jung
      Blog Post Author

      Yes. It’s fine to break a single line of code over multiple lines in the editor. That improves readability which is also an important factor here.

      Author's profile photo Frederic Girod
      Frederic Girod

      I am not sure to be able to do it better. And I have a CHECK … a LOOP …

      but, it is dynamic, unreadable, … perfect developer code 🙂

       

       

      And there is no hungarian notation ( for Matthew Billingham )

       

      Author's profile photo Bärbel Winkler
      Bärbel Winkler

      Not to be a spoilsport, Fred - but shouldn't "Number of words" be 3? ?

      Author's profile photo Frederic Girod
      Frederic Girod

      A yes ... need to move my check 🙂

      Author's profile photo Frederic Girod
      Frederic Girod

      After the good remark of Bärbel Winkler, I have to change the code, I replace a line by another line. So  same score

       

       

       

      Author's profile photo Florian Henninger
      Florian Henninger

      MAybe there are other reasons 🙂

      Author's profile photo Robin Wagner
      Robin Wagner

      6 lines of code, beat me! 😀

      Author's profile photo Jacek Wozniczak
      Jacek Wozniczak

      As Thomas said on Twitter "Until you hit the period it’s one line of code even if spread across multiple lines in the editor" I got 4 lines of super modern, dirty, expression-style nightmare code I made just as an exercise 😀 [as opposite to my elegant solution].

      Author's profile photo Sreenivasan Ravi
      Sreenivasan Ravi

      If that the case, I got 3 lines of code. I don't know how my code works 🙂

      Author's profile photo jonathan albert
      jonathan albert

      Hi Thomas,

      I have a doubt, in the challenge they say nothing about the use of classes that attend to solve the problem, but if I use a method that has 20 lines of code that solve the problem, 1 line of code is counted by the method call or 20 lines of code for the implementation of the method?

       

      Author's profile photo Thomas Jung
      Thomas Jung
      Blog Post Author

      Well did you write the class or your reusing something supplied by the platform? But in the end I hope people aren't getting too tied up in the lines of code. I called that out mostly to challenge people to think of more inventive ways of solving the problem than just brute force approaches. Hopefully this gives you an opportunity to research some syntax that is new to you.  Elegance, readability, maintainability are all factors as well. We will only use these criteria to narrow down to the finalist. Ultimately the community will vote on the winner.

      Author's profile photo Frederic Girod
      Frederic Girod

      I think the sentence

      The goal is to accomplish the task with as few lines of code as possible

      make the chalenge based on the number of lines.

       

      Maybe next time, we could have "Clean Code Challenge".

      Author's profile photo Thomas Jung
      Thomas Jung
      Blog Post Author

      The next paragraph does add to that:

      Consider processing efficiency and ABAP best practices as well.  How clean is your code? https://github.com/SAP/styleguides/blob/master/clean-abap/CleanABAP.md  Usage of Hungarian notation will be severely ridiculed.

      But point taken that people have taken to the lines of code aspect a bit more than I had personally intended. I was hoping that aspect would push people to explore some new ABAP syntax (which seeing the submissions it certainly has).  Its one of several interesting aspects for people I hope. But we will be sure to balance or rotate the challenge modifiers in the future.

      Author's profile photo Thomas Jung
      Thomas Jung
      Blog Post Author

      I'll add that I've been really impressed by how many unique and creative approaches there have been in the submissions. I think the community is going to have lots of fun looking at them once the submission period is over.

      Author's profile photo Frederic Girod
      Frederic Girod

      Done !

      It was easier in Clean Code / more readable

      So if I don't count the class/method/.. lines, I am now with 7 lines.

      It is interesting to see, with the same number of lines, but with Class & methods, the code is simpler.

       

      Author's profile photo Frederic Girod
      Frederic Girod

      Do you think I could propose a Clean-Code answer if I have already send my proposal ?

       

      Author's profile photo Thomas Jung
      Thomas Jung
      Blog Post Author

      Sure there was no rule against multiple submissions. Others have done so.

      Author's profile photo Mahesh Palavalli
      Mahesh Palavalli

      I've sent like 4 mails till now 😀  (Continuously every day !!)

      Author's profile photo Mahesh Palavalli
      Mahesh Palavalli

      Exactly, if there was no mention of no of lines, I sure would have not taken it as a challenge 😉

      Just because of that criteria, I tried different ways and used many new functions and syntax. It sure was a fun learning experience for me 😀 ,

      one of my colleague saw me doing this today and she started to learn about constructor expressions, reduce, for,  e.t.c, to do this challenge.

      With normal abap, surely we cannot do it in less lines, we have to use some new features to do this. maybe 2 categories would be nice, one with Clean ABAP and one with less lines. So people can push both ways.

       

      Author's profile photo jonathan albert
      jonathan albert
      Yes i think that the idea of ​​Master Thomas is that we look 
      for new ways to do it, not only using the old sentences 
      that we are used to using. I personally have had to 
      investigate many sentences that I did not know and 
      I think that is the objective of the challenge, 
      to challenge the knowledge we already have in order 
      to be better. Thanks to Master Thomas and team.
      Author's profile photo Sandra Rossi
      Sandra Rossi

      I did it with 2 lines of code including the initial line, but one line for everything is just madness, especially the logic to count unique characters… The choice of auxiliary variable names is essential to make the code understandable. And if you do it “right” by using translatable texts, that’s even more madness ?

      Author's profile photo Frederic Girod
      Frederic Girod

      we need to discuss !  😉

      How could you split without split ... or how could you split & use directly the result .. ??

      Author's profile photo Mahesh Palavalli
      Mahesh Palavalli

      ? I wouldn’t say it is madness,  It’s more like an art :P, I’ve sent the code both with 1 line(included data declaration inside) and 2 lines with separate data declaration.

      but have you done that using regex?

      Author's profile photo Sandra Rossi
      Sandra Rossi

      Frederic Girod Mahesh Kumar Palavalli The answer is simple: what solution other than SPLIT can you use? Of course, the issue with doing it in one statement is that fellow developers who will maintain the code need to master many aspects of the ABAP language, including the regular expressions which are widely adopted in many languages, understand very well the constructor expressions. It's often not the case, so my code fits "shortest" but doesn't fit "Elegance, readability, maintainability".

      Author's profile photo Frederic Girod
      Frederic Girod

      I have tried REGEX ... but ...

      Bravo !!

      Author's profile photo Frederic Girod
      Frederic Girod

      I cannot see how it could be Clean-Coded. We do several actions in the same set of code lines.

       

      Author's profile photo Mahesh Palavalli
      Mahesh Palavalli

      it's not clean for sure unless we format it into multiple lines, but without the .(dot).. then it is still a single line 😛

      Author's profile photo Sai Kowluri
      Sai Kowluri

      Less lines of code is often confused with clean code. To me, clean code should be easy to read and easy to troubleshoot by anyone other than the author.

      Author's profile photo Frederic Girod
      Frederic Girod

      I am not agree, Clean Code is easy to maintain, it is the main objective. Check the SOLID principle.

      We create more class/interfaces with sometimes design pattern. It is not easy to read.

      Author's profile photo Oleg Bashkatov
      Oleg Bashkatov

      Could I exclude the line

      DATA(sentence) = `ABАP  is excellent `.

      but pass the string as a parameter to out ?

      Author's profile photo Thomas Jung
      Thomas Jung
      Blog Post Author

      Yes as long as you start with that string literal and be sure to still cut and paste to pickup some of the "special" conditions we created in that string.

      Author's profile photo Oleg Bashkatov
      Oleg Bashkatov

      Thanks! it will save one line and increase readability.

      Author's profile photo Enno Wulff
      Enno Wulff

      One "line" of code.

      cl_demo_output=>display
                        REDUC
                        LET s
                            s
                            n
                        INIT 
                        FOR w
                        IN RE
                        INIT 
                        FOR i
                        THEN 
                        UNTIL
                        NEXT 
                        NEXT 
                        BASE 
                        lines
                        REDUC
                        INIT 
                        FOR p
                        NEXT 
                        WHEN 
                        THEN 
                        ELSE 

      now working a bit on clean code... 😀

      Author's profile photo Peter Jaeckel
      Peter Jaeckel

      thanks for the discussion, I have it now in one line as well. I thought it might be overkill to replace the split by reduce expression. but its okay. We should compare results next time.

      Author's profile photo Enno Wulff
      Enno Wulff

      better late than never.

      this is my result

          result = REDUCE string_table( "STRING_TABLE is the output...
                   LET
                     "clean the text from spaces
                     text_wo_double_spaces = replace( regex = '(\s)(?=\1)' val = text  with = `` occ = 0 )
                     "delete trailing and leading blanks
                     cleaned_text = shift_left( val = shift_right( val = text_wo_double_spaces ) )
                     "number of words is the number of blanks + 1
                     num_words = count_any_of( val = cleaned_text sub = ` ` ) + 1 IN
                   INIT
                     "fill first result line with "number of words"
                     lines = VALUE #( ( |Number of words: { num_words }| ) )
                   " derive words via SEGMENT of cleaned string
                   FOR word
                   IN REDUCE string_table(
                   INIT w = VALUE #( )
                   "do read the words and store in table (equals SPLIT INTO TABLE)
                   FOR i = 1 THEN i + 1 UNTIL i > num_words
                   NEXT w = VALUE #( BASE w ( segment( val = cleaned_text index = i ) ) ) )
                   NEXT lines = VALUE #(
                     "transfer word-string to result table (BASE for keeping existing lines)
                     BASE lines ( |Number of unique characters in the word: { word } - {
                          "now calculate number of distinct characters in each word
                          lines( REDUCE string_table(
                            "prepare the character table
                            INIT chars = VALUE string_table( )
                            "do aas many times as the word has characters; pos is the offset for current character
                            FOR pos = 0 THEN pos + 1 WHILE pos < strlen( word )
                            NEXT chars = COND #(
                              "if current character does not exist in character table,...
                              WHEN NOT line_exists( chars[ table_line = word+pos(1) ] )
                              "...then insert current character
                              THEN VALUE #( BASE chars ( word+pos(1) ) )
                              " otherwise keep current character table as it is
                              ELSE chars ) ) ) }| ) )  ) .
      Author's profile photo Jörg Wulf
      Jörg Wulf

      I have to admit, that one poked at my ambitions. So here's my version of a one-liner:

      cl_demo_output=>new(                                              "make use of output class
        )->write( REDUCE string( LET sentence = `ABАP  is excellent `   "Why waste a data statement?
                                     rgx_word = '\w+'                   "regular expression matches word
                                     rgx_unique_char = '(.)(?!.*\1)'    "regular expression matches unique 
                                                                        "characters
                                     mx      = count( val   = sentence 
                                                      regex = rgx_word )      "count the words with regexp
                                     content = VALUE string_table( FOR c = 1  "for the table
                                                                   THEN c + 1 "initialization
                                                                   UNTIL c GT mx               
                                              ( match( val   = sentence       "in order to get them separated
                                                       regex = rgx_word 
                                                       occ   = c ) ) ) IN 
                                 INIT text = |Number of words: { mx }|
                                   FOR i = 1 THEN i + 1 UNTIL i GT mx       "loop the words
                                   NEXT text = text && |\rNumber of unique characters in the word: { 
                                                        content[ i ] } - {  "every word-output starts 
                                                                            "with new-line
                                   count( val = content[ i ] regex = rgx_unique_char )  "counting unique 
                                                                                        "Characters with regexp
                                         }| ) )->display( ).                "finally producing the output
      Author's profile photo Enno Wulff
      Enno Wulff

      Wow! That one looks really neat and elegant Jörg Wulf ! 👍

      Author's profile photo Stephan Heinberg
      Stephan Heinberg

      Thanks, Thomas for the challenge. Great, I didn’t know, you still do ABAP.
      There is no plain text ;->

      Author's profile photo Pierre COL
      Pierre COL

      Having created the SAP Intelligent RPA Tutorials Challenge which ended Feb 29th, I totally support this very good initiative and I wish I knew ABAP programming to participate!

      Kudos Thomas Jung!

      Author's profile photo Thomas Jung
      Thomas Jung
      Blog Post Author

      Its never too late to start learning ABAP 🙂

      Author's profile photo Pierre COL
      Pierre COL

      You are definitely right: I started programming computers in 1978, being 15yo, and it was Z80 assembler, so, indeed, why not ABAP?  ?

      Author's profile photo Sai Kowluri
      Sai Kowluri

      Thanks for this Thomas.

      Author's profile photo Vishnu Gudi
      Vishnu Gudi

      I did it with 13 lines of code with basic abap, let me cutdown number of lines.

      Author's profile photo James Bungay
      James Bungay

      Hi guys,

      Below is my simplified version for anyone interested.  A little Reduce and some regex.

      Neat challenge, can't wait for the next one.

      Cheers,

      James

      CLASS ycl_sap_cc_20200228_dirty DEFINITION
        PUBLIC
        FINAL
        CREATE PUBLIC .
      
      PUBLIC SECTION.
          interfaces if_oo_adt_classrun.
      PROTECTED SECTION.
      PRIVATE SECTION.
      ENDCLASS.
      
      CLASS ycl_sap_cc_20200228_dirty IMPLEMENTATION.
        METHOD if_oo_adt_classrun~main.
          DATA(sentence) = `ABАP  is excellent `.
      
          data(number_of_words) = count( val = condense( sentence ) regex = `(\b[^\s]+\b)` ).
      
          out->write( reduce stringtab(
                      init output = value #( ( |Number of words: { conv string( number_of_words ) }| ) )
                           word type string
                           number_of_unique_characters type i
                      for index = 1 until index > number_of_words
                      next word = segment( val = sentence index = index space = ` ` )
                           number_of_unique_characters = count( val = word regex = `(\S)(?!.*\1.*)` )
                           output = value #( BASE output ( |Number of unique characters in the word: { word } - { conv string( number_of_unique_characters ) }| ) ) ) ).
        ENDMETHOD.
      ENDCLASS.
      

       

       

       

      Author's profile photo Guilherme Gaino Galindo
      Guilherme Gaino Galindo

      I’ve made a simple solution, using two REDUCE operations, no more than that. I believe most people went for that, or using LOOP, FOR, DO, etc., the result is the same. I thought about doing sorted tables, select, char/number calculation, or even Regex. But favored the REDUCE one. Nice to see different approaches for the same problem.

      Author's profile photo Tomas Buryanek
      Tomas Buryanek

      Hi,

      I just found this challenge blog. Love the announcement video stream where you two comment on the submitted code! Funny reactions and comments on regex etc. Relatable 🙂

      I will try get up to date with blogs and hopefully join some of the upcoming challenges.

      Author's profile photo Angel Pachon Solana
      Angel Pachon Solana

      Great challenge!

      Author's profile photo Andrei Touzik
      Andrei Touzik

      One more solution

        METHOD if_oo_adt_classrun~main.
          DATA(sentence) = `ABАP  is excellent `.
          SPLIT condense( sentence ) AT space INTO TABLE DATA(words).
          out->write( |Number of words: { lines( words ) }\n| && REDUCE #( INIT s = `` FOR <word> IN words NEXT s = s &&
          |Number of unique characters in the word:  { <word> } - { REDUCE #( INIT c = 0 r = <word> FOR i = 0 WHILE i < strlen( <word> )
          NEXT c = c + COND #( WHEN count( val = r regex = <word>+i(1) ) > 0 THEN 1 ) r = replace( val = r sub = <word>+i(1) with = `` occ = 0 ) ) }\n| ) ).
        ENDMETHOD.