카타 8급 Beginner - Lost Without a Map

Jmnote bot (토론 | 기여)님의 2020년 11월 2일 (월) 02:41 판 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)

1 C[ | ]

#include <stddef.h>
int *maps(const int *arr, size_t size) {
  int *res = calloc(size, sizeof(int));
  for(int i=0; i<size; i++) res[i] = arr[i]*2;
  return res;
}

2 C++[ | ]

std::vector<int> maps(const std::vector<int> & values) {
  std::vector<int> res = values;
  for(int& v: res) v *= 2;
  return res;
}
std::vector<int> maps(const std::vector<int> & values) {
  std::vector <int> v = values;
  std::transform(std::begin(v),std::end(v),std::begin(v),[](int x){return 2*x;});
  return v;
}
std::vector<int> maps(const std::vector<int> & values) {
  std::vector<int> res;
  for(auto a:values) res.push_back(a*2);
  return res;
}
std::vector<int> maps(const std::vector<int> & values) {
  std::vector<int> res = values;
  for(int& v: res) v *= 2;
  return res;
}

3 Kotlin[ | ]

fun maps(x: IntArray): IntArray {
    return x.map { it * 2 }.toIntArray()
}
fun maps(x : IntArray) = x.map { it * 2 }.toIntArray()
fun maps(x: IntArray): IntArray {
    for (i in 0 until x.size) x[i] *= 2
    return x
}

4 R[ | ]

maps <- function(v) {
  v * 2
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}