銆怟ata Daily 2銆慍alculating with Functions
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了銆怟ata Daily 2銆慍alculating with Functions相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/and' title='and'>and
example comment 5* not 鍒ゆ柇 lam 褰㈠弬 鑷韩 鍘熼锛?/h3>
This time we want to write calculations using functions and get the results. Let鈥榮 have a look at some examples:
seven(times(five())) # must return 35
four(plus(nine())) # must return 13
eight(minus(three())) # must return 5
six(divided_by(two())) # must return 3
Requirements:
- There must be a function for each number from 0 ("zero") to 9 ("nine")
- There must be a function for each of the following mathematical operations: plus, minus, times, dividedBy (
divided_by
in Ruby and Python)
- Each calculation consist of exactly one operation and two numbers
- The most outer function represents the left operand, the most inner function represents the right operand
- Divison should be integer division. For example, this should return
2
, not 2.666666...
:
eight(divided_by(three()))
-----------------------------------------------------------------------------------------------------------------------------
棰樼洰鐨勮姹傚氨鏄埄鐢ㄥ嚱鏁扮殑鏂瑰紡杩涜鍔犲噺涔橀櫎锛屽锛?/code>seven(times(five())) ------->鐩稿綋浜庯細7*5=35
瑙i杩囩▼
杩欓亾鐞嗕笉浼氬仛 o(鈺ワ箯鈺?o銆?br />
浠ヤ笅鏄竴浣嶇綉鍙嬬殑瑙e喅鎬濊矾锛?br />
def zero(a=鈥?/span>0鈥?/span>):
if a != 鈥?/span>0鈥?/span>:
if a[0]==鈥?/span>+鈥?/span>:
return 0+int(a[1])
if a[0]==鈥?/span>-鈥?/span>:
return 0-int(a[1])
if a[0]==鈥?/span>*鈥?/span>:
return 0*int(a[1])
if a[0]==鈥?/span>/鈥?/span>:
return int(0/int(a[1]))
else:
return 鈥?/span>0鈥?/span>
def one(a=鈥?/span>1鈥?/span>):
if a != 鈥?/span>1鈥?/span>:
if a[0]==鈥?/span>+鈥?/span>:
return 1+int(a[1])
if a[0]==鈥?/span>-鈥?/span>:
return 1-int(a[1])
if a[0]==鈥?/span>*鈥?/span>:
return 1*int(a[1])
if a[0]==鈥?/span>/鈥?/span>:
return int(1/int(a[1]))
else:
return 鈥?/span>1鈥?/span>
def two(a=鈥?/span>2鈥?/span>):
if a != 鈥?/span>2鈥?/span>:
if a[0]==鈥?/span>+鈥?/span>:
return 2+int(a[1])
if a[0]==鈥?/span>-鈥?/span>:
return 2-int(a[1])
if a[0]==鈥?/span>*鈥?/span>:
return 2*int(a[1])
if a[0]==鈥?/span>/鈥?/span>:
return int(2/int(a[1]))
else:
return 鈥?/span>2鈥?/span>
def three(a=鈥?/span>3鈥?/span>):
if a != 鈥?/span>3鈥?/span>:
if a[0]==鈥?/span>+鈥?/span>:
return 3+int(a[1])
if a[0]==鈥?/span>-鈥?/span>:
return 3-int(a[1])
if a[0]==鈥?/span>*鈥?/span>:
return 3*int(a[1])
if a[0]==鈥?/span>/鈥?/span>:
return int(3/int(a[1]))
else:
return 鈥?/span>3鈥?/span>
def four(a=鈥?/span>4鈥?/span>):
if a != 鈥?/span>4鈥?/span>:
if a[0]==鈥?/span>+鈥?/span>:
return 4+int(a[1])
if a[0]==鈥?/span>-鈥?/span>:
return 4-int(a[1])
if a[0]==鈥?/span>*鈥?/span>:
return 4*int(a[1])
if a[0]==鈥?/span>/鈥?/span>:
return int(4/int(a[1]))
else:
return 鈥?/span>4鈥?/span>
def five(a=鈥?/span>5鈥?/span>):
if a != 鈥?/span>5鈥?/span>:
if a[0]==鈥?/span>+鈥?/span>:
return 5+int(a[1])
if a[0]==鈥?/span>-鈥?/span>:
return 5-int(a[1])
if a[0]==鈥?/span>*鈥?/span>:
return 5*int(a[1])
if a[0]==鈥?/span>/鈥?/span>:
return int(5/int(a[1]))
else:
return 鈥?/span>5鈥?/span>
def six(a=鈥?/span>6鈥?/span>):
if a != 鈥?/span>6鈥?/span>:
if a[0]==鈥?/span>+鈥?/span>:
return 6+int(a[1])
if a[0]==鈥?/span>-鈥?/span>:
return 6-int(a[1])
if a[0]==鈥?/span>*鈥?/span>:
return 6*int(a[1])
if a[0]==鈥?/span>/鈥?/span>:
return int(6/int(a[1]))
else:
return 鈥?/span>6鈥?/span>
def seven(a=鈥?/span>7鈥?/span>):
if a != 鈥?/span>7鈥?/span>:
if a[0]==鈥?/span>+鈥?/span>:
return 7+int(a[1])
if a[0]==鈥?/span>-鈥?/span>:
return 7-int(a[1])
if a[0]==鈥?/span>*鈥?/span>:
return 7*int(a[1])
if a[0]==鈥?/span>/鈥?/span>:
return int(7/int(a[1]))
else:
return 鈥?/span>7鈥?/span>
def eight(a=鈥?/span>8鈥?/span>):
if a != 鈥?/span>8鈥?/span>:
if a[0]==鈥?/span>+鈥?/span>:
return 8+int(a[1])
if a[0]==鈥?/span>-鈥?/span>:
return 8-int(a[1])
if a[0]==鈥?/span>*鈥?/span>:
return 8*int(a[1])
if a[0]==鈥?/span>/鈥?/span>:
return int(8/int(a[1]))
else:
return 鈥?/span>8鈥?/span>
def nine(a=鈥?/span>9鈥?/span>):
if a != 鈥?/span>9鈥?/span>:
if a[0]==鈥?/span>+鈥?/span>:
return 9+int(a[1])
if a[0]==鈥?/span>-鈥?/span>:
return 9-int(a[1])
if a[0]==鈥?/span>*鈥?/span>:
return 9*int(a[1])
if a[0]==鈥?/span>/鈥?/span>:
return int(9/int(a[1]))
else:
return 鈥?/span>9鈥?/span>
def plus(a):
return 鈥?/span>+鈥?/span>+a
def minus(a):
return 鈥?/span>-鈥?/span>+a
def times(a):
return 鈥?/span>*鈥?/span>+a
def divided_by(a):
return 鈥?/span>/鈥?/span>+a
杩欎綅缃戝弸鐨勬€濊矾寰堝ソ鐞嗚В锛?/p>
銆€銆€鏁板瓧鍑芥暟鐨勪紶鍊奸粯璁や负鏈韩锛屼笖榛樿浼犲洖鑷韩鐨勫€笺€傚鏋滃弬鏁板€间负鍏朵粬锛屽垯鏍规嵁鍙傛暟鍊煎垽鏂畻娉曠被鍨嬬鍚堝摢绉嶇畻娉曪紝鏈€鍚庤繘琛岀浉鍔犲噺銆?nbsp; 绠楁硶鍑芥暟鍙鏄皢绠楁硶鐨勭鍙峰拰鍊艰繘琛屽瓧绗︿覆鍚堝苟锛岀敤浜庡湪鏁板瓧鍑芥暟涓繘琛屽垽鏂€傚锛?code class="language-python">seven(times(five()))
銆€銆€绗竴姝ワ細鍏堟墽琛宖ive()鍑芥暟锛岀敱浜庢病鏈夊弬鏁板€硷紝鍥犳浣跨敤榛樿鐨勫弬鏁板€硷紝杩斿洖鑷韩锛屽嵆锛?銆?/p>
銆€銆€绗簩姝ワ細澶勭悊times(5)鍑芥暟锛屽皢5鍜岀畻娉曠鍙疯繘琛屽悎骞讹紝鍗筹細绗﹀彿鈥?鈥樺拰5鍚堝苟锛岃繑鍥炲瓧绗︿覆"*5"
銆€銆€绗笁姝ワ細澶勭悊seven("*5")鍑芥暟锛岀敱浜庢暟瀛楀嚱鏁版湁鍙傛暟锛屽洜姝ゆ墽琛宔lse閲岄潰鐨勫唴瀹癸紝鏍规嵁a[0]鍗冲瓧绗︿覆鐨勭涓€涓瓧绗︽潵鍒ゆ柇绗﹀悎鍝绠楁硶锛屽啀灏嗗€硷紙鍗砤[1]锛夎繘琛岃繍绠楋紝鍗筹細5*7=35
浼樿川瑙g瓟锛?/p>
def zero(f = None): return 0 if not f else f(0) def one(f = None): return 1 if not f else f(1) def two(f = None): return 2 if not f else f(2) def three(f = None): return 3 if not f else f(3) def four(f = None): return 4 if not f else f(4) def five(f = None): return 5 if not f else f(5) def six(f = None): return 6 if not f else f(6) def seven(f = None): return 7 if not f else f(7) def eight(f = None): return 8 if not f else f(8) def nine(f = None): return 9 if not f else f(9) def plus(y): return lambda x: x+y def minus(y): return lambda x: x-y def times(y): return lambda x: x*y def divided_by(y): return lambda x: int(x/y)
鎴戜滑鏉ユ帰璁ㄤ竴涓嬭繖绉嶇畻娉曠殑鎬濊矾锛?/p>
銆€銆€鏁板瓧鍑芥暟涓紝鍏堝垽鏂槸鍚︿负None锛屼负None鍒欒繑鍥炶嚜韬€傚鏋滀笉涓篘one鍒欐墽琛屽嚱鏁癴銆傜畻鏈嚱鏁拌繑鍥炰竴涓尶鍚嶅嚱鏁般€傝繕鏄€夋嫨渚嬪瓙锛?code class="language-python">seven(times(five()))
銆€銆€绗竴姝ワ細鍏堝鐞唂ive()鍑芥暟锛屽嵆杩斿洖鑷韩5銆倀imes(5)
銆€銆€绗簩姝ワ細澶勭悊times(5)锛岀粨鏋滀负杩斿洖涓€涓尶鍚嶅嚱鏁帮紝鍑芥暟鍐呴儴鐨剏鍊间负5銆?/p>
銆€銆€绗笁姝ワ細澶勭悊f鍑芥暟锛屽嚱鏁癴涓虹畻鏈嚱鏁板鐞嗙粨鏋滐紝鍗宠繑鍥炵殑鍖垮悕鍑芥暟銆俧(7)鐩稿綋浜庢墽琛屽尶鍚嶅嚱鏁帮細lambda 7锛?*5澶勭悊缁撴灉涓?5銆?/p>
鐭ヨ瘑鐐癸細
銆€銆€1銆佷娇鐢ㄤ簡鍑芥暟榛樿鍙傛暟鍊硷細
銆€銆€銆€銆€def one(a=1): 鍗抽粯璁ゅ弬鏁癮榛樿浼犲€间负1锛屽彲鑷瀹氫箟a鐨勫弬鏁板€笺€?/p>
銆€銆€2銆佷娇鐢ㄤ簡鍖垮悕鍑芥暟lambda
銆€銆€銆€銆€lambda 鐨勬牸寮忎负锛歭ambda x, y: x + y銆倄鍜寉鏄渶瑕佷紶鍏ョ殑褰㈠弬銆倄+y鏄嚱鏁扮殑澶勭悊缁撴灉銆?/p>
銆€銆€3銆佷娇鐢ㄤ簡闂寘
銆€銆€銆€銆€绠楁湳鍑芥暟鐨勮繑鍥炲€兼槸涓€涓嚱鏁帮紝鍗冲皢鍏堜竴姝ュ鐞嗙殑缁撴灉锛坹=5锛夊拰鍖垮悕鍑芥暟涓€璧蜂繚瀛樹簡涓嬫潵銆傝€宻even(f)锛屽氨鏄皢杩欎釜鍖呰祴缁欎簡f锛宖鍙樻垚浜嗗惈鏈墆鍊肩殑鍖垮悕鍑芥暟銆?/p>
銆€銆€4銆?/p>
以上是关于銆怟ata Daily 2銆慍alculating with Functions的主要内容,如果未能解决你的问题,请参考以下文章