Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Ghost Detect by Roronoamonn
"""
本プログラムはsignalで与えられた非負整数の2進数表現において"1"のみで構成された部分文字列の個数が3つでありそれら部分文字列の長さが
全て同じかどうかを調べ同じならTrueをそうでないならFalseを返すものである。
list(filter(lambda x: x!="",bin(signal)[2:].split("0")))でsignalの2進数表現を"0"をデリミタとして区切りそれらのうち""を取り除いたもの、
即ち"1"のみで構成された部分文字列を抽出しそれを要素とするリストを生成しbitsに代入する。
len(bits)==3("1"のみの部分文字列の個数が3つかどうか) and len(set(len(a) for a in bits))==1("1"のみの部分文字列の長さがすべて同じかどうか)
のbool計算の結果を返す。
cf: signal=1587
bits=list(filter(lambda x: x!="",["11","","","11","","11"]))=["11","11","11"]
len(bits)==3 and len(set(len(a) for a in bits))==1 -> 3==3 and 1==1 -> True(返り値)
"""
def recognize(signal: int) -> bool:
bits=list(filter(lambda x: x!="",bin(signal)[2:].split("0")))
return len(bits)==3 and len(set(len(a) for a in bits))==1
print("Example:")
print(recognize(21))
# These "asserts" are used for self-checking
assert recognize(21) == True
assert recognize(1587) == True
assert recognize(3687) == False
print("The mission is done! Click 'Check Solution' to earn rewards!")
Dec. 21, 2025