Course1_Week1_ProgrammingHomeWork
Exercise 1: Pascal’s Triangle
The following pattern of numbers is called Pascal’s triangle.
1
1 1
1 2 1
1 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 aList[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 denomination 1 and 2: 1+1+1+1, 1+1+2, 2+2.
Do this exercise by implementing the countChange function inMain.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.
Code
/**
* Exercise 1
*/
def pascal(c: Int, r: Int): Int = {
// 满足条件始终返回1
if (c == 0 || r == c)
1
else
pascal(c - 1, r - 1) + pascal(c, r - 1)
}
/**
* Exercise 2
*/
def balance(chars: List[Char]): Boolean = {
@scala.annotation.tailrec
def loop(chars: List[Char], cnt: Int): Boolean = {
if (cnt < 0)
false
else if (chars.isEmpty && cnt == 0)
true
else {
if (chars.head == '(')
loop(chars.tail, cnt + 1)
else if (chars.head == ')')
loop(chars.tail, cnt - 1)
else
loop(chars.tail, cnt)
}
}
loop(chars, 0)
}
/**
* Exercise 3
*/
def countChange(money: Int, coins: List[Int]): Int = {
if (money < 0 || coins.isEmpty)
0
else if (money == 0)
1
else // 很好的形式,值得借鉴
countChange(money - coins.head, coins) + countChange(money, coins.tail)
}
Course1_Week1_ProgrammingHomeWork的更多相关文章
随机推荐
- [唐胡璐]Selenium技巧 - 定制元素属性检查,并写到ReportNG中
QTP 和Selenium 都会有这种要检查某一个控件元素属性的情况,比如去检查一个Button的显示文字是什么? 为了更方便的书写程序,并优美的显示到HTML测试报告中,做了以下几个小小的封装,只是 ...
- 开发中少不了的Fun -- 前端本地存储
存储sessionStorage function setSessionStore (name, content) { if (!name) return if (typeof content !== ...
- C# 加载显示ftp上图片方法
ftp://用户名:密码@IP/路径 FTP的一种登陆方式 如ftp://sing:song@192.168.1.133/upload/pic/236.jpg
- 005_linux驱动之_class_device_create函数
(一)解析class_device_create函数 (二)当我们使用class_create创建一个类之后我们就可以使用class_device_create函数在这个类下面创建一个设备了,cl ...
- 开源分布式中间件 DBLE 快速入门指南
GitHub:https://github.com/actiontech/dble 官方中文文档:https://actiontech.github.io/dble-docs-cn/ 一.环境准备 D ...
- CF940F Machine Learning 带修改莫队
题意:支持两种操作:$1.$ 查询 $[l,r]$ 每个数字出现次数的 $mex$,$2.$ 单点修改某一位置的值. 这里复习一下带修改莫队. 普通的莫队中,以左端点所在块编号为第一关键字,右端点大小 ...
- Visual Studio Code:以十六进制格式显示文件内容
造冰箱的大熊猫@cnblogs 2019/9/4 发现Visual Studio Code很好用,无论是作为源代码编辑器还是文本编辑器在Win平台下用的都很不错.但有时候需要以十六进制格式查看数据文件 ...
- 【概率论】3-6:条件分布(Conditional Distributions Part II)
title: [概率论]3-6:条件分布(Conditional Distributions Part II) categories: Mathematic Probability keywords: ...
- ie和vuex的兼容
vuex requires a Promise polyfill in this browser. 在ie中的报错 需要安卓babel-polyfill, 然后在webpack.base.confi ...
- Pstools使用
pstool的介绍 PsTools是Sysinternals Suite中一款排名靠前的一个安全管理工具套件.现在被微软收购.目前pstools中含有12款各式各样的小工具.如果将它们灵活的运用,将会 ...