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 Divide and Conquer

Posted at

Codewars 7 kyu Divide and Conquer

Task

Given a mixed array of number and string representations of integers, add up the non-string integers and subtract the total of the string integers.
Return as a number.

Verbalization

  1. 整数のみ抽出
  2. 1.を足す
  3. 文字列のみ抽出
  4. 抽出した文字列を整数にする
  5. 4.を足す

#Code

def div_con(x):
    n = 0  
    for i in x:
        if isinstance(i, int): 
            n += i
        else:
            n -= int(i)
    return n

Other example

#もともとやりたかった方法に近い
def div_con(x):
    integer = []
    string = []
    for element in x:
        if type(element) == int:
            integer.append(element)
        else:
            string.append(int(element))
    return sum(integer) - sum(string)
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?