I was trying to write an array which has classes that are "search queries" and I'm writing a for in loop that goes through them and picks out keywords using .rangeOfString:
import UIKit
var str = "Hello, playground"
class Search {
var searchQuery:String
init (query:String) {
self.searchQuery = query
}
}
var innocent = 0
var suspicious = 0
let keyword = "terrorist"
let queries = [Search(query: "How to be a terrorist"), Search(query: "How to join
Islamic State"), Search(query: "I want to be an astronaut")]
for query in queries {
if (query.searchQuery.rangeOfString(keyword) != nil){
suspicious++
}
else {
innocent++
}
}
print(suspicious)
I want to make it such that I can have more than one keyword (e.g Islamic State and terrorist) so if it detects either of these it increments suspicious, but I can't apply an OR operator to two strings for some reason, not sure why. Does anyone have any idea how this may be possible in Swift?
I'm new to Swift and my programming is rusty in general, so any tips on how to improve this code in general (is there another, better way to do this besides .rangeOfString, how would filtering through search queries actually be done in real life in search engines?) is much appreciated. Thank you!
Related
I have a sequence of code points as Sequence<Int>.
I want to get this into a String.
What I currently do is this:
val string = codePoints
.map { codePoint -> String(intArrayOf(codePoint), 0, 1) }
.joinToString()
But it feels extremely hairy to create a string for each code point just to concatenate them immediately after. Is there a more direct way to do this?
So far the best I was able to do was something like this:
val string2 = codePoints.toList().toIntArray()
.let { codePoints -> String(codePoints, 0, codePoints.size) }
The amount of code isn't really any better, and it has a toList().toIntArray() which I'm not completely fond of. But it at least avoids the packaging of everything into dozens of one-code-point strings, and the logic is still written in the logical order.
You can either go for the simple:
val string = codePoints.joinToString("") { Character.toString(it) }
// or
val string = codePoints.joinToString("", transform = Character::toString)
Or use a string builder:
fun Sequence<Int>.codePointsToString(): String = buildString {
this#codePointsToString.forEach { cp ->
appendCodePoint(cp)
}
}
This second one expresses exactly what you want, and may benefit from future optimizations in the string builder.
it feels extremely hairy to create a string for each code point just to concatenate them immediately after
Did you really measure a performance issue with the extra string objects created here? Using toList() would also create a bunch of object arrays behind the scenes (one for each resize), which is a bit less, but not tremendously better. And as you pointed out toIntArray on top of that is yet another array creation.
Unless you know the number of elements in the sequence up front, I don't believe there is much you can do about that (the string builder approach will also likely use a resizable array behind the scenes, but at least you don't need extra array copies).
val result = codePoints.map { Character.toString(it) }.joinToString("")
Edit, based on Joffrey's comment below:
val result = codePoints.joinToString("") { Character.toString(it) }
Additional edit, full example:
val codePoints: Sequence<Int> = sequenceOf(
'a'.code,
Character.toCodePoint(0xD83D.toChar(), 0xDE03.toChar()),
Character.toCodePoint(0xD83D.toChar(), 0xDE04.toChar()),
Character.toCodePoint(0xD83D.toChar(), 0xDE05.toChar())
)
val result = codePoints.joinToString("") { Character.toString(it) }
println(result)
This will print: a😃😄😅
I am making a little doc's text searching app using technical stack (react + nodejs + mongodb).
For example if I type "chicken" on my search bar , i will have a whole article about chicken in my result (e.g : The most popular chicken's restaurant closed tonight because of lot of complain about chicken taste, all the employee lost their jobs ) .
The text is often too long to display , what i want to do is get an extract of the word with keyword like : "The most popular chicken's restaurant ... about chicken taste..." .
Do you have any idea about a function than can do that ?
( keyword + max length in parameter). I tried with substring and splice and split but i can't find out the easiest way to do that.
Thanks!
I have given a try to search for the keyword, take two intial words and two words after keyword and joined them with "...".
var longString = "The most popular chicken's restaurant closed tonight because of lot of complain about chicken taste, all the employee lost their jobs";
var keyword = "chicken";
var allWords = longString.split(" ");
var indices = [];
allWords.forEach((word,index)=>{(word.indexOf(keyword)>=0) ? indices.push(index):""});
var newStrings = indices.map((i)=>{
let temp="";
temp=allWords[i-2]?allWords[i-2]+" ":"";
temp+=allWords[i-1]?allWords[i-1]+" ":"";
temp+=allWords[i];
temp+=allWords[i+1]?" "+allWords[i+1]:"";
temp+=allWords[i+2]?" "+allWords[i+2]:"";
return temp;
})
console.log("..."+newStrings.join("...")+"...");
You could use a regular expression that matches your keyword and some words before and after it, e.g. like this:
(?:\w+[\s,;:'".!?]+){0,3}(chicken)(?:[\s,;:'".!?]+\w+){0,3}
This will need some finetuning to take account of all possible punctuations, and other weird stuff of language that does not go well with the "word followed by space/comma,etc" schema. Take a look at the ASCII character classes, probably they will come handy.
const test_str = ` The most popular chicken's restaurant closed tonight because of lot of complain about chicken taste, all the employee lost their jobs`;
function mySearchFunc(str, lookup) {
const regex = new RegExp("(?:\\w+[\\s,;:'\".!?]+){0,3}" + lookup + "(?:[\\s,;:'\".!?]+\\w+){0,3}", "gm");
let m;
let result = [];
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
if (m)
//console.log(`Found: ${m}`);
result.push(m[0]);
}
return result.join(' ... ');
}
console.log(mySearchFunc(test_str, "chicken"));
I need to create a string from a array of map in groovy.
Required string = ^(123|456|789)
At present I am doing something like below, will this cause performance issue in production box or should I use StringBuffer java class?
def getProjectList(def caseResult) {
def projectList = ""
caseResult.each { projno ->
if (projectList.length() == 0) {
projectList = "^(${projno.project_no}|"
} else {
if (projectList.indexOf(projno.project_no) == -1)
projectList+="${projno.project_no}|"
}
}
projectList = projectList.substring(0, projectList.length() - 1)
projectList += ')'
return projectList
}
I'd go for ease of reading...
def getProjectList(def caseResult) {
"^(${caseResult.project_no.join('|')})"
}
Actually, you just want the unique ones don't you?
def getProjectList(def caseResult) {
"^(${caseResult.project_no.unique().join('|')})"
}
I need to create a string from a array of map in groovy.
It would be extremely useful to define parameter type then.
The return type too.
will this cause performance issue in production box
Well, define performance issue first. Have you measured anything to think your code has any performance issues? If not, it looks like a typical "premature optimization"
should I use StringBuffer java class
If you worry about performance, then you should rather use StringBuilder, as StringBuffer is thread-safe, taking a little performance hit.
If your code suffers from anything, it's rather readability than performance.
And I recommend you this StackExchange site, dedicated to such questions - https://codereview.stackexchange.com/ - give it a try!
"When you've found the treasure, stop digging!"
I'm wanting to use more functional programming in Groovy, and thought rewriting the following method would be good training. It's harder than it looks because Groovy doesn't appear to build short-circuiting into its more functional features.
Here's an imperative function to do the job:
fullyQualifiedNames = ['a/b/c/d/e', 'f/g/h/i/j', 'f/g/h/d/e']
String shortestUniqueName(String nameToShorten) {
def currentLevel = 1
String shortName = ''
def separator = '/'
while (fullyQualifiedNames.findAll { fqName ->
shortName = nameToShorten.tokenize(separator)[-currentLevel..-1].join(separator)
fqName.endsWith(shortName)
}.size() > 1) {
++currentLevel
}
return shortName
}
println shortestUniqueName('a/b/c/d/e')
Result: c/d/e
It scans a list of fully-qualified filenames and returns the shortest unique form. There are potentially hundreds of fully-qualified names.
As soon as the method finds a short name with only one match, that short name is the right answer, and the iteration can stop. There's no need to scan the rest of the name or do any more expensive list searches.
But turning to a more functional flow in Groovy, neither return nor break can drop you out of the iteration:
return simply returns from the present iteration, not from the whole .each so it doesn't short-circuit.
break isn't allowed outside of a loop, and .each {} and .eachWithIndex {} are not considered loop constructs.
I can't use .find() instead of .findAll() because my program logic requires that I scan all elements of the list, nut just stop at the first.
There are plenty of reasons not to use try..catch blocks, but the best I've read is from here:
Exceptions are basically non-local goto statements with all the
consequences of the latter. Using exceptions for flow control
violates the principle of least astonishment, make programs hard to read
(remember that programs are written for programmers first).
Some of the usual ways around this problem are detailed here including a solution based on a new flavour of .each. This is the closest to a solution I've found so far, but I need to use .eachWithIndex() for my use case (in progress.)
Here's my own poor attempt at a short-circuiting functional solution:
fullyQualifiedNames = ['a/b/c/d/e', 'f/g/h/i/j', 'f/g/h/d/e']
def shortestUniqueName(String nameToShorten) {
def found = ''
def final separator = '/'
def nameComponents = nameToShorten.tokenize(separator).reverse()
nameComponents.eachWithIndex { String _, int i ->
if (!found) {
def candidate = nameComponents[0..i].reverse().join(separator)
def matches = fullyQualifiedNames.findAll { String fqName ->
fqName.endsWith candidate
}
if (matches.size() == 1) {
found = candidate
}
}
}
return found
}
println shortestUniqueName('a/b/c/d/e')
Result: c/d/e
Please shoot me down if there is a more idiomatic way to short-circuit in Groovy that I haven't thought of. Thank you!
There's probably a cleaner looking (and easier to read) solution, but you can do this sort of thing:
String shortestUniqueName(String nameToShorten) {
// Split the name to shorten, and make a list of all sequential combinations of elements
nameToShorten.split('/').reverse().inject([]) { agg, l ->
if(agg) agg + [agg[-1] + l] else agg << [l]
}
// Starting with the smallest element
.find { elements ->
fullyQualifiedNames.findAll { name ->
name.endsWith(elements.reverse().join('/'))
}.size() == 1
}
?.reverse()
?.join('/')
?: ''
}
I found myself confronted with an interview question where the goal was to write a sorting algorithm that sorts an array of unsorted int values:
int[] unsortedArray = { 9, 6, 3, 1, 5, 8, 4, 2, 7, 0 };
Now I googled and found out that there are so many sorting algorithms out there!
Finally I could motivate myself to dig into Bubble Sort because it seemed pretty simple to start with.
I read the sample code and came to a solution looking like this:
static int[] BubbleSort(ref int[] array)
{
long lastItemLocation = array.Length - 1;
int temp;
bool swapped;
do
{
swapped = false;
for (int itemLocationCounter = 0; itemLocationCounter < lastItemLocation; itemLocationCounter++)
{
if (array[itemLocationCounter] > array[itemLocationCounter + 1])
{
temp = array[itemLocationCounter];
array[itemLocationCounter] = array[itemLocationCounter + 1];
array[itemLocationCounter + 1] = temp;
swapped = true;
}
}
} while (swapped);
return array;
}
I clearly see that this is a situation where the do { //work } while(cond) statement is a great help to be and prevents the use of another helper variable.
But is this the only case that this is more useful or do you know any other application where this condition has been used?
In general:
use do...while when you want the body to be executed at least once.
use while... when you may not want the body to be executed at all.
EDIT: I'd say the first option comes up about 10% of the time and the second about 90%. You can always re-factor to use either, in either circumstance. Use the one that's closest to what you want to say.
do...while guarantees that the body of code inside the loop executes at least once. This can be handy under certain conditions; when coding a REPL loop, for example.
Anytime you need to loop through some code until a condition is met is a good example of when to use do...while or while...
A good example of when to use do...while or while... is if you have a game or simulation where the game engine is continuously running the various components until some condition occurs like you win or lose.
Of course this is only one example.
The above posts are correct regarding the two conditional looping forms. Some languages have a repeat until form instead of do while. Also there's a minimalist view where only the necessary control structures should exist in a language. The while do is necessary but the do while isn't. And as for bubble sort you'll want to avoid going there as it's the slowest of the commonly known sorting algorithms. Look at selection sort or insertion sort instead. Quicksort and merge sort are fast but are hard to write without using recursion and perform badly if you happen to chose a poor pivot value.