searching and replacing using turing machine - turing-machines

[The language consists of 'a's and 'b's. Capital 'B' depicts the end points of the string stored on the tape]
http://i.stack.imgur.com/Y1H50.jpg
Search the string left-to-right for an a; as soon as one is found, replace
it by X, return to the left end, and search for b; replace it by X; return
to the left end and repeat these steps until one of the two searches is
unsuccessful.

Related

Parsing or extracting text from a single cell in a spreadsheet

Here is what I'm trying to do:
Starting with a cell that has something like "receipt 28.19 walmart" or "receipt 117.58 amazon", I would like to be able to extract the dollar amount to a different cell. So a function that will put 28.19 and 117.58 in each cell, respectively.
I know you can use the MID function to extract part of a string of text if you know where it starts and how long it is, but the problem here is it could be different lengths. For example, the 28.19 is length 5, but the 117.58 is length 6, so I couldn't use MID to always get the total dollar amount because the length of the desired substring is not always the same.
If anyone knows a solution to this, please respond. Also, if there is a solution for it that works in google spreadsheets, that would be acceptable as well. I'm assuming a lot of the functions are the same from excel to google sheets.
You want the number between the two blank space in the string given in the cell. So, for string like "receipt 2356.14789 amazon", we have to find pos of blanks.
Now, to find the position of nth space in a string we can use these formulas
1st Position : =find(" ",a1)
2nd Position : =find(" ",a1,find(" ",a1)+1)
3rd Position : =find(" ",a1,find(" ",a1,find(" ",a1)+1)+1)
...........
Nth position : =find(" ",a1,find(" ",a1,find(" ",a1,find(" ",a1,......n-1 times find(" ",a1)+1)+1)+1.....n-1 times +1)
So, using above formula finding first and second blank in the string :
The values we have 8 and 19 in B1 and C1 are the positions of the blank spaces in the string. Now our desired result need to be, let string be s then
s with removal of "receipt " & " amazon"
Now we can use the MID function to get the output.
MID function : =MID(text,start_num,num_chars)
returns the character from the middle of a text string, given a starting position and length.
So, we have starting position i.e first blank position(8), we have to calculate the num_chars i.e the length.
To calculate length we have to do following calculation :
subtract the len of "amazon" from total len of string
length of substring s2 : =len(a1)- FIND(" ",A1,FIND(" ",A1)+1) which is second blank pos.
add len of sub string1 and sub string2
s2 + FIND(" ",A1)
Now, using mid function we will get,
=MID(A1,FIND(" ",A1),F1,(LEN S1+ LEN S2))
Now trim this value since it will be containing a blank space in start of "receipt ".
=trim(MID(A1,FIND(" ",A1),F1,(LEN S1+ LEN S2)))
So, final formula is below :
=TRIM(MID(A1,FIND(" ",A1),LEN(A1)-FIND(" ",A1,FIND(" ",A1)+1)+FIND(" ",A1)))
2nd Method :
Use Delimit with select space. Shortcut ALT-> A->A->E , delimit select space , next then enter.
Output :
If you can guarantee that the format is [word][space][number][space][word], then this should work:
From here:
=TRIM(MID(SUBSTITUTE(A1," ",REPT(" ",LEN(A1))), (2-1)*LEN(A1)+1, LEN(A1)))
Where A1 has "receipt 28.19 walmart"
The simplest way and my personal favorite method of doing this is using regex extract:
=REGEXEXTRACT ("YOUR TEXT", "\D+(\d+\.?\d+)\D+?")
This is basically saying your string starts with any number of non digit characters \D+ followed by some digits \d+ , which may or may not include a decimal with another digit , possibly followed by more non digit characters
The parentheses around the digit patterns are what directs it to extract that value only
Note this is for Google sheets specifically since you said you were also wanting one on there
If you need to be able to sum your extracted text you can just wrap it in value:
=VALUE(REGEXEXTRACT (A1 , "\D+(\d+\.?\d+)\D+?"))
OR for even MORE convenience if you have a whole column of values you can enter a single formula that will run your regex all the way down:
=ARRAYFORMULA(IF(LEN(O1:O)>0,VALUE(REGEXEXTRACT (O1:O , "\D+(\d+\.?\d+)\D+?")),))
Consider the following. Take a look at how I went step by step to find the answer. =MID(A3,SEARCH(" ",A3)+1,SEARCH(" ",MID(A3,SEARCH(" ",A3)+1,LEN(A3)))-1). If you want to learn more, checkout this free lesson Len(), Search(), Trim() (link expires in 10 days).

number of strings which differ at atmost n positions?

I have been given a string T comprised of only 's','t','u','v' as characters . I want to find numbers of strings with length |T| which differs at atmost n position from T. Also each such string must not have same character at three different locations which are separated by same distance . My approach is using dynamic programming method such that dp[i][j][k] denotes number of such strings of length i , differing at j position while ending at kth character , where k=s,t,u,v .
if(k is same at ith character of T)
dp[i][j][k]=dp[i-1][j][0]+dp[i-1][j][1]+dp[i-1][j][2]-(strings which are extra due to k added at ith location and violates condition of same separated distance )
but i know this is wrong ?
eg for this is suppose T ='sstt' and we have to find strings which differ at most 2 locations then strings which do not have same character at three different location separated by same distance are 'tstt', 'ssts' and etc
This seems to be really interesting. Perhaps you could enumerate the number of two-pairs and then proceed with that?

Displaying positive symbol for positive elements in MATLAB array?

I have an array in MATLAB, and I wanted to display the positive symbol, "+" in front of positive elements, and keep the negative symbol, "-" in already existing negative values. I thought I could do the following:
I was thinking of constructing a sort of cell string or string array, and having an if, else system where if the numbers magnitude was >0, then I should store the value as '+' concatenated with the conversion of the element. If it was 0, just do a straight up char conversion since 0 has no sign, and if it was negative, just convert it. I know what to do, however, logistically, I think my order of commands is whacky.
How can I implement this?
I have the following script for an array x, but it just spews out values, I want an orderly string array I can copy and paste for use outside of MATLAB.
x;
pos = '+';
bound = length(x);
for i=1:bound
if(x(i)==0)
num2str(x(i))
end
if(x(i)>0)
num2str(x(i))
strcat(pos,num2str(x(i)))
end
if(x(i)<0)
num2str(x(i))
strcat(pos,num2str(x(i)))
end
end
I think you are searching for this.
Let's make an example.
First type in your command window :
test = 5;
Then:
sprintf('%+d',test)
You should have in this way what you want.
Of course you need to adapt it to your case. I suggest you to read this.
I hope it helps.

word search algorithm matlab [closed]

I am looking for an efficient word search algorithm. If you can help me come up with it it would be even better
a h c k
x r j i
b v t l
c y q s
and I want to find 'arts' . If 'stra' was also a valid word I want that to be found as well. (vertical, horizontal, diagonal and reverse). I came up with a few algorithms but thy don't seem efficient and consume long coding. First one included using find() to get the first letter and look at that column or rows.
Here's one way:
%// Example word grid
C = [
'a' 'h' 'c' 'k' 'r'
'x' 'r' 'j' 'i' 'p'
'b' 'v' 't' 'l' 'q'
'a' 'y' 'q' 's' 'o'];
%// Your search term
target = 'arts';
%// Optionally, do some obvious checks here.
%// - length of the search string may exceeds the grid's dimensions
%// - there are no matches for the first letter
%// - and similar
%// Form single cellstring containing all search directions
allDirections = [
%{
// horizontal, horizontal-reversed
%}
cellstr([C ; C(:,end:-1:1)])
%{
// vertical, vertical-reversed
%}
cellstr([C'; C(:,end:-1:1)'])
%{
// Diagonal, top-left to bottom-right, and reversed
%}
arrayfun(#(ii){diag(C,ii)'}, -size(C,1)+2:size(C,2)-2)';
arrayfun(#(ii){diag(C(end:-1:1,end:-1:1),ii)'}, -size(C,1)+2:size(C,2)-2)';
%{
// Diagonal, top-right to bottom-left, and reversed
%}
arrayfun(#(ii){diag(C(:,end:-1:1),ii)'}, -size(C,1)+2:size(C,2)-2)';
arrayfun(#(ii){diag(C(end:-1:1,:),ii)'}, -size(C,1)+2:size(C,2)-2)';
];
%// And now just find the string
strfind(allDirections , target)
Of course, you could to improve (memory) efficiency by
doing the strfind on all the directions separately
doing 2 × strfind on the same direction, but with the target inverted
etc.
But aside from these relatively minor optimizations, I don't think you can do much better in MATLAB in practice.
A theoretically more efficient recursive, branch-and-bound type search roughly goes like this:
find all occurrences of the first letter
eliminate all of those occurrences that cannot satisfy the length of the target based on the dimensions of the grid
Search the neighborhoods of all hits for occurrences of the second letter
Eliminate occurrences based on length, etc.
(Don't forget to filter on direction after the second letter, as the hits for the second letter fixes the search directions)
As far as I can see, this would need a lot less reads and comparisons than my version. However, my guess would be that using canned routines (as I did) is going to be faster and less complicated than using (nested) loops. But I could be wrong.
Try. Profile. Learn. Smile. Correct me :)

Lua - How to find a substring with 1 or 2 characters discrepancy

Say I have a string
local a = "Hello universe"
I find the substring "universe" by
a:find("universe")
Now, suppose the string is
local a = "un#verse"
The string to be searched is universe; but the substring differs by a single character.
So obviously Lua ignores it.
How do I make the function find the string even if there is a discrepancy by a single character?
If you know where the character would be, use . instead of that character: a:find("un.verse")
However, it looks like you're looking for a fuzzy string search. It is out of a scope for a Lua string library. You may want to start with this article: http://ntz-develop.blogspot.com/2011/03/fuzzy-string-search.html
As for Lua fuzzy search implementations — I haven't used any, but googing "lua fuzzy search" gives a few results. Some are based on this paper: http://web.archive.org/web/20070518080535/http://www.heise.de/ct/english/97/04/386/
Try https://github.com/ajsher/luafuzzy.
It sounds like you want something along the lines of TRE:
TRE is a lightweight, robust, and efficient POSIX compliant regexp matching library with some exciting features such as approximate (fuzzy) matching.
Approximate pattern matching allows matches to be approximate, that is, allows the matches to be close to the searched pattern under some measure of closeness. TRE uses the edit-distance measure (also known as the Levenshtein distance) where characters can be inserted, deleted, or substituted in the searched text in order to get an exact match. Each insertion, deletion, or substitution adds the distance, or cost, of the match. TRE can report the matches which have a cost lower than some given threshold value. TRE can also be used to search for matches with the lowest cost.
A Lua binding for it is available as part of lrexlib.
If you are really looking for a single character difference and do not care about performance, here is a simple approach that should work:
local a = "Hello un#verse"
local myfind = function(s,p)
local withdot = function(n)
return p:sub(1,n-1) .. '.' .. p:sub(n+1)
end
local a,b
for i=1,#s do
a,b = s:find(withdot(i))
if a then return a,b end
end
end
print(myfind(a,"universe"))
A simple roll your own approach (based on the assumption that the pattern keeps the same length):
function hammingdistance(a,b)
local ta={a:byte(1,-1)}
local tb={b:byte(1,-1)}
local res = 0
for k=1,#a do
if ta[k]~=tb[k] then
res=res+1
end
end
print(a,b,res) -- debugging/demonstration print
return res
end
function fuz(s,pat)
local best_match=10000
local best_location
for k=1,#s-#pat+1 do
local cur_diff=hammingdistance(s:sub(k,k+#pat-1),pat)
if cur_diff < best_match then
best_location = k
best_match = cur_diff
end
end
local start,ending = math.max(1,best_location),math.min(best_location+#pat-1,#s)
return start,ending,s:sub(start,ending)
end
s=[[Hello, Universe! UnIvErSe]]
print(fuz(s,'universe'))
Disclaimer: not recommended, just for fun:
If you want a better syntax (and you don't mind messing with standard type's metatables) you could use this:
getmetatable('').__sub=hammingdistance
a='Hello'
b='hello'
print(a-b)
But note that a-b does not equal b-a this way.

Resources