Related
I am new to Elixir language and I am having some issues while writing a piece of code.
What I am given is a 2D array like
list1 = [
[1 ,2,3,4,"nil"],
[6,7,8,9,10,],
[11,"nil",13,"nil",15],
[16,17,"nil",19,20] ]
Now, what I've to do is to get all the elements that have values between 10 and 20, so what I'm doing is:
final_list = []
Enum.each(list1, fn row ->
Enum.each(row, &(if (&1 >= 10 and &1 <= 99) do final_list = final_list ++ &1 end))
end
)
Doing this, I'm expecting that I'll get my list of numbers in final_list but I'm getting blank final list with a warning like:
warning: variable "final_list" is unused (there is a variable with the same name in the context, use the pin operator (^) to match on it or prefix this variable with underscore if it is not meant to be used)
iex:5
:ok
and upon printing final_list, it is not updated.
When I try to check whether my code is working properly or not, using IO.puts as:
iex(5)> Enum.each(list1, fn row -> ...(5)> Enum.each(row, &(if (&1 >= 10 and &1 <= 99) do IO.puts(final_list ++ &1) end))
...(5)> end
...(5)> )
The Output is:
10
11
13
15
16
17
19
20
:ok
What could I possibly be doing wrong here? Shouldn't it add the elements to the final_list?
If this is wrong ( probably it is), what should be the possible solution to this?
Any kind of help will be appreciated.
As mentioned in Adam's comments, this is a FAQ and the important thing is the message "warning: variable "final_list" is unused (there is a variable with the same name in the context, use the pin operator (^) to match on it or prefix this variable with underscore if it is not meant to be used)" This message actually indicates a very serious problem.
It tells you that the assignment "final_list = final_list ++ &1" is useless since it just creates a local variable, hiding the external one. Elixir variables are not mutable so you need to reorganize seriously your code.
The simplest way is
final_list =
for sublist <- list1,
n <- sublist,
is_number(n),
n in 10..20,
do: n
Note that every time you write final_list = ..., you actually declare a new variable with the same name, so the final_list you declared inside your anonymous function is not the final_list outside the anonymous function.
I want to print a specified element from each list until it stops, however I am not sure of what loop conditions to use.
Code
For example, I have printed element[0] of each list but I want it to loop until it prints all the elements.
Any help is appreciated.
Thanks
The following code is, maybe, the simplest solution to print all the cats. If you want to print a single list, use only the last 2 lines after replacing "l" with your list variable.
a = ['Ginger', 'Lion', 'Persian', 'Sphynx']
b = ['Mainecoon', 'Tiger', 'Ragdoll', 'Birman']
c = ['Lynx', 'Shorthair', 'Abyssinian', 'Scottishfold']
d = ['Ocelot', 'Jaguar', 'Chartreux', 'Korat']
lists = [a, b, c, d]
for l in lists:
for cat in l:
print(cat)
I am wondering if its possible to search in the database with the given scrambled words.
I have a mobs table in database and it holds the name of the monster names
If given monster name is A Golden Dregon or A Golden Dfigon or A Gelden Dragon I want it to find A Golden Dragon or with the matches that close to it from database. Usually one or two letters at max is given like this as scrambled.
Is that possible with just SQL queries? Or should I build the query by parsing the given monster name?
I am using LUA for the code side.
I have come to know this search type as a fuzzy search. I mainly program in JS and use fuse.js all the time for this kind of problem.
Fuzzy Searches are based on the Levenshtein algorithm that rate the distance of two strings. When you have this distance value you can sort or drop elements from a list based on the score.
I found the algorithm in lua here.
function levenshtein(s, t)
local s, t = tostring(s), tostring(t)
if type(s) == 'string' and type(t) == 'string' then
local m, n, d = #s, #t, {}
for i = 0, m do d[i] = { [0] = i } end
for j = 1, n do d[0][j] = j end
for i = 1, m do
for j = 1, n do
local cost = s:sub(i,i) == t:sub(j,j) and 0 or 1
d[i][j] = math.min(d[i-1][j]+1, d[i][j-1]+1, d[i-1][j-1]+cost)
end
end
return d[m][n]
end
end
As explained in the site you compare two strings like so and get a score based on the distance of them, then sort or drop the items being search based on the scores given. As this is CPU expensive I would suggest caching or use a memoize function to store common mistakes.
levenshtein('referrer', 'referrer') -- zero distance
>>> 0
levenshtein('referrer', 'referer') -- distance of one character
>>> 1
levenshtein('random', 'strings') -- random big distance
>>> 6
Got a simple version of it working in lua here I must say lua is an easy language to pick up and start coding with.
local monsters = {'A Golden Dragon', 'Goblins', 'Bunny', 'Dragoon'}
function levenshtein(s, t)
local s, t = tostring(s), tostring(t)
if type(s) == 'string' and type(t) == 'string' then
local m, n, d = #s, #t, {}
for i = 0, m do d[i] = { [0] = i } end
for j = 1, n do d[0][j] = j end
for i = 1, m do
for j = 1, n do
local cost = s:sub(i,i) == t:sub(j,j) and 0 or 1
d[i][j] = math.min(d[i-1][j]+1, d[i][j-1]+1, d[i-1][j-1]+cost)
end
end
return d[m][n]
end
end
--Fuzzy Search Returns the Best Match in a list
function fuzzySearch(list, searchText)
local bestMatch = nil;
local lowestScore = nil;
for i = 1, #list do
local score = levenshtein(list[i], searchText)
if lowestScore == nil or score < lowestScore then
bestMatch = list[i]
lowestScore = score
end
end
return bestMatch
end
print ( fuzzySearch(monsters, 'golen dragggon') )
print ( fuzzySearch(monsters, 'A Golden Dfigon') )
print ( fuzzySearch(monsters, 'A Gelden Dragon') )
print ( fuzzySearch(monsters, 'Dragooon') ) --should be Dragoon
print ( fuzzySearch(monsters, 'Funny') ) --should be Bunny
print ( fuzzySearch(monsters, 'Gob') ) --should be Goblins
Output
A Golden Dragon
A Golden Dragon
A Golden Dragon
Dragoon
Bunny
Goblins
For SQL
You can try to do this same algorithm in T-SQL as talked about here.
In SQLlite there is an extension called editdist3 which also uses this algorithm the docs are here.
I would be hard to compensate for all the different one and two letter scrambled combinations, but you could create a lua table of common misspellings of "A Golden Dragon" check if it is in the table. I have never used lua before but here is my best try at some sample code:
local mob_name = "A Golden Dregon"--you could do something like, input("Enter mob name:")
local scrambled_dragon_names = {"A Golden Dregon", "A Golden Dfigon", "A Gelden Dragon"}
for _,v in pairs(scrambled_dragon_names) do
if v == mob_name then
mob_name = "A Golden Dragon"
break
end
end
I really hope I have helped!
P.S. If you have anymore questions go ahead and comment and I will try to answer ASAP.
You will have to parse the given monster name to some extent, by making assumptions about how badly it is misspelled. For example, if the user supplied the name
b fulden gorgon
There is no way in hell you can get to "A Golden Dragon". However, if you assume that the user will always get the first and last letters of every word correctly, then you could parse the words in the given name to get the first and last letters of each word, which would give you
"A", "G" "n", "D" "n"
Then you could use the LIKE operator in your query, like so:
SELECT * FROM mobs WHERE monster_name LIKE 'A G%n D%n';
The main point here is what assumptions you make about the misspelling. The closer you can narrow it down, the better your query results will be.
I'm trying to get the sum of the first 120 terms of a series:
a(n) = (10+a(n-1))(1.005)^n
I've tried two approaches, but I don't know which mistakes I'm making.
Approach #1:
nval = 1
mval = zeros(120,1)
for nval=1:120
if nval <= 120 %counting up from 1 to 120
if nval == 1 %modifying first term to avoid 0 index
mval(1) = (10)*(1.005)
nval = nval +1;
else
mval(nval) = (10+mval(nval-1))*(1.005)^nval; %Assigning
nval = nval +1;
end
end
end
which results in a column vector with 10.05 at the top and zeroes for the rest. If if omit the opening definition of mval as zeroes, I get the same result as
Approach #2:
a(1)=10.05; %defining first term to avoid 0 index
for n=2:120, a(n)= (10+a(n-1))*(1.005)^n; end
which results in something far too large.
(The correct value for a(120) is ~1646.99)
Edit: my mistake and apologies - my series was wrong; the (1.005)^n is NOT to the nth power, but merely 1.005. Using this, both methods work.
Thanks to one and all for answers and suggestions
I'm a beginner with R (and coding in general). In January 14 I hopefully can begin and finish a R course, but I would like to learn before. I have understanding of the basics and have used functions like read.table,intersect,cbind,paste,write.table.
But I only was able to achieve partially what I want to do with two input files (shortened samples):
REF.CSV
SNP,Pos,Mut,Hg
M522 L16 S138 PF3493 rs9786714,7173143,G->A,IJKLT-M522
P128 PF5504 rs17250121,20837553,C->T,KLT-M9
M429 P125 rs17306671,14031334,T->A,IJ-M429
M170 PF3715 rs2032597,14847792,A->C,I-M170
M304 Page16 PF4609 rs13447352,22749853,A->C,J-M304
M172 Page28 PF4908 rs2032604,14969634,T->G,J2-M172
L228,7771358,C->T,J2-M172
L212,22711465,T->C,J2a-M410
SAMPLE.CSV
SNP,Chr,Allele1,Allele2
L16,Y,A,A
P128,Y,C,C
M170,Y,A,A
P123,Y,C,C
M304,Y,C,C
M172,Y,T,G
L212,Y,-0,-0
Description what I like to do:
A) Check if SAMPLE.SNP is in REF.SNP
B) if YES check SAMPLE.Allele status (first read, second read) vs REF.Mut (Ancestral->Derived)
B1) if both Alleles are the same and match Derived create output "+ Allele1-Allele2"
B2) if both Alleles are the same and match Ancestral create output "- Allele1-Allele2"
B3) if Alleles are not the same check if Allele2 is Derived and create output "+ Allele1-Allele2"
B4) if both Alleles are "-0" create output "? NC"
B5) else create output "? Allele1-Allele2"
B6) if NO create output "? NA"
C) Write REF.CSV + output in new row (Sample) and create OUTPUT file
OUTPUT.CSV (like wanted)
SNP,Pos,Mut,Hg,Sample
M522 L16 S138 PF3493 rs9786714,7173143,G->A,IJKLT-M522,+ A-A
P128 PF5504 rs17250121,20837553,C->T,KLT-M9,- C-C
M429 P125 rs17306671,14031334,T->A,IJ-M429,? NA
M170 PF3715 rs2032597,14847792,A->C,I-M170,- A-A
M304 Page16 PF4609 rs13447352,22749853,A->C,J-M304,+ C-C
M172 Page28 PF4908 rs2032604,14969634,T->G,J2-M172,+ T-G
L228,7771358,C->T,J2-M172,? NA
L212,22711465,T->C,J2a-M410,? NC
What functions I have found interesting and tried so far.
Variant1: A) is done, but I guess it is not possible to write C) with this?
Have not tried to code down B) here
GT <- read.table("SAMPLE.CSV",sep=',',skip=1)[,c(1,3,4)]
REF <- read.table("REF.CSV",sep=',')
rownames(REF) <- REF[,1]
COMMON <- intersect(rownames(GT),rownames(REF))
REF <- REF[COMMON,]
GT <- GT[COMMON,]
GT<-cbind(REF,paste(GT[,2],'-',X[,3],sep=','))
write.table(GT,file='OUTPUT.CSV',quote=F,row.names=F,col.names=Fââ)
Variant2: This is probably a complete mess, forgive me. I was just rying to build a solution on for if looping functions, but I haven't understood R's syntax and logic in this probably.
I was not able to get this to run - A) and C)
Have not tried to code down B) here
GT<-read.table("SAMPLE.CSV",sep=',',skip=1)[,c(1,3,4)]
rownames(GT)<-GT[,1]
REF <- read.table("REF.CSV",sep=',')
rownames(REF)<-REF[,1]
for (i in (nrow(REF))) {
for (j in (nrow(GT))) {
if (GT[j,] %in% REF[i,]) {
ROWC[i,]<-cbind(REF[i,],paste(GT[j,2],"-",GT[j,3],sep=','))
} else {
ROWC[i,]<-cbind(REF[i,],"NA",sep=',')
}
}
}
write.table(ROWC,file='OUTPUT.CSV',quote=F,row.names=F,col.names=F)
I would be just happy if you can indicate what logic/functions would lead to reach the task I have described. I will then try to figure it out. Thx.