26. Using the higher order function reduce(), write a function max_in_list() that takes a list of numbers and returns the largest one. Then ask yourself: why define and call a new function, when I can just as well call the reduce() function directly?

from functools import reduce

def max_in_list(num_list):
def max_of_two(a, b):
return a if a >= b else b
biggest = float("-inf")
return reduce(max_of_two, num_list, biggest) print(max_in_list([100,-2,3,4,5]))

【june】reduce和map是函数式编程最明显的特点,也是两个相反的过程。这个函数在真实的编程中用的并不多,完全可以用for来实现同样的功能,只是代码多几行而已。

27. Write a program that maps a list of words into a list of integers representing the lengths of the correponding words. Write it in three different ways: 1) using a for-loop, 2) using the higher order function map(), and 3) using list comprehensions.

# 1) use loop
def words_to_length(words):
lens = []
for word in words:
lens.append(len(word))
return lens # 2) use map
def words_to_length(words):
return list(map(len, words)) # 3) use list comprehension
def words_to_length(words):
return [len(word) for word in words] words_to_length(["i", "am", "newbie"]) 

28. Write a function find_longest_word() that takes a list of words and returns the length of the longest one. Use only higher order functions.

from functools import reduce

def find_longest_word(words):
return reduce(max, map(len, words)) print(find_longest_word(["a", "student", "good"]))

46 Simple Python Exercises-Higher order functions and list comprehensions的更多相关文章

  1. 46 Simple Python Exercises (前20道题)

    46 Simple Python Exercises This is version 0.45 of a collection of simple Python exercises construct ...

  2. 46 Simple Python Exercises-Very simple exercises

    46 Simple Python Exercises-Very simple exercises 4.Write a function that takes a character (i.e. a s ...

  3. [CS61A] Lecture 5&6&7. Environments & Design & Functions Examples & Homework 2: Higher Order Functions

    [CS61A] Lecture 5&6&7. Environments & Design & Functions Examples & Homework 2: ...

  4. [Redux] Understand Redux Higher Order Reducers

    Higher Order Reducers are simple reducer factories, that take a reducer as an argument and return a ...

  5. [React] Higher Order Components (replaces Mixins)

    Higher order components will allow you to apply behaviors to multiple React components. So the idea ...

  6. [React] Implement a Higher Order Component with Render Props

    When making a reusable component, you'll find that people often like to have the API they're most fa ...

  7. [React Intl] Use a react-intl Higher Order Component to format messages

    In some cases, you might need to pass a string from your intl messages.js file as a prop to a compon ...

  8. [React] Cleanly Map Over A Stateless Functional Component with a Higher Order Component

    In this lesson we'll create a Higher Order Component (HOC) that takes care of the key property that ...

  9. simple python code when @ simplnano

    code: import serial,time,itertools try: ser=serial.Serial(2,115200,timeout=0) except: print 'Open CO ...

随机推荐

  1. 「USACO」「LuoguP2731」 骑马修栅栏 Riding the Fences(欧拉路径

    Description Farmer John每年有很多栅栏要修理.他总是骑着马穿过每一个栅栏并修复它破损的地方. John是一个与其他农民一样懒的人.他讨厌骑马,因此从来不两次经过一个栅栏.你必须编 ...

  2. 【Codeforces 582A】 GCD Table

    [题目链接] 点击打开链接 [算法] G中最大的数一定也是a中最大的数.          G中次大的数一定也是a中次大的数. 第三.第四可能是由最大和次大的gcd产生的 那么就不难想到下面的算法: ...

  3. 【JSOI 2014】序列维护

    [题目链接] 点击打开链接 [算法] 线段树 注意标记下传 [代码] #include<bits/stdc++.h> using namespace std; #define MAXN 5 ...

  4. linux的存储结构

    在linux中存储结构如下: Linux系统中的文件存储结构 那么在linux中每个目录都是什么含义呢 在linux中相对路径和绝对路径是必须要了解的一个概念 绝对路径(absolute path): ...

  5. Crontab Build_setting的定期检查

    一.脚本功能 (1)检查所有的builting_setting.h是否能够编译通过,并将编译结果写入 编译结果.h文件中. (2)将编译结果通过邮箱发送给相关负责人. (3)系统定期执行任务,检查bu ...

  6. margin和padidng的使用

    何时应当使用margin:需要在border外侧添加空白时.空白处不需要背景(色)时.上下相连的两个盒子之间的空白,需要相互抵消时.如15px + 20px的margin,将得到20px的空白. 何时 ...

  7. C++实现合并两个已经排序的链表

    /* * 合并两个已经排序的链表.cpp * * Created on: 2018年4月11日 * Author: soyo */ #include<iostream> using nam ...

  8. 【215】◀▶ IDL 文件操作说明 (黑底)

    参考:I/O - General File Access Routines —— 基本文件操作函数 01   CD 修改当前的工作空间路径. 02   FILE_SEARCH 对文件名进行特定的查找. ...

  9. 洛谷 - P1403 - 约数研究 - 数论

    https://www.luogu.org/problemnew/show/P1403 可以直接用线性筛约数个数求出来,但实际上n以内i的倍数的个数为n/i的下整,要求的其实是 $$\sum\limi ...

  10. 了解Web Uploader

    1.简介WebUploader是由Baidu WebFE(FEX)团队开发的一个简单的以HTML5为主,FLASH为辅的现代文件上传组件.在现代的浏览器里面能充分发挥HTML5的优势,同时又不摒弃主流 ...