Coderbyte Data Structures and Algorithms Easy Python Uncategorized

[Coderbyte] – Array Addition I [Easy]

I want to start this post by mentioning that I have moved from LeetCode to Coderbyte for a while. The reason for doing so is because Coderbyte offers a discount on the premium plan for students. The premium membership offers access to all the coding challenges (200+), and it offers step-by-step solutions as well. Also, LeetCode feels too advanced for my knowledge at the moment. Thus, the plan is to complete Coderbyte and afterwards to move to LeetCode again.

I am taking the challenges in the same order they are displayed on Coderbyte. Thus, the first challenge explained is the “First Reverse” problem.

Have the function FirstReverse(str) take the str parameter being passed and return the string in reversed order. For example: if the input string is “Hello World and Coders” then your program should return the string sredoC dna dlroW olleH.

Sample Test Cases:
Input: “coderbyte”
Output: “etybredoc”

Input: “I Love Code”
Output: “edoC evoL I”

The solution I have submitted is very concise, but there is a second solution which is more beginner-friendly.

What is my logic for this challenge? First of all, I am splitting the string into a list of words. By splitting the string in a list of words, then each word can be reversed individually. Also, the order of the words in the list is in reverse order. This is exactly what this line does:

    wordsList = str.split(" ")[::-1]

It takes the string passed as an argument, it splits it by the white space and then it stores the words in reverse order. Thus, if the string is “I love solving coding challenges.”, the wordsList variable is assigned the list [‘challenges.’, ‘coding’, ‘solving’, ‘love’, ‘I’].

The next step is to reverse each word from the wordsList and then to join the reversed words. That forms the new reversed string. This part of the return statement

word[::-1] for word in wordsList

iterates over the list of words, it takes each word from the list, and it reverses it. For instance, ‘coding’ becomes ‘gnidoc’. The last step left is to join the reversed words preserving the white space. That is done by the built-in join method – ' '.join. Thus, putting it together, we get the last line ' '.join(word[::-1] for word in wordsList).

The solution put together is below:

def FirstReverse(str): 
    # split the String in a list of words
    # reverse the order of words
    # e.g. the last word becomes the first word
    wordsList = str.split(" ")[::-1]

    # for each word in the wordsList, take the word and reverse it
    # once the word is reversed, join it to the String preserving the white spaces
    return ' '.join(word[::-1] for word in wordsList)

However, as I have said, below is a more beginner friendly solution. It is also self-explanatory what I have done. In this solution, the second part is changed completely. Now there is a new variable that is assigned the reversed string. The function iterates over each word from the wordsList, reverses it and then it appends the new reversed word to the string.

def FirstReverse(str): 
    # split the String in a list of words
    # reverse the order of words
    # e.g. the last word becomes the first word
    wordsList = str.split(" ")[::-1]

    # the new string that will be returned
    reversedStr = ''
    # iterate over each word from the wordsList
    for word in wordsList:
        # append the reversed word to the reversedStr variable
        reversedStr += word[::-1]
        # append the whitespace between the words
        reversedStr += ' '
    return reversedStr

Therefore, these are my two attempts for this challenge. All constructive comments are much appreciated.

Leave a Reply

Your email address will not be published. Required fields are marked *