Thursday, December 7, 2023
HomeSoftware EngineeringTaking part in the Alphabet Conflict in Python

Taking part in the Alphabet Conflict in Python


The problem

Introduction

There’s a struggle and no person is aware of – the alphabet struggle!
There are two teams of hostile letters. The strain between left facet letters and proper facet letters was too excessive and the struggle started.

Job

Write a operate that accepts battle string consists of solely small letters and return who wins the battle. When the left facet wins return Left facet wins!, when the appropriate facet wins return Proper facet wins!, in different case return Let's battle once more!.

The left facet letters and their energy:

 w - 4
 p - 3
 b - 2
 s - 1

The proper facet letters and their energy:

 m - 4
 q - 3
 d - 2
 z - 1

The opposite letters don’t have energy and are solely victims.

Instance

AlphabetWar("z");        //=> Proper facet wins!
AlphabetWar("zdqmwpbs"); //=> Let's battle once more!
AlphabetWar("zzzzs");    //=> Proper facet wins!
AlphabetWar("wwwwwwz");  //=> Left facet wins!

The answer in Python code

Choice 1:

def alphabet_war(battle):
    left = {
        'w': 4,
        'p': 3,
        'b': 2,
        's': 1
    }
    proper = {
        'm': 4,
        'q': 3,
        'd': 2,
        'z': 1
    }
    
    rating = 0
    
    for i in checklist(battle):
        if i in left:
            rating -= left[i]
        elif i in proper:
            rating += proper[i]
            
    if rating > 0:
        return "Proper facet wins!"
    elif rating < 0:
        return "Left facet wins!"
    else:
        return "Let's battle once more!"

Choice 2:

def alphabet_war(battle):
    d = {'w':4,'p':3,'b':2,'s':1,
         'm':-4,'q':-3,'d':-2,'z':-1}
    r = sum(d[c] for c in battle if c in d)
    
    return {r==0:"Let's battle once more!",
            r>0:"Left facet wins!",
            r<0:"Proper facet wins!"
            }[True]

Choice 3:

def alphabet_war(battle):
    a, b = 'sbpw', 'zdqm'
    l, r = sum([a.find(x)+1 for x in fight]), sum([b.find(y)+1 for y in fight])
    return "Proper facet wins!" if l<r else "Left facet wins!" if r<l else "Let's battle once more!" 

Check circumstances to validate our resolution

import take a look at
from resolution import alphabet_war

@take a look at.describe("Mounted Checks")
def fixed_tests():
    @take a look at.it('Fundamental Check Circumstances')
    def basic_test_cases():
        take a look at.assert_equals(alphabet_war("z"), "Proper facet wins!")
        take a look at.assert_equals(alphabet_war("zdqmwpbs"), "Let's battle once more!")
        take a look at.assert_equals(alphabet_war("wq"), "Left facet wins!")
        take a look at.assert_equals(alphabet_war("zzzzs"), "Proper facet wins!")
        take a look at.assert_equals(alphabet_war("wwwwww"), "Left facet wins!")
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments