Wednesday, December 26, 2018

codingdirectional: Put the exclamation marks and question marks to the balance, are they balanced?

Hello, we are supposed to move to another site today but let us finish up another python problem on codewars first before we proceed to a new site. The problem goes like this.

A function will take in two parameters consist of a string, each string consists of the exclamation mark and the question mark. Each exclamation mark weight is 2; Each question mark weight is 3. Sum up all the character values that consist of a question mark and exclamation mark on the left parameter and the right parameter, if the left side is heavier, return “Left”; If the right side is heavier, return “Right”; If they are balanced, return “Balance”. This is a very simple question so you will be able to answer it easily, come out with your own solution before looking at the below answer.

def balance(left, right):

    left_i = list(left)
    right_i = list(right)
    left_sum = calculate(left_i)
    right_sum = calculate(right_i)

    if(left_sum > right_sum):
        return "Left"
    elif(left_sum < right_sum):
        return "Right"
    else:
        return "Balance"

def calculate(a_list):
    sum = 0
    
    for i in range(len(a_list)):
        if(a_list[i] == '!'):
            sum += 2
        elif(a_list[i] == '?'):
            sum += 3
    return sum

Hope you do enjoy this python problem and don’t forget to subscribe to this website through the bell button if you like this article.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...