프로그래머스 문제풀이

기능개발_2LV_스택/큐(Python/JavaScript)

codermun 2021. 6. 26. 12:53
728x90

Python 풀이

하루에 완성되는 개발을 고려내기 위해 Time 변수를 어떻게 사용햇는지

주목하자!!!

from collections import deque
""" Time 을 이용한 조건문 구성하기"""
def solution(progresses, speeds):
    answer = []
    p = deque(progresses)
    s = deque(speeds)
    time = 0
    count = 0
    
    while p:
        # print(f'1 : P : {p}, tiem : {time} , Count : {count}')
        if p[0] + time * s[0] >= 100:
            # print(f'22 : P : {p}, tiem : {time} , Count : {count}')
            p.popleft()
            s.popleft()
            count +=1
        else:
            if count == 0:
                # print(f'333 : P : {p}, tiem : {time} , Count : {count}')
                time +=1
            else:
                # print(f'4444: P : {p}, tiem : {time} , Count : {count}')
                answer.append(count)
                count = 0
    
    answer.append(count)
            
    return answer
from collections import deque
""" Time 을 이용한 조건문 구성하기"""
def solution(progresses, speeds):
    answer = []
    p = deque(progresses)
    s = deque(speeds)
    time = 0
    count = 0
    
    while p:
        if p[0] + time * s[0] >= 100:
            p.popleft()
            s.popleft()
            count +=1
        else:
            if count == 0:
                time +=1
            else:
                answer.append(count)
                count = 0
    
    answer.append(count)
            
    return answer


Javascript

function solution(progresses, speeds) {
    let answer = [];
    let time = 0
    let count = 0
 
    while (progresses[0]) {
        if (progresses[0] + (time * speeds[0]) >= 100){
            progresses.shift()
            speeds.shift()
            count += 1
        } else {
            if (count === 0){
                time += 1
            } else {
                answer.push(count)
                count = 0
            }
        }
    }
    answer.push(count)
    return answer;
}

 

 


Javascript (함수형 프로그래밍)

function* range(start = 0, stop = start, step = 1) {
  if (arguments.length === 1) start = 0;
  if (arguments.length < 3 && start > stop) step *= -1;

  if (start < stop) {
    while (start < stop) {
      yield start;
      start += step;
    }
  } else {
    while (start > stop) {
      yield start;
      start += step;
    }
  }
}

function map(f) {
  return function* (iter) {
    for (const a of iter) yield f(a);
  }
}

function filter(f) {
  return function* (iter) {
    for (const a of iter) if (f(a)) yield a;
  }
}

function take(limit) {
  return function* (iter) {
    for (const a of iter) {
      yield a;
      if (--limit === 0) break;
    }
  }
}

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

function each(f) {
  return function(iter) {
    for (const a of iter) f(a);
    return iter;
  }
}

function go(arg, ...fs) {
  return reduce((arg, f) => f(arg))(arg, fs);
}

const head = ([a]) => a;

const find = (f) => (iter) => head(filter(f)(iter));

function inc(parent, k) {
  parent[k] ? parent[k]++ : (parent[k] = 1);
  return parent;
}

const countBy = (f) => (iter) =>
  reduce((counts, a) => inc(counts, f(a)))({}, iter);

const identity = a => a;

const count = countBy(identity);

const groupBy = (f) => (iter) =>
  reduce(
    (group, a, k = f(a)) => ((group[k] = (group[k] || [])).push(a), group)
  )({}, iter);

function* entries(obj) {
  for (const k in obj) yield [k, obj[k]];
}

function* values(obj) {
  for (const k in obj) yield obj[k];
}

const isFlatable = a =>
  a != null && !!a[Symbol.iterator] && typeof a !== 'string';

function* flat(iter) {
  for (const a of iter) isFlatable(a) ? yield* a : yield a;
}

function zip(a) {
  return function* (b) {
    a = a[Symbol.iterator]();
    b = b[Symbol.iterator]();
    while (true) {
      const { value, done } = a.next();
      const { value: value2, done: done2 } = b.next();
      if (done && done2) break;
      yield [value, value2];
    }
  }
}

function concat(...args) {
  return flat(args);
}

const dayCount = periods =>
    reduce(([counts, total_period], period) =>
        total_period < period
            ? [inc(counts, counts.length), period]
            : [inc(counts, counts.length-1), total_period])([[], 0], periods);

function solution(progresses, speeds) {
    /*
    zip 을 이용해 작업의 진도와 개발 속도를 문제를 편하게 해결하기 위해
    [93, 1], [30, 30] , [55, 5] 과 같이 만들어줌 이때, zip은 well foremd iterator를 반환

    map 을 이용해 작업일을 계산하여 배열로 변경함.
    이때 Math.ceil 로 결과값을 올려 정수로 받음.
    
    dayCount 함수에서 작업일 배열을 받아, 
    reduce로 배포일 배열을 만듬 2차원 배열의 식에 이용되는 마지막 요소를 제외한
    첫번째 배열을 head로 출력.  
    [ [ 2, 1 ], 9 ]
    */

    return go(
        zip(progresses)(speeds),
        map(([progress, speed]) => Math.ceil((100 - progress) / speed)),
        dayCount,
        head
    ) 
}

 

728x90