나는 이렇게 학습한다/Algorithm & SQL

1226. Alphabet war

daco2020 2022. 12. 27. 00:49
반응형

Introduction

There is a war and nobody knows - the alphabet war! There are two groups of hostile letters. The tension between left side letters and right side letters was too high and the war began.

Task

Write a function that accepts fight string consists of only small letters and return who wins the fight. When the left side wins return Left side wins!, when the right side wins return Right side wins!, in other case return Let's fight again!.

The left side letters and their power:

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

The right side letters and their power:

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

The other letters don't have power and are only victims.

Example

AlphabetWar("z");        //=> Right side wins!
AlphabetWar("zdqmwpbs"); //=> Let's fight again!
AlphabetWar("zzzzs");    //=> Right side wins!
AlphabetWar("wwwwwwz");  //=> Left side wins!

Alphabet war Collection



Solution:

left = {
    "w": 4,
    "p": 3,
    "b": 2,
    "s": 1,
}

right = {
    "m": 4,
    "q": 3,
    "d": 2,
    "z": 1,
}

def alphabet_war(fight):
    left_point = sum(left.get(i, 0) for i in fight)
    right_point = sum(right.get(i, 0) for i in fight)

    result = {
        left_point > right_point: "Left side wins!",
        left_point < right_point: "Right side wins!",
        left_point == right_point: "Let's fight again!"
    }

    return result[True]


반응형

'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글

1228. Kata Example Twist  (0) 2022.12.28
1227. CSV representation of array  (0) 2022.12.27
1225. Switcheroo  (0) 2022.12.25
1224. Regex count lowercase letters  (0) 2022.12.25
1223. validate code with simple regex  (0) 2022.12.23