Parsing array to string from nested array - ruby

Let's say I have nested array like:
nested = [
[0.5623507523876472, ["h", "e", "l", "l", "o"]],
[0.07381531933500263, ["h", "a", "l", "l", "o"]],
[0.49993338806153054, ["n", "i", "h", "a", "o"]],
[0.6499234734532127, ["k", "o", "n", "n", "i", "c", "h", "i", "w", "a"]]
]
Initially I wanted to convert it into hash. But first I have to convert array(above example ["h", "e", "l", "l", "o"]) to "hello".
So my question is how to convert nested into :
[
[0.5623507523876472, "hello"],
[0.07381531933500263, "hallo"],
[0.49993338806153054, "nihao"],
[0.6499234734532127, "konnichiwa"]
]

If you don't want to destroy the source array nested :
Use Array#map :
nested = [
[0.5623507523876472, ["h", "e", "l", "l", "o"]],
[0.07381531933500263, ["h", "a", "l", "l", "o"]],
[0.49993338806153054, ["n", "i", "h", "a", "o"]],
[0.6499234734532127, ["k", "o", "n", "n", "i", "c", "h", "i", "w", "a"]]
]
nested_map = nested.map { |a,b| [a,b.join] }
# => [[0.5623507523876472, "hello"],
# [0.07381531933500263, "hallo"],
# [0.49993338806153054, "nihao"],
# [0.6499234734532127, "konnichiwa"]]
If you want to destroy the source array nested
Use Arry#map! method :
nested = [
[0.5623507523876472, ["h", "e", "l", "l", "o"]],
[0.07381531933500263, ["h", "a", "l", "l", "o"]],
[0.49993338806153054, ["n", "i", "h", "a", "o"]],
[0.6499234734532127, ["k", "o", "n", "n", "i", "c", "h", "i", "w", "a"]]
]
nested.map! { |a,b| [a,b.join] }
# => [[0.5623507523876472, "hello"],
# [0.07381531933500263, "hallo"],
# [0.49993338806153054, "nihao"],
# [0.6499234734532127, "konnichiwa"]]

Related

how can you make a random password generator in lua?

