"카타 8급 Count of positives / sum of negatives"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
 
(사용자 2명의 중간 판 2개는 보이지 않습니다)
1번째 줄: 1번째 줄:
==C==
==C==
{{카타|8급|C|5}}
{{카타|8급|C|5}}
<source lang='C'>
<syntaxhighlight lang='C'>
#include <stddef.h>
#include <stddef.h>
void count_positives_sum_negatives(
void count_positives_sum_negatives(
10번째 줄: 10번째 줄:
   }
   }
}
}
</source>
</syntaxhighlight>
<source lang='C'>
<syntaxhighlight lang='C'>
#include <stddef.h>
#include <stddef.h>
void count_positives_sum_negatives(
void count_positives_sum_negatives(
24번째 줄: 24번째 줄:
   *negativesSum = negSum;
   *negativesSum = negSum;
}
}
</source>
</syntaxhighlight>


==C++==
==C++==
{{카타|8급|C++|4}}
{{카타|8급|C++|4}}
<source lang='cpp'>
<syntaxhighlight lang='cpp'>
#include <vector>
#include <vector>


39번째 줄: 39번째 줄:
     return {posCnt, negSum};
     return {posCnt, negSum};
}
}
</source>
</syntaxhighlight>
<source lang='cpp'>
<syntaxhighlight lang='cpp'>
#include <vector>
#include <vector>


56번째 줄: 56번째 줄:
     return res;
     return res;
}
}
</source>
</syntaxhighlight>


==Kotlin==
==Kotlin==
{{카타|8급|Kotlin|2}}
{{카타|8급|Kotlin|2}}
<source lang='kotlin'>
<syntaxhighlight lang='kotlin'>
fun countPositivesSumNegatives(input : Array<Int>?) : Array<Int> {
fun countPositivesSumNegatives(input : Array<Int>?) : Array<Int> {
     if( input == null || input.size == 0 ) return arrayOf<Int>()
     if( input == null || input.size == 0 ) return arrayOf<Int>()
71번째 줄: 71번째 줄:
     return arrayOf(count, sum)
     return arrayOf(count, sum)
}
}
</source>
</syntaxhighlight>
<source lang='kotlin'>
<syntaxhighlight lang='kotlin'>
fun countPositivesSumNegatives(input : Array<Int>?) : Array<Int> {
fun countPositivesSumNegatives(input : Array<Int>?) : Array<Int> {
     if (input == null || input.isEmpty()) return emptyArray()
     if (input == null || input.isEmpty()) return emptyArray()
78번째 줄: 78번째 줄:
     return arrayOf(positive.count(), negative.sum())
     return arrayOf(positive.count(), negative.sum())
}
}
</source>
</syntaxhighlight>
<source lang='kotlin'>
<syntaxhighlight lang='kotlin'>
fun countPositivesSumNegatives(input : Array<Int>?) : Array<Int> {
fun countPositivesSumNegatives(input : Array<Int>?) : Array<Int> {
     if (input == null || input.isEmpty()) return arrayOf()
     if (input == null || input.isEmpty()) return arrayOf()
     return arrayOf(input.filter{ it > 0 }.count(), input.filter{ it < 0 }.sum())
     return arrayOf(input.filter{ it > 0 }.count(), input.filter{ it < 0 }.sum())
}
}
</source>
</syntaxhighlight>
<syntaxhighlight lang='kotlin'>
fun countPositivesSumNegatives(input : Array<Int>?) = when {
  input.isNullOrEmpty() -> emptyArray<Int>()
  else -> arrayOf(input.filter{ it > 0 }.count(), input.filter{ it < 0 }.sum())
}
</syntaxhighlight>


==PHP==
==PHP==
<source lang='php'>
{{카타|8급|PHP|5}}
<syntaxhighlight lang='php'>
function countPositivesSumNegatives($input) {
function countPositivesSumNegatives($input) {
   if (empty($input)) return [];
   if (empty($input)) return [];
97번째 줄: 104번째 줄:
   return [$posCount, $negSum];
   return [$posCount, $negSum];
}
}
</source>
</syntaxhighlight>
<source lang='php'>
<syntaxhighlight lang='php'>
function countPositivesSumNegatives($input) {
function countPositivesSumNegatives($input) {
   $count = count($input);
   $count = count($input);
110번째 줄: 117번째 줄:
   return [$posCount, $negSum];
   return [$posCount, $negSum];
}
}
</source>
</syntaxhighlight>

2020년 11월 2일 (월) 02:42 기준 최신판

1 C[ | ]

#include <stddef.h>
void count_positives_sum_negatives(
  int *values, size_t count, int *positivesCount, int *negativesSum) {
  for(int i = 0; i < count; i ++) {
    if(values[i] > 0) *positivesCount += 1;
    else  *negativesSum += values[i];
  }
}
#include <stddef.h>
void count_positives_sum_negatives(
  int *values, size_t count, int *positivesCount, int *negativesSum) {
  int posCount = 0; 
  int negSum = 0;
  for(int i=0; i<count; i++) {
    if( values[i]>0 ) posCount++;
    else negSum += values[i];
  }
  *positivesCount = posCount;
  *negativesSum = negSum;
}

2 C++[ | ]

#include <vector>

std::vector<int> countPositivesSumNegatives(std::vector<int> input)
{
    if(input.empty()) return {};
    int posCnt=0, negSum=0;
    for(int x: input)
      x>0 ? posCnt++ : negSum+=x;
    return {posCnt, negSum};
}
#include <vector>

std::vector<int> countPositivesSumNegatives(std::vector<int> input)
{
    std::vector<int> res = {};
    if(input.empty()) return res;
    int posCnt=0, negSum=0;
    for(int num: input) {
      if(num>0) posCnt++;
      else negSum += num;
    }
    res.push_back(posCnt);
    res.push_back(negSum);
    return res;
}

3 Kotlin[ | ]

fun countPositivesSumNegatives(input : Array<Int>?) : Array<Int> {
    if( input == null || input.size == 0 ) return arrayOf<Int>()
    var count: Int = 0
    var sum: Int = 0
    input.forEach {
        if( it > 0 ) count++
        else sum += it
    }
    return arrayOf(count, sum)
}
fun countPositivesSumNegatives(input : Array<Int>?) : Array<Int> {
    if (input == null || input.isEmpty()) return emptyArray()
    val (positive, negative) = input.partition { it > 0 }
    return arrayOf(positive.count(), negative.sum())
}
fun countPositivesSumNegatives(input : Array<Int>?) : Array<Int> {
    if (input == null || input.isEmpty()) return arrayOf()
    return arrayOf(input.filter{ it > 0 }.count(), input.filter{ it < 0 }.sum())
}
fun countPositivesSumNegatives(input : Array<Int>?) = when {
  input.isNullOrEmpty() -> emptyArray<Int>()
  else -> arrayOf(input.filter{ it > 0 }.count(), input.filter{ it < 0 }.sum())
}

4 PHP[ | ]

function countPositivesSumNegatives($input) {
  if (empty($input)) return [];
  $posCount = $negSum = 0;
  foreach($input as $value) {
    if ($value > 0) $posCount++;
    else $negSum += $value;
  }
  return [$posCount, $negSum];
}
function countPositivesSumNegatives($input) {
  $count = count($input);
  if( $count < 1 ) return [];
  $posCount = 0;
  $negSum = 0;
  for($i=0; $i<$count; $i++) {
    if( $input[$i] > 0 ) $posCount++;
    else $negSum += $input[$i];
  }
  return [$posCount, $negSum];
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}