[プログラマー]楽透の最高と最低レベル(JavaScript、Python)


https://programmers.co.kr/learn/courses/30/lessons/77484

問題を理解する


2つの配列を比較する問題->同じものはいくつありますか?=1つ
私の配列の中で0の個数はいくらですか?
最下位-0すべて異なる=>同じ:a個
最高順位-0すべて同じ=>同じ:a個+0個
同じように多ければ多いほど順位が高くなります.

問題を解く

1)内循環配列と当選配列の比較->ダブルforゲート=>同じ場合count+1
2)内部配列.count(0)で0の個数を知る
3)最高順位=count+0個
4)最下位=count

JavaScript

function solution(lottos, win_nums) {
    const rank = [6, 6, 5, 4, 3, 2, 1]
    let num_0 = 0;
    let count = 0;
    for ( i = 0 ; i < lottos.length ; i++) {
        if (lottos[i] === 0) num_0 ++;
        
        for (j = 0 ; j < win_nums.length ; j ++) {
            if (lottos[i] === win_nums[j]) count ++;
        }
    }
    
    const maxRank = rank[count + num_0];
    const minRank = rank[count];
    return [maxRank, minRank]
}
const lottos = [44, 1, 0, 0, 31, 25];
const win_nums = [31, 10, 45, 1, 6, 19];
console.log(solution(lottos, win_nums))
  • 二重ゲート比較配列因子
  • 事前に
  • rankを並べて順番を決めました
  • パイ

    def solution(lottos, win):
        count = 0
        num_0 = lottos.count(0)
        rank = [6, 6, 5, 4, 3, 2, 1]
        for x in lottos:
            if x in win:
                count += 1
                
        high_score = rank[count + num_0]
        low_score = rank[count]
        return [high_score, low_score]
    
    lottos = [44, 1, 0, 0, 31, 25]
    win_list = [31, 10, 45, 1, 6, 19]
    print(solution(lottos, win_list))
  • Pythonはfor-in文を用いて配列中の因子を比較した.
  • .このパラメータの個数はcount()関数で知ることができる.(文字列と配列が使用可能)
    ex) lottos.count(0)