楼主: Lisrelchen
2187 7

Ninety-Nine Scala Problems [推广有奖]

  • 0关注
  • 62粉丝

VIP

院士

67%

还不是VIP/贵宾

-

TA的文库  其他...

Bayesian NewOccidental

Spatial Data Analysis

东西方数据挖掘

威望
0
论坛币
49957 个
通用积分
79.5487
学术水平
253 点
热心指数
300 点
信用等级
208 点
经验
41518 点
帖子
3256
精华
14
在线时间
766 小时
注册时间
2006-5-4
最后登录
2022-11-6

相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
S-99: Ninety-Nine Scala Problems

These are an adaptation of the Ninety-Nine Prolog Problems written by Werner Hett at the Berne University of Applied Sciences in Berne, Switzerland. I (Phil Gold) have altered them to be more amenable to programming in Scala. Feedback is appreciated, particularly on anything marked TODO.

The problems have different levels of difficulty. Those marked with a single asterisk (*) are easy. If you have successfully solved the preceeding problems you should be able to solve them within a few (say 15) minutes. Problems marked with two asterisks (**) are of intermediate difficulty. If you are a skilled Scala programmer it shouldn't take you more than 30-90 minutes to solve them. Problems marked with three asterisks (***) are more difficult. You may need more time (i.e. a few hours or more) to find a good solution. The difficulties were all assigned for the Prolog problems, but the Scala versions seem to be of roughly similar difficulty.

Your goal should be to find the most elegant solution of the given problems. Efficiency is important, but clarity is even more crucial. Some of the (easy) problems can be trivially solved using built-in functions. However, in these cases, you learn more if you try to find your own solution.

Solutions are available by clicking on the link at the beginning of the problem description.

[I don't have example solutions to all of the problems yet. I'm working on getting them all done, but in the meantime, contributed solutions, particularly from seasoned Scala programmers would be appreciated. If you feel a particular problem can be solved in a better manner than I did, please let me know that, too. <PMG>]


本帖隐藏的内容

99-Scala-Problems-master.zip (110.01 KB)




二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Problems problem SCALA Nine LEM difficulty University different anything problems

沙发
Lisrelchen 发表于 2017-1-25 09:44:12 |只看作者 |坛友微信交流群
  1. // P01 (*) Find the last element of a list.
  2. //     Example:
  3. //     scala> last(List(1, 1, 2, 3, 5, 8))
  4. //     res0: Int = 8

  5. // The start of the definition of last should be
  6. //     def last[A](l: List[A]): A = ...
  7. // The `[A]` allows us to handle lists of any type.

  8. object P01 {
  9.   // There are several ways to solve this problem.  If we use builtins, it's very
  10.   // easy.
  11.   def lastBuiltin[A](ls: List[A]): A = ls.last

  12.   // The standard functional approach is to recurse down the list until we hit
  13.   // the end.  Scala's pattern matching makes this easy.
  14.   def lastRecursive[A](ls: List[A]): A = ls match {
  15.     case h :: Nil  => h
  16.     case _ :: tail => lastRecursive(tail)
  17.     case _         => throw new NoSuchElementException
  18.   }
  19. }
复制代码

使用道具

