Functional Programming Principles in Scala 
by Martin Odersky

Martin教授是scala语言的creator,在coursera上面有scala课程。本文是第一周的作业。
作业应该做Eclipse里面编辑,使用WorkSheet实时检查。然后使用sbt运行styleCheck,test命令进行测试,run命令来运行main函数,如果没有错误,就可使用submit命令提交作业了。
本周作业不难。
pascal三角需要注意运算过程中尽量不要超出Int的表示范围;我在程序中使用了一个小技巧来做到这一点。
balance程序需要使用多个if else 来控制流程。
countChange则使用迭代的方法实现,如果没有想起来这个方法会比较难。还要注意程序什么条件下终止。
作业要求:
Download the recfun.zip handout archive file and extract it somewhere on your machine.This assignment counts towards your final grade. Please refer to the Grading Policy for more details.Do not forget to submit your work using the submit task from SBT. Please refer to the example assignment for instructions.Exercise 1: Pascal’s Triangle
The following pattern of numbers is called Pascal’s triangle.11 11 2 11 3 3 1
1 4 6 4 1...
The numbers at the edge of the triangle are all 1, and each number inside the triangle is the sum of the two numbers above it. Write a function that computes the elements of Pascal’s triangle by means of a recursive process.Do this exercise by implementing the pascal function in Main.scala, which takes a column c and a row r, counting from 0 and returns the number at that spot in the triangle. For example, pascal(0,2)=1, pascal(1,2)=2 and pascal(1,3)=3.def pascal(c: Int, r: Int): Int
Exercise 2: Parentheses Balancing
Write a recursive function which verifies the balancing of parentheses in a string, which we represent as a List[Char] not a String. For example, the function should return true for the following strings:(if (zero? x) max (/ 1 x))
I told him (that it’s not (yet) done). (But he wasn’t listening)
The function should return false for the following strings::-)
())(
The last example shows that it’s not enough to verify that a string contains the same number of opening and closing parentheses.Do this exercise by implementing the balance function in Main.scala. Its signature is as follows:def balance(chars: List[Char]): Boolean
There are three methods on List[Char] that are useful for this exercise:chars.isEmpty: Boolean returns whether a list is empty
chars.head: Char returns the first element of the list
chars.tail: List[Char] returns the list without the first element
Hint: you can define an inner function if you need to pass extra parameters to your function.Testing: You can use the toList method to convert from a String to a List[Char]: e.g. "(just an) example".toList.Exercise 3: Counting Change
Write a recursive function that counts how many different ways you can make change for an amount, given a list of coin denominations. For example, there are 3 ways to give change for 4 if you have coins with denomiation 1 and 2: 1+1+1+1, 1+1+2, 2+2.Do this exercise by implementing the countChange function in Main.scala. This function takes an amount to change, and a list of unique denominations for the coins. Its signature is as follows:def countChange(money: Int, coins: List[Int]): Int
Once again, you can make use of functions isEmpty, head and tail on the list of integers coins.Hint: Think of the degenerate cases. How many ways can you give change for 0 CHF? How many ways can you give change for >0 CHF, if you have no coins?

源程序如下:

package recfun
import common._object Main {def main(args: Array[String]) {println("Pascal's Triangle")for (row <- 0 to 10) {//change to 22 to check pascal if out of Int rangefor (col <- 0 to row)print(pascal(col, row) + " ")println()}}/*** Exercise 1*/def pascal(c: Int, r: Int): Int = {//everytime mutiply a little num in case of overflow Int rangedef factB(c: Int, r: Int): Int =if (c==0) 1 else factB(c-1,r)*(r-c+1)/cfactB(c,r)}/*** Exercise 2*/def balance(chars: List[Char]): Boolean ={def count(char:Char):Int ={if (char == '(') 1else if (char == ')') -1else 0}def isOK(len:Int,chars:List[Char]):Boolean={if (chars.isEmpty) len==0else if (len==0 && count(chars.head) == -1) falseelse isOK(len+count(chars.head),chars.tail)}isOK(0,chars)}/*** Exercise 3*/def countChange(money: Int, coins: List[Int]): Int =if (money==0) 1else if (coins.isEmpty || money<0) 0else countChange(money,coins.tail)+countChange(money-coins.head,coins)
}

Martin Odersky Scala编程公开课 第一周作业相关推荐

  1. Martin Odersky Scala编程公开课 第二周作业

    Functional Programming Principles in Scala  by Martin Odersky 这一周的主要内容是函数.函数是scala语言最重要的概念,既可以当作函数的参 ...

  2. Martin Odersky Scala编程公开课 第三周作业

    Functional Programming Principles in Scala  by Martin Odersky 这次的作业叫做Object-Oriented Sets.要完成一个完整的类, ...

  3. 吴恩达机器学习公开课第一周学习笔记

    Octave是一种编程语言,旨在解决线性和非线性的数值计算问题.Octave为GNU项目下的开源软件,早期版本为命令行交互方式,4.0.0版本发布基于QT编写的GUI交互界面.Octave语法与Mat ...

  4. SAP Fiori Elements 公开课第一单元概要介绍

    Open SAP 课程地址 很多 SAP 从业者反映,open SAP 上的视频,因为网络原因无法访问,所以我会陆续在我的个人微 信 号"汪子熙"上面,把这些视频配上中文字幕并发布 ...

  5. 2021阿里巴巴大数据技术公开课第一季:外部工具连接SaaS模式云数仓MaxCompute实战

    简介:MaxCompute 是面向分析的企业级 SaaS 模式云数据仓库,以 Serverless 架构提供快速.全托管的在线数据仓库服务,消除了传统数据平台在资源扩展性和弹性方面的限制,最小化用户运 ...

  6. 【中文】【吴恩达课后编程作业】Course 5 - 序列模型 - 第一周作业

    [中文][吴恩达课后编程作业]Course 5 - 序列模型 - 第一周作业 - 搭建循环神经网络及其应用 上一篇:[课程5 - 第一周测验]※※※※※ [回到目录]※※※※※下一篇:[课程5 - 第 ...

  7. 【中文】【吴恩达课后编程作业】Course 2 - 改善深层神经网络 - 第一周作业(123)

    [中文][吴恩达课后编程作业]Course 2 - 改善深层神经网络 - 第一周作业(1&2&3) - 初始化.正则化.梯度校验 上一篇:[课程2 - 第一周测验]※※※※※ [回到目 ...

  8. OUC2022秋季软件工程第一周作业

    注:本博客为OUC2022秋季软件工程第一周作业 文章目录 注:本博客为OUC2022秋季软件工程第一周作业 软件工程第18小组 成员: 一.个人简介 罗浩宇 二.四个问题 问题① 问题② 问题③ 问 ...

  9. 第一周作业(零基础)

    第一周作业 一.选择题 下列变量名中不合法的是?(C) A. abc B. Npc C. 1name D ab_cd 下列选项中不属于关键字的是?(B) A. and B. print C. True ...

最新文章

  1. python libusb1库
  2. java switch char_Java7中Switch为什么只支持byte、short、char、int、String
  3. Codeforces Round #631 (Div. 2) D. Dreamoon Likes Sequences 思维 + 组合数学
  4. obs可以装手机吗?_原神PC和手机数据互通吗 PC和手机可以一起玩吗
  5. python的基本数据结构_Python学习笔记——基本数据结构
  6. Spring_自动装配
  7. 四个角不是直角的四边形_同步资料人教版四上数学第五单元平行四边形和梯形5.1...
  8. 今天我的生日,纪念一下
  9. 亲历者说:Kubernetes API 与 Operator,不为人知的开发者战争
  10. ubuntu上如何安装mysql
  11. 组态王与DLT645-2007电能表通讯调试总结
  12. C盘pc的Android文件夹,清理系统盘C盘的无用文件
  13. 如何在python导入包_python如何导入包
  14. anaconda清华镜像源使用
  15. 【Go实战基础】程序里面数据是如何显示到浏览器当中的
  16. Http请求体被转义
  17. 认识java安全管理器SecurityManager
  18. vue项目性能优化——断点续传
  19. linux下apache 的安装,php安装过程
  20. ARM APCS 学习笔记

热门文章

  1. Ajax的简单使用方法
  2. 计算机中i o接口,计算机组成原理 输入输出(I/O)I/O 接口(I/O 控制器)
  3. php 获取mac地址栏,php 获取网卡物理(MAC)地址的实现方法
  4. Python练习:阶乘累计求和
  5. android数据库开发案例教程,Android Studio项目开发教程 第6章 数据库编程(30页)-原创力文档...
  6. 菜鸟教程 php mysql_PHP MySQL 读取数据 | 菜鸟教程
  7. 六界仙尊h5服务器维护多久,《六界仙尊》5月6日更新维护 公开虚天BOSS坐标
  8. java常问的报错_java常见报错及解决
  9. python 装饰器的讲解
  10. java开发安装程序_创建java开发环境安装包