銆怟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的主要内容,如果未能解决你的问题,请参考以下文章

daily 2018.2.10

Leetcode daily 20/03/13

如何获取 HDD 支持的 ATA 标准?

REVERSE-DAILY-rev2

Kata Daily 191010Grasshopper - Summation(加总)

币安第20期 挖矿打新 ATA