선택 언어 : nodejs
백준 배열 문제 모음 : 🔗 BaaaaaaaaaaarkingDog 부록 A - 문자열 기초
let [sentence, word] = require('fs').readFileSync('./input.txt').toString().split('\n')
let count = 0,
index = sentence.indexOf(word)
while (index >= 0) {
count++
sentence = sentence.substring(index + word.length)
index = sentence.indexOf(word)
}
console.log(count)
문제를 꼼꼼히 안읽어서 어렵게 풀었다. 문제를 잘 읽자!! 😓
const [numAry, ...input] = require('fs').readFileSync('./input.txt').toString().trim().split('\n')
const [count, DNA_length] = numAry.split(' ').map(Number)
// count: 5 strLength: 8
// input: [ 'TATGATAC', 'TAAGCTAC', 'AAAGATCC', 'TGAGATAC', 'TAAGATGT' ]
let str_count = []
for (let i = 0; i < DNA_length; i++) {
str_count.push({})
for (let j = 0; j < count; j++) {
let cur_DNA = input[j][i]
str_count[i][cur_DNA] ? str_count[i][cur_DNA]++ : (str_count[i][cur_DNA] = 1)
}
}
/* str_count:
[
{ T: 4, A: 1 },
{ A: 4, G: 1 },
{ T: 1, A: 4 },
{ G: 5 },
{ A: 4, C: 1 },
{ T: 5 },
{ A: 3, C: 1, G: 1 },
{ C: 4, T: 1 }
]
*/
// 각 객체에서 values 가 가장 큰 값의 key 를 찾는다
let newArray = str_count.map(obj => {
let max = Math.max(...Object.values(obj))
let keys = Object.keys(obj).filter(key => obj[key] === max)
return keys.sort()[0]
})
let differCount = 0
for (let str of input) {
let differ = newArray.filter((al, idx) => al !== str[idx])
differCount += differ.length
}
let newDNA = newArray.join('')
console.log(newDNA)
console.log(differCount)
const [numAry, ...input] = require('fs').readFileSync('./input.txt').toString().trim().split('\n')
const [count, DNA_length] = numAry.split(' ').map(Number)
// count: 5 strLength: 8
// input: [ 'TATGATAC', 'TAAGCTAC', 'AAAGATCC', 'TGAGATAC', 'TAAGATGT' ]
let maxAry = []
for (let i = 0; i < DNA_length; i++) {
let counts = { A: 0, T: 0, G: 0, C: 0 },
max = 'A'
for (let j = 0; j < count; j++) {
let cur_DNA = input[j][i]
counts[cur_DNA]++
if (counts[max] < counts[cur_DNA] || (counts[max] === counts[cur_DNA] && max > cur_DNA)) {
max = cur_DNA
}
}
maxAry.push(max)
}
console.log('maxAry:', maxAry)
/* maxAry: [
'T', 'A', 'A',
'G', 'A', 'T',
'A', 'C'
]
*/
let differCount = 0
for (let str of input) {
let differ = maxAry.filter((al, idx) => al !== str[idx])
differCount += differ.length
}
let newDNA = maxAry.join('')
console.log(newDNA)
console.log(differCount)
const input = require('fs').readFileSync('./input.txt').toString().trim()
let N = input.length
let R = Math.floor(Math.sqrt(N))
while (N % R !== 0) {
R--
}
let C = N / R
let regex = new RegExp(`.{1,${R}}`, 'g')
let result = input.match(regex)
let answer = ''
for (let i = 0; i < R; i++) {
for (let j = 0; j < C; j++) {
answer += result[j][i]
}
}
console.log(answer)
const input = require('fs').readFileSync('./input.txt').toString().trim()
let N = input.length
let R = Math.floor(Math.sqrt(N))
while (N % R !== 0) {
R--
}
let C = N / R
let answer = ''
for (let a = 0; a < R; ++a) {
for (let b = 0; b < C; ++b) {
answer += input[b * R + a]
}
}
console.log(answer)
.split(':')
.splice(emptyIdx, 0, '')
.splice(emptyIdx, 1)
.padStart(4, '0')
.join(':')
const input = require('fs').readFileSync('./input.txt').toString().trim()
let array = input.split(':')
while (array.length < 8) {
let emptyIdx = array.indexOf('')
array.splice(emptyIdx, 0, '')
console.log('중간', array, array.length)
}
while (array.length > 8) {
let emptyIdx = array.indexOf('')
array.splice(emptyIdx, 1)
}
let strAry = array.map(str => str.padStart(4, '0')).join(':')
console.log(strAry)
const [A, B] = require('fs').readFileSync('./input.txt').toString().trim().split('\n')
// 알파벳 객체
const alphabetNum = [3, 2, 1, 2, 3, 3, 2, 3, 3, 2, 2, 1, 2, 2, 1, 2, 2, 2, 1, 2, 1, 1, 1, 2, 2, 1]
const alphabet = {}
for (let i = 65; i <= 90; i++) {
alphabet[String.fromCharCode(i)] = alphabetNum[i - 65]
}
// 이름에 들어간 알파벳들 획 순을 모은 배열
let nameNum = []
for (let i = 0; i < A.length; i++) {
nameNum.push(alphabet[A[i]])
nameNum.push(alphabet[B[i]])
}
const nameTest = ary => {
// 2 글자만 남았을 때 리턴
if (ary.length <= 2) return console.log(ary.join('').padStart(2, '0'))
let i = 1,
answer = []
while (ary[i] !== undefined) {
let a = ary[i] + ary[i - 1]
answer.push(a % 10)
i += 1
}
return nameTest(answer)
}
nameTest(nameNum)