I want to get the most frequent k-sized substring in a string. To achieve this, I'm using a table to store the number of occurrences for each substring. Here's the code:
function frequentWords(seq, k)
local subs = ""
local counter = {}
for i = 1,(seq:len()-k+1) do
subs = seq:sub(i, i+k-1)
counter[subs] = (counter[subs] and counter[subs] + 1 or 1)
--print(subs .. ": " .. counter[subs])
end
end
The line counter[subs] = (counter[subs] and counter[subs] + 1 or 1) has the same mean of counter[subs] = (counter[subs] ? counter[subs]+1 : 1). This line would be only counter[subs] = counter[subs] + 1 if we could set every new counter element with 0. Is this possible in Lua? If not, what's the best way of doing something similar?
For instance, in Ruby, this is done by declaring a Hash like this:
counter = Hash.new(0)
You can set an __index metamethod in counter to return 0:
setmetatable(counter,{__index=function () return 0 end})
but this is easier and clearer:
counter[subs] = (counter[subs] or 0) + 1
For your case, lhf's solution is sufficient. Just for completeness, I would like to mention there is a more complex way, that enables some slightly more complex functionality. Specifically, it acts "as you would expect" when you use it with mutable values, such as table: it both creates the item, and assigns it to the key when created.
function defaultTable(constructor)
local new = {}
local function initIndex(key)
local value = constructor()
new[key] = value
return value
end
setmetatable(new, {__index=initIndex})
return new
end
Related
I have an array like t. It contains the numbers and I would like to add to each number the previous ones. For example: t=[0,2,3,5] and I would like to get tnew=[0,2,5,10]. I tried out this code but it is wrong for sure. (There are 5292 values)
for i=0:5292
t(i)=t(i)+t(i+1)
end
For some array t = [0,2,3,5];, you can just do tnew = cumsum(t).
If you really want to do this in a loop, you need to start from the 2nd index, and keep adding to the value from the previous index
t = [0,2,3,5];
tnew = t;
for ii = 2:numel(t)
tnew(ii) = t(ii) + tnew(ii-1);
end
i would like to work with cellranges within my macro.
Function SumIfColor(SumRange)
Dim oRange as object
Dim oSheet as object
' Get Access to the Active Spreadsheet
oSheet = ThisComponent.CurrentController.ActiveSheet
' Get access to the Range listed in Sum Range
oRange = oSheet.getCellRangeByName(SumRange).RangeAddress
End Function
The question is how can I call this function with real cellRange object instead of String. Because getCellRangeByName works only with String variable.
Because when I call the function like this
sumifcolor(B1:B3)
I got the following error:
"Object variable not set"
I read some hint here but it did not helped me.
It is not possible to pass an actual CellRange object. One solution is to pass the row and column number, similar to the second part of #Axel Richter's answer in the link:
Function SumIfColor(lcol1, lrow1, lcol2, lrow2)
sum = 0
oCellRange = ThisComponent.CurrentController.ActiveSheet.getCellRangeByPosition(_
lcol1-1,lrow1-1,lcol2-1,lrow2-1)
For lCol = 0 To oCellRange.Columns.Count -1
For lRow = 0 To oCellRange.Rows.Count -1
oCell = oCellRange.getCellByPosition(lCol, lRow)
If oCell.CellBackColor > -1 Then
sum = sum + oCell.Value
End If
Next
Next
SumIfColor = sum
End Function
To call it:
=SUMIFCOLOR(COLUMN(B1:B3),ROW(B1),COLUMN(B3),ROW(B3))
The sum will be recalculated whenever a value in the range B1:B3 is changed, because of COLUMN(B1:B3). However, apparently changing only the color of a cell does not cause it to be recalculated.
I am working on implementing queue data structures using cell arrays in Matlab. I am trying to write functions to advance the queue by one, as well as search the queue for a specific item. At the moment the function looks like this (car types are the example data).
function q = createQueue()
q={};
q = enqueue(q,'Hilux');
q = enqueue(q,'E-Type');
q = enqueue(q,'Beetle');
q = enqueue(q,'Enzo');
q = enqueue(q,'Boxter');
q = dequeue(q)
q = searchQueue(q,'Boxter')
end
% Adds an item to the end of the queue. Returns the new queue.
function q = enqueue(q,item)
q{end+1} = item;
end
function [q item] = dequeue(q)
q{1} = {};
q{1} = q{2};
q{2} = q{3};
q{3} = q{4};
q{4} = q{5};
q{5} = {};
end
function answer = searchQueue(q, item)
for i = 1:length(q)
if q{i} == item
answer = fprintf('FOUND AT INDEX %d',i);
break
else
disp('-1')
end
end
end
Currently, the dequeue function leaves an empty cell, rather than removing the cell entirely. Is this able to be avoided? The searchQueue function also returns an error, and i am lost as to why. Thanks
Here's a rough first cut at it using matlab's object oriented capabilities. The reason for creating a class is to get reference semantics from the handle type, which allows for the dequeue/enqueue functions to modify the cell array directly, removing the need for reassignment.
I believe the code example below answers your main question about how to dequeue without leaving an empty cell (could be used without the OOP approach to the same effect).
To answer your question about what's wrong with your search: 1) the comparison q{i} == item gives you problems because == compares characters (which fails if there is a string size mismatch) whereas you wanted to use isequal(); 2) you wanted sprintf rather than fprintf; and 3) while not strictly wrong, the else in your loop happens on every item that doesn't match, which is probably not what you wanted.
classdef Queue < handle
properties
data = {}
end
methods
function q = Queue()
% TODO: do something in constructor if desired
end
function item = dequeue(q)
% TODO: bounds check
item = q.data(1);
q.data = q.data(2:end);
end
function enqueue(q,item)
q.data{end+1} = item;
end
function answer = search(q, item)
% TODO: check for multiple occurrences
answer = 'NOT FOUND';
for i = 1:length(q.data)
if isequal(q.data{i},item)
answer = sprintf('FOUND AT INDEX %d',i);
break
end
end
end
end
end
I have created a matrix with row 1 full of strings and 4 other rows with numbers. They are created in a handle class with the object "Projekter".
So in the object "Projekter" row 1, the first value is blank, but the second value is 'Ole'. So I know that 'Ole' is in (1,2). x is the name/string I want to search for, which in this case is 'Ole'.
As you see below it should search row 1 from column 2 untill the last name/string and if i = 'Ole', it should bring me the value 2 because " i " should be equal 2.
A is just a controller if the function works, but at this point it doesn't.
The error it gives is "Undefined function 'eq' for input arguments of type 'cell'."
How do I fix this so it return the " i " value when the statement is correct?
Thank you in advance!
function number(obj,x)
A = [];
for i = 2:size(obj.Projekter,2)
if obj.Projekter(1,i)==x
A = A + 1;
end
end
disp(A)
end
Maybe you have to index the cell content:
your_cell = {'a_string'};
your_string = your_cell{1};
function [returnValue] = number(obj,x)
for i = 2:size(obj.Projekter,2)
if obj.Projekter{1,i}==x
returnValue = i;
return;
end
end
end
Note the change from obj.Projekter(1,i)==x to obj.Projekter{1,i}==x (use curly braces instead of parens). I have then specified that returnValue will hold the value that should be returned by doing function [returnValue] = number(obj,x). We then set returnValue equal to i and return from the function when the condition of the if statement is true.
As suggested in the comments, it is probably better to do:
function [returnValue] = number(obj, x)
returnValue = find(strcmp(x, obj.Projekter) == 1);
strcmp(x, obj.Projektor) will give you an array the length of obj.Projekter with 1's wherever the strings match, and 0's where they don't, you can then find the indices that are set to 1. This has the added benefit of
not using a loop so it's faster
Giving you every occurrence of a match, not just the first one.
I am writing code which is supposed to read in data from an Excel worksheet, save it as strings into variables contained in an object of a class which I have defined and then add this object to an object tree of a class which I have also defined.
Dim ProdTreeMain As New CProdTree
Dim nR As Range
Dim nnR As Range
Set nR = oXS.Range("A1")
Set nnR = oXS.Range("A1")
dim r as integer
r = 1
Do While Not (nR.Text = "" And nnR.Text = "")
If CONDITION IS TRUE:
Dim currProd As New CProduct
ProdTreeMain.addProduct (currProd) '<-- error 438 "Object doesn't support property or method
End If
r = r + 1
Set nR = oXS.Range("A" & CStr(r + 1))
Set nR = oXS.Range("A" & CStr(r + 2))
Loop
The class CProdTree contains a sub "addProduct" which takes an input object of class CProduct by reference.
Public Sub addProduct(ByRef Prod As CProduct)
What the hell is going on? The class is defined, the sub correct, the variable type being passed to the sub is of the correct class and yet I get this error ... :/
You need to drop the parentheses around the argument. My favorite explanation is this Daily Dose of Excel post.
This line:
ProdTreeMain.addProduct (currProd)
becomes:
ProdTreeMain.addProduct currProd