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. 如何python循环中删除字典元素

    //下面这行就是在循环中遍历删除字典元素的方法! for i in list(dictheme2.keys()): if dictheme2[i]<self.countFortheme: dic ...

  2. 验证码输入自动聚焦下一个input或者删除自动聚焦上一个input

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  3. 修改HTTPS加密协议TLS1.0为TLS1.2

    一:首先为什么要改为TLS1.2 因为各大浏览器相继发布声明将停止支持 TLS 1.0 和 TLS 1.1 https://www.cnblogs.com/jpush88/p/9846047.html ...

  4. 006_linux驱动之_ioremap函数使用

    (一)学习linux驱动之初,对ioremap函数的个人理解 (二)博客:实验探究 ioremap 这篇文章作者通过验证来阐述自己的观点,个人觉得挺好的 (三)函数原型 基本简介 void * __i ...

  5. 为什么说Redis是单线程的?

    一.前言 近乎所有与Java相关的面试都会问到缓存的问题,基础一点的会问到什么是“二八定律”.什么是“热数据和冷数据” ,复杂一点的会问到缓存雪崩.缓存穿透.缓存预热.缓存更新.缓存降级等问题,这些看 ...

  6. word如何选择图片粘贴

    自动导入Word图片,或者粘贴Word内容时自动上传所有的图片,并且最终保留Word样式,这应该是Web编辑器里面最基本的一个需求功能了.一般情况下我们将Word内容粘贴到Web编辑器(富文本编辑器) ...

  7. 可以粘贴Word文档中图片的编辑器

    Chrome+IE默认支持粘贴剪切板中的图片,但是我要发布的文章存在word里面,图片多达数十张,我总不能一张一张复制吧?Chrome高版本提供了可以将单张图片转换在BASE64字符串的功能.但是无法 ...

  8. [USACO10HOL]赶小猪

    嘟嘟嘟 这题和某一类概率题一样,大体思路都是高斯消元解方程. 不过关键还是状态得想明白.刚开始令\(f[i]\)表示炸弹在点\(i\)爆的概率,然后发现这东西根本无法转移(或者说概率本来就是\(\fr ...

  9. java试题复盘——9月8日

    上: 1.可将语句块或方法设为同步使用的语句是(A) A synchronized              用于方法或者代码块前,使此方法或者代码变成同步的 B static             ...

  10. Java并发概念-1

    一,同步 和 异步: 同步:调用方需要等待被调用方回应之后,才能进行下一步动作. 异步:调用方不需要等待被调用方回应,直接继续自己的动作.在未来某个时间点可能会有被调用方的回应. 二,并发 和 并行 ...