Ruby string to array conversion using 'scan' function - ruby

I have a string:
"10/2+3*3/2-3/2+10"
I need to convert it to an array:
["10", "/", "2", "+", "3", "*", "3", "/", "2", "-", "3", "/", "2", "+", "10"]
without using any gem.
I am using the following code to try to convert it:
"10/2+3*3/2-3/2+10".scan(/[\d*,+,\-,*,\/]/)
# => ["1", "0", "/", "2", "+", "3", "*", "3", "/", "2", "-", "3", "/", "2", "+", "1", "0"]
The output is not what I was expecting.

Use String#split
"10/2+3*3/2-3/2+10".split(/(\D)/)
#=> ["10", "/", "2", "+", "3", "*", "3", "/", "2", "-", "3", "/", "2", "+", "10"]

Commas inside square brackets in regular expression are treated as commas. What you want is the union of patterns.
"10/2+3*3/2-3/2+10".scan(/\d+|\+|\*|\/|\-/)

"10/2+3*3/2-3/2+10".scan(/\d+|./)
#=> ["10", "/", "2", "+", "3", "*", "3", "/", "2", "-", "3", "/", "2", "+", "10"]

Related

Find adjacents in bidimensional array

I have a 2d array like this.
[
["+", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "+"],
["|", "*", "*", " ", " ", "*", " ", " ", " ", " ", "*", "*", "*", "*", "*", "*", "*", "*", " ", "*", " ", "|"],
["|", " ", "*", "*", "*", "*", " ", " ", " ", " ", "*", "*", " ", " ", "*", "*", " ", "*", " ", "*", " ", "|"],
["|", "*", " ", "*", " ", " ", " ", " ", "*", " ", "*", " ", "*", "*", "*", " ", "*", "*", "*", " ", "*", "|"],
["|", "*", "*", " ", " ", "*", " ", " ", "*", "*", "*", " ", "*", " ", "*", "*", " ", "*", "*", " ", " ", "|"],
["|", "*", " ", " ", " ", "*", " ", "*", " ", " ", " ", "*", "*", "*", "*", "*", " ", " ", " ", "*", "*", "|"],
["|", " ", " ", " ", " ", " ", "*", " ", "*", " ", " ", " ", "*", " ", " ", " ", " ", " ", "*", " ", " ", "|"],
["|", "*", "*", " ", "*", "*", "*", " ", "*", " ", " ", "*", "*", "*", "*", " ", "*", " ", " ", "*", " ", "|"],
["|", " ", "*", " ", "*", "*", "*", "*", " ", " ", " ", "*", "*", " ", " ", " ", " ", "*", " ", "*", "*", "|"],
["|", " ", "*", " ", " ", "*", " ", " ", "*", " ", "*", "*", "*", " ", "*", " ", "*", " ", " ", " ", "*", "|"],
["|", " ", "*", "*", "*", "*", " ", " ", " ", " ", "*", "*", " ", "*", "*", "*", " ", " ", " ", "*", "*", "|"],
["|", " ", " ", "*", "*", " ", "*", "*", "*", " ", " ", " ", " ", " ", " ", "*", "*", " ", " ", " ", " ", "|"],
["|", "*", " ", "*", "*", " ", "*", " ", "*", "*", " ", " ", " ", "*", " ", "*", "*", "*", " ", " ", " ", "|"],
["|", " ", " ", " ", "*", " ", "*", " ", "*", " ", "*", " ", " ", " ", " ", " ", " ", "*", "*", " ", " ", "|"],
["|", "*", " ", "*", " ", "*", " ", "*", " ", " ", " ", "*", " ", "*", "*", " ", "*", "*", "*", "*", "*", "|"],
["|", " ", " ", " ", "*", " ", "*", "*", "*", "*", "*", "*", " ", " ", "*", " ", " ", "*", "*", " ", "*", "|"],
["|", "*", "*", "*", "*", "*", " ", " ", " ", "*", " ", "*", " ", "*", "*", " ", "*", "*", " ", " ", "*", "|"],
["|", "*", " ", " ", " ", "*", "*", "*", "*", " ", " ", "*", " ", "*", " ", "*", " ", "*", " ", "*", "*", "|"],
["|", "*", " ", "*", "*", "*", "*", "*", "*", " ", "*", "*", "*", "*", " ", " ", " ", "*", " ", " ", " ", "|"],
["|", " ", " ", "*", " ", " ", "*", "*", " ", " ", " ", "*", " ", "*", "*", " ", " ", " ", " ", " ", "*", "|"],
["|", " ", "*", " ", " ", " ", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", " ", " ", " ", "*", "|"],
["+", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "+"]
]
and i need to find the number of bombs that are near each "*" as shown in the image below.
Im doing this to get indexes but i dont know how to get the adjacents.
#array_map.each_index do |i|
subarray = #array_map[i]
subarray.each_index do |x|
puts String(i) << " " << String(x) << "... " << #array_map[i][x]
end
end
minefield = [
["+", "-", "-", "-", "-", "-", "-", "-"],
["|", "*", "*", " ", " ", "*", " ", " "],
["|", " ", "*", "*", "*", "*", " ", " "],
["|", "*", " ", "*", " ", " ", " ", " "],
["|", "*", "*", " ", " ", "*", " ", " "],
["|", "*", " ", " ", " ", "*", " ", "*"],
["|", " ", " ", " ", " ", " ", "*", " "]
]
last_row = minefield.size-1
#=> 6
last_col = minefield.first.size-1
#=> 7
adj_cols = (0..last_col).each_with_object({}) do |j,h|
h[j] = ([j-1, 0].max..[j+1, last_col].min).to_a
end
#=> {0=>[0, 1], 1=>[0, 1, 2], 2=>[1, 2, 3], 3=>[2, 3, 4], 4=>[3, 4, 5],
# 5=>[4, 5, 6], 6=>[5, 6, 7], 7=>[6, 7]}
arr = (0..last_row).each_with_object(minefield.dup.map(&:dup)) do |i,a|
adj_rows = ([i-1, 0].max..[i+1, last_row].min).to_a
(0..last_col).each do |j|
next unless a[i][j] == ' '
a[i][j] = adj_rows.product(adj_cols[j]).count do |r,c|
minefield[r][c] == '*'
end.to_s
end
end
arr.each { |row| p row }
displays
["+", "-", "-", "-", "-", "-", "-", "-"]
["|", "*", "*", "4", "4", "*", "2", "0"]
["|", "4", "*", "*", "*", "*", "2", "0"]
["|", "*", "6", "*", "5", "3", "2", "0"]
["|", "*", "*", "2", "3", "*", "3", "1"]
["|", "*", "3", "1", "2", "*", "4", "*"]
["|", "1", "1", "0", "1", "2", "*", "2"]
See Array#product. adj_cols is a hash that gives an array of column indices to identify adjacent positions for each column index. It makes sense to do this once at the beginning rather than repeat the calculation for each element (row) of minefield.
The array to be returned is initialized to
minefield.dup.map(&:dup)
so that minefield will not be mutated (modified).
Here's an example calculation.
i = 3
adj_rows = ([i-1, 0].max..[i+1, last_row].min).to_a
#=> ([2, 0].max..[4, 6].min).to_a
#=> (2..4).to_a
#=> [2, 3, 4]
j = 4
a = adj_cols[j]
#=> [3, 4, 5]
b = adj_rows.product(a)
#=> [[2, 3], [2, 4], [2, 5],
# [3, 3], [3, 4], [3, 5],
# [4, 3], [4, 4], [4, 5]]
b.count { |r,c| minefield[r][c] == '*' }
#=> 5
Note that b includes [3, 4], which we know equals ' '. It does no harm to leave it in the count operation, however, as minefield[r][c] == '*' #=> false). We could alternatively write:
b = adj_rows.product(a) - [[i, j]]
#=> [[2, 3], [2, 4], [2, 5],
# [3, 3], [3, 5],
# [4, 3], [4, 4], [4, 5]]
Here's a second example (that would apply if minefield[0][4] #=> ' ').
i = 0
j = 4
adj_rows = ([i-1, 0].max..[i+1, last_row].min).to_a
#=> [0, 1]
j = 4
a = adj_cols[j]
#=> [3, 4, 5]
b = adj_rows.product(a)
#=> [[0, 3], [0, 4], [0, 5],
# [1, 3], [1, 4], [1, 5]]
For the minefield array given in the question the return value is as follows.
["+", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "+"]
["|", "*", "*", "4", "4", "*", "2", "0", "0", "2", "*", "*", "*", "*", "*", "*", "*", "*", "4", "*", "2", "|"]
["|", "4", "*", "*", "*", "*", "2", "1", "1", "4", "*", "*", "6", "7", "*", "*", "7", "*", "6", "*", "3", "|"]
["|", "*", "6", "*", "5", "3", "2", "2", "*", "6", "*", "6", "*", "*", "*", "6", "*", "*", "*", "4", "*", "|"]
["|", "*", "*", "2", "3", "*", "3", "3", "*", "*", "*", "6", "*", "8", "*", "*", "5", "*", "*", "5", "3", "|"]
["|", "*", "3", "1", "2", "*", "4", "*", "4", "4", "3", "*", "*", "*", "*", "*", "3", "3", "4", "*", "*", "|"]
["|", "3", "3", "2", "3", "5", "*", "5", "*", "2", "2", "5", "*", "7", "5", "4", "2", "2", "*", "4", "3", "|"]
["|", "*", "*", "4", "*", "*", "*", "6", "*", "2", "2", "*", "*", "*", "*", "2", "*", "3", "4", "*", "3", "|"]
["|", "4", "*", "5", "*", "*", "*", "*", "3", "3", "4", "*", "*", "6", "3", "4", "3", "*", "3", "*", "*", "|"]
["|", "3", "*", "6", "6", "*", "5", "3", "*", "3", "*", "*", "*", "5", "*", "4", "*", "2", "3", "5", "*", "|"]
["|", "2", "*", "*", "*", "*", "4", "4", "3", "4", "*", "*", "4", "*", "*", "*", "4", "2", "1", "*", "*", "|"]
["|", "2", "5", "*", "*", "6", "*", "*", "*", "4", "3", "2", "3", "3", "6", "*", "*", "3", "2", "2", "2", "|"]
["|", "*", "3", "*", "*", "6", "*", "7", "*", "*", "2", "1", "1", "*", "3", "*", "*", "*", "3", "1", "0", "|"]
["|", "2", "4", "4", "*", "5", "*", "5", "*", "4", "*", "2", "3", "3", "4", "4", "6", "*", "*", "4", "2", "|"]
["|", "*", "2", "*", "4", "*", "5", "*", "5", "5", "5", "*", "3", "*", "*", "3", "*", "*", "*", "*", "*", "|"]
["|", "3", "5", "5", "*", "5", "*", "*", "*", "*", "*", "*", "5", "5", "*", "5", "5", "*", "*", "6", "*", "|"]
["|", "*", "*", "*", "*", "*", "6", "6", "6", "*", "6", "*", "5", "*", "*", "4", "*", "*", "5", "5", "*", "|"]
["|", "*", "6", "5", "7", "*", "*", "*", "*", "4", "5", "*", "7", "*", "5", "*", "5", "*", "4", "*", "*", "|"]
["|", "*", "4", "*", "*", "*", "*", "*", "*", "3", "*", "*", "*", "*", "5", "2", "3", "*", "3", "3", "3", "|"]
["|", "2", "4", "*", "4", "5", "*", "*", "6", "5", "6", "*", "8", "*", "*", "4", "3", "2", "1", "2", "*", "|"]
["|", "1", "*", "2", "1", "2", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "1", "0", "2", "*", "|"]
["+", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "+"]
This would work:
minefield = [
["+", "-", "-", "-", "-", "-", "+"],
["|", "*", "*", "*", "*", "*", "|"],
["|", "*", " ", "*", "*", " ", "|"],
["|", " ", " ", " ", "*", " ", "|"],
["|", "*", " ", "*", "*", "*", "|"],
["|", "*", "*", "*", " ", " ", "|"],
["+", "-", "-", "-", "-", "-", "+"]
]
minefield.each_with_index do |row, y|
row.each_with_index do |item, x|
next unless item == '*'
(y - 1).upto(y + 1) do |j|
(x - 1).upto(x + 1) do |i|
next if minefield[j][i] =~ /[*|+-]/
minefield[j][i] = minefield[j][i].to_i.succ.to_s
end
end
end
end
It traverses the minefield array, skipping everything but bombs (next unless item == '*'). Once we encounter a bomb, we traverse its surroundings, skipping any special fields like *, |, +, or -. (this will also skip the bomb itself)
And since bombs don't occur on the minefield's border, we can omit bounds checking.
For the remaining surrounding fields (i.e. blanks and numeric strings), we convert that field to integer (so " " becomes 0), add 1 (via succ), convert it back to string and re-assign it to the field: (succ also works on strings, but we have to handle " " anyway)
# blank # numeric
minefield[j][i] #=> " " # "1"
.to_i #=> 0 # 1
.succ #=> 1 # 2
.to_s #=> "1" # "2"
In the end, the blank fields have been replaced by the bomb counts:
[
["+", "-", "-", "-", "-", "-", "+"],
["|", "*", "*", "*", "*", "*", "|"],
["|", "*", "5", "*", "*", "4", "|"],
["|", "2", "4", "5", "*", "4", "|"],
["|", "*", "5", "*", "*", "*", "|"],
["|", "*", "*", "*", "4", "2", "|"],
["+", "-", "-", "-", "-", "-", "+"]
]

Error in crossprod(test$inc_rat, nation_agegroup2010$total_prop) : non-conformable arguments

I run the following lines to obtain the bootstrapped standardized rate for a group of people, but it return a error that I can't solve after a couple of days.
Error in crossprod(test$inc_rat, nation_agegroup2010$total_prop) :
non-conformable arguments
# to build the dataset
ZN_2011NGT_ADA <- c(0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
age_rage_ZN <- c("2", "3", "4", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "4", "4", "4", "4", "4", "4", "4", "2", "3", "4", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "5", "5", "5")
zzz <- data.frame(ZN_2011NGT_ADA, age_rage_ZN)
# define the function for bootstrap
ZN_boot <- function(total_dat, rows){
data <- total_dat[rows,]
test <- merge(aggregate(data$ZN_2011NGT_ADA, by = list(age = data$age_rage_ZN), length),
aggregate(data$ZN_2011NGT_ADA, by = list(age = data$age_rage_ZN), sum),
by = 'age')
test$inc_rat <- test$x.y/test$x.x
return(crossprod(test$inc_rat, nation_agegroup2010$total_prop))}
# to run bootstrap
library(boot)
set.seed(1234
boot(data = zzz, statistic = ZN_boot, R = 5000)
The purpose of this block of codes is to get the bootstrapped rate (the value that returned in the function after bootstrapping in the boot function). I run similar codes on other datasets and it turned out to be fine. I am wondering why the last code always reports the above mentioned error? Can anyone be of help?

In d3 4.0, how can I get extra data from the stratified object?

I'm a new to D3 also stackoverflow, and have some question.
[
{"id": "1", "name": "Eve", "parent": "" , "txt": "text1."},
{"id": "2", "name": "Cain", "parent": "1", "txt": "text2."},
{"id": "3", "name": "Seth", "parent": "1", "txt": "text3."},
{"id": "4", "name": "Enos", "parent": "3", "txt": "text4."},
{"id": "5", "name": "Noam", "parent": "3", "txt": "text5."},
{"id": "6", "name": "Abel", "parent": "1", "txt": "text6."},
{"id": "7", "name": "Awan", "parent": "1", "txt": "text7."},
{"id": "8", "name": "Enoch", "parent": "7", "txt": "text8."},
{"id": "9", "name": "Azura", "parent": "1", "txt": "text9."}
];
Above is array object which is not stratified yet.
below is the stratified result and code.
var stratify = d3.stratify()
.id(function(d){ return d.id; })
.parentId(function(d){ return d.parent; });
stratified object
I want to use the data object, like below code
node.append("text")
.text(function(d) { return d.name; });
but, above doesn't work.
refered to https://github.com/d3/d3-hierarchy/blob/master/README.md#stratify
and I edited the array object a little.
thanks & best regards. and It would be greatly appreciated if you could give me a feedback.
Finally, I found the answer.
return d.data.name
what an embarrassingly amateurish thing...

gets.chomp find a string in a list

Is it possible to find a string in a list for a gets.chomp for if, elsif?
def number_problem()
puts "Please enter a number 1-100 for a phrase or word back!"
number = gets.chomp
mult_of_three = ["3", "6", "9", "12", "15", "18", "21", "24", "27", "30", "33", "36", "39", "42", "45", "48", "51", "54", "57", "60", "63", "66", "69", "72", "75", "78", "81", "84", "87", "90", "93", "96", "99"]
mult_of_five = ["5", "10", "15", "20", "25", "30", "35", "40", "45", "50", "55", "60", "65", "70", "75", "80", "85", "90", "95", "100"]
mult_of_both = ["15", "30", "45", "60", "75", "90"]
if number == mult_of_three
puts "Bit"
To find if an element is present in an Array, you can use include?. Here:
if mult_of_three.include?(number)
Example:
number = gets.chomp
36
# => "36"
mult_of_three.include?(number)
# => true
number = gets.chomp
98
# => "98"
mult_of_three.include?(number)
# => false

Grep and cut command in putty terminal

I'm trying to cut a specific line from several files.
I have about 300 json files.
file structure:
{
"FieldName1": "XXXX",
"FieldName2": "1",
"FieldName3": "XXX",
"FieldName4": "XXX",
"FieldName5": "1",
.
.
.
}
,
{
"FieldName1": "XXXX",
"FieldName2": "2",
"FieldName3": "XXX",
"FieldName4": "XXX",
"FieldName5": "2",
.
.
.
}
some of the files are written in one row:
{"FieldName1": "XXXX", "FieldName2": "3", "FieldName3": "XXX", "FieldName4": "XXX", "FieldName5": "3"}, {"FieldName1": "XXXX", "FieldName2": "4", "FieldName3": "XXX", "FieldName4": "XXX", "FieldName5": "4"}, ...
}
What i need to get in my output file is just a list of 2 fields from the files :
"FieldName1": "1",
"FieldName2": "1",
"FieldName1": "2",
"FieldName2": "2",
"FieldName1": "3",
"FieldName2": "3",
"FieldName1": "4",
"FieldName2": "4",
.
.
.
Is there an easy way to do so?
Thanks!
For the single line, you can use this gnu awk (do to multiple characters in RS)
awk -v RS='"FieldName' 'NR>1 {sub(/, "/,",");print RS$0}' file
"FieldName1": "XXXX",
"FieldName2": "3",
"FieldName3": "XXX",
"FieldName4": "XXX",
For multiple line use grep or awk like this:
awk '/FieldName/' file
"FieldName1": "XXXX",
"FieldName2": "1",
"FieldName3": "XXX",
"FieldName4": "XXX",
"FieldName5": "1",

Resources