カスタムメイドの習慣
コード2020日6の出現
タスク:Xのどこに解決する.
第1部
X = the sum of each group's count of unique questions answered 'yes'
第2部X = the sum of each group's count of shared questions answered 'yes'
例入力
abc
a
b
c
ab
ac
a
a
a
a
b
表す24579172の質問は、「はい」と答えました
につき一つの乗客による
空のライン
第1部
それはセット内のユニークな値を数えるように単純ですか?
与えられたグループ
abcx
abcy
abcz
文字列で表される'abcx\nabcy\nabcz'
を削除する'abcxabcyabcz'
文字の配列に分割します.['a','b','c','x','a','b','c','y','a','b','c','z']
各文字は、ユニークな値のセットに追加abcxabcyabcz
++++xxx+xxx+
abcx y z
abcxyz
集合のサイズを返す6
はい、それは簡単でした!
Split the input at each double-new-line character to generate an array of strings
For each string, accumulate a sum - starting from 0
Add to the sum the result of these operations:
Remove any new-line characters in the current string
Split the string into an array of characters
For each character, accumulate a unique set of values - starting empty
Attempt to add the current character to the set
Return the set containing all unique characters added
Return the size of the unique set
Return the sum of all set sizes
このアルゴリズムはJavaScriptで書かれていますinput
.split('\n\n')
.reduce((a,c) => a += c
.replaceAll('\n','')
.split('')
.reduce((a,c) => a.add(c), new Set()).size
,0)
ここでは、アルゴリズムの可視化です第2部
リファクタリングパート1
Split the input at each double-new-line character to generate an array of strings
For each string, accumulate a sum - starting from 0
Add to the sum the result of these operations:
Store as the variable, groups:
Split each string at the new-line character to create an array of strings
Split each string into an array of characters
Checking only the first array in groups:
For each character, accumulate a unique set of values - starting empty
If the character exists in each of the arrays inside groups
Attempt to add the current character to the set
Return the set containing all unique characters added
Return the size of the unique set
Return the sum of all set sizes
JavaScriptで書かれたアルゴリズムはこちらstream
.split('\n\n')
.reduce(
(a1, c1) => {
let groups = c1
.split('\n')
.map(el => el.split(''))
return a1 += groups[0]
.reduce(
(a2,c2) => groups.every(i => i.includes(c2))
? a2.add(c2)
: a2, new Set()
)
.size
}, 0)
私のアルゴリズムの視覚化は、ここにありますそのパズルは簡単だった.
どれだけ私が学んだかの証明です.
私は最初にこのパズルをスキップしたことを思い出します.
自己改善と達成の実証可能な感覚のためのhooray!
Reference
この問題について(カスタムメイドの習慣), 我々は、より多くの情報をここで見つけました https://dev.to/rmion/custom-customs-2mjeテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol