Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

안진우: 과제 3일차 #13

Open
wants to merge 22 commits into
base: main
Choose a base branch
from

Conversation

jinwoo0207
Copy link
Contributor

No description provided.

Comment on lines +3 to +5
let sum = 0
const nString = n.toString()
const nArray = nString.split('')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

; 세미콜론을 습관화 해주세요

@@ -0,0 +1,4 @@
function solution(s) {
let answer = true;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기서 answer는 필요가 없네요.

Suggested change
let answer = true;

@@ -0,0 +1,4 @@
function solution(s) {
let answer = true;
return (s.length===4||s.length===6)&&isNaN(s)===false?true:false
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return (s.length===4||s.length===6)&&isNaN(s)===false?true:false
return (s.length === 4 || s.length === 6) && isNaN(s) === false ? true : false

제 눈건강과 가독성을 살려주세요

@@ -0,0 +1,9 @@
function solution(s) {
let answer = true;
if((s.length===4||s.length===6)&&isNaN(s)===false){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if((s.length===4||s.length===6)&&isNaN(s)===false){
if ((s.length === 4 || s.length === 6) && isNaN(s) === false) {

Comment on lines +4 to +8
answer=true
}else{
answer=false
}
return answer;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
answer=true
}else{
answer=false
}
return answer;
return answer = true
}
return answer = false;

여기서 리턴을 바로 해주면 else가 필요 없어집니다. 당연히 아시겠지만 return이 실행되면 함수 실행이 끝납니다.

const nArray = nString.split('')

for (let i = 0; i < nArray.length; i++){
sum += parseInt(nArray[i])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
sum += parseInt(nArray[i])
sum += +nArray[i]

크게 상관은 없는데 이런 방법도 있다는걸 알려드립니다.

@@ -0,0 +1,4 @@
function solution(s) {
let answer = true;
return (s.length===4||s.length===6)&&isNaN(s)===false?true:false
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return (s.length===4||s.length===6)&&isNaN(s)===false?true:false
return (s.length === 4 || s.length === 6) && !isNaN(s)

이렇게 바꿔도 무방합니당 물론 테스트케이스 11은 통과 못할거에요 ㅋㅋㅋ

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

감사합니다!

let answer = [];
let cutArray = [];
for (let i = 0; i < commands.length; i++){
cutArray = array.slice (commands[i][0]-1, commands[i][1]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cutArray = array.slice (commands[i][0]-1, commands[i][1]);
cutArray = array.slice (commands[i][0] - 1, commands[i][1]);

let cutArray = [];
for (let i = 0; i < commands.length; i++){
cutArray = array.slice (commands[i][0]-1, commands[i][1]);
cutArray.sort ( (a, b) => a - b );
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cutArray.sort ( (a, b) => a - b );
cutArray.sort((a, b) => a - b);

for (let i = 0; i < commands.length; i++){
cutArray = array.slice (commands[i][0]-1, commands[i][1]);
cutArray.sort ( (a, b) => a - b );
answer[i] = cutArray[commands[i][2]-1];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
answer[i] = cutArray[commands[i][2]-1];
answer[i] = cutArray[commands[i][2] - 1];

Comment on lines +7 to +9
let inumindex = numbers[i];
let jnumindex = numbers[j];
answer.push(inumindex + jnumindex);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let inumindex = numbers[i];
let jnumindex = numbers[j];
answer.push(inumindex + jnumindex);
answer.push(numbers[i] + numbers[j]);

불필요한 변수 선언은 하지 않는게 좋습니다.

Comment on lines +12 to +14
answer.sort(function(a, b){
return a - b;
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
answer.sort(function(a, b){
return a - b;
});
answer.sort((a, b) => a - b);

k번째수보다 이게 더 먼저 푼 문제인가요? 크게 상관은 없는데 arrow function이 좀 더 깔끔하겠네요

Comment on lines +15 to +16
const setAnser = [...new Set(answer)];
return setAnser;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const setAnser = [...new Set(answer)];
return setAnser;
return [...new Set(answer)];

불필요한 변수선언 2

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

어때 배열로만 접근하니까 깔끔하지??
그래도 무조건 객체로 접근해서 풀어봐야됩니다

return participant[i];
}
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좋습니다! 화이팅 :)
자신이 생각한 알고리즘으로 먼저 풀어보고, 추후 조금 더 나은 코드는 어떻게 짜야할지 생각한다면 앞으로도 훨씬 더 많은 도움이 됩니다!

function solution(absolutes, signs) {
let sum = 0;
for(let i = 0; i<absolutes.length; i++){
if(signs[i] == true){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if(signs[i] == true){
if(signs[i]){

문제를 보니까. signs는 bollean값인걸 확인했습니다!
js에서는 bollean값일 경우에는 비교연산자를 사용하지 않고도 signs[ i ] 가 true일경우와 false일 경우가 구분됩니다!
바꾸지 마시고 참고만 해두시면 좋을 것 같아요! 잘 푸셨습니다 👍

return answer;
}


Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이해하기 어려웠던 문제는, 다른 방법을 통해서 다시 풀어보는 것도 좋은 방법입니다 👍
�예시로, split함수 후에, map함수와 join을 통해서 푸는 방법도 있습니다!

참고자료 : js 메소드
https://velog.io/@yoojinpark/Javascriptmapfilter

// zeroCount값을 증가시킨다음에 count값이 6개면 다 맞았기 때문에 결과값에 1,1 을 넣어주고
// count값이 5개면 나머지 하나 값이 0인지 그냥 틀린 값인지 검사해서 0이면 1,2 를 틀린값이면 2,2를 결과값을 출력한다.
// 이것을 count 6...0까지 zeroCount 값이 0...6까지 나올 수 있는 모든 경우의 수를 검사한다.

Copy link
Collaborator

@2swo 2swo Dec 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

알고리즘을 자신이 생각한 데로 모든 경우의 수를 검사하는 의지.. 너무 좋습니다! 💯

하지만, 조금 더 코드를 간결하게 구성할 수 있는 방안으로 알고리즘에 힌트를 짧게 드릴게요.

  1. 0을 넣기 위한 배열과, 일치값을 넣기 위한 배열을 만들어서도 풀어보세요!
  2. 문제를 보면, 결국 원하는 것은 최고 순위와 최저 순위입니다. 모든 케이스를 적용하면서 이해했으니. 이제 최고 순위와 최저 순위만 계산하는 로직을 짜 보면 더 간결해질 것 같아요!

let answer = [];
let count = 0;
let zeroCount = 0;
for (let i=0; i<lottos.length; i++){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for (let i=0; i<lottos.length; i++){
for (let i=0; i<6; i++){

문제의 제한사항- 1번 항목입니다. [lottos는 길이 6인 정수 배열입니다.]

}else if(lottos[i] === 0){
zeroCount++;
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기서부터 모든 케이스를 적용하는데, 이 코드를 축약시키려면 문제에서 요구하는 최대 등수와 최소 등수만 구하면 가능합니다!
count를 맞춘 개수로 하신 것 같고, zeroCount를 0의 개수로 하신 것 같은데 이 두개를 이용해서 if문 또는 for문으로 최대 등수와 최소 등수를 구해보세요 👍

@2swo 2swo self-requested a review December 23, 2023 01:36
let twoTester = [2, 1, 2, 3, 2, 4, 2, 5];
let threeTester = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5];
let count = [0, 0, 0];

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이것도 다시 한번 풀어봤으면 하는 문제입니다!

  1. % -> 나머지 연산자. 왜 %5, %8, %10을 하는지 떠올리세요
  2. let count = [0, 0, 0]처럼, tester 또한 3개의 배열로 묶을 수 있습니다
  3. Math.max함수를 기억하세요

Comment on lines +3 to +9
let step1 = /[A-Z]/g; //대문자 찾기
let step2 = /[^a-z0-9-_.]/g; //소문자, 숫자, 빼기, 밑줄, 마침표를 제외한 문자 제거
let step3 = /\.+/g; //마침표 2개 이상이면 1개로 고정
let step4_1 = /^[\.]/g; //첫 번째 마침표 찾기
let step4_2And6 = /[\.]$/g; //마지막 마침표 찾기
let step5 = /\s/g; //공백 찾기
let step7 = /.$/g; //마지막 문자 찾기
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let step1 = /[A-Z]/g; //대문자 찾기
let step2 = /[^a-z0-9-_.]/g; //소문자, 숫자, 빼기, 밑줄, 마침표를 제외한 문자 제거
let step3 = /\.+/g; //마침표 2개 이상이면 1개로 고정
let step4_1 = /^[\.]/g; //첫 번째 마침표 찾기
let step4_2And6 = /[\.]$/g; //마지막 마침표 찾기
let step5 = /\s/g; //공백 찾기
let step7 = /.$/g; //마지막 문자 찾기
let step1 = /[A-Z]/g, //대문자 찾기
step2 = /[^a-z0-9-_.]/g, //소문자, 숫자, 빼기, 밑줄, 마침표를 제외한 문자 제거
step3 = /\.+/g, //마침표 2개 이상이면 1개로 고정
step4_1 = /^[\.]/g, //첫 번째 마침표 찾기
step4_2And6 = /[\.]$/g, //마지막 마침표 찾기
step5 = /\s/g, //공백 찾기
step7 = /.$/g; //마지막 문자 찾기

let을 한 번만 사용하고 선언이 가능합니다.

@@ -0,0 +1,20 @@
function solution(N, stages) {
let answer = [];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let answer = [];
const answer = [];

배열이나 객체 같은 경우 배열, 객체 그 차제의 값이 바뀌는게 아니라면, 안에있는 값을 참조해서 변경하는 것이기 때문에 const로 선언하는게 좋습니다.

원시값과, 참조값에 대해서 공부해 보시면 좋을 듯 싶습니다.
참고사이트

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants