we are trying to add single quotes to each element of a set of string's in Scala
val s : Set[String] = Set("abcd","cdef")
Expected outcome is val s : Set[String] = Set("'abcd'","'cdef'")
tried multiple approaches
Approach1:
s.map(x => x.mkString("'", "', '", "'"))
Approach2:
s.map(x => '\'' + x + '\'')
Fairly new to Scala if someone could please point us in the right direction it would be of great help
I don't think there is no big difference between Scala and whichever language you've been using.
One thing you might want to use is String interpolation
and then you can write
s.map(x => s"'$x'")
you cannot similar similar pattern to wrap it with double quotes. So, when it comes to single or double quotes wrapping, go for hex values. hex 22 is for double quotes and hex 27 for single quotes. It will be easy to remember.
scala> val sq = Integer.parseInt("27",16).toChar
sq: Char = '
scala> val dq = Integer.parseInt("22",16).toChar
dq: Char = "
scala> val s : Set[String] = Set("abcd","cdef")
s: Set[String] = Set(abcd, cdef)
scala> s.map(x => s"$sq$x$sq")
res14: scala.collection.immutable.Set[String] = Set('abcd', 'cdef')
scala> s.map(x => s"$dq$x$dq")
res15: scala.collection.immutable.Set[String] = Set("abcd", "cdef")
hope, this adds clarity to such scenarios.
Related
val s = """ """Shom """
gives
:1: error: ';' expected but string literal found.
val s = """ """Shom"""
Tried to escape
val s = """ ""\"Shom """
result is not as expected.
s: String = " ""\"Shom"
Try with s string interpolator
val tripleQuote = """"""""" // 9 quotes in total
s"""${tripleQuote}Shom"""
res2: String = """Shom
or even inline it
s"""${"""""""""}Shom"""
s"""${List.fill(3)('"').mkString}Shom"""
s"""${"\""*3}Shom"""
which all output
"""Shom
because s String interpolators can take arbitrary expressions
s"meaning is ${41 + 1}"
// res4: String = meaning is 42
I want to know how can I replace a given string with stars except first character of string in kotlin
For e.g i have string "Rizwan" , I want it to be R*****
You can do it with padEnd():
val name = "Rizwan"
val newName = name[0].toString().padEnd(name.length, '*')
Result:
"R*****"
Try replace center of String like Phone number. with ★:
phone.replaceRange(2 , phone.length-3 , "★".repeat(phone.length-5))
pay attention to 2 + 3 = 5 :D
Result:
"09★★★★★229"
Try replacing (?<=.). with *:
val input = "Rizwan"
val output = input.replace(Regex("(?<=.)."), "*")
println(output)
This prints:
R*****
The lookbehind (?<=.) in the regex pattern ensures that we only replace a character if at least one character precedes it. This spares the first character from being replaced.
I am not an expert in Kotlin so this may not be the best way to do it but it will work for sure.
var s = "Rizwan"
var l = s.length
val first = s[0]
s=""
while(l>1) {
s=s+"*"
l--
}
s=first+s
print(s)
Basic Algorithm..... using no library or functions
val name = "Rizwan"
val newName = name[0].toString().padEnd(name.length, '*')
Result:
"R*****"
To remove characters from a List of Strings I use :
val validLines : List[String] = List("test[" , "test]")
val charsToClean: List[String] = List("\"", "[", "]", "'")
val filtered = validLines.map(line => line.replace(charsToClean(0), "")
.replace(charsToClean(1), "")
.replace(charsToClean(2), "")
.replace(charsToClean(3), ""))
I'm attempting use a inner map function instead of hard coding the positions of the chars to replace :
val filtered1 : List[String] = validLines.map(line => charsToClean.map {c => line.replace(c , "") })
But receive compiler error :
mismatch; found : List[List[String]] required: List[String]
Should result of line.replace(c , "") not be returned ?
No, because your code is more like: for every string and for every unwanted char, return replacement for this very symbol (n^2 strings).
What you probably wanted to do can be achieved using the following snippet:
val rawLines : List[String] = List("test[" , "test]")
val blacklist: Set[Char] = Set('\"', '[', ']' ,''')
rawLines.map(line => line.filterNot(c => blacklist.contains(c)))
// res2: List[String] = List(test, test)
Or alternatively, using regexp, as #ka4eli has shown in his answer.
replaceAll can contain more complex regex constructed with | (or) operator:
validLines.map(_.replaceAll("\\[|\\]|'|\"", ""))
You need to use foldLeft instead of map.
val validLines : List[String] = List("test[" , "test]")
val charsToClean: List[String] = List("\"", "[", "]", "'")
val filtered : List[String] = validLines.map(line => charsToClean.foldLeft(line)(_.replace(_, "")))
Equivalent to #om-nom-nom though using a for comprehension syntax; given a set of characters to erase,
val charsToClean = """"[]'""".toSet
then
for (l <- validLines) yield l.filterNot(charsToClean)
where implicit set inclusion Char => Boolean function is applied in filterNot.
I am writing a small program in Spark using Scala, and came across a problem. I have a List/RDD of single word strings and a List/RDD of sentences which might or might not contain words from the list of single words. i.e.
val singles = Array("this", "is")
val sentence = Array("this Date", "is there something", "where are something", "this is a string")
and I want to select the sentences that contains one or more of the words from singles such that the result should be something like:
output[(this, Array(this Date, this is a String)),(is, Array(is there something, this is a string))]
I thought about two approaches, one by splitting the sentence and filtering using .contains. The other is to split and format sentence into a RDD and use the .join for RDD intersection. I am looking at around 50 single words and 5 million sentences, which method would be faster? Are there any other solutions? Could you also help me with the coding, I seem to get no results with my code (although it compiles and run without error)
You can create a set of required keys, look up the keys in sentences and group by keys.
val singles = Array("this", "is")
val sentences = Array("this Date",
"is there something",
"where are something",
"this is a string")
val rdd = sc.parallelize(sentences) // create RDD
val keys = singles.toSet // words required as keys.
val result = rdd.flatMap{ sen =>
val words = sen.split(" ").toSet;
val common = keys & words; // intersect
common.map(x => (x, sen)) // map as key -> sen
}
.groupByKey.mapValues(_.toArray) // group values for a key
.collect // get rdd contents as array
// result:
// Array((this, Array(this Date, this is a string)),
// (is, Array(is there something, this is a string)))
I've just tried to solve your problem and I've ended up with this code:
def check(s:String, l: Array[String]): Boolean = {
var temp:Int = 0
for (element <- l) {
if (element.equals(s)) {temp = temp +1}
}
var result = false
if (temp > 0) {result = true}
result
}
val singles = sc.parallelize(Array("this", "is"))
val sentence = sc.parallelize(Array("this Date", "is there something", "where are something", "this is a string"))
val result = singles.cartesian(sentence)
.filter(x => check(x._1,x._2.split(" ")) == true )
.groupByKey()
.map(x => (x._1,x._2.mkString(", ") )) // pay attention here(*)
result.foreach(println)
The last map line (*) is there just beacause without it I get something with CompactBuffer, like this:
(is,CompactBuffer(is there something, this is a string))
(this,CompactBuffer(this Date, this is a string))
With that map line (with a mkString command) I get a more readable output like this:
(is,is there something, this is a string)
(this,this Date, this is a string)
Hope it could help in some way.
FF
When I split a String using , works as expected :
val line1 = "this,is,a,test" //> line1 : String = this,is,a,test
val sLine = line1.split(",")
however if I use | the String is split into its character elements and added to array :
val line1 = "this|is|a|test" //> line1 : String = this|is|a|test
val sLine = line1.split("|") //> sLine : Array[String] = Array("", t, h, i, s, |, i, s, |, a, |, t, e, s, t)
Why is this occurring because of | character ?
possible solutions
val sLine2 = line1.split('|')
because ' denotes a character, a single character, split does not treat it as a regexp
val sLine2 = line1.split("\\|")
to escape the special alternation | regexp character. This is why it isn't working. split is treating | as a zero width regexp and so the string is vapourized into its component characters
As pipe is a special regex character, I believe you need to escape it like so "\\|" in order for it to work
scala> val line1 = "this,is,a,test"
line1: java.lang.String = this,is,a,test
scala> line1.split(",")
res2: Array[java.lang.String] = Array(this, is, a, test)
scala> var line2 = "this|is|a|test"
line2: java.lang.String = this|is|a|test
scala> line2.split("\\|")
res3: Array[java.lang.String] = Array(this, is, a, test)