0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Codewars 7 kyu Alphabet war

Posted at

Codewars 7 kyu Alphabet war

Task

Write a function, persistence, that takes in a positive parameter num and returns its multiplicative persistence, which is the number of times you must multiply the digits in num until you reach a single digit.

Verbalization

  1. 下記のleftside とrightsideのdictionaryをそれぞれつくる
    leftside
    w - 4
    p - 3
    b - 2
    s - 1

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

  1. For 文で(fight)内の文字をひとつづつとりだす
  2. もし、w,p,b,sがでたら、dictionary leftsideのvalueの合計数字を足し、
    3, もしm,q,d,zがdictionary rightsideのvalueの合計数字を足す
  3. 2.と3.を比べ、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!.

Code

def alphabet_war(fight):
    leftside = {"w":4,"p":3,"b":2,"s":1}
    rightside ={"m":4,"q":3,"d":2,"z":1}
    left_side = 0
    right_side = 0
    for i in fight:
        if i in leftside:
            left_side += leftside[i]
        elif i in rightside:
            right_side += rightside[i]
    if left_side > right_side:
        return "Left side wins!"
    elif left_side < right_side:
        return "Right side wins!"
    else:
        return "Let's fight again!"

Reference

if ... elif ... elseと、if ... if ...の使い分け

書き方 意味 Trueのときに実行される数
if ... elif ... else 条件を順番に見て、最初の1つだけ実行 1個
if ... if ... それぞれ独立に判断、複数実行されることもある 複数ありうる
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?