Last active
April 3, 2018 14:13
-
-
Save mkhuda/c5a14b9d917ab98dc8868c409c273614 to your computer and use it in GitHub Desktop.
Looping an array by indexing some element in array itself
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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
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"]]