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)
}

最新文章

  1. Linux 中的数值计算和符号计算
  2. Glide.js:响应式 &amp; 触摸友好的 jQuery 滑块插件
  3. python走起之第三话
  4. 【PHPsocket编程专题(实战篇③)】构建基于socket的HTTP请求类
  5. UIALertView的基本用法与UIAlertViewDelegate对对话框的事件处理方法
  6. linux命令学习7-jstat命令
  7. jq 中each的用法 (share)
  8. 使用Hexo+github搭建个人博客
  9. 使用SpringSecurity体验OAUTH2之一 (入门1)
  10. Redis在CentOS和Windows安装过程
  11. 官方解析Cookies和Session的区别
  12. Java 线程使用注意事项
  13. WebView之禁止调用第三方浏览器
  14. Selenium上传文件
  15. Qt 文档编辑设置
  16. bt z
  17. Android学习笔记——Menu(三)
  18. C#二进制位算 权限
  19. NUC972 MDK NON-OS
  20. Python归纳 | WSGI协议

热门文章

  1. 《流畅的Python》Data Structures--第2章序列array
  2. SQLCommand命令、DbTransaction事务
  3. adb端口被自己占用,或者用adb连不上模拟器最终解决办法
  4. ELK---- kibana 安装 学习
  5. 题解[NOIP2017] 列队
  6. 冒泡排序之javascript
  7. SIGAI机器学习第六集 决策树
  8. Flutter 初始化数据完成后再加载页面
  9. Codeforces 1272 A-E
  10. nginx 配置 nodejs 反向代理