Scala学习(九)练习
文件正则表达式&练习 |
1. 编写一小段Scala代码,将某个文件中的行倒转顺序,将最后一行作为第一行,依此类推
程序代码:
- import scala.io.Source
- import java.io.PrintWriter
- object ReverseLines extends App {
- val filename="File.txt"
- val RefileName="ReverseFile.txt"
- val source=Source.fromFile("src\\"+filename)
- lazy val ReSource=Source.fromFile("src\\"+RefileName)
- lazy val pw = new PrintWriter("src\\"+RefileName)
- val linesIterator=source.getLines()
- val linesRecord=linesIterator.toArray
- val reverseRecord=linesRecord.reverse
- reverseRecord.foreach {
- line =>pw.write(line+"\n")
- }
- pw.close()
- println(filename+"文件内容如下:")
- linesRecord.foreach (line=>println(line))
- println(RefileName+"文件内容如下:")
- ReSource.getLines().foreach(line=>println(line))
- }
运行结果:
File.txt文件内容如下:
Inc said they plan to form a venture to manage the money market
borrowing and investment activities of both companies.
BP North America is a subsidiary of British Petroleum Co
Plc <BP>, which also owns a 55 pct interest in Standard Oil.
The venture will be called BP/Standard Financial Trading
and will be operated by Standard Oil under the oversight of a
joint management committee.
ReverseFile.txt文件内容如下:
joint management committee.
and will be operated by Standard Oil under the oversight of a
The venture will be called BP/Standard Financial Trading
Plc <BP>, which also owns a 55 pct interest in Standard Oil.
BP North America is a subsidiary of British Petroleum Co
borrowing and investment activities of both companies.
Inc said they plan to form a venture to manage the money market
2. 编写Scala程序,从一个带有制表符的文件读取内容,将每个制表符替换成一组空格,使得制表符隔开的n列仍然保持纵向对齐,并将结果写入同一个文件
程序代码:
- object TabSpace extends App{
- val FileName="TabSpace"
- val path="src\\"+FileName+".txt"
- val linesIterator=Source.fromFile(path).getLines()
- lazy val TabIterator=Source.fromFile(path).getLines()
- val linesRecord=linesIterator.toArray
- lazy val pw=new PrintWriter(path)
- println(FileName+"文件内容如下:")
- linesRecord.foreach(println)
- linesRecord.foreach {
- line =>pw.write(line.replaceAll("\t", "")+"\n")
- }
- pw.close
- println("替换后"+FileName+"文件内容如下:")
- TabIterator.foreach(println)
- }
运行结果:
TabSpace文件内容如下:
Inc said they plan to form a venture to manage the money market
Inc ssss adfs sdjf sd dskl s jdakwus sd sdskkl sds sdsds djslkl
Inc said they plan to form a venture to manage the money market
Inc ssss adfs sdjf sd dskl s jdakwus sd sdskkl sds sdsds djslkl
Inc said they plan to form a venture to manage the money market
替换后TabSpace文件内容如下:
Inc said they plan to form a venture to manage the money market
Inc ssss adfs sdjf sd dskl s jdakwus sd sdskkl sds sdsds djslkl
Inc said they plan to form a venture to manage the money market
Inc ssss adfs sdjf sd dskl s jdakwus sd sdskkl sds sdsds djslkl
Inc said they plan to form a venture to manage the money market
3. 编写一小段Scala代码,从一个文件读取内容并把所有字符数大于12的单词打印到控制台。如果你能用单行代码完成会有额外奖励
程序代码:
- object CheckString extends App{
- val FileName="CheckString"
- val path="src\\"+FileName+".txt"
- println(FileName+"的字符串为:")
- io.Source.fromFile(path).mkString.split("\\s+").foreach (str => if(str.length()>12) println(str))
- }
运行结果:
的字符串为:
Incsaidtheyplan
jdakwussdsdskkl
managethemoneymarket
dsklsjdakwussdsdskkl
venturetomanagethe
4. 编写Scala程序,从包含浮点数的文本文件读取内容,打印出文件中所有浮点数之和,平均值,最大值和最小值
程序代码:
- object ReadNumber extends App{
- val pattern="(\\d+[.]\\d+)".r
- val pattern1="^\\d+(\\.\\d+)?".r
- val pattern2="[0-9]+(\\.\\d+)?".r
- val FileName="NumberFile"
- val path = "src\\"+FileName+".txt"
- val FileStr=io.Source.fromFile(path).mkString
- val StrArray=pattern2.findAllIn(FileStr).toArray
- var total=0d
- val len=StrArray.length
- StrArray.foreach (total +=_.toDouble)
- println("文本中浮点数总和: "+total)
- println("文本中浮点数平均数: "+total/len+len)
- println("文本中浮点数的最大值: "+StrArray.max)
- println("文本中浮点数的最大值: "+StrArray.min)
- }
测试数据:
joint 55 666.0 management 13.5 committee 12.5
joint 6.0 123.4 management 3.14 committee 170.5
joint 52 63.32 management 10.4 committee 12.5
运行结果:
文本中浮点数总和: 1188.26
文本中浮点数平均数: 99.0216666666666612
文本中浮点数的最大值: 666.0
文本中浮点数的最大值: 10.4
5. 编写Scala程序,向文件中写入2的n次方及其倒数,指数n从0到20。对齐各列:
1 1
2 0.5
4 0.25
... ...
程序代码:
- import java.io.PrintWriter
- object index extends App{
- val FileName="Index"
- val path="src\\"+FileName+".txt"
- val out=new PrintWriter(path)
- for (i <- 0 to 20)
- out.println(OutIndex(i))
- out.close
- def OutIndex(n:Int)={
- val value=math.pow(2, n)
- ""*4+value.toInt+""*(11-value.toString().size)+math.pow(2, -n)
- }
- }
运行结果:
1 1.0
2 0.5
4 0.25
8 0.125
16 0.0625
32 0.03125
64 0.015625
128 0.0078125
256 0.00390625
512 0.001953125
1024 9.765625E-4
2048 4.8828125E-4
4096 2.44140625E-4
8192 1.220703125E-4
16384 6.103515625E-5
32768 3.0517578125E-5
65536 1.52587890625E-5
131072 7.62939453125E-6
262144 3.814697265625E-6
524288 1.9073486328125E-6
1048576 9.5367431640625E-7
6. 编写正则表达式,匹配Java或C++程序代码中类似"like this,maybe with \" or\\"这样的带引号的字符串。编写Scala程序将某个源文件中所有类似的字符串打印出来
描述:没看太懂,按自己意思来的
程序代码:
- import scala.io.Source
- object regExp extends App{
- val FileName="Regexp"
- val path="src\\"+FileName+".txt"
- val pat1=""""like this,maybe with \\" or\\{2}"""".r
- val pat2="""like this,maybe with \\" or\\{2}""".r
- val pat3="""\w+\s+\\"""".r
- val linesIterator1=Source.fromFile(path).getLines()
- val linesIterator2=Source.fromFile(path).getLines()
- val linesIterator3=Source.fromFile(path).getLines()
- println("文本中包含:"+""""like this,maybe with \" or\\"""")
- linesIterator1.foreach(line=>pat1.findAllIn(line).foreach (println))
- println("文本中包含:"+"""like this,maybe with \" or\\""")
- linesIterator2.foreach(line=>pat2.findAllIn(line).foreach (println))
- println("文本中包含:"+"\\w+\\s+\"")
- linesIterator3.foreach(line=>pat3.findAllIn(line).foreach(println))
- }
运行结果:
文本中包含:"like this,maybe with \" or\\"
"like this,maybe with \" or\\"
文本中包含:like this,maybe with \" or\\
like this,maybe with \" or\\
like this,maybe with \" or\\
like this,maybe with \" or\\
文本中包含:\w+\s+"
with \"
with \"
with \"
7. 编写Scala程序,从文本文件读取内容,并打印出所有的非浮点数的词法单位。要求使用正则表达式
程序代码:
- import io.Source
- object NonFloat extends App{
- val source = Source.fromFile("src\\NumberFile.txt").mkString
- val pat1 = """[^((\d+\.){0,1}\d+)^\s+]+$""".r//去掉+试试
- val pat2 = """^((?!^[-]?\d*\.\d+$).)+$""".r
- println("不包含整数:")
- for(token <- source.split("\\s+")){
- for(word <- pat1.findAllIn(token))
- if(!word.equals("")){
- println(token)
- }
- }
- println("包含整数:")
- for(token <- source.split("\\s+")){
- for(word <- pat2.findAllIn(token))
- println(word)
- }
- }
测试数据:
joint 55 666.0 management 13.5 committee 12.5
joint 6.0 123.4 management 3.14 committee 170.5
joint 52 63.32 management 10.4 committee 12.5
0.12t 20 5.6ef 45.77ghjss 5.94 dfdxsccxz 7.9
运行结果:
不包含整数:
joint
management
committee
joint
management
committee
joint
management
committee
0.12t
5.6ef
45.77ghjss
dfdxsccxz
包含整数:
joint
management
committee
joint
management
committee
joint
management
committee
0.12t
5.6ef
45.77ghjss
dfdxsccxz
8. 编写Scala程序打印出某个网页中所有img标签的src属性。使用正则表达式和分组
程序代码:
- object WebSrc extends App{
- val pat = """<img.*?src=["'](.+?)["'].*?>""".r
- for (pat(src) <-pat.findAllIn(io.Source.fromURL("http://www.baidu.com").mkString)) {
- println(src)
- }
- }
运行结果:
//www.baidu.com/img/bd_logo1.png
//www.baidu.com/img/baidu_jgylogo3.gif
9. 编写Scala程序,盘点给定目录及其子目录中总共有多少以.class为扩展名的文件
程序代码:
- import java.io.File
- object NumDir extends App{
- val path = "."
- val dir = new File(path)
- def subdirs(dir:File):Iterator[File]={
- val children = dir.listFiles().filter(_.getName.endsWith("class"))
- children.toIterator ++ dir.listFiles().filter(_.isDirectory).toIterator.flatMap(subdirs _)
- }
- val n = subdirs(dir).length
- println(n)
- }
运行结果:
52
10. 扩展那个可序列化的Person类,让它能以一个集合保存某个人的朋友信息。构造出一些Person对象,让他们中的一些人成为朋友,然后将Array[Person]保存到文件。将这个数组从文件中重新读出来,校验朋友关系是否完好 注意,请在main中执行。脚本执行无法序列化。
程序代码:
- import collection.mutable.ArrayBuffer
- import java.io.{ObjectInputStream, FileOutputStream, FileInputStream, ObjectOutputStream}
- class Person(var name:String) extends Serializable{
- val friends = new ArrayBuffer[Person]()
- def addFriend(friend : Person){
- friends += friend
- }
- override def toString() = {
- var str = "My name is " + name + " and my friends name is "
- friends.foreach(str += _.name + ",")
- str
- }
- }
- object PersonTest extends App{
- val p1 = new Person("JackChen")
- val p2 = new Person("Jhon·D")
- val p3 = new Person("Sunday")
- p1.addFriend(p2)
- p1.addFriend(p3)
- println(p1)
- val out = new ObjectOutputStream(new FileOutputStream("src\\Person.txt"))
- out.writeObject(p1)
- out.close()
- val in = new ObjectInputStream(new FileInputStream("src\\Person.txt"))
- val p = in.readObject().asInstanceOf[Person]
- println(p)
- }
运行结果:
My name is JackChen and my friends name is Jhon·D,Sunday,
My name is JackChen and my friends name is Jhon·D,Sunday,
如果,您认为阅读这篇博客让您有些收获,不妨点击一下右下角的【推荐】。
如果,您希望更容易地发现我的新博客,不妨点击一下左下角的【关注我】。
如果,您对我的博客所讲述的内容有兴趣,请继续关注我的后续博客,我是【Sunddenly】。本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利
Scala学习(九)练习的更多相关文章
- Scala学习(九)---文件和正则表达式
文件和正则表达式 摘要: 在本篇中,你将学习如何执行常用的文件处理任务,比如从文件中读取所有行或单词,或者读取包含数字的文件等.本篇的要点包括: 1. Source.fromFile(...).get ...
- Scala学习九——文件和正则表达式
一.本章要点 Source.fromFile(...).getLines.toArray输出文件的所有行; Source.fromFile(...).mkString以字符串形式输出文件内容; 将字符 ...
- Scala学习资源
Scala学习资源: Scala官方网站:http://www.scala-lang.org/ Scala github:https://github.com/scala/scala Twitter ...
- 【Todo】【读书笔记】大数据Spark企业级实战版 & Scala学习
下了这本<大数据Spark企业级实战版>, 另外还有一本<Spark大数据处理:技术.应用与性能优化(全)> 先看前一篇. 根据书里的前言里面,对于阅读顺序的建议.先看最后的S ...
- 机器学习(三)--- scala学习笔记
Scala是一门多范式的编程语言,一种类似Java的编程语言,设计初衷是实现可伸缩的语言.并集成面向对象编程和函数式编程的各种特性. Spark是UC Berkeley AMP lab所开源的类Had ...
- 【Scala】Scala学习资料
Scala学习资料 java 树形 分类器_百度搜索 决策树分类器-Java实现 - CSDN博客 KNN分类器-Java实现 - CSDN博客 学习:java设计模式—分类 - 飞翔荷兰人 - 博客 ...
- Scala学习网址
scala学习网址为:https://twitter.github.io/scala_school/zh_cn https://www.zhihu.com/question/26707124
- Spark之Scala学习
1. Scala集合学习: http://blog.csdn.net/lyrebing/article/details/20362227 2. scala实现kmeans算法 http://www.t ...
- 基于.net的分布式系统限流组件 C# DataGridView绑定List对象时,利用BindingList来实现增删查改 .net中ThreadPool与Task的认识总结 C# 排序技术研究与对比 基于.net的通用内存缓存模型组件 Scala学习笔记:重要语法特性
基于.net的分布式系统限流组件 在互联网应用中,流量洪峰是常有的事情.在应对流量洪峰时,通用的处理模式一般有排队.限流,这样可以非常直接有效的保护系统,防止系统被打爆.另外,通过限流技术手段,可 ...
随机推荐
- Android 9.0更新
北京时间2018年8月7日上午,Google 发布了 Android 9.0 操作系统.并宣布系统版本 Android P 被正式命名为代号"Pie". Android 9.0 利 ...
- [随时更新][Android]小问题记录
此文随时更新,旨在记录平时遇到的不值得单独写博客记录的细节问题,当然如果问题有拓展将会另外写博客. 原文地址请保留http://www.cnblogs.com/rossoneri/p/4040314. ...
- Android Thread 官方说明
Thread官方说明 https://developer.android.google.cn/reference/java/lang/Thread Thread是程序中执行的线程.Java虚拟机允许应 ...
- linux(centos)无中文输入,如何解决
1.终端执行安装命令 yum install "@Chinese Support" 2.如下图,多出Input method 3.点击进行配置 4.reboot重启系统,新建一个文 ...
- 【redis专题(7)】命令语法介绍之Pub/Sub
Redis 发布订阅(pub/sub)是一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收消息.主要的目的是解耦消息发布者和消息订阅者之间的耦合,这点和设计模式中的观察者模式比较相似.p ...
- java----八种排序算法
1.直接插入排序 经常碰到这样一类排序问题:把新的数据插入到已经排好的数据列中. 将第一个数和第二个数排序,然后构成一个有序序列 将第三个数插入进去,构成一个新的有序序列. 对第四个数.第五个数……直 ...
- IIS ip访问限制插件
Dynamic IP Restrictions Overview The Dynamic IP Restrictions Extension for IIS provides IT Professio ...
- CentOS7中安装MySQL5.7
安装必要的组件 yum install –y autoconf automake imake libxml2-devel expat-devel cmake gcc gcc-c++ libaio li ...
- Process 0:0:0 (0x1ffc) Worker 0x00000001E580A1A0 appears to be non-yielding on Scheduler 3. Thread creation time: 13153975602106.
现场报错如下: Process 0:0:0 (0x1ffc) Worker 0x00000001E580A1A0 appears to be non-yielding on Scheduler 3. ...
- Android重复依赖解决办法
参考文章:https://blog.csdn.net/qq_24216407/article/details/72842614 在build.gradle引用了Vlc的安卓包:de.mrmaffen: ...