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的更多相关文章
随机推荐
- [转载]Java 内存分配全面浅析
Java 内存分配全面浅析 2013-02-20 17:54:45 袭烽 阅读数 91353更多 分类专栏: java基础 本文将由浅入深详细介绍Java内存分配的原理,以帮助新手更轻松的学习Ja ...
- linux实操_shell自定义函数
基本语法: #定义函数 function 函数名(){ 函数体 } #调用函数 函数名 参数1 参数2... 实例:计算两个数的和. 运行后
- Excle导出优化(poi)
搜索词条 1.idea报java.lang.OutOfMemoryError: Java heap space怎么解决? 2.java.lang.OutOfMemoryError: GC overhe ...
- P3474 [POI2008]KUP-Plot purchase
思路:单调栈 提交:>5次 错因:单调栈写法有问题+前缀和写错 题解: 若有\(>=k\ \&\&\ <=2\times k\)的点,显然直接选他就行了. 否则,我们 ...
- jQuery.map(arr|obj,callback)
jQuery.map(arr|obj,callback) 概述 将一个数组中的元素转换到另一个数组中.广州大理石机械构件 作为参数的转换函数会为每个数组元素调用,而且会给这个转换函数传递一个表示被转换 ...
- C++泛型编程-扩展
类型做参数是C++模板实现的主要形式.由此实现了类模板-->模板类-->实例的过程 当然除此之外也可以参考bitset的实现方式,参数决定类型的做法. #include <iostr ...
- AGC024E Sequence Growing Hard
题意 给出\(n\),\(m\),\(mu\),问有多少个序列组\((A_0,A_1,\dots,A_n)\)满足: 序列\(Ai\)的长度恰好为\(i\) 所有元素均在\([1,m]\) \(A_{ ...
- nginx配置不当引起的错误
1.CRLF注入 1.1环境配置 apt install nginx vi /etc/nginx/sites-available/default location / { return 302 htt ...
- 网络文件共享服务—NFS服务
NFS服务 NFS:Network File System 网络文件系统,基于内核的文件系统: Sun公司开发,通过使用NFS,用户和程序可以像访问本地文件一样访问远端系统上的文件,基于RPC(Rem ...
- centos7 设置 mysql 开机自启
前述 CentOS 7是目前较为流行的Linux发行版本.CentOS 7比起之前版本有了许多的变更.如firewall不在用iptables管理,而交由firewall-cmd管理.同样的,在Cen ...