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

0125. Counting sheep...

daco2020 2023. 1. 25. 21:27
반응형

Consider an array/list of sheep where some sheep may be missing from their place. We need a function that counts the number of sheep present in the array (true means present).

For example,

[True,  True,  True,  False,
  True,  True,  True,  True ,
  True,  False, True,  False,
  True,  False, False, True ,
  True,  True,  True,  True ,
  False, False, True,  True]

The correct answer would be 17.

Hint: Don't forget to check for bad values like null/undefined



Solution:

def count_sheeps(sheeps):
    return sum(int(sheep) for sheep in sheeps if sheep)
def count_sheeps(sheeps):
  return sheeps.count(True)


반응형

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

0127. Remove the time  (0) 2023.01.28
0126. Over The Road  (0) 2023.01.27
0124. Regular Ball Super Ball  (0) 2023.01.24
0123. Check same case  (0) 2023.01.23
0122. The Feast of Many Beasts  (0) 2023.01.23