这与音乐:这和弦里有什么?相反,它是在给定的和弦中打印音符。这一次输入是一个和弦中的音符列表,你的任务是输出它是哪一个和弦。
您的程序应该支持以下三进和弦。用根C给出例子。与其他根的和弦是相同的和弦,所有的音都是旋转的,所以C将成为那个根音,例如Dmaj由D,F#和A组成。
C C#D D#E F F#G G#A A#B
Db Eb Gb Ab Bb
Cmaj C E G
Cm C D# G
Caug C E G#
Cdim C D# F#
Csus4 C F G
Csus2 C D G请注意,Caug与Eaug和G#aug相同,Csus4与Fsus2相同。你可以输出任何一个,但是如果你把它们全部输出,就会有一个额外的结果。
奖金的第七和弦列于下表:
C C#D D#E F F#G G#A A#B
Db Eb Gb Ab Bb
C7 C E G A#
Cm7 C D# G A#
Cmmaj7 C D# G B
Cmaj7 C E G B
Caug7 C E G# A#
Cdim7 C D# F# AC作为C大调,您应该在这两种情况下都添加一个人类可读的前缀,以区分单个音符的和弦。C D# G输出:Cm。C Eb G输出:Cm。C Eb F#输出:Cdim。F A C#输出:Faug,Aaug,C#aug,Dbaug或Faug Aaug C#aug,Faug Aaug Dbaug按任何顺序排列。F D F F F F A A F输出:Dm。C D输出:C D。发布于 2015-01-08 00:26:04
=Q{cQdL+x"C D EF G A B"hb&tlbt%hx" #b"eb3FZQJx[188 212 199 213 200 224 2555 2411 2412 2556 2567 2398)u+*G12hHSm%-dyZ12mykQ0IhJ+Z@c"sus2 maj dim aug m sus4 7 m7 mmaj7 maj7 aug7 dim7"dJ=T0;ITQ不太满意。用硬编码的和弦。
在这里试试:Pyth编译器/执行器。禁用调试模式并使用"C D# G"作为输入。
首先是一些准备工作:
=Q{cQd
cQd split chord into notes "C D# G" -> ["C", "D#", "G"]
{ set (eliminate duplicates)
=Q Q = ...然后,将注释转换为整数的函数。
L+x"C D EF G A B"hb&tlbt%hx" #b"eb3
defines a function g(b),
returns the sum of
index of "D" in "C D EF G A B"
and the index of "#" in " #b"
(if b than use -1 instead of 2)然后,对每个音符,移动coord并在一张表中查找它。
FZQJx[188 ...)u+*G12hHSm%-dyZ12mykQ0IhJ+Z@c"sus2 ..."dJ=T0;ITQ
implicit T=10
FZQ for note Z in chord Q:
mykQ map each note of Q to it's integer value
m%-dyZ12 shift it by the integer value of Z modulo 12
S sort it
u+*G12hH 0 convert it to an integer in base 12
x[188 ...) look it up in the list (-1 if not in list)
J and store the value in J
IhJ if J>=0:
+Z@c"sus2 ..."dJ print the note Z and the chord in the list
=T0 and set T=0
; end loop
ITQ if T:print chord (chord not in list)发布于 2015-01-08 14:27:25
第一次尝试一个稍长一点的高尔夫,所以我可能错过了一些明显的技巧。
def f(s,N="C D EF G A B",r=range,u=1):
for i in r(12):
for t in r(12):
if(set((N.find(n[0])+" #".find(n[1:]))%12for n in s.split())==set(map(lambda n:(int(n,16)+i)%12,"0"+"47037048036057027047A37A37B47B48A369"[3*t:3*t+3]))):print(N[i],N[i+1]+"b")[N[i]==" "]+"M - + o sus4 sus2 7 -7 -M7 M7 +7 o7".split()[t];u=0
if(u):print s评论:
>>> f("C D# G")
C-
>>> f("C Eb G")
C-
>>> f("C Eb F#")
Co
>>> f("F A C#")
Db+
F+
A+
>>> f("F D F F F F A A F")
D-
>>> f("C D")
C D
>>> f("C Eb Gb A")
Co7
Ebo7
Gbo7
Ao7https://codegolf.stackexchange.com/questions/44310
复制相似问题