Giter Club home page Giter Club logo

my-study's Introduction

Hi there 👋

my-study's People

Contributors

claygao avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

my-study's Issues

[面試題] 幾雙襪子 ?

測驗連結

感謝 RZ 提供的面試題型

const socketsTypeA = [20, 20] // 1
const socketsTypeB = [2, 20, 3, 40, 50, 50, 60, 70] // 1
const socketsTypeC = [25, 281, 306, 4, 250, 25, 771, 306, 306, 25, 4, 25, 281] // 5


function countDoubleSocketsNumber(sockets) {
    let doubleSocketsNumber = 0, tmp = 0, isCompared = null, compareRecord = [], socketsTypeNumber = []
    sockets.forEach(socket => {
        // isCompared: 是一個 Boolean,藉由 compareRecord 來判斷之前是否比對過該種類的襪子,若 isCompared 為 true,則會在下列 if 跳出
        isCompared = compareRecord.find(element => { return element === socket })
        if (isCompared) return;
        sockets.forEach(otherSocket=> {
            // 該襪子種類若等於其他的襪子,則 tmp +1 
            tmp += (socket === otherSocket)   
        })
        // 將比對紀錄納入 compareRecord
        compareRecord.push(socket)
        // 將該種類襪子總數納入 socketsTypeNumber 中,比如說比對後只有 1 隻,就放入 1
        if(tmp) socketsTypeNumber.push(tmp)
        tmp = 0
    })
    // 一個 index 代表一種,將每種的襪子隻數 / 2 取整數並加總,就可以算出總共幾雙
    socketsTypeNumber.forEach(element => {
        doubleSocketsNumber += Math.floor(element / 2)
    })
    console.log(doubleSocketsNumber) 
}

countDoubleSocketsNumber(socketsTypeA) 
countDoubleSocketsNumber(socketsTypeB)
countDoubleSocketsNumber(socketsTypeC)

/* 原本寫的
for (let i = 0; i < socketsNumber ; i++) {
    isCompared = compareRecord.find(element => {
        return element === sockets[i]
    })
    for(let j = 0; j < socketsNumber ; j++) {
        if(!isCompared && sockets[i] === sockets[j]) {
            tmp++
        }
    }
    compareRecord.push(sockets[i])
    if(tmp) socketsTypeNumber.push(tmp)
    tmp = 0
}
//console.log(socketsTypeNumber)
socketsTypeNumber.forEach(element => {
    doubleSocketsNumber += Math.floor(element / 2)
})
return doubleSocketsNumber
*/

太久沒解類似題目,花了兩個小時解題,不太及格

思路是先算出單種類襪子的數量並放到一個新陣列,比如說給我隨機一個亂數陣列,我可以就每種襪子各有幾隻,最後直接將每種襪子的數量除以二,算出總共幾雙

比如說 [A, A, B, B, B, C]

新陣列就是 [2, 3 ,1]

襪子共幾雙就是 2 整除 2 等於 1,3 整除 2 等於 1 ,1 整除 2 等於 0

1 + 1 等於 2,共兩雙

另外有一個名為 comparedrecord (比對紀錄的陣列),會先檢查如果我要比對的種類已經有在這個陣列裡面,就不比對,所以不用對測資做排序

另外就是將上述程式碼放到測驗網站是錯誤的,目前不知道原因

應該不是最好解法,上述解法僅供紀錄

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.