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. Redis做消息队列

    1.连接从Redis中获取日志文件并存储到ES中 [root@Logstash ~]# vim /usr/local/logstash/config/redis.conf input {     be ...

  2. linux实操_shell自定义函数

    基本语法: #定义函数 function 函数名(){ 函数体 } #调用函数 函数名 参数1 参数2... 实例:计算两个数的和. 运行后

  3. 2018web前端面试题总结

      web面试题 css面试 一.css盒模型 css中的盒子模型包括IE盒子模型和标准的W3C盒子模型.border-sizing: border-box, inherit, content-box ...

  4. Spark1

    Spark集群 0.0体验安装Spark在集群单节点 1.tar tar -xzvf xxx.tgz -C /soft/ ln -s /soft/spark-2.1.0-bin-hadoop2.7 / ...

  5. c++ string类基本使用

    初始化用法 #include <iostream> #include "string" using namespace std; void main() { strin ...

  6. [2019牛客多校第三场][G. Removing Stones]

    题目链接:https://ac.nowcoder.com/acm/contest/883/G 题目大意:有\(n\)堆石头,每堆有\(a_i\)个,每次可以选其中两堆非零的石堆,各取走一个石子,当所有 ...

  7. java-十五周作业

    题目1:编写一个应用程序,输入用户名和密码,访问test数据库中t_login表(字段包括id.username.password),验证登录是否成功. 题目2:在上一题基础上,当登录成功后,将t_u ...

  8. 数据库读写分离、分表分库——用Mycat

    转:     https://www.cnblogs.com/joylee/p/7513038.html 系统开发中,数据库是非常重要的一个点.除了程序的本身的优化,如:SQL语句优化.代码优化,数据 ...

  9. leetcode解题报告(6):Remove Duplicates from Sorted List

    描述 Given a sorted linked list, delete all duplicates such that each element appear only once. For ex ...

  10. [Luogu] 矩阵加速(数列)

    题面:https://www.luogu.org/problemnew/show/P1939 题解:https://www.zybuluo.com/wsndy-xx/note/1153810