알고리즘 풀이

[20211004]알고리즘 풀이

JoyYellow 2021. 10. 5. 00:41
한 개의 문자열을 입력받아 해당 문자열에 알파벳 대문자가 몇 개 있는지 알아내는 프로그램 을 작성하세요.
function solution(s){         
    let answer=0;
    for (let i of s) {
        const unitCode = i.charCodeAt(0)
        if (unitCode >= 60 && unitCode <= 90) answer++
    }
    return answer;
}

let str="KoreaTimeGood";
console.log(solution(str));

 

대문자와 소문자가 같이 존재하는 문자열을 입력받아 대문자로 모두 통일하여 문자열을 출력 하는 프로그램을 작성하세요.
function solution(s){         
  let answer="";
  for (let i of s) {
    if (i.charCodeAt() >= 97 && i.charCodeAt() <= 122) answer += String.fromCharCode(i.charCodeAt() - 32)
    else answer += i
  }
  return answer;
}
let str="ItisTimeToStudy";
console.log(solution(str));

 

N개의 문자열이 입력되면 그 중 가장 긴 문자열을 출력하는 프로그램을 작성하세요.
function solution(s){  
	let answer="", max=Number.MIN_SAFE_INTEGER;
  	for (let i = 0; i < 5; i++) {
        if (max < s[i].length) {
          max = s[i].length
          answer = s[i]
        }
    }
  return answer;
}
let str=["teacher", "time", "student", "beautiful", "good"];
console.log(solution(str));

 

소문자로 된 단어(문자열)가 입력되면 그 단어의 가운데 문자를 출력하는 프로그램을 작성하세요. 단 단어의 길이가 짝수일 경우 가운데 2개의 문자를 출력합니다.
function solution(s){  
  let answer;
  const length = Math.floor(s.length / 2)
  if (s.length % 2 === 1) {
    // 홀수 일 때
    answer = s[length]
  } else {
    // 짝수 일 때
    answer = s[length - 1] + s[length]
  }
  return answer;
}
console.log(solution("study"));

 

소문자로 된 한개의 문자열에 입력되면 중복된 문자를 제거하고 출력하는 프로그램을 작성하세요. 제거된 문자열의 각 문자는 원래 문자열의 순서를 유지합니다.
function solution(s){  
  let answer="";
  for (let e of s) {
    let count = 0
    for (let i = 0; i < answer.length; i++) {
      if (answer[i] === e) count++
      }
    if (count === 0) answer += e
  }
  return answer;
}
console.log(solution("ksekkset"));

 

N개의 문자열이 입력되면 중복된 문자열은 제거하고 출력하는 프로그램을 작성하세요. 출력하는 문자열은 원래의 입력순서를 유지합니다.
function solution(s){  
  let answer = [];
  for (let e of s) {
    let count = 0
    for (let i = 0; i < answer.length; i++) {
    	if (answer[i] === e) count++
    }
    if (count === 0) answer.push(e)
  }
  return answer;
}
let str=["good", "time", "good", "time", "student"];
console.log(solution(str));