Strange Ruby String Selection - ruby

The string in question (read from a file):
if (true) then
{
_this = createVehicle ["Land_hut10", [6226.8901, 986.091, 4.5776367e-005], [], 0, "CAN_COLLIDE"];
_vehicle_10 = _this;
_this setDir -2.109278;
};
Retrieved from a large list of similar (all same file) strings via the following:
get_stringR(string,"if","};")
And the function code:
def get_stringR(a,b,c)
b = a.index(b)
b ||= 0
c = a.rindex(c)
c ||= b
r = a[b,c]
return r
end
As so far, this works fine, but what I wanted to do is select the array after "createVehicle", the following (I thought) should work.
newstring = get_string(myString,"\[","\];")
Note get_string is the same as get_stringR, except it uses the first occurrence of the pattern both times, rather then the first and last occurrence.
The output should have been: ["Land_hut10", [6226.8901, 986.091, 4.5776367e-005], [], 0, "CAN_COLLIDE"];
Instead it was the below, given via 'puts':
["Land_hut10", [6226.8901, 986.091, 4.5776367e-005], [], 0, "CAN_COLLIDE"];
_vehicle_10 = _this;
_this setDir
Some 40 characters past the point it should have retrieve, which was very strange...
Second note, using both get_string and get_stringR produced the exact same result with the parameters given.
I then decided to add the following to my get_string code:
b = a.index(b)
b ||= 0
c = a.index(c)
c ||= b
if c > 40 then
c -= 40
end
r = a[b,c]
return r
And it works as expected (for every 'block' in the file, even though the strings after that array are not identical in any way), but something obviously isn't right :).

You want r = a[b..c] instead of r = a[b,c].
Difference is: b..c = start from b, go to c, while b,c = start from b and move c characters to the right.
Edit: You don't have to/shouldn't escape the [ and ] either, because you are using strings and not regexen. Also, you have to take the length of the end ("];") into consideration, or you will cut off parts of the end.
def get_stringR(a,b,c)
bp = a.index(b) || 0
cp = a.rindex(c) || bp
r = a[bp..cp + c.size - 1]
return r
end
def get_string(a,b,c)
bp = a.index(b) || 0
cp = a.index(c) || bp
r = a[bp..cp + c.size - 1]
return r
end

Related

get pairs / triple / quadruple... of elements from vector by function

I have a vector with a couple of elements and I want to write a function that returns me all combinations of x items from this vector.
The following code produces the right output for the case x=2 or x=3 or x=4.
However, I can not implement a solution for every possible x following this idea.
values = {'A','B','C','D','E'};
n = length(values);
data2 = {}; % case x=2
for i = 1:n
for j = i+1:n
data2{end+1} = {values{i}, values{j}};
fprintf('%s %s\n',values{i}, values{j})
end
end
data3 = {}; % case x=3
for i = 1:n
for j = i+1:n
for k = j+1:n
data3{end+1} = {values{i}, values{j}, values{k}};
fprintf('%s %s %s\n',values{i}, values{j}, values{k})
end
end
end
data4 = {}; % case x=4
for i = 1:n
for j = i+1:n
for k = j+1:n
for l = k+1:n
data4{end+1} = {values{i}, values{j}, values{k}, values{l}};
fprintf('%s %s %s %s\n',values{i}, values{j}, values{k}, values{l})
end
end
end
end
How would a function look like which would be able to return my data variable?
data = getCombinations(values, x) %values is vector with elements, x is integer value
EDIT
The following code comes pretty close:
data = perms(values)
data = data(:,1:x)
data = unique(data,'rows')
but it still produces output like A,B and B,A
EDIT2
This fixed it somehow but it is not very nice to look at and it does not work for text entries in cells but only for numbers
data = perms(values)
data = data(:,1:x)
data = sort(data,2)
data = unique(data,'rows')
EDIT3
This did it but it is not very nice to look at... Maybe there is a better solution?
function [data] = getCombinations(values,x)
i = 1:length(values);
d = perms(i);
d = d(:,1:x);
d = sort(d,2);
d = unique(d,'rows');
data = v(d);
end
If you don't want repetitions (and your example suggests you don't) then try nchoosek as nchoosek(1:n, x) to give indices:
values = {'A','B','C','D','E'};
n = length(values);
x = 3;
C = nchoosek(1:n, x);
data = values(C)
In the above, each row is a unique combination of 3 of the 5 elements of values.
Alternatively pass in the values directly:
data = nchoosek(values, x);

Sorting large text file in python

