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的更多相关文章

随机推荐

  1. POJ-1661-Help Jimmy(DP, 递推)

    链接: https://vjudge.net/problem/POJ-1661 题意: "Help Jimmy" 是在下图所示的场景上完成的游戏. 场景中包括多个长度和高度各不相同 ...

  2. The Preliminary Contest for ICPC Asia Xuzhou 2019 【 题目:so easy】{并查集维护一个数的下一个没有被删掉的数} 补题ING

    题意:给[1,n],n个数,有两种操作: 1 x,删去x2 x,查询还未被删去的数中大于等于x的最小的数是多少. input: output: 做法:按照并查集的方法压缩路径 代码: #include ...

  3. Shell 07 项目案例

    案例1:编写一键部署软件脚本 案例2:启动脚本 案例3:编写监控脚本 案例4:编写安全检测脚本 案例5:编写进度显示脚本   一.编写一键部署软件脚本 #!/bin/bash N=$(yum repo ...

  4. 7.26T3小游戏

    小游戏 题目描述 有一个简单的小游戏.游戏的主人公是一个勇士,他要穿过一片黑森林,去 解救公主.黑森林的地图可以用一张 V 个点.E 条边的无向图来描述,起点是 1 号点,终点是 V 号点.勇士从起点 ...

  5. 1628:X-factor Chain

    1628:X-factor Chain 时间限制: 1000 ms         内存限制: 524288 KB提交数: 122     通过数: 68 [题目描述] 原题来自 POJ 3421 输 ...

  6. leveldb源码分析之Slice

    转自:http://luodw.cc/2015/10/15/leveldb-02/ leveldb和redis这样的优秀开源框架都没有使用C++自带的字符串string,redis自己写了个sds,l ...

  7. let和const关键字

    一:let 关键字 1.作用: - 与var类似, 用于声明一个变量 2.特点 - 在块作用域内有效 - 不能重复声明 - 不会预处理, 不存在提升 3.应用 - 循环遍历加监听 - 使用let取代v ...

  8. Sublime Markdown预览插件安装流程

    使用方法 在sublime中已编辑好的markdown使用快捷键 Alt+M 即可在浏览器预览效果. 需要安装的插件 Markdown Editting:主要用来做 Markdown 编辑时的语法高亮 ...

  9. Flutter移动电商实战 --(16)切换后页面状态的保持AutomaticKeepAliveClientMixin

    底栏切换每次都重新请求是一件非常恶心的事,flutter 中提供了AutomaticKeepAliveClientMixin 帮我们完成页面状态保存效果. 1.AutomaticKeepAliveCl ...

  10. LINUX 上 实现SNTP (时间同步)

    Network Time Protocol (NTP) 也是RHCE新增的考试要求. 学习的时候也顺便复习了一下如何设置Linux的时间,现在拿出来和大家分享 设置NTP服务器不难但是NTP本身是一个 ...