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. django--没有整理,笔记

    https://docs.djangoproject.com/en/2.2/常用的数据路命令:python manage.py makemigrations 数据移植准备python manage.p ...

  2. jaxb生成cdata块

    1.需要添加一个适配器: import javax.xml.bind.annotation.adapters.XmlAdapter; public class AdapterCDATA extends ...

  3. 2019牛客多校第五场 generator 1——广义斐波那契循环节&&矩阵快速幂

    理论部分 二次剩余 在数论中,整数 $X$ 对整数 $p$ 的二次剩余是指 $X^2$ 除以 $p$ 的余数. 当存在某个 $X$,使得式子 $X^2 \equiv d(mod \ p)$ 成立时,称 ...

  4. 洛谷P1273 有线电视网【树形dp】

    题目:https://www.luogu.org/problemnew/show/P1273 题意:一棵树,叶子节点是用户,每天边有一个权值表示花费,每一个用户有一个值表示他们会交的钱. 问在不亏本的 ...

  5. 运算符重载之new与delete

    关于new/delete,援引C++ Primer中的一段话: 某些应用程序对内存分配有特殊的要求,因此我们无法直接将标准的内存管理机制直接应用于这些程序.他们常常需要自定义内存分配的细节,比如使用关 ...

  6. (转)实验文档3:在kubernetes集群里集成Apollo配置中心

    使用ConfigMap管理应用配置 拆分环境 主机名 角色 ip HDSS7-11.host.com zk1.od.com(Test环境) 10.4.7.11 HDSS7-12.host.com zk ...

  7. Try-Catch-Finally代码块中的return

    测试类的原型是这样子的 public class TryCatchFinallyToReturn { public static void main(String[] args) { System.o ...

  8. Ubuntu 在VirtualBox里无法联网【已解决】

    1. 在virtualBox中设置网络:设置->网络->将连接方式选定为“网络地址转换(NAT)” 点确定 2.进入Ubuntu系统中:系统设置->网络, 在弹出的对话框中选择:有线 ...

  9. getFieldDecorator用法(二)——封装表单模块

    后台管理系统经常用到表单查询,因此做了个封装 myorder.js import React from 'react'; import { Card, Button, Table, Form, Sel ...

  10. MySQL:数据库名或者数据表名包含-

    [参考文章]:mysql数据库名称中包含短横线的对应方式 1. 现象 命令行下操作 名称包含 " - " 数据库或者数据表时,语句执行报错: 2. 解决方案: 使用 `` 字符(E ...