Skip to content

Instantly share code, notes, and snippets.

@mkhuda
Last active April 3, 2018 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkhuda/c5a14b9d917ab98dc8868c409c273614 to your computer and use it in GitHub Desktop.
Save mkhuda/c5a14b9d917ab98dc8868c409c273614 to your computer and use it in GitHub Desktop.
Looping an array by indexing some element in array itself
def self.readfile
### best case for indexing text file containing number followed by total number of paragraph
file = File.open('test.in').read
readresult = file.split("\n")
filelength = readresult.length
i = 0
x = 0
m = 0
reschild = Array.new
resparent = Array.new
while (i < filelength) do
count_row = Integer(readresult[i])
length_text = Integer(readresult[i+1])
if (count_row > 0)
first_x = i + 2
reschild = Array.new
x = first_x
while (x < count_row + i + 2) do
reschild.push(readresult[x])
x += 1
end
i = x
resparent.push(reschild)
else
break
end
m += 1
end
return resparent
end
@mkhuda
Copy link
Author

mkhuda commented Mar 31, 2018

example input

ar = ["5", "3", "jhj", "opp", "tyt", "hkl", "aji", "6", "3", "kak", "lop", "gyt", "uyt", "ert", "wqa", "2", "3", "hhh", "bbb", "2", "5", "jonru", "bando"]

output

[["jhj", "opp", "tyt", "hkl", "aji"], ["kak", "lop", "gyt", "uyt", "ert", "wqa"], ["hhh", "bbb"], ["jonru", "bando"]]

@mkhuda
Copy link
Author

mkhuda commented Apr 3, 2018

Input file as test

5
3
jhj
opp
tyt
hkl
aji
as
6
3
kak
lop
gyt
uyt
ert
wqa
ac
2
3
hhh
bbb
avv
2
5
jonru
bando
we

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment