프로그래머스 : 기능개발 ( 함수형 프로그래밍 사용 )

KHW·2021년 8월 12일
0

코딩테스트

목록 보기
3/17
post-custom-banner

기능개발

1. 문제풀이 (기본)

function solution(progresses, speeds) {
    const real = [];
    let max = -Infinity;
    let count = 0;
    const result = progresses
    .map((val,i)=>Math.ceil((100-val) / speeds[i]))
    .map((val,i)=>{
        if(val == max)
            count++;
        else{
        max = Math.max(val,max);
        if(i == 0)
            count++;
        else if(val == max){
            real.push(count);
            count = 1;
        }
        else{
            count++;
        }
        }
    })
    if(count != 0)
        real.push(count);
    return real;
}

2. map 함수형 프로그래밍 작성

const map = (f, iter) => {
  let res = [];
  for (const idx in iter) {
    res.push(f(iter[idx],idx));
  }
  return res;
};

function solution(progresses, speeds) {
    const real = [];
    let max = -Infinity;
    let count = 0;
    
    let result = map((val,i)=>Math.ceil((100-val) / speeds[i]),progresses)
    map((val,i)=>{
        if(val == max)
            count++;
        else{
        max = Math.max(val,max);
        if(i == 0)
            count++;
        else if(val == max){
            real.push(count);
            count = 1;
        }
        else{
            count++;
        }
        }
    },result)
    if(count != 0)
        real.push(count);
    return real;

}

3. map / go 함수 사용

const reduce = (f, acc, iter) => {
  if (!iter) {
    iter = acc[Symbol.iterator]();
    acc = iter.next().value;
  }
  for (const a of iter) {
    acc = f(acc, a);
  }
  return acc;
};

const go = (...list) => reduce((a, f) => f(a), list);

const map = (f, iter) => {
  let res = [];
  for (const idx in iter) {
    res.push(f(iter[idx],idx));
  }
  return res;
};

function solution(progresses, speeds) {
    const real = [];
    let max = -Infinity;
    let count = 0;
    
    go(progresses,
      progresses =>map((val,i)=>Math.ceil((100-val) / speeds[i]),progresses),
      result => map((val,i)=>{
        if(val == max)
            count++;
        else{
        max = Math.max(val,max);
        if(i == 0)
            count++;
        else if(val == max){
            real.push(count);
            count = 1;
        }
        else{
            count++;
        }
        }
    },result))
    if(count != 0)
        real.push(count);
    return real;

}

4. map go pipe 사용



const reduce = (f, acc, iter) => {
  if (!iter) {
    iter = acc[Symbol.iterator]();
    acc = iter.next().value;
  }
  for (const a of iter) {
    acc = f(acc, a);
  }
  return acc;
};
const go = (...list) => reduce((a, f) => f(a), list);
const pipe = (...fs) => (a) => go(a, ...fs);
const map = (f, iter) => {
  let res = [];
  for (const idx in iter) {
    res.push(f(iter[idx],idx));
  }
  return res;
};

function solution(progresses, speeds) {
  const real = [];
  let max = -Infinity;
  let count = 0;

  const f = pipe(
    progresses =>map((val,i)=>Math.ceil((100-val) / speeds[i]),progresses),
    result => map((val,i)=>{
      if(val == max)
        count++;
      else{
        max = Math.max(val,max);
        if(i == 0)
          count++;
        else if(val == max){
          real.push(count);
          count = 1;
        }
        else{
          count++;
        }
      }
    },result))

  f(progresses);

  if(count != 0)
    real.push(count);
  return real;

}

5. map go curry 사용

const curry = (f) => (a, ..._) =>
  _.length ? f(a, ..._) : (..._) => f(a, ..._);

const reduce = (f, acc, iter) => {
  if (!iter) {
    iter = acc[Symbol.iterator]();
    acc = iter.next().value;
  }
  for (const a of iter) {
    acc = f(acc, a);
  }
  return acc;
};

const go = (...list) => reduce((a, f) => f(a), list);

const map = curry((f, iter) => {
  let res = [];
  for (const idx in iter) {
    res.push(f(iter[idx],idx));
  }
  return res;
});

function solution(progresses, speeds) {
    const real = [];
    let max = -Infinity;
    let count = 0;
    
    go(progresses,
      map((val,i)=>Math.ceil((100-val) / speeds[i])),
      map((val,i)=>{
        if(val == max)
            count++;
        else{
        max = Math.max(val,max);
        if(i == 0)
            count++;
        else if(val == max){
            real.push(count);
            count = 1;
        }
        else{
            count++;
        }
        }
    }))
    if(count != 0)
        real.push(count);
    return real;

}

6. map go pipe curry 모두 사용

const curry = (f) => (a, ..._) =>
  _.length ? f(a, ..._) : (..._) => f(a, ..._);

const reduce = (f, acc, iter) => {
  if (!iter) {
    iter = acc[Symbol.iterator]();
    acc = iter.next().value;
  }
  for (const a of iter) {
    acc = f(acc, a);
  }
  return acc;
};

const go = (...list) => reduce((a, f) => f(a), list);
const pipe = (...fs) => (a) => go(a, ...fs);
const map = curry((f, iter) => {
  let res = [];
  for (const idx in iter) {
    res.push(f(iter[idx],idx));
  }
  return res;
});

function solution(progresses, speeds) {
    const real = [];
    let max = -Infinity;
    let count = 0;
    
    const f = pipe(
      map((val,i)=>Math.ceil((100-val) / speeds[i])),
      map((val,i)=>{
        if(val == max)
            count++;
        else{
        max = Math.max(val,max);
        if(i == 0)
            count++;
        else if(val == max){
            real.push(count);
            count = 1;
        }
        else{
            count++;
        }
        }
    }))
    
    f(progresses);
    
    if(count != 0)
        real.push(count);
    return real;

}
profile
나의 하루를 가능한 기억하고 즐기고 후회하지말자
post-custom-banner

0개의 댓글