Fun with Functions ★★★★☆

Have fun with functions and share it here for others to see and explore

[Question]★☆☆ Complete the solution so that it strips all text that follows any of a set of comment markers passed in. Any whitespace at the end of the line should also be stripped out.

apples, pears # and bananas
bananas !apples

apples, pears

Function Arguments: solution(string,markers)
string = “apples, pears # and bananas\ngrapes\nbananas !apples”
markers = ["#", “!”]
solution(string,markers) >>> "apples, pears\ngrapes\nbananas"

What would be the real life use case for this problem?

The most useful use case of this problem would be a modern text editor where you could add comments in your code file and the editor skips the commented part, one of those things which has gotten so common that we tend to ignore that it even exists

How to approach this problem?

  1. Get to understand the inputs

    In this case, we’ve a string and a list of markers

  2. Think of all possible approaches that you could take to solve this problem.

    I could think of two way to solve this
    (a) Regular Expressions
    (b) General Splitting & Joining the string based on given markers

  3. Once you’ve got the solution, try to analyze all of them based on complexity(aakashns/python-binary-search - Jovian)

First Approach: Regular Expression

import re 

def solution(string,markers):
    #check if the markers list isn't empty
    if markers != []:

        # create a pattern based on given marker(s)
        _pattern = re.compile(r'(\%s).*' % ('|\\'.join(markers))) 

        # split on new line(\n) and join the string after applying the substitute using compiled _pattern
        return '\n'.join([_pattern.sub('',each).strip() for each in string.split('\n')])

     # if marker list was empty, then return the entire string as given
        return string

Second Approach: General Splitting & Joining the string based on given markers

def solution(string,markers):

    # split on (\n) for new line characters
    parts = string.split('\n')
    # iterate over markers to split on list contents
    for s in markers:
        # iteratively split on each markers in the list and consider the item at 0th index
       # and remove any spaces at the right side onto all the items in parts(list)
        parts = [v.split(s)[0].rstrip() for v in parts]
    # Finally join the list back again based on `\n`
    return '\n'.join(parts)

My preferred way is second approach as it looks clean and easy to understand, even though it isn’t the fastest way to solve this problem. How would you have solved this question?

This question was taken from Codewars

[Question]★☆☆ A rectangle with sides equal to even integers a and b is drawn on the Cartesian plane. Its center (the intersection point of its diagonals) coincides with the point (0, 0), but the sides of the rectangle are not parallel to the axes; instead, they are forming 45 degree angles with the axes.

How many points with integer coordinates are located inside the given rectangle (including on its sides)?

For a = 6 and b = 4, the output should be 23

The following picture illustrates the example, and the 23 points are marked green.


[input] integer a

A positive even integer.

Constraints: 2 ≤ a ≤ 10000.

[input] integer b

A positive even integer.

Constraints: 2 ≤ b ≤ 10000.

[output] an integer

The number of inner points with integer coordinates.

Solution goes here…

Note: This problem was taken from codewars as well. I’ll be updating my solution once i have it, feel free to update your version of solution

1 Like