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. Oracle:通过oracle sql developer工具导入excel数据

    我使用的是oracle sql developer3.1版本,以前developer2.×老版本的excel导入功能有问题. excel文件内容如下: 第一步:找到要导入的表,右键-->导入数据 ...

  2. COGS-2049 疯狂动物城

    Description 你意外来到了一个未知的星球, 这里是一个动物乌托邦, 生活着一群拥有非凡智力的动物. 你遇到了一个叫做尼克的狐狸, 他准备给他的 GF 过生日 . 他将制作一个巨大的多层蛋糕, ...

  3. codeforces 690D1 D1. The Wall (easy)(dfs)

    题目链接: D1. The Wall (easy) time limit per test 0.5 seconds memory limit per test 256 megabytes input ...

  4. VC++读写文件

    目录 第1章读写文件    1 1.1 API    1 1.2 低级IO    1 1.2.1 文件序号    1 1.2.2 文本文件与二进制文件    1 1.3 流IO    2 1.4 Un ...

  5. sharepoint服务器修改密码后出现HTTP Error 503

    HTTP Error 503   解决办法: 更改sharepoint 网站应用程序池标示后,更改标示重新输入管理员密码,问题解决!

  6. 021--python装饰器

    一.装饰器含义 装饰器本质就是函数,为其它函数添加附加功能 二.装饰器原则 1.不修改被修饰函数的代码 2.不修改被修饰函数的调用方式 三.装饰器知识 装饰器 = 高阶函数 + 函数嵌套 + 闭包 四 ...

  7. 任务39:Role以及Claims授权

    基于Role角色的授权 asp.net core在逐渐淘汰这种基于RoleBase的基于角色的授权.鼓励大家使用基于Claim的授权 在认证的时候Cliam已经加入了Role 注释38节课的 自定义验 ...

  8. 看鸟哥的Linux私房菜的一些命令自我总结(三)

    -修改文件创建时间或创建新文件 touch [-acdmt] -a  :仅修改访问时间 -c  :仅修改文件的时间,若该文件不存在则不创建新文件 -d  :后面可以接想要修改的日期而不用当前的日期 - ...

  9. win10家庭版安装

    https://www.microsoft.com/zh-cn/software-download/windows10ISO https://www.2cto.com/os/201704/621770 ...

  10. hdu 3038 How Many Answers Are Wrong【带权并查集】

    带权并查集,设f[x]为x的父亲,s[x]为sum[x]-sum[fx],路径压缩的时候记得改s #include<iostream> #include<cstdio> usi ...