카타 8급 Counting sheep...

Jmnote (토론 | 기여)님의 2019년 3월 16일 (토) 13:15 판

1 C++

#include <algorithm>
using namespace std; 
int count_sheep(vector<bool> arr) {
  return count(arr.cbegin(), arr.cend(), true);
}
#include <algorithm>
using namespace std; 
int count_sheep(vector<bool> arr) {
  return count(arr.begin(), arr.end(), true);
}
using namespace std; 
int count_sheep(vector<bool> arr) {
  int res = 0;
  for(bool x: arr) {
    if( x ) res++;
  }
  return res;
}

2 Java

public class Counter {
  public int countSheeps(Boolean[] arrayOfSheeps) {
    int count = 0;
    for (Boolean b : arrayOfSheeps) if (b) count++;
    return count;
  }
}

3 Python

def count_sheeps(arrayOfSheeps):
  return arrayOfSheeps.count(True)
def count_sheeps(arrayOfSheeps):
  return len([x for x in arrayOfSheeps if x])
def count_sheeps(arrayOfSheeps):
  count = 0
  for sheep in arrayOfSheeps:
    if sheep:
      count += 1
  return count