Skip to content

Instantly share code, notes, and snippets.

@dtinth
Created January 23, 2017 18:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dtinth/586c4f61fb8ccbbaf7b3fb2c4369a021 to your computer and use it in GitHub Desktop.
Save dtinth/586c4f61fb8ccbbaf7b3fb2c4369a021 to your computer and use it in GitHub Desktop.

โจทย์หลักที่ต้องการแก้

เลขสามหลัก.filter(n => เงื่อนไข.every(ใช้ได้กับ(n)))

แก้ปัญหาย่อย

  • เลขสามหลัก

    Array(1e3).fill().map((x, i) => `000${i}`.slice(-3))
  • เงื่อนไข (อยู่ในรูปแบบ [ตัวเลขที่กำหนดให้, ถูก, ตรง])

    [['682', 1, 1], ['614', 0, 1], ['206', 0, 2], ['738', 0, 0], ['870', 0, 1]]
  • ใช้ได้กับ

    n => ([t, e, m]) => (count => e === count((v, i) => t[i] === v) && m === count(v => t.includes(v)))(f => n.split('').filter(f).length)

รวมโค้ด!

Array(1e3).fill().map((x, i) => `000${i}`.slice(-3)).filter(n => [['682', 1, 1], ['614', 0, 1], ['206', 0, 2], ['738', 0, 0], ['870', 0, 1]].every(([t, e, m]) => (count => e === count((v, i) => t[i] === v) && m === count(v => t.includes(v)))(f => n.split('').filter(f).length)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment