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 로 따졌을때 가장 빠르다고 생각하는 풀이를 써달라고 하긴 했으나... 그냥 되는대로 한번 짜보았다
나중에 비슷한 문제가 있으면 참고해서 더 빠른 방법이 있나 한번 찾아봐야겠다
'개발' 카테고리의 다른 글
가상 메모리란? (0) | 2025.01.22 |
---|---|
스프링 프레임워크의 특징: 개발자가 알아야 할 핵심 포인트 (0) | 2025.01.21 |
자바스크립트 모듈(type="module") (0) | 2025.01.21 |
<script> 태그 사용법과 팁 (0) | 2025.01.21 |
디바운싱(Debouncing)과 쓰로틀링(Throttling): 개념과 활용 방법 (0) | 2025.01.21 |