[알고리즘] 2024-02-01(목)

dev-riverkim·2024년 3월 13일
0

8 kyu - Reversed Strings

Complete the solution so that it reverses the string passed into it.

'world'  =>  'dlrow'
'word'   =>  'drow'

Solution

function solution(str){
  return str.split('').reverse().join('')
}
const solution = s => [...s].reverse().join('')

8 kyu - Convert boolean values to strings 'Yes' or 'No'.

Complete the method that takes a boolean value and return a "Yes" string for true, or a "No" string for false.

Solution

function boolToWord( bool ){
  return bool === true ? "Yes" : "No"
}
function boolToWord( bool ){
  return bool ? 'Yes':'No';
}
let boolToWord = bool => bool ? 'Yes' : 'No';

8 kyu - You Can't Code Under Pressure #1

Code as fast as you can! You need to double the integer and return it.

Solution

function doubleInteger(i) {
  // i will be an integer. Double it and return it.
  return i * 2;
}
function doubleInteger(i) {
  // i will be an integer. Double it and return it.
  return i+i;
}

8 kyu - MakeUpperCase

Write a function which converts the input string to uppercase.

Solution

function makeUpperCase(str) {
  return str.toUpperCase()
}

8 kyu - Grasshopper - Grade book

Grade book

Complete the function so that it finds the average of the three
scores passed to it and returns the letter value associated with that
grade.

Numerical ScoreLetter Grade
90 <= score <= 100'A'
80 <= score < 90'B'
70 <= score < 80'C'
60 <= score < 70'D'
0 <= score < 60'F'

Tested values are all between 0 and 100. Theres is no need to check for negative values or values greater than 100.

Solution

function getGrade (s1, s2, s3) {
  let score = (s1+s2+s3)/3
  
  if(score >= 90) {
    return 'A'
  } else if (score >= 80){
    return 'B'
  } else if(score >= 70){
    return 'C'
  } else if(score >= 60){
    return 'D'
  } else {
    return 'F'
  }
}
function getGrade (s1, s2, s3) {
     let score = (s1 + s2 + s3) / 3;
     if (score >= 90) {
         return 'A';
     }
     if (score >= 80) {
         return 'B';
     }
     if (score >= 70) {
         return 'C';
     }
     if (score >= 60) {
         return 'D';
     }
     return 'F';
}
function getGrade (s1, s2, s3) {
  avg = (s1+s2+s3)/3;
  if (avg < 60)  return "F";
    else if (avg < 70) return "D";
    else if (avg < 80) return "C";
    else if (avg < 90) return "B";
    else return "A";
}
function getGrade (s1, s2, s3) {
  var s = (s1 + s2 + s3) / 3
  return s >= 90 ? "A" : s >= 80 ? "B" : s >= 70 ? "C" : s >= 60 ? "D" : "F"
}

7 kyu - Number of People in the Bus

There is a bus moving in the city which takes and drops some people at each bus stop.

You are provided with a list (or array) of integer pairs. Elements of each pair represent the number of people that get on the bus (the first item) and the number of people that get off the bus (the second item) at a bus stop.

Your task is to return the number of people who are still on the bus after the last bus stop (after the last array). Even though it is the last bus stop, the bus might not be empty and some people might still be inside the bus, they are probably sleeping there :D

Take a look on the test cases.

Please keep in mind that the test cases ensure that the number of people in the bus is always >= 0. So the returned integer can't be negative.

The second value in the first pair in the array is 0, since the bus is empty in the first bus stop.

Solution

var number = function(busStops){
  // Good Luck!
//  도시에는 각 버스 정류장에서 일부 사람들을 태우고 내리는 버스가 있습니다.
//  정수 쌍의 목록(또는 배열)이 제공됩니다.  각 쌍의 요소는 버스에 타는 사람의 수(첫 번째 항목)와 버스 정류장에서 버스에서 내리는 사람의 수(두 번째 항목)를 나타냅니다.
//  당신의 임무는 마지막 버스 정류장 이후(마지막 배열 이후) 버스에 아직 남아 있는 사람들의 수를 반환하는 것입니다.  
// 마지막 정류장임에도 불구하고 버스가 비어있지 않을 수도 있고, 아직 버스 안에 사람들이 있을 수도 있고, 아마 거기서 자고 있을 수도 있어요 :D

//  테스트 케이스를 살펴보세요.
  
  console.log(busStops)
  
  // 배열을 돌면서 첫번째 자릿수를 더하고
  // 배열을 돌면서 두번째 자리수를 더해서
  
  // 두 값의 차이를 결과로 반환
  
  let value1 = 0;
  let value2 = 0;
  let a = busStops.map((i)=>{
     
      value1 += i[0]
      value2 += i[1]
  })
  
  return value1 - value2
}
const number = (busStops) => busStops.reduce((rem, [on, off]) => rem + on - off, 0);
var number = function(busStops){
	var totalPeople = 0;
  for (var i=0; i<busStops.length; i++) {
  	totalPeople += busStops[i][0];
    totalPeople -= busStops[i][1];
  }
  return totalPeople;
}
var number = function(busStops){
  // Good Luck!
  var num=0;
  for(var i=0;i<busStops.length;i++){
      num+=busStops[i][0]-busStops[i][1]
  }
  return num;
}

7 kyu - Mumbling

This time no story, no theory. The examples below show you how to write function accum:

Examples:

accum("abcd") -> "A-Bb-Ccc-Dddd"
accum("RqaEzty") -> "R-Qq-Aaa-Eeee-Zzzzz-Tttttt-Yyyyyyy"
accum("cwAt") -> "C-Ww-Aaa-Tttt"

The parameter of accum is a string which includes only letters from a..z and A..Z.

Solution

function accum(s) {
  
  // 문자열을 입력받고 소문자로 변경한 뒤
  // 배열로 쪼갠 뒤 배열의 자릿수 만큼 문자열을 반복
  
  const list = s.toLowerCase().split("")
  const newList = list.map((i,z)=>{
    return  i.toUpperCase() + i.toLowerCase().repeat(z)
  })
  
  return newList.join("-")

}
function accum(s) {
	return s.split('').map((c, i) => (c.toUpperCase() + c.toLowerCase().repeat(i))).join('-');
}
function accum(s) {
	return s.split('').map((x,index) => x.toUpperCase()+Array(index+1).join(x.toLowerCase())).join('-');
}
function accum(str) {
	var letters = str.split('');
  var result = [];
  for (var i = 0; i < letters.length; i++) {
    result.push(letters[i].toUpperCase() + Array(i + 1).join(letters[i].toLowerCase()));
  }
  return result.join('-');
}
profile
dev-riverkim

0개의 댓글