Sort the content of a file based on second field, e.g.
Input file:
Jervie,12,M
Jaimy,11,F
Tony,23,M
Janey,11,F
Output file:
Jaimy,11,F
Janey,11,F
Jervie,12,M
Tony,23,M
We need to use external sort.
Input file can be of size 4GB. RAM is 1GB.
I used this but it does not work as it treats all the content as int. Also I have doubt related to the buffer size in each turn of the external sort. How to decide on that?
This sorts file with integers only.
file = open("i2.txt","r")
temp_files = []
e = []
while True:
temp_file = tempfile.TemporaryFile()
e = list(islice(file,2))
if not e:
break
e.sort(key=lambda line: int(line.split()[0]))
temp_file.writelines(e)
temp_files.append(temp_file)
temp_file.flush()
temp_file.seek(0)
file.close()
with open('o.txt', 'w') as out:
out.writelines(imap('{}\n'.format, heapq.merge(*(imap(int, f) for f in temp_files))))
out.close()
I am able to create temporary files sorted on the second field, but how do I merge them based on that?
I did it with the following code :
Divide the big file into smaller files. Here it is assumed that max 4 lines can be read. So I initially divide the file into lines of 4 and sort them and write into temp files. Then read these files in pairs 2 lines from each file and merge them. Corner cases are not handled but, this should be a starter for others to think.
f = open("i1.txt", "r")
temp_files = []
e = []
while True:
temp_file = tempfile.NamedTemporaryFile()
e = list(islice(f, 4))
if not e:
temp_file.close()
break
# e.sort(key=lambda line:int(line.split()[1]))
e.sort(key=lambda line: int(line.split()[1]))
temp_file.writelines(e)
temp_files.append(temp_file)
temp_file.flush()
temp_file.seek(0)
f.close()
aux = []
z = 0
while len(temp_files) != 1:
while z < len(temp_files)-1:
tem = tempfile.NamedTemporaryFile()
t1 = temp_files[z]
t2 = temp_files[z+1]
t1.seek(0)
t2.seek(0)
n = 2
e1 = None
e2 = None
while True:
if not e1:
e1 = list(islice(t1, 2))
if not e2:
e2 = list(islice(t2, 2))
if not e1 and not e2:
break
elif e1 and not e2:
tem.writelines(imap('{}'.format,e1))
e1 = None
continue
elif not e1 and e2:
tem.writelines(imap('{}'.format,e2))
e2 = None
continue
i = 0
j = 0
while i<len(e1) and j<len(e2):
l1 = e1[i]
l2 = e2[j]
if int(l1.split()[1]) == int(l2.split()[1]):
tem.writelines(imap('{}'.format,[l1,l2]))
i+=1
j+=1
elif int(l1.split()[1]) < int(l2.split()[1]):
tem.writelines(imap('{}'.format,[l1]))
i+=1
else:
tem.writelines(imap('{}'.format,[l2]))
j+=1
if i>=len(e1):
e1 = None
else:
e1 = e1[i:]
if j>= len(e2):
e2 = None
else:
e2 = e2[j:]
z+=2
aux.append(tem)
t1.close()
t2.close()
tem.flush()
tem.seek(0)
temp_files = aux
z = 0
aux = []
with open("o.txt",'w') as out:
out.writelines(imap('{}'.format,temp_files[0]))
Try using out of the core processing with Blaze (http://blaze.readthedocs.io/en/latest/ooc.html)

Lua: How can I concatenate methods, as with the string methods?

The string funtions can used with this ways:
string.FUNCTION('myString', PARAMETER)
or replace 'string' with the string to use and call it as method
('myString'):METHOD(PARAMETER)
The last way is very fine to read and allows to concatenate methods.
-- example string operation
some_str, pos = ' some string', 1
-- default string syntax
while string.find(string.sub(some_str, pos, pos), '%s') do pos = pos +1 end
-- the same with syntactic sugar
while some_str:sub(pos, pos):find('%s') do pos = pos +1 end
So I tried to get the same behaviour with my own functions. But this fails.
The only way I found, was to use an additional parameter to say: return the object itself or the result.
Here a simple example for this.
calc = {
result = 0,
operator = '',
run = function(self, a, b, r) -- return self with r='s'
if b == 's' then r, b = b, nil end
if not b then b, a = a, self.result end
if self.operator == '+' then self.result = (a) + (b)
elseif self.operator == '-' then self.result = (a) - (b)
elseif self.operator == '*' then self.result = (a) * (b)
elseif self.operator == '/' then self.result = (a) / (b) end
if r ~= nil then return self else return self.result end
end,
add = function(self, a, b, r) self.operator = '+' return self:run(a, b, r) end,
sub = function(self, a, b, r) self.operator = '-' return self:run(a, b, r) end,
mul = function(self, a, b, r) self.operator = '*' return self:run(a, b, r) end,
div = function(self, a, b, r) self.operator = '/' return self:run(a, b, r) end
}
-- single operation
result = calc:add(12, 5)
-- concatenated operations
result = calc:add(12, 5, 's'):sub(3, 's'):mul(2, 's'):div(7)
Exists any way to do it same like in string operations?
Thanks in advance.
Your subsequent calls assign 's' to b parameter, not to r. Of course check for return self fails. Rather than give different behaviour to methods with some flags make them always return self instead and make a separate method to return current result - it will be much cleaner to read and program.
After that your call will look like:
result = calc:new(12):add(5):sub(3):mul(2):div(7):result()
Also, you don't really need proxy functions that go into one big function that splits into ifs anyway - just do everything inside add/sub/mul/div themselves.
You'll probably want more than one calc object as well, with each one having its own separate current result. Store common functions in a metatable and make :new create new instances with this metatable and separate entry for result.
local calc_meta = { __index = {
add = function(self, number) self._r = self._r + number return self end,
sub = function(self, number) self._r = self._r - number return self end,
mul = function(self, number) self._r = self._r * number return self end,
div = function(self, number) self._r = self._r / number return self end,
result = function(self) return self._r end
}}
local calc = {
new = function(self, number)
return setmetatable({
_r = number or 0
}, calc_meta) end
}
result = calc:new(12):add(5):sub(3):mul(2):div(7):result()
print(result)
-- 4
You can't completely duplicate Lua's behavior with strings - it is built-in into VM to treat string table as metatable for string values and cannot be programmed without modifying VM itself. You can get rid of extra result at end though if you add __add/__sub and other numeric methods to metatable so they would automatically "unwrap" your object to basic number value. Of course you won't be able to apply your methods to "unwrapped" value after that.

scala : better solution

QUESTION:
We define super digit of an integer x using the following rules:
Iff x has only 1 digit, then its super digit is x.
Otherwise, the super digit of x is equal to the super digit of the digit-sum of x. Here, digit-sum of a number is defined as the sum of its digits.
For example, super digit of 9875 will be calculated as:
super-digit(9875) = super-digit(9+8+7+5)
= super-digit(29)
= super-digit(2+9)
= super-digit(11)
= super-digit(1+1)
= super-digit(2)
= 2.
You are given two numbers - n k. You have to calculate the super digit of P.
P is created when number n is concatenated k times. That is, if n = 123 and k = 3, then P = 123123123.
Input Format
Input will contain two space separated integers, n and k.
Output Format
Output the super digit of P, where P is created as described above.
Constraint
1≤n<10100000
1≤k≤105
Sample Input
148 3
Sample Output
3
Explanation
Here n = 148 and k = 3, so P = 148148148.
super-digit(P) = super-digit(148148148)
= super-digit(1+4+8+1+4+8+1+4+8)
= super-digit(39)
= super-digit(3+9)
= super-digit(12)
= super-digit(1+2)
= super-digit(3)
= 3.
I have written the following program to solve the above problem , but how to solve it even efficiently and is string operation efficient than math operation ??? and for few inputs it takes a long time for example
861568688536788 100000
object SuperDigit {
def main(args: Array[String]) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution
*/
def generateString (no:String,re:BigInt , tot:BigInt , temp:String):String = {
if(tot-1>re) generateString(no+temp,re+1,tot,temp)
else no
}
def totalSum(no:List[Char]):BigInt = no match {
case x::xs => x.asDigit+totalSum(xs)
case Nil => '0'.asDigit
}
def tot(no:List[Char]):BigInt = no match {
case _ if no.length == 1=> no.head.asDigit
case no => tot(totalSum(no).toString.toList)
}
var list = readLine.split(" ");
var one = list.head.toString();
var two = BigInt(list(1));
//println(generateString("148",0,3,"148"))
println(tot(generateString(one,BigInt(0),two,one).toList))
}
}
One reduction is to realise that you do not have to concatenate the number considered as a string k times but rather can start with the number k * qs(n) (where qs is the function that maps a number to its sum of digits, i. e. qs(123) = 1+2+3). Here is a more functional programming stylish approach. I do not know whether it can be made faster than this.
object Solution {
def qs(n: BigInt): BigInt = n.toString.foldLeft(BigInt(0))((n, ch) => n + (ch - '0').toInt)
def main(args: Array[String]) {
val input = scala.io.Source.stdin.getLines
val Array(n, k) = input.next.split(" ").map(BigInt(_))
println(Stream.iterate(k * qs(n))(qs(_)).find(_ < 10).get)
}
}

