| 1 |
# from http://www.bagley.org/~doug/shootout/bench/lists/lists.ruby |
| 2 |
# which is lost long time ago. The past content can be retrieved from |
| 3 |
# https://web.archive.org/web/20040805115204/http://www.bagley.org/~doug/shootout/bench/lists/lists.ruby |
| 4 |
|
| 5 |
NUM = 300 |
| 6 |
SIZE = 10000 |
| 7 |
|
| 8 |
def test_lists |
| 9 |
# create a list of integers (Li1) from 1 to SIZE |
| 10 |
li1 = (1..SIZE).to_a |
| 11 |
# copy the list to li2 (not by individual items) |
| 12 |
li2 = li1.dup |
| 13 |
# remove each individual item from left side of li2 and |
| 14 |
# append to right side of li3 (preserving order) |
| 15 |
li3 = Array.new |
| 16 |
while (not li2.empty?) |
| 17 |
li3.push(li2.shift) |
| 18 |
end |
| 19 |
# li2 must now be empty |
| 20 |
# remove each individual item from right side of li3 and |
| 21 |
# append to right side of li2 (reversing list) |
| 22 |
until li3.empty? |
| 23 |
li2.push(li3.pop) |
| 24 |
end |
| 25 |
# li3 must now be empty |
| 26 |
# reverse li1 in place |
| 27 |
li1.reverse! |
| 28 |
# check that first item is now SIZE |
| 29 |
if li1[0] != SIZE |
| 30 |
p "not SIZE" |
| 31 |
0 |
| 32 |
else |
| 33 |
# compare li1 and li2 for equality |
| 34 |
if li1 != li2 |
| 35 |
return(0) |
| 36 |
else |
| 37 |
# return the length of the list |
| 38 |
li1.length |
| 39 |
end |
| 40 |
end |
| 41 |
end |
| 42 |
|
| 43 |
i = 0 |
| 44 |
while i<NUM |
| 45 |
i += 1 |
| 46 |
result = test_lists() |
| 47 |
end |
| 48 |
|
| 49 |
result |