正剧开始:
星历2016年01月16日 09:46:47, 银河系厄尔斯星球中华帝国江南行省。
[工程师阿伟]正在和[机器小伟]一起观看练气期第五层功法的最后部分:总复习。
<span style="font-size:18px;">>>> 1/2 1/2 1/3 1/3 1/3 1/4 1/4 1/4 1/4 1/5 1/5 1/5 1/5 1/5 1/6 1/6 1/6 1/6 1/6 1/6 1/7 1/7 1/7 1/7 1/7 1/7 1/7 1/8 1/8 1/8 1/8 1/8 1/8 1/8 1/8 1/9 1/9 1/9 1/9 1/9 1/9 1/9 1/9 1/9 #分数墙 def fractionWall(): for i in range(2,10): for j in range(i): print(Fraction(1, i), end=' '); print(''); >>> 1/2 1/2 1/3 1/3 1/3 1/4 1/4 1/4 1/4 1/5 1/5 1/5 1/5 1/5 1/6 1/6 1/6 1/6 1/6 1/6 1/7 1/7 1/7 1/7 1/7 1/7 1/7 1/8 1/8 1/8 1/8 1/8 1/8 1/8 1/8 1/9 1/9 1/9 1/9 1/9 1/9 1/9 1/9 1/9 #分数墙 def fractionWall(): lineLen = 4*9; for i in range(2,10): wordWidth = lineLen//i; for j in range(i): print('{0:^{width}}'.format(Fraction(1, i), width=wordWidth), end=' '); print('');</span>
看到了这道题,小伟想到了一个问题:怎样知道从A到D怎么走呢?
阿伟给小伟做了一个分离线段的工具,下面看看这个工具能不能解决好这些问题。
<span style="font-size:18px;">### # @usage 线段分离系列工具 # @author mw # @date 2016年01月16日 星期六 11:17:57 # @param # @return # ### #字符在数组中与否的判断 def charInArray(array, c): length = len(array); for i in range(length): if c == array[i]: return True; return False; #有效的路径字符串 def validPathString(array, s): length = len(s); for i in range(length): if charInArray(array, s[i]) == False: return False; return True; #去除相邻的相同字符 def removeAdjacentSameChar(s): sNew = s[0]; length = len(s); for i in range(length): if sNew[-1] != s[i]: sNew+=s[i]; return sNew; #分离路径表达式 def dividePath(array, s): s = removeAdjacentSameChar(s); if validPathString(array, s) == False: return ''; length = len(s); expression = ''; expression += s + ' = '; for i in range(length-1): pos1 = array.index(s[i]); pos2 = array.index(s[i+1]); if (pos1 > pos2): direction = -1; elif (pos1 < pos2): direction = 1; else: direction = 0; while pos1 != pos2: expression += array[pos1]+array[pos1+direction]; if (i >= length-2 and pos1+direction == pos2): pass; else: expression+= ' + '; pos1 += direction; print(expression); return expression;</span>
小伟先看的是给的信息:
<span style="font-size:18px;">>>> AC = AB + BC -小刚家--学校- = -小刚家--书店- + -书店--学校- AD = AB + BC + CD -小刚家--超市- = -小刚家--书店- + -书店--学校- + -学校--超市- BD = BC + CD -书店--超市- = -书店--学校- + -学校--超市- if __name__ == '__main__': dictionary = dict({'A':'小刚家', 'B':'书店', 'C':'学校', 'D':'超市'}); array = ['A', 'B', 'C', 'D']; #小刚家到学校 s = 'AC'; expression = dividePath(array, s); print(expression); expression = expression.replace('A', '-小刚家-'); expression = expression.replace('B', '-书店-'); expression = expression.replace('C', '-学校-'); expression = expression.replace('D', '-超市-'); print(expression); #小刚家到超市 s = 'AD'; expression = dividePath(array, s); print(expression); expression = expression.replace('A', '-小刚家-'); expression = expression.replace('B', '-书店-'); expression = expression.replace('C', '-学校-'); expression = expression.replace('D', '-超市-'); print(expression); #书店到超市 s = 'BD'; expression = dividePath(array, s); print(expression); expression = expression.replace('A', '-小刚家-'); expression = expression.replace('B', '-书店-'); expression = expression.replace('C', '-学校-'); expression = expression.replace('D', '-超市-'); print(expression); </span>
<span style="font-size:18px;">>>> 2500 = AB + BC , 6000 = AB + BC + CD , 4000 = BC + CD if __name__ == '__main__': s = 'AC = AB + BC , AD = AB + BC + CD , BD = BC + CD'; s = s.replace('AC', '2500'); s = s.replace('AD', '6000'); s = s.replace('BD', '4000'); print(s);</span>
接着小伟又看到了一个简单分数的计算问题,上一层功法修炼时,阿伟做了一个计算分数的工具,
但是那个工具太简单,后来阿伟就进行了改进,现在可以计算任何的表达式了:
<span style="font-size:18px;">#输入 0.3 -0.3 0.9-0.6 -0.9-0.6 (-0.9-0.6) 0.9-0.3/2 (0.9-0.3)/2 -(0.9-0.3/2)/2 1/7+2/5 2*2*5/15 2*5/15+5 2*(-5/15)+5/7 #输出 0.3 = Fraction('0.3') = 3/10, -0.3 = -Fraction('0.3') = -3/10, 0.9-0.6 = Fraction('0.9')-Fraction('0.6') = 3/10, -0.9-0.6 = -Fraction('0.9')-Fraction('0.6') = -3/2, (-0.9-0.6) = (-Fraction('0.9')-Fraction('0.6')) = -3/2, 0.9-0.3/2 = Fraction('0.9')-Fraction('0.3')/Fraction('2') = 3/4, (0.9-0.3)/2 = (Fraction('0.9')-Fraction('0.3'))/Fraction('2') = 3/10, -(0.9-0.3/2)/2 = -(Fraction('0.9')-Fraction('0.3')/Fraction('2'))/Fraction('2') = -3/8, 1/7+2/5 = Fraction('1')/Fraction('7')+Fraction('2')/Fraction('5') = 19/35, 2*2*5/15 = Fraction('2')*Fraction('2')*Fraction('5')/Fraction('15') = 4/3, 2*5/15+5 = Fraction('2')*Fraction('5')/Fraction('15')+Fraction('5') = 17/3, 2*(-5/15)+5/7 = Fraction('2')*(-Fraction('5')/Fraction('15'))+Fraction('5')/Fraction('7') = 1/21, ### # @usage 分数运算 # @author mw # @date 2016年01月15日 星期五 09:04:31 # @param # @return # ### def fractionCalc(): # 可以将任意的四则运算表达式用分数进行计算, # 而不是将浮点结果简单转化成分数。 fin = open('input.txt'); fout= open('output.txt', 'a'); for line in fin.readlines(): if line[-1] == '\n': line = line[:-1]; if line == '': continue; elif line.startswith('#'): print(line); fout.write(line+'\n'); else: lines = line.split(sep=' '); for i in range(len(lines)): if lines[i]=='': continue; #消除空格 lines[i] = lines[i].replace(' ', ''); expCount = len(lines[i]); expression = ''; #分割操作数 operands = []; stmp = ''; indexBeg = 0; indexEnd = 0; for j in range(expCount): if isOps(lines[i][j]): if stmp != '': operands.append(stmp); stmp = ''; else: stmp+=lines[i][j]; if stmp != '': operands.append(stmp); print(operands); #操作数修饰 operandCount = len(operands); operandString = []; #数字如1/7要转化成Fraction('1/7')这种样式才可以算出正确结果,引号不可以少掉。 for j in range(operandCount): stmp = ''; stmp = 'Fraction(\''+operands[j]+'\')'; operandString.append(stmp); #组装新表达式 typechange = 1; index = 0; expression = ''; for j in range(expCount): #操作符直接添加 if isOps(lines[i][j]): expression+=lines[i][j]; else: if j > 0 and typechange == 0 and isOps(lines[i][j-1]): typechange = 1; #从操作数序列中选择对应操作数。 if typechange == 1: if index > len(operandString): break; expression += operandString[index]; index+=1; typechange = 0; #表达式结果的运算打印 s = '{0} = {1} = {2}'.format(lines[i],expression, eval(expression)); print(s, end=', '); fout.write(s + ', '); print('\n'); fout.write('\n'); fout.close(); fin.close(); def isOps(c): if c == '+' or c == '-' or c == '/' or c == '*' or c == '(' or c ==')': return 1; else: return 0;</span>
<span style="font-size:18px;">#输入 3/5+1/5 5/7-2/7 4/9+1/9 6/6-3/6 1-2/3 1/6+5/6 7/8-4/8 1/4+3/4 #输出 3/5+1/5 = Fraction('3')/Fraction('5')+Fraction('1')/Fraction('5') = 4/5, 5/7-2/7 = Fraction('5')/Fraction('7')-Fraction('2')/Fraction('7') = 3/7, 4/9+1/9 = Fraction('4')/Fraction('9')+Fraction('1')/Fraction('9') = 5/9, 6/6-3/6 = Fraction('6')/Fraction('6')-Fraction('3')/Fraction('6') = 1/2, 1-2/3 = Fraction('1')-Fraction('2')/Fraction('3') = 1/3, 1/6+5/6 = Fraction('1')/Fraction('6')+Fraction('5')/Fraction('6') = 1, 7/8-4/8 = Fraction('7')/Fraction('8')-Fraction('4')/Fraction('8') = 3/8, 1/4+3/4 = Fraction('1')/Fraction('4')+Fraction('3')/Fraction('4') = 1, </span>
<span style="font-size:18px;">function myDraw() { var config = new PlotConfiguration(); config.init(); config.setPreference(); //config.setSector(1,1,1,1); //config.axis2D(0, 0, 180); var vertExp = new VerticalExpression(); var row = 1, col=3, width = 600, height = 400; var r = 20; var x = 0, y=20; quest = [[563,344],[928,687], [889, 142]]; len = quest.length; for (var i = 0; i < row; i++) { for (var j=0; j < col; j++) { x = width/col*(j+1); y = 20 + height/row*i; if (i*col+j != 1 ){ vertExp.add(quest[i*col+j][0], quest[i*col+j][1], x, y, r); } else { vertExp.sub(quest[i*col+j][0], quest[i*col+j][1], x, y, r); } } } }</span>
这里是小伟得到的功法:
从现在开始,小伟就要进入练气期第六层的修炼了。
本节到此结束,欲知后事如何,请看下回分解。