[알고리즘] 2024-02-26(월)

dev-riverkim·2024년 3월 13일
0

7 kyu - Testing 1-2-3

Your team is writing a fancy new text editor and you've been tasked with implementing the line numbering.

Write a function which takes a list of strings and returns each line prepended by the correct number.

The numbering starts at 1. The format is n: string. Notice the colon and space in between.

Examples: (Input --> Output)

[] --> []
["a", "b", "c"] --> ["1: a", "2: b", "3: c"]

Solution

var number=function(array){
  //your awesome code here
  return array.map((item,index) => {
    return `${index+1}: ${item}`
  })
  
}
var number = function(array) {
  return array.map(function (line, index) {
    return (index + 1) + ": " + line;
  });
}
let number = (a) => a.map((v, i) => `${i + 1}: ${v}`)

8 kyu - Area or Perimeter

You are given the length and width of a 4-sided polygon. The polygon can either be a rectangle or a square.

If it is a square, return its area. If it is a rectangle, return its perimeter.

Example(Input1, Input2 --> Output):

6, 10 --> 32
3, 3 --> 9

Note: for the purposes of this kata you will assume that it is a square if its length and width are equal, otherwise it is a rectangle.

Solution

const areaOrPerimeter = function(l , w) {
  return l === w ? l * w : (l * 2) + (w * 2)
};
const areaOrPerimeter = (l , w) => l === w ? l*w : 2*(l+w);
const areaOrPerimeter = function(l , w) {
  let area = l * w;
  let perimeter = (l + w) * 2;
  
  return l === w ? area : perimeter;
};

// const areaOrPerimeter = (l, w) => l === w ? l * w : (l + w) * 2;

8 kyu - Beginner - Lost Without a Map

Given an array of integers, return a new array with each value doubled.

For example:

[1, 2, 3] --> [2, 4, 6]

Solution

function maps(x){ 
 return x.map((i) => {
    return i + i
  })
}
function maps(x){
  return x.map(n => n * 2);
}
maps = x => x.map(e => e * 2);

8 kyu - Beginner Series #1 School Paperwork

Your classmates asked you to copy some paperwork for them. You know that
there are 'n' classmates and the paperwork has 'm' pages.

Your task is to calculate how many blank pages do you need. If n < 0 or m < 0 return 0.

Example:

n= 5, m=5: 25
n=-5, m=5:  0

Waiting for translations and Feedback! Thanks!

Solution

function paperwork(n, m) {
  return n < 0 || m < 0 ? 0 : n * m
}
function paperwork(n, m) {
  return n > 0 && m > 0 ? n * m : 0
}
function paperwork(n, m) {
  if (m < 0 || n < 0) {
    return 0;
  }
  return m * n;
}

7 kyu - Anagram Detection

An anagram is the result of rearranging the letters of a word to produce a new word (see wikipedia).

Note: anagrams are case insensitive

Complete the function to return true if the two arguments given are anagrams of each other; return false otherwise.

Examples

  • "foefet" is an anagram of "toffee"
  • "Buckethead" is an anagram of "DeathCubeK"

Solution

// write the function isAnagram
var isAnagram = function(test, original) {
  
  // 애너그램은 단어의 글자를 재배열하여 새로운 단어를 만든 결과물입니다(위키백과 참조).
  // 참고: 애너그램은 대소문자를 구분하지 않습니다.

  // 주어진 두 인수가 서로의 애너그램이면 참을 반환하고, 그렇지 않으면 거짓을 반환하는 함수를 완성합니다.
  // 예제
  // "foefet"은 "toffee"의 아나그램입니다.
  // "버킷헤드"는 "DeathCubeK"의 아나그램입니다.
  
  // 두 문자열을 비교하여 문자가 몇 개 있는지 비교?
  
  test = test.toLowerCase()
  original = original.toLowerCase()
  
  // 문자열을 배열로 변환한 뒤 정렬하여 비교합니다.
  
  return test.split('').sort().join('') === original.split('').sort().join('');
  
  
};
// write the function isAnagram
var isAnagram = function(test, original) {
  var t = test.toLowerCase().split('').sort().join('');
  var o = original.toLowerCase().split('').sort().join('');
  return (t==o)?true:false;
};
function isAnagram (test, original) {
	return test.toLowerCase().split("").sort().join("") === original.toLowerCase().split("").sort().join("");
}
profile
dev-riverkim

0개의 댓글