|
2 | 2 | //완벽한 정답이 아닙니다.
|
3 | 3 | //정답 1 - codeisneverodd
|
4 | 4 | function solution(lottos, win_nums) {
|
5 | | - const zeroCount = lottos.filter(e => e === 0).length |
6 | | - const matchCount = win_nums.filter(e => lottos.includes(e)).length |
7 | | - const matchToRank = [6, 6, 5, 4, 3, 2, 1] |
8 | | - const lowRank = matchToRank[matchCount] |
9 | | - const highRank = zeroCount === 6 ? 1 : matchToRank[matchCount + zeroCount] |
10 | | - |
11 | | - var answer = [highRank, lowRank]; |
12 | | - return answer; |
| 5 | + const zeroCount = lottos.filter((e) => e === 0).length |
| 6 | + const matchCount = win_nums.filter((e) => lottos.includes(e)).length |
| 7 | + const matchToRank = [6, 6, 5, 4, 3, 2, 1] |
| 8 | + const lowRank = matchToRank[matchCount] |
| 9 | + const highRank = zeroCount === 6 ? 1 : matchToRank[matchCount + zeroCount] |
| 10 | + |
| 11 | + var answer = [highRank, lowRank] |
| 12 | + return answer |
13 | 13 | }
|
14 | 14 |
|
15 | 15 | //정답 2 - codeisneverodd
|
16 | 16 | function solution(lottos, win_nums) {
|
17 | | - // 0이 없는 경우 > 최저 순위 == 최고 순위 |
18 | | - // 0이 있는 경우 > 모두 0인경우 > 1위 |
19 | | - // > 0이 아닌 수가 있는 경우 > 최저 순위 - (0의 개수) = 최고순위 |
20 | | - // 0이 있는 경우 0만 중복이 가능하므로, 0의 개수를 (배열 길이 - 집합 길이 + 1)를 통해 구함. |
21 | | - // 순위는 7 - hit |
22 | | - // 최종적으로 7위인 경우 6위로 변경 |
23 | | - var answer = [] |
24 | | - if (lottos.indexOf(0) === -1) { |
25 | | - answer[0] = answer[1] = 7 - hit(lottos, win_nums) |
26 | | - } else { |
27 | | - const zeroCount = lottos.length - [...new Set(lottos)].length + 1 |
28 | | - answer[1] = 7 - hit(lottos, win_nums) |
29 | | - zeroCount === 6 ? answer[0] = 1 : answer[0] = answer[1] - zeroCount |
30 | | - } |
31 | | - for (let i = 0; i < 2; i++) { |
32 | | - answer[i] >= 7 ? answer[i] = 6 : null |
33 | | - } |
34 | | - return answer; |
| 17 | + // 0이 없는 경우 > 최저 순위 == 최고 순위 |
| 18 | + // 0이 있는 경우 > 모두 0인경우 > 1위 |
| 19 | + // > 0이 아닌 수가 있는 경우 > 최저 순위 - (0의 개수) = 최고순위 |
| 20 | + // 0이 있는 경우 0만 중복이 가능하므로, 0의 개수를 (배열 길이 - 집합 길이 + 1)를 통해 구함. |
| 21 | + // 순위는 7 - hit |
| 22 | + // 최종적으로 7위인 경우 6위로 변경 |
| 23 | + var answer = [] |
| 24 | + if (lottos.indexOf(0) === -1) { |
| 25 | + answer[0] = answer[1] = 7 - hit(lottos, win_nums) |
| 26 | + } else { |
| 27 | + const zeroCount = lottos.length - [...new Set(lottos)].length + 1 |
| 28 | + answer[1] = 7 - hit(lottos, win_nums) |
| 29 | + zeroCount === 6 ? (answer[0] = 1) : (answer[0] = answer[1] - zeroCount) |
| 30 | + } |
| 31 | + for (let i = 0; i < 2; i++) { |
| 32 | + answer[i] >= 7 ? (answer[i] = 6) : null |
| 33 | + } |
| 34 | + return answer |
35 | 35 | }
|
36 | 36 |
|
37 | 37 | function hit(lottos, win_nums) {
|
38 | | - let result = 0 |
39 | | - lottos.forEach((element) => { |
40 | | - win_nums.indexOf(element) === -1 ? null : result += 1 |
41 | | - }) |
42 | | - return result |
| 38 | + let result = 0 |
| 39 | + lottos.forEach((element) => { |
| 40 | + win_nums.indexOf(element) === -1 ? null : (result += 1) |
| 41 | + }) |
| 42 | + return result |
43 | 43 | }
|
44 | 44 |
|
45 | 45 | // 정답 3 - jaewon1676
|
46 | 46 | function solution(lottos, win_nums) {
|
47 | | - var answer = []; |
48 | | - const correct = lottos.filter(lotto => win_nums.includes(lotto)).length; |
49 | | - // lottos배열을 순회하며 당첨배열에 있는 수를 return 하고 총 개수를 correct에 저장 |
50 | | - |
51 | | - const zeros = lottos.filter(lotto => lotto === 0).length; |
52 | | - // lottos배열을 순회하며 0인 총 개수를 zeros에 저장 |
53 | | - |
54 | | - let min = 7-correct >= 6 ? 6 : 7-correct; |
55 | | - |
56 | | - let max = min-zeros < 1 ? 1 : min-zeros; |
57 | | - |
58 | | - answer = [max,min] |
59 | | - |
60 | | - return answer; |
| 47 | + var answer = [] |
| 48 | + const correct = lottos.filter((lotto) => win_nums.includes(lotto)).length |
| 49 | + // lottos배열을 순회하며 당첨배열에 있는 수를 return 하고 총 개수를 correct에 저장 |
| 50 | + |
| 51 | + const zeros = lottos.filter((lotto) => lotto === 0).length |
| 52 | + // lottos배열을 순회하며 0인 총 개수를 zeros에 저장 |
| 53 | + |
| 54 | + let min = 7-correct >= 6 ? 6 : 7-correct |
| 55 | + |
| 56 | + let max = min-zeros < 1 ? 1 : min-zeros |
| 57 | + |
| 58 | + answer = [max,min] |
| 59 | + |
| 60 | + return answer |
61 | 61 | }
|
62 | 62 |
|
63 | 63 | // 정답 4 - jaewon1676
|
64 | 64 | function solution(lottos, win_nums) {
|
65 | | - var answer = []; |
66 | | - let max = 7; |
67 | | - let min = 7; |
68 | | - console.log(lottos) |
69 | | - console.log(win_nums) |
70 | | - for(let i=0; i<6; i++){ |
71 | | - if (lottos.includes(win_nums[i])){ |
72 | | - max--; |
73 | | - } |
74 | | - } |
75 | | - min = max; |
76 | | - for(let i=0; i<6; i++){ |
77 | | - if (lottos[i] == 0) min--; |
| 65 | + var answer = [] |
| 66 | + let max = 7 |
| 67 | + let min = 7 |
| 68 | + console.log(lottos) |
| 69 | + console.log(win_nums) |
| 70 | + for (let i = 0; i < 6; i++) { |
| 71 | + if (lottos.includes(win_nums[i])) { |
| 72 | + max-- |
78 | 73 | }
|
79 | | - if (max == 7) max = 6 |
80 | | - if (min == 7) min = 6 |
81 | | - answer = [min, max] |
82 | | - return answer; |
| 74 | + } |
| 75 | + min = max |
| 76 | + for (let i = 0; i < 6; i++) { |
| 77 | + if (lottos[i] == 0) min-- |
| 78 | + } |
| 79 | + if (max == 7) max = 6 |
| 80 | + if (min == 7) min = 6 |
| 81 | + answer = [min, max] |
| 82 | + return answer |
83 | 83 | }
|
84 | 84 |
|
85 | 85 | //정답 5 - yongchanson
|
86 | 86 | function solution(lottos, win_nums) {
|
87 | | - //최고당첨개수 : maxPoint + basicPoint |
88 | | - //최저당첨개수 : basicPoint |
89 | | - |
90 | | - let basicPoint = 0; |
91 | | - let maxPoint = 0; |
92 | | - let answer = []; |
93 | | - |
94 | | - lottos.forEach(function (lottos_item) { |
95 | | - win_nums.forEach(function (win_nums_item) { |
96 | | - if(lottos_item==win_nums_item) { |
97 | | - basicPoint++; |
98 | | - } |
99 | | - }) |
100 | | - }) |
| 87 | + //최고당첨개수 : maxPoint + basicPoint |
| 88 | + //최저당첨개수 : basicPoint |
| 89 | + |
| 90 | + let basicPoint = 0 |
| 91 | + let maxPoint = 0 |
| 92 | + let answer = [] |
101 | 93 |
|
102 | | - lottos.forEach(function (item) { |
103 | | - if(item==0) { |
104 | | - maxPoint++; |
105 | | - } |
| 94 | + lottos.forEach(function (lottos_item) { |
| 95 | + win_nums.forEach(function (win_nums_item) { |
| 96 | + if (lottos_item == win_nums_item) { |
| 97 | + basicPoint++ |
| 98 | + } |
106 | 99 | })
|
| 100 | + }) |
| 101 | + |
| 102 | + lottos.forEach(function (item) { |
| 103 | + if (item == 0) { |
| 104 | + maxPoint++ |
| 105 | + } |
| 106 | + }) |
| 107 | + |
| 108 | + maxPoint + basicPoint >= 2 |
| 109 | + ? answer.push(7 - maxPoint - basicPoint) |
| 110 | + : answer.push(6) |
| 111 | + basicPoint >= 2 ? answer.push(7 - basicPoint) : answer.push(6) |
107 | 112 |
|
108 | | - maxPoint+basicPoint >= 2 ? answer.push(7- maxPoint - basicPoint) : answer.push(6); |
109 | | - basicPoint >= 2 ? answer.push(7- basicPoint) : answer.push(6); |
| 113 | + return answer |
| 114 | +} |
| 115 | + |
| 116 | +//정답 6 - prove-ability |
| 117 | +function solution(lottos, win_nums) { |
| 118 | + // 맞춘 수와 0의 갯수 활용할 변수 0으로 초기화 |
| 119 | + let winCount = 0 |
| 120 | + let zeroCount = 0 |
| 121 | + // 내 로또 번호 하니씩 접근 |
| 122 | + lottos.forEach((num) => { |
| 123 | + // 번호가 0이 아니고 당첨 번호라면 |
| 124 | + if (num !== 0 && win_nums.includes(num)) { |
| 125 | + winCount++ |
| 126 | + // 번호가 0이라면 |
| 127 | + } else if (num === 0) { |
| 128 | + zeroCount++ |
| 129 | + } |
| 130 | + }) |
| 131 | + // 일치한 수와 등수는 반비례하기 때문에 빼기 7 |
| 132 | + // 이때 등수를 벗어나면 낙첨(6) 으로 고정 |
| 133 | + let max = 7 - (winCount + zeroCount) |
| 134 | + if (max > 5) max = 6 |
| 135 | + let min = 7 - winCount |
| 136 | + if (min > 5) min = 6 |
110 | 137 |
|
111 | | - return answer; |
| 138 | + return [max,min] |
112 | 139 | }
|
0 commit comments