System Stack Error

So I have the following code for a Merge Sort in Ruby.
class MergeSort
def sort(array)
if array.length == 1 || array.length == 0
return array
end
firstHalf = array[0..array.length / 2]
secondHalf = array[(array.length / 2) + 1..array.length]
firstHalf = sort(firstHalf)
secondHalf = sort(secondHalf)
b = 0
c = 0
for i in (0..(firstHalf.length - 1))
while b < secondHalf.length && firstHalf[i] >= secondHalf[b]
array[c] = secondHalf[b]
b = b + 1
c = c + 1
array[c] = firstHalf[i]
c = c + 1
end
return array
end
end
array = [1,4,9,14,20,25]
puts MergeSort::new.sort(array)
When I run the code, I get a SystemStackError. Can someone tell me why this is happening? Thanks.
At a guess, once the array length gets to 3 (i.e. elements [0..2]), the call
firstHalf = array[0..array.length / 2]
evaluates to
0..1.5 and if 1.5 is rounded up to 2
which then calls sort [0..2] again
and eventually you get a stack overflow?
In order to call .new you have to have an initialize method in your class.
What you probably wanted to do was calling .sort on the class itself, in which case you have to prefix it with self, so:
class MergeSort
def self.sort(array)
...
Afterwards you can call it like this:
MergeSort.sort(array)

Resources