藤椅
Lisrelchen 发表于 2017-1-25 09:45:31 |只看作者 |坛友微信交流群
  1. // P02 (*) Find the last but one element of a list.
  2. //     Example:
  3. //     scala> penultimate(List(1, 1, 2, 3, 5, 8))
  4. //     res0: Int = 5

  5. object P02 {
  6.   // Again, with builtins this is easy.
  7.   def penultimateBuiltin[A](ls: List[A]): A =
  8.     if (ls.isEmpty) throw new NoSuchElementException
  9.     else ls.init.last

  10.   // But pattern matching also makes it easy.
  11.   def penultimateRecursive[A](ls: List[A]): A = ls match {
  12.     case h :: _ :: Nil => h
  13.     case _ :: tail     => penultimateRecursive(tail)
  14.     case _             => throw new NoSuchElementException
  15.   }
  16.   
  17.   
  18.   // Just for fun, let's look at making a generic lastNth function.

  19.   // An obvious modification of the builtin solution works.
  20.   def lastNthBuiltin[A](n: Int, ls: List[A]): A = {
  21.     if (n <= 0) throw new IllegalArgumentException
  22.     if (ls.length < n) throw new NoSuchElementException
  23.     ls.takeRight(n).head
  24.   }

  25.   // Here's one approach to a non-builtin solution.
  26.   def lastNthRecursive[A](n: Int, ls: List[A]): A = {
  27.     def lastNthR(count: Int, resultList: List[A], curList: List[A]): A =
  28.       curList match {
  29.         case Nil if count > 0 => throw new NoSuchElementException
  30.         case Nil              => resultList.head
  31.         case _ :: tail        =>
  32.           lastNthR(count - 1,
  33.                    if (count > 0) resultList else resultList.tail,
  34.                    tail)
  35.       }
  36.     if (n <= 0) throw new IllegalArgumentException
  37.     else lastNthR(n, ls, ls)
  38.   }
  39. }
复制代码

使用道具

板凳
Lisrelchen 发表于 2017-1-25 09:46:18 |只看作者 |坛友微信交流群
  1. // P03 (*) Find the Kth element of a list.
  2. //     By convention, the first element in the list is element 0.
  3. //
  4. //     Example:
  5. //     scala> nth(2, List(1, 1, 2, 3, 5, 8))
  6. //     res0: Int = 2

  7. object P03 {
  8.   // Trivial with builtins.
  9.   def nthBuiltin[A](n: Int, ls: List[A]): A =
  10.     if (n >= 0) ls(n)
  11.     else throw new NoSuchElementException

  12.   // Not that much harder without.
  13.   def nthRecursive[A](n: Int, ls: List[A]): A = (n, ls) match {
  14.     case (0, h :: _   ) => h
  15.     case (n, _ :: tail) => nthRecursive(n - 1, tail)
  16.     case (_, Nil      ) => throw new NoSuchElementException
  17.   }
  18. }
复制代码

使用道具

报纸
Lisrelchen 发表于 2017-1-25 09:47:23 |只看作者 |坛友微信交流群
  1. // P04 (*) Find the number of elements of a list.
  2. //     Example:
  3. //     scala> length(List(1, 1, 2, 3, 5, 8))
  4. //     res0: Int = 6

  5. object P04 {
  6.   // Builtins.
  7.   def lengthBuiltin[A](ls: List[A]): Int = ls.length

  8.   // Simple recursive solution.
  9.   def lengthRecursive[A](ls: List[A]): Int = ls match {
  10.     case Nil       => 0
  11.     case _ :: tail => 1 + lengthRecursive(tail)
  12.   }

  13.   // Tail recursive solution.  Theoretically more efficient; with tail-call
  14.   // elimination in the compiler, this would run in constant space.
  15.   // Unfortunately, the JVM doesn't do tail-call elimination in the general
  16.   // case.  Scala *will* do it if the method is either final or is a local
  17.   // function.  In this case, `lengthR` is a local function, so it should
  18.   // be properly optimized.
  19.   // For more information, see
  20.   // http://blog.richdougherty.com/2009/04/tail-calls-tailrec-and-trampolines.html
  21.   def lengthTailRecursive[A](ls: List[A]): Int = {
  22.     def lengthR(result: Int, curList: List[A]): Int = curList match {
  23.       case Nil       => result
  24.       case _ :: tail => lengthR(result + 1, tail)
  25.     }
  26.     lengthR(0, ls)
  27.   }

  28.   // More pure functional solution, with folds.
  29.   def lengthFunctional[A](ls: List[A]): Int = ls.foldLeft(0) { (c, _) => c + 1 }
复制代码

使用道具

地板
w-long 发表于 2017-1-25 11:34:11 |只看作者 |坛友微信交流群
Thanks.
Ninety-Nine Scala Problems

使用道具

7
franky_sas 发表于 2017-1-25 16:55:10 |只看作者 |坛友微信交流群

使用道具

8
iid_garch 发表于 2017-1-25 21:31:50 |只看作者 |坛友微信交流群

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加JingGuanBbs
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-20 01:25