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. html5--6-11 CSS选择器7--伪类选择器

    html5--6-11 CSS选择器7--伪类选择器 #E:target 选择当前活动的锚点元素. 学习要点 掌握常用的CSS选择器 了解不太常用的CSS选择器 什么是选择器 当我们定义一条样式时候, ...

  2. 【Cocos2dx】新建场景、场景的切换、设置启动场景与菜单的新建

    这是Cocos2dx最简单的部分.主要是体现对场景的操作,其实这东西就是Flash的舞台,安卓的Activity,WIN32窗体程序的Framework窗体,网页的body,反正就是对那个容纳各种东西 ...

  3. javascript ==等于与===恒等于

    ==判断是否等于,只判断数值 ===代表恒等于,不仅判断数值,而且判断类型 var a=5; var b="5"; (a b 只是数值上相等,但是类型并不相等) a==b //tr ...

  4. 879C

    贪心 题目看错了...还以为是从操作序列中选5个...然后半个小时没了... 我们把每位分别用0和1带入,看看返回值是什么,然后分类讨论.千万不用特判!!!之前忘了删了就fst... #include ...

  5. html中target的用法

  6. Linux ubuntu 下寻找 texlive 缺失包 texlive 缺失包(转载)

    转自:http://blog.sina.com.cn/s/blog_87315ca60101d4d1.html 在Linux下用 latex 编译 ××.tex 文件有时候时会提示:  ! LaTeX ...

  7. 51单片机 HC05蓝牙模块

    一.注意事项 1.烧写程序时,要把蓝牙tx,rx信号线拔掉,对烧写程序有影响. 2.执行HC05集命令时,均以\r\n结尾.串口中断若选择“发送新行”时,不用添加\r\n.原理相同. 二.准备软硬件 ...

  8. 聊聊Java并发面试问题之公平锁与非公平锁是啥?

    一.什么是非公平锁? 先来聊聊非公平锁是啥,现在大家先回过头来看下面这张图. 如上图,现在线程1加了锁,然后线程2尝试加锁,失败后进入了等待队列,处于阻塞中.然后线程1释放了锁,准备来唤醒线程2重新尝 ...

  9. Event Handling Guide for iOS--事件驱动指南

    事件是发送给应用程序来通知它用户动作的对象.在iOS中,事件可以有多种形式:多触摸事件,motion(,移动,手 势)事件---例如,设备的加速计(accelerometer)--和控制多媒体的事件. ...

  10. 工作中常用css样式总结

    一.HTML隐藏文本输入框 有三种方法: 1.<input type="hidden" value=""> 这是对任何元素都起作用的: 2.< ...