개발

코테 연습

Jude.R 2021. 11. 9. 13:46
반응형

1.

String Challenge

Have the function StringChallenge(sen) take the sen parameter being passed and return the longest word in the string. If there are two or more words that are the same length, return the first word from the string with that length. Ignore punctuation and assume sen will not be empty. Words may also contain numbers, for example "Hello world123 567"

Examples

Input: "fun&!! time"
Output: time

Input: "I love dogs"
Output: love

 

function StringChallenge(sen) { 
  let regex = /[\w]+/g;
  let array = sen.match(regex);
  sen = ''
  array.map(element =>{
    element.length > sen.length? sen=element:'';
  })
  // code goes here  
  return sen; 

}
   
// keep this function call here 
console.log(StringChallenge(readline()));

2.

String Challenge

Have the function StringChallenge(str) take the str parameter being passed and return the first word with the greatest number of repeated letters. For example: "Today, is the greatest day ever!" should return greatest because it has 2 e's (and 2 t's) and it comes before ever which also has 2 e's. If there are no words with repeating letters return -1. Words will be separated by spaces.

Examples

Input: "Hello apple pie"
Output: Hello

Input: "No words"
Output: -1

 

function StringChallenge(str) { 
  let counter = new Array();
  let strArr = str.match(/[\w]+/g)
  strArr.map((element, index)=>{
    let flag
    let tmp ={}
    for(let i=0; i<element.length; i++){
      let ind = element[i]
      tmp[ind] = (tmp[ind]||0)+1
      !flag? flag=ind:
        tmp[flag]<=tmp[ind]? flag=ind:''
    }
    counter[index]=tmp[flag]
  })
  let max = Math.max(...counter)
  max==1? str=-1:str=strArr[counter.indexOf(max)]
  // code goes here  
  return str; 

}
   
// keep this function call here 
console.log(StringChallenge(readline()));

3.

Array Challenge

Have the function ArrayChallenge(arr) take the array of integers stored in arr, and determine if any two numbers (excluding the first element) in the array can sum up to the first element in the array. For example: if arr is [7, 3, 5, 2, -4, 8, 11], then there are actually two pairs that sum to the number 7: [5, 2] and [-4, 11]. Your program should return all pairs, with the numbers separated by a comma, in the order the first number appears in the array. Pairs should be separated by a space. So for the example above, your program would return: 5,2 -4,11

If there are no two numbers that sum to the first element in the array, return -1

Examples

Input: [17, 4, 5, 6, 10, 11, 4, -3, -5, 3, 15, 2, 7]
Output: 6,11 10,7 15,2

Input: [7, 6, 4, 1, 7, -2, 3, 12]
Output: 6,1 4,3

 

function ArrayChallenge(arr) { 
  let target = arr[0]
  let str = ''
  for(let i=1;i<arr.length;i++){
    let index=arr.indexOf(target-arr[i],i)
    index!=-1?
    str+=`${arr[i]},${arr[index]} `:''
  }
  // code goes here  
  return str; 

}
   
// keep this function call here 
console.log(ArrayChallenge(readline()));

 

문제에서는 big-O 로 따졌을때 가장 빠르다고 생각하는 풀이를 써달라고 하긴 했으나... 그냥 되는대로 한번 짜보았다

 

나중에 비슷한 문제가 있으면 참고해서 더 빠른 방법이 있나 한번 찾아봐야겠다

반응형