I have this string:
"The quick brown f0x jumps 0ver the lazy d0g, the quick brown f0x jumps 0ver the lazy d0g.".
I need a function that will replace all zeros between "brown" and "lazy" with "o". So the output will look like this:
"The quick brown fox jumps over the lazy d0g, the quick brown fox jumps over the lazy d0g.".
So it will look all over the string and most importantly will leave all other zeros intact.
function(text, leftBorder, rightBorder, searchString, replaceString) : string;
Is there any good algorithm?
If you have Python, here's an example using just string manipulation, eg split(), indexing etc. Your programming language should have these features as well.
>>> s="The quick brown f0x jumps 0ver the lazy d0g, the quick brown f0x jumps 0ver the lazy d0g."
>>> words = s.split("lazy")
>>> for n,word in enumerate(words):
... if "brown" in word:
... w = word.split("brown")
... w[-1]=w[-1].replace("0","o")
... word = 'brown'.join(w)
... words[n]=word
...
>>> 'lazy'.join(words)
'The quick brown fox jumps over the lazy d0g, the quick brown fox jumps over the lazy d0g.'
>>>
The steps:
Split the words on "lazy" to an array A
Go through each element in A to look for "brown"
if found , split on "brown" into array B. The part you are going to change is the
last element
replace it with whatever methods your programming language provides
join back the array B using "brown"
update this element in the first array A
lastly, join the whole string back using "lazy"
Related
If I have a search box and want to find if a string contains certain words (not case sensitive) and/or numbers.
search = "Brown lazy 46"
textline = "The quick brown fox jumped over 46 lazy dogs"
if string.match(textline, search) then
result = textline
end
just like a web search.
search = "Brown lazy 46"
textline = "The quick brown fox jumped over 46 lazy dogs"
for item in string.gmatch(search, "%S+") do
if string.find(textline, string.lower(item)) then
result = textline
end
end
You break up the word values you are looking for and convert them into an array. Then you should loop that array and check if your main variable is in it.
If I understood correctly what you want to do this should solve your problem.
I've try to break down a sentence to letters and rearrange them alphabetically.
Please see if i could improve this code in someway.
Kind regards
sen = "the quick brown fox jumps over the lazy dog"
smallest=[]
re=''
while len(sen) >0:
smallest.append( min(sen))
print(ord(min(sen)))
re=re+min(sen)
sen = sen[:sen.index(min(sen))]+sen[sen.index(min(sen))+1:]
counter+=1
print(smallest) #list
print(re) #string
What you're doing is exactly like sorting an array of number. You got a value for each char. There is many way to sort an array of number some are pretty fast but depends on what you're seeking. I think the fastest are insertion sort, bubble sort, or selection sort.
You can code them or find them already done in many languages.
There is other way to sort an array you can fin all of them here :
https://en.wikipedia.org/wiki/Sorting_algorithm#Comparison_of_algorithms
In Python, I can splice string like this:
solo = A quick brown fox jump over the lazy dog
solo[3:5]
I know substr and comb is enough, I want to know if it's possible, However. Should I use a role to do this?
How to slice a string
Strings (represented as class Str) are seen as single values and not positional data structures in Perl 6, and thus can't be indexed/sliced with the built-in [ ] array indexing operator.
As you already suggested, comb or substr is the way to go:
my $solo = "A quick brown fox jumps over the lazy dog";
dd $solo.comb[3..4].join; # "ui"
dd $solo.comb[3..^5].join; # "ui"
dd $solo.substr(3, 2); # "ui"
dd $solo.substr(3..4); # "ui"
dd $solo.substr(3..^5); # "ui"
If you want to modify the slice, use substr-rw:
$solo.substr-rw(2..6) = "slow";
dd $solo; # "A slow brown fox jumps over the lazy dog"
How to make operator [ ] work directly on strings
If you really wanted to, you could compose a role into your string that adds method AT-POS, which would make the [ ] operator work on it:
my $solo = "A quick brown fox jumps over the lazy dog" but role {
method AT-POS ($i) { self.substr($i, 1) }
}
dd $solo[3..5]; ("u", "i", "c")
However, this returns slices as a list, because that's what the [ ] operator does by default. To make it return a concatenated string, you'd have to re-implement [ ] for type Str:
multi postcircumfix:<[ ]>(Str $string, Int:D $index) {
$string.substr($index, 1)
}
multi postcircumfix:<[ ]>(Str $string, Range:D $slice) {
$string.substr($slice)
}
multi postcircumfix:<[ ]>(Str $string, Iterable:D \slice) {
slice.map({ $string.substr($_, 1) }).join
}
my $solo = "A quick brown fox jumps over the lazy dog";
dd $solo[3]; # "u"
dd $solo[3..4]; # "ui"
dd $solo[3, 5]; # "uc"
You could extend the [ ] multi-candidates added here to handle all the other magic that the operator provides for lists, like:
from-the-end indices, e.g. $solo[*-1]
truncating slices, e.g. $solo[3..*]
adverbs, e.g. $solo[3..5]:kv
assignment, e.g. $solo[2..6] = "slow"
But it would take a lot of effort to get all of that right.
Also, keep in mind that overriding built-in operators to do things they weren't supposed to do, will confuse other Perl 6 programmers who will have to review or work with your code in the future.
I have a string, and I want to remove a specific section from it.
The situation is as follows:
var input:String = "The quick brown fox jumped over the lazy dog.";
var scrubStart:Number = input.indexOf("br");
var scrubEnd: Number = input.indexOf("la");
var output:String = input.substring(0,scrubStart) + input.substring(scrubEnd,input.length);
I have it set up like this because the input text is different every time, and the section I want to remove is in a different place every time, but the content of the section is the same.
The code I have doesn't work, and it returns everything after scrubStart. Where have I gone wrong?
Code from your question appears to operate as expected, resulting in:
The quick lazy dog.
This could be accomplished using string methods of either slice or substring.
Slice:
const input:String = "The quick brown fox jumped over the lazy dog.";
var output:String = input.slice(0, input.indexOf("br")) +
input.slice(input.indexOf("la"));
Substring:
const input:String = "The quick brown fox jumped over the lazy dog.";
var output:String = input.substring(0, input.indexOf("br")) +
input.substring(input.indexOf("la"));
Regex:
You could also implement regular expressions; although, by pattern instead of index.
const input:String = "The quick brown fox jumped over the lazy dog.";
var pattern:RegExp = /brown.*?the /;
var output:String = input.replace(pattern, "");
I need to know if two strings "match" where "matching" basically means that there is significant overlap between the two strings. For example, if string1 is "foo" and string2 is "foobar", this should be a match. If string2 was "barfoo", that should also be a match with string1. However, if string2 was "fobar", this should not be a match. I'm struggling to find a clever way to do this. Do I need to split the strings into lists of characters first or is there a way to do this kind of comparison already in Groovy? Thanks!
Using Apache commons StringUtils:
#Grab( 'org.apache.commons:commons-lang3:3.1' )
import static org.apache.commons.lang3.StringUtils.getLevenshteinDistance
int a = getLevenshteinDistance( 'The quick fox jumped', 'The fox jumped' )
int b = getLevenshteinDistance( 'The fox jumped', 'The fox' )
// Assert a is more similar than b
assert a < b
Levenshtein Distance tells you the number of characters that have to change for one string to become another
So to get from 'The quick fox jumped' to 'The fox jumped', you need to delete 6 chars (so it has a score of 6)
And to get from 'The fox jumped' to 'The fox', you need to delete 7 chars.
As per your examples, plain old String.contains may suffice:
assert 'foobar'.contains('foo')
assert 'barfoo'.contains('foo')
assert !'fobar'.contains('foo')