local function generator()
local capital_letters = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
local low_letters = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
local numbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
local Random_capital_letters = math.random(26)
local Random_low_letters = math.random(26)
local Random_numbers = math.random(10)
local length = 10
print("this is your generatet password: "..Random_capital_letters, Random_low_letters, Random_numbers[length])
math.randomseed(os.time())
end
generator()
it just gives me an error all the time, would be cool if anyone could help me!
You must initialize random seed before using math.random
It's better to use length of your tables (capital_letters, low_letters, numbers) to use math.random function to pick a value and create your password.
10 value must not be in numbers table
A loop is necessary to create your password : iterate from 1 to length and in each step, pick a random value in capital_letters, low_letters, numbers tables.
A working version adapted from your Lua code :
local function generator()
local capital_letters = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
local low_letters = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
local numbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
math.randomseed(os.time())
local length = 10
local pass = ""
local choice = 0
for _ = 1, length do
choice = math.random(3)
-- Capital letters
if choice == 1 then
pass = pass .. capital_letters[math.random(#capital_letters)]
-- Low letters
elseif choice == 2 then
pass = pass .. low_letters[math.random(#low_letters)]
-- Numbers
else
pass = pass .. numbers[math.random(#numbers)]
end
end
print(pass)
end
generator()

How do I flatten a nested hash, recursively, into an array of arrays with a specific format?

I have a nested hash that looks something like this:
{
'a' => {
'b' => ['c'],
'd' => {
'e' => ['f'],
'g' => ['h', 'i', 'j', 'k']
},
'l' => ['m', 'n', 'o', 'p']
},
'q' => {
'r' => ['s']
}
}
The hash can have even more nesting, but the values of the last level are always arrays.
I would like to "flatten" the hash into a format where I get a an array of arrays representing all keys and values that makes up an entire "path/branch" of the nested hash all they way from lowest level value to the top of the hash. So kind of like traversing up through the "tree" starting from the bottom while collecting keys and values on the way.
The output of that for the particular hash should be:
[
['a', 'b', 'c'],
['a', 'd', 'e', 'f'],
['a', 'd', 'g', 'h', 'i', 'j', 'k'],
['a', 'l', 'm', 'n', 'o', 'p'],
['q', 'r', 's']
]
I tried many different things, but nothing worked so far. Again keep in mind that more levels than these might occur, so the solution has to be generic.
Note: the order of the arrays and the order of the elements in them is not important.
I did the following, but it's not really working:
tree_def = {
'a' => {
'b' => ['c'],
'd' => {
'e' => ['f'],
'g' => ['h', 'i', 'j', 'k']
},
'l' => ['m', 'n', 'o', 'p']
},
'q' => {
'r' => ['s']
}
}
branches = [[]]
collect_branches = lambda do |tree, current_branch|
tree.each do |key, hash_or_values|
current_branch.push(key)
if hash_or_values.kind_of?(Hash)
collect_branches.call(hash_or_values, branches.last)
else # Reached lowest level in dependency tree (which is always an array)
# Add a new branch
branches.push(current_branch.clone)
current_branch.push(*hash_or_values)
current_branch = branches.last
end
end
end
collect_branches.call(tree_def, branches[0])
branches #=> wrong result
As hinted at in the comments:
Looks pretty straightforward. Descend into hashes recursively, taking note of keys you visited in this branch. When you see an array, no need to recurse further. Append it to the list of keys and return
Tracking is easy, just pass the temp state down to recursive calls in arguments.
I meant something like this:
def tree_flatten(tree, path = [], &block)
case tree
when Array
block.call(path + tree)
else
tree.each do |key, sub_tree|
tree_flatten(sub_tree, path + [key], &block)
end
end
end
tree_flatten(tree_def) do |path|
p path
end
This code simply prints each flattened path as it gets one, but you can store it in an array too. Or even modify tree_flatten to return you a ready array, instead of yielding elements one by one.
You can do it like that:
def flat_hash(h)
return [h] unless h.kind_of?(Hash)
h.map{|k,v| flat_hash(v).map{|e| e.unshift(k)} }.flatten(1)
end
input = {
'a' => {
'b' => ['c'],
'd' => {
'e' => ['f'],
'g' => ['h', 'i', 'j', 'k']
},
'l' => ['m', 'n', 'o', 'p']
},
'q' => {
'r' => ['s']
}
}
p flat_hash(input)
The output will be:
[
["a", "b", "c"],
["a", "d", "e", "f"],
["a", "d", "g", "h", "i", "j", "k"],
["a", "l", "m", "n", "o", "p"],
["q", "r", "s"]
]
This of course calls for a recursive solution. The following method does not mutate the original hash.
Code
def recurse(h)
h.each_with_object([]) do |(k,v),arr|
v.is_a?(Hash) ? recurse(v).each { |a| arr << [k,*a] } : arr << [k,*v]
end
end
Example
h = { 'a'=>{ 'b'=>['c'],
'd'=>{ 'e'=>['f'], 'g' => ['h', 'i', 'j', 'k'] },
'l' => ['m', 'n', 'o', 'p'] },
'q'=>{ 'r'=>['s'] } }
recurse h
#=> [["a", "b", "c"],
# ["a", "d", "e", "f"],
# ["a", "d", "g", "h", "i", "j", "k"],
# ["a", "l", "m", "n", "o", "p"],
# ["q", "r", "s"]]
Explanation
The operations performed by recursive methods are always difficult to explain. In my experience the best way is to salt the code with puts statements. However, that in itself is not enough because when viewing output it is difficult to keep track of the level of recursion at which particular results are obtained and either passed to itself or returned to a version of itself. The solution to that is to indent and un-indent results, which is what I've done below. Note the way I've structured the code and the few helper methods I use are fairly general-purpose, so this approach can be adapted to examine the operations performed by other recursive methods.
INDENT = 8
def indent; #col += INDENT; end
def undent; #col -= INDENT; end
def pu(s); print " "*#col; puts s; end
def puhline; pu('-'*(70-#col)); end
#col = -INDENT
def recurse(h)
begin
indent
puhline
pu "passed h = #{h}"
h.each_with_object([]) do |(k,v),arr|
pu " k = #{k}, v=#{v}, arr=#{arr}"
if v.is_a?(Hash)
pu " calling recurse(#{v})"
ar = recurse(v)
pu " return value=#{ar}"
pu " calculating recurse(v).each { |a| arr << [k,*a] }"
ar.each do |a|
pu " a=#{a}"
pu " [k, *a] = #{[k,*a]}"
arr << [k,*a]
end
else
pu " arr << #{[k,*v]}"
arr << [k,*v]
end
pu "arr = #{arr}"
end.tap { |a| pu "returning=#{a}" }
ensure
puhline
undent
end
end
recurse h
----------------------------------------------------------------------
passed h = {"a"=>{"b"=>["c"], "d"=>{"e"=>["f"], "g"=>["h", "i", "j", "k"]},
"l"=>["m", "n", "o", "p"]}, "q"=>{"r"=>["s"]}}
k = a, v={"b"=>["c"], "d"=>{"e"=>["f"], "g"=>["h", "i", "j", "k"]},
"l"=>["m", "n", "o", "p"]}, arr=[]
calling recurse({"b"=>["c"], "d"=>{"e"=>["f"], "g"=>["h", "i", "j", "k"]},
"l"=>["m", "n", "o", "p"]})
--------------------------------------------------------------
passed h = {"b"=>["c"], "d"=>{"e"=>["f"], "g"=>["h", "i", "j", "k"]},
"l"=>["m", "n", "o", "p"]}
k = b, v=["c"], arr=[]
arr << ["b", "c"]
arr = [["b", "c"]]
k = d, v={"e"=>["f"], "g"=>["h", "i", "j", "k"]}, arr=[["b", "c"]]
calling recurse({"e"=>["f"], "g"=>["h", "i", "j", "k"]})
------------------------------------------------------
passed h = {"e"=>["f"], "g"=>["h", "i", "j", "k"]}
k = e, v=["f"], arr=[]
arr << ["e", "f"]
arr = [["e", "f"]]
k = g, v=["h", "i", "j", "k"], arr=[["e", "f"]]
arr << ["g", "h", "i", "j", "k"]
arr = [["e", "f"], ["g", "h", "i", "j", "k"]]
returning=[["e", "f"], ["g", "h", "i", "j", "k"]]
------------------------------------------------------
return value=[["e", "f"], ["g", "h", "i", "j", "k"]]
calculating recurse(v).each { |a| arr << [k,*a] }
a=["e", "f"]
[k, *a] = ["d", "e", "f"]
a=["g", "h", "i", "j", "k"]
[k, *a] = ["d", "g", "h", "i", "j", "k"]
arr = [["b", "c"], ["d", "e", "f"], ["d", "g", "h", "i", "j", "k"]]
k = l, v=["m", "n", "o", "p"],
arr=[["b", "c"], ["d", "e", "f"], ["d", "g", "h", "i", "j", "k"]]
arr << ["l", "m", "n", "o", "p"]
arr = [["b", "c"], ["d", "e", "f"], ["d", "g", "h", "i", "j", "k"],
["l", "m", "n", "o", "p"]]
returning=[["b", "c"], ["d", "e", "f"], ["d", "g", "h", "i", "j", "k"],
["l", "m", "n", "o", "p"]]
--------------------------------------------------------------
return value=[["b", "c"], ["d", "e", "f"], ["d", "g", "h", "i", "j", "k"],
["l", "m", "n", "o", "p"]]
calculating recurse(v).each { |a| arr << [k,*a] }
a=["b", "c"]
[k, *a] = ["a", "b", "c"]
a=["d", "e", "f"]
[k, *a] = ["a", "d", "e", "f"]
a=["d", "g", "h", "i", "j", "k"]
[k, *a] = ["a", "d", "g", "h", "i", "j", "k"]
a=["l", "m", "n", "o", "p"]
[k, *a] = ["a", "l", "m", "n", "o", "p"]
arr = [["a", "b", "c"], ["a", "d", "e", "f"], ["a", "d", "g", "h", "i", "j", "k"],
["a", "l", "m", "n", "o", "p"]]
k = q, v={"r"=>["s"]}, arr=[["a", "b", "c"], ["a", "d", "e", "f"],
["a", "d", "g", "h", "i", "j", "k"], ["a", "l", "m", "n", "o", "p"]]
calling recurse({"r"=>["s"]})
--------------------------------------------------------------
passed h = {"r"=>["s"]}
k = r, v=["s"], arr=[]
arr << ["r", "s"]
arr = [["r", "s"]]
returning=[["r", "s"]]
--------------------------------------------------------------
return value=[["r", "s"]]
----------------------------------------------------------------------
calculating recurse(v).each { |a| arr << [k,*a] }
a=["r", "s"]
[k, *a] = ["q", "r", "s"]
arr = [["a", "b", "c"], ["a", "d", "e", "f"], ["a", "d", "g", "h", "i", "j", "k"],
["a", "l", "m", "n", "o", "p"], ["q", "r", "s"]]
returning=[["a", "b", "c"], ["a", "d", "e", "f"], ["a", "d", "g", "h", "i", "j", "k"],
["a", "l", "m", "n", "o", "p"], ["q", "r", "s"]]
----------------------------------------------------------------------
#=> [["a", "b", "c"], ["a", "d", "e", "f"], ["a", "d", "g", "h", "i", "j", "k"],
# ["a", "l", "m", "n", "o", "p"], ["q", "r", "s"]]
This will return an Array with all the paths.
def paths(element, path = [], accu = [])
case element
when Hash
element.each do |key, value|
paths(value, path + [key], accu)
end
when Array
accu << (path + element)
end
accu
end
For nicer printing you can do
paths(tree_def).map { |path| path.join(".") }
See following which will keep calling recursively till it reaches to array values.
This recursion call will go with multiple branches and op should be individual copy for each branch so I used string which is always created as a new object here otherwise array will be like going with call by reference
hash = {"a"=>{"b"=>["c"], "d"=>{"e"=>["f"], "g"=>["h", "i", "j", "k"]}, "l"=>["m", "n", "o", "p"]}, "q"=>{"r"=>["s"]}}
#output = []
def nested_array(h, op='')
h.map do |k,v|
if Hash === v
nested_array(v, op+k)
else
#output << (op+k+v.join).chars
end
end
end
nested_array(hash)
#output will be your desired array.
[
["a", "b", "c"],
["a", "d", "e", "f"],
["a", "d", "g", "h", "i", "j", "k"],
["a", "l", "m", "n", "o", "p"],
["q", "r", "s"]
]
update: key values pair can be more than single character so following approach for nested_array may work better.
def nested_array(h, op=[])
h.map do |k,v|
if Hash === v
nested_array(v, Array.new(op) << k)
else
#output << ((Array.new(op) << k) + v)
end
end
end
All the solutions here are recursive, below is a non-recursive
solution.
def flatten(input)
sol = []
while(input.length > 0)
unprocessed_input = []
input.each do |l, r|
if r.is_a?(Array)
sol << l + r
else
r.each { |k, v| unprocessed_input << [l + [k], v] }
end
end
input = unprocessed_input
end
return sol
end
flatten([[[], h]])
Code Explanation:
Hash in array form is [[k1, v1], [k2, v2]].
When input_hash is presented in the above form, [[], { a: {..} }], partial_solutions of this form, [ [a], {..} ], can be generated. Index '0' holds the partial solution and Index '1' holds the yet to be processed input.
As this format is easy to map partial_solution with unprocessed input and accumulate unprocessed input, converting input_hash to this format result in, [[[], input_hash]]
Solution:
[["a", "b", "c"], ["a", "l", "m", "n", "o", "p"], ["q", "r", "s"], ["a", "d", "e", "f"], ["a", "d", "g", "h", "i", "j", "k"]]

Removing whitespace, tabs and new lines from array

How do I go about removing the tabs, new lines, and whitespaces from this array?
array1 = ["E", "A", "C", "H", " ", "L", "I", "N", "E", " ", "E", "N", "D", "S", " ", "W", "I", "T", "H", " ", "A", " ", "A", "C", "C", "I", "D", "E", "N", "T", "A", "L", "L", "Y", " ", " ", "A", "D", "\"", "A", " ", "A", "C", "C", "I", "\n", "\""]
I have tried the following, and none of these seem to work properly.
array1.map!(&:strip)
array1.reject!(&:empty?)
array1.reject(&:empty?)
array1 - [""]
array1.delete_if {|x| x == " " }
array1 = ["E", " ", ":", "L", "É", "\t", "T",
"-", "H", "\n", "\""]
array1.reject { |s| s.match? /\s/ }
#=> ["E", ":", "L", "É", "T", "-", "H", "\""]
\s in a regular expression matches all whitespace characters, namely, spaces, tabs ("\t") newlines ("\n"), carriage returns ("\r") and formfeeds ("\f").
The latter two have their origins from the days when teletype machines were used, the carriage return being the movement of the printhead from the end to the beginning of the line and the formfeeds advancing the paper being printed one line.1
1 Microsoft Windows still recognizes carriage returns and formfeeds, thereby maintaining support for teletype machines. ¯\_(ツ)_/¯
You can use grep to select elements matching a pattern. That pattern can be a simple regexp like /\s/ which matches whitespace characters:
array1.grep(/\s/)
#=> [" ", " ", " ", " ", " ", " ", " ", " ", "\n"]
The result is an array with all elements containing at least one whitespace character.
There's also \S (uppercase) which matches non-whitespace characters:
array1.grep(/\S/)
#=> ["E", "A", "C", "H", "L", "I", "N", "E", "E", "N", "D", "S", "W",
# "I", "T", "H", "A", "A", "C", "C", "I", "D", "E", "N", "T", "A",
# "L", "L", "Y", "A", "D", "\"", "A", "A", "C", "C", "I", "\""]
And we have grep_v which is the inverted version of grep. This would be useful if you wanted to specify space, tab and newline explicitly:
array1.grep_v(/[ \t\n]/)
#=> ["E", "A", "C", "H", "L", "I", "N", "E", "E", "N", "D", "S", "W",
# "I", "T", "H", "A", "A", "C", "C", "I", "D", "E", "N", "T", "A",
# "L", "L", "Y", "A", "D", "\"", "A", "A", "C", "C", "I", "\""]
In addition, just other possible variants:
array1 = [" ", "A", "\n", "\t", "B", "\r"]
array1.delete_if { |s| s.match? /\s/ }
#=> ["A", "B"]
array1 = [" ", "A", "\n", "\t", "B", "\r"]
array1.keep_if { |s| !s.match? /\s/ }
#=> ["A", "B"]
array1 = [" ", "A", "\n", "\t", "B", "\r"]
array1.select! { |s| !s.match? /\s/ }
#=> ["A", "B"]
Using match? rather than match is more preferable not only because we don’t use MatchData.
The point is that the benchmark shows that match? is almost 2 times faster.
This can be significant when working with large amounts of data.

pushing array to array in ruby have a wrong result

I have problem when pushing array to array,
the result is not as I expected.
I run this code below :
#arr = ["e", "s", "l", "e"]
def permutations(array, i=0)
combined_array = Array.new
(i..array.size-1).each do |j|
array[i], array[j] = array[j], array[i]
puts "ARRAY : #{array}"
combined_array << array.to_a
end
return combined_array
end
permutations(#arr)
I got the output :
ARRAY : ["e", "s", "l", "e"]
ARRAY : ["s", "e", "l", "e"]
ARRAY : ["l", "e", "s", "e"]
ARRAY : ["e", "e", "s", "l"]
=> [["e", "e", "s", "l"], ["e", "e", "s", "l"], ["e", "e", "s", "l"], ["e", "e", "s", "l"]]
Result expected :
ARRAY : ["e", "s", "l", "e"]
ARRAY : ["s", "e", "l", "e"]
ARRAY : ["l", "e", "s", "e"]
ARRAY : ["e", "e", "s", "l"]
=> [["e", "s", "l", "e"], ["s", "e", "l", "e"], ["l", "e", "s", "e"], ["e", "e", "s", "l"]]
How to solve this problem ?
According to the documentation, #to_a called on an Array returns self (the array itself, not a copy).
You are adding the same array to combined_array multiple times.
Change the .to_a to .dup and it will work fine.
I think #GolfWolf has solved your problem.
But you don't have to write such a function to solve your problem in Ruby, Ruby has permutation method which you can use it.
p arr.permutation.to_a
If you want to get first 4 element then you can do this,
p arr.permutation.take(4)

How to create / generate permutation of characters and numbers with length control in ruby

I need to create / generate long string contains permutation of characters and numbers at the same time I need to control the string length the most famous examples of this approach are
Metasploit project :
pettern_create.rb [string length as arg]
ex. pattern_create.rb 100
and
Ronin project:
String.generate([:alpha, 1], :numeric).take(100).each do |pattern|
print pattern
end
both will generate
A0A1A2A3A4A5A6A7A8A9B0B1B2B3B4B5B6B7B8B9C0C1C2C3C4C5C6C7C8C9D0D1D2D3D4D5D6D7D8D9E0E1E2E3E4E5E6E7E8E9F0F1F2F3F4F5F6F7F8F9G0G1G2G3G4G5G6G7G8G9H0H1H2H3H4H5H6H7H8H9I0I1I2I3I4I5I6I7I8I9J0J1J2J3J4J5J6J7J8J9
I tried with
all = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
"0","1", "2", "3", "4", "5", "6", "7", "8", "9"]
puts all.permutation(4).to_a
puts this will take a huge time because it'll generate ALL permutations without control, the control is the way to prevent unwanted strings
there is no issue in specifying the particular position of a string like "4C5C6"
I can use
str = "A0A1A2A3A4A5A6A7A8A9B0B1B2B3B4B5B6B7B8B9C0C1C2C3C4C5C6C7C8C9D0D1D2D3D4D5D6D7D8D9E0E1E2E3E4E5E6E7E8E9F0F1F2F3F4F5F6F7F8F9G0G1G2G3G4G5G6G7G8G9H0H1H2H3H4H5H6H7H8H9I0I1I2I3I4I5I6I7I8I9J0J1J2J3J4J5J6J7J8J9"
puts str.index("4C5C6") #=> 46
Notes:
permutation should be generated with same sequence all the time.
I don't want to force user to gem huge project for small function
I need to understand the algorithm or programmatic trick to do that
Thanks
The permutation method results in a enumerator, which is what you want. Don't turn it into an array with to_a, just take what you need.
all = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
"0","1", "2", "3", "4", "5", "6", "7", "8", "9"]
p all.permutation(4).first(100) #take(100) is fine too
Ruby strings work in a Range btw, so you can do:
('aaaa'..'zzzz').first(100) #or
('A0'..'Z9').first(100).join
#=> "A0A1A2A3A4A5A6A7A8A9B0B1B2B3B4B5B6B7B8B9C0C1C2C3C4C5C6C7C8C9D0D1D2D3D4D5D6D7D8D9E0E1E2E3E4E5E6E7E8E9F0F1F2F3F4F5F6F7F8F9G0G1G2G3G4G5G6G7G8G9H0H1H2H3H4H5H6H7H8H9I0I1I2I3I4I5I6I7I8I9J0J1J2J3J4J5J6J7J8J9"

Resources