Print Next Line When Error Occurs - oracle

Using exception I need to print the next value after an error occurred.
For example
FOR i IN 1..50
IF MOD(i,5) <> 0 THEN
dbms_output.put_line(i);
I need to print all values except values divisible by 5.
Thanks in advance.

If this would be code in pl/sql then you are very close - just try to add: BEGIN, LOOP, END IF, END LOOP and END clauses:
begin
FOR i IN 1..50 LOOP
IF MOD(i,5) <> 0 THEN
dbms_output.put_line(i);
end if;
end loop;
end;
/
1
2
3
4
6
7
8
9
11
12
13
14
16
17
18
19
21
22
23
24
26
27
28
29
31
32
33
34
36
37
38
39
41
42
43
44
46
47
48
49

Related

An allocation problem of people into groups getting them to meet as little as possible

I have been trying to solve this for quite a while without success.
I have not found (I searched though) theory that could help me on wikipedia.
Here is the problem.
I have a group of n players (more than 7)
I have a game (diplomacy for those who know !) that requires 7 players, one for these roles : E,F,G,I,A,R and T (countries in fact)
I want to set up a tournament (many games).
There will be n games.
(*) Every player gets into 7 different games, with different role each time
(**) Every game gets 7 different players
=> That is very easy to do.
However, when things get tough, is when you want to limit interactions between players.
What I want is any player to interact (interact = play in same game) at most with one other player.
(In other words, I want to prevent players from making such deals : "I help you in game A, you help me in game B")
So:
Question 1 : For which n is this possible ? (obviously at least 50)
Question 2 : When it is possible, how do you do it ?
Question 3 : What is the algo to minimize these interactions when it is not possible ?
For the record, I did implement a try-and-error program in python (using recursion), working quite well, but I never can get maximum intearctions between players limited to 1 (endless calculations)
thanks for any help !
PS This is no homework, it is for actually designing game tournaments :-)
I did some doodling and I think; If I understand you correctly, that you can do the following:
28 people to do the 7 roles/7 games meeting other players only once.
If the position of a person in the following games is allocated to distinct roles then no person plays the same role in the games they play.
Python
# -*- coding: utf-8 -*-
"""
https://stackoverflow.com/questions/71143133/an-allocation-problem-of-people-into-groups-getting-them-to-meet-as-little-as-po
Created on Sat Feb 19 21:15:02 2022
#author: paddy3118
By hand to get the algo:
0 roles, 0 people for 0 interactions
1 role, 1 person
2 roles, 3 people: p0+p1, p1+p2
3 roles, 012, 134, 235 = 6 people
4 roles, 0123, 1456, 2478, 3579 = 10 people
"""
from itertools import count
def new_person():
yield from count()
def people_allocation(roles: int=4):
games, new_p = [], count()
for i in range(roles):
game = []
games.append(game)
for j in range(i):
game.append(games[j][i])
for _ in range(i, roles):
game.append(next(new_p))
return games, next(new_p)
for roles in range(8):
print(f"Roles = {roles}:")
games, people = people_allocation(roles)
print(f" Takes {people} people in the following games")
print(' ',
', '.join('+'.join(f"P{x}" for x in game) for game in games))
Output
Roles = 0:
Takes 0 people in the following games
Roles = 1:
Takes 1 people in the following games
P0
Roles = 2:
Takes 3 people in the following games
P0+P1, P1+P2
Roles = 3:
Takes 6 people in the following games
P0+P1+P2, P1+P3+P4, P2+P4+P5
Roles = 4:
Takes 10 people in the following games
P0+P1+P2+P3, P1+P4+P5+P6, P2+P5+P7+P8, P3+P6+P8+P9
Roles = 5:
Takes 15 people in the following games
P0+P1+P2+P3+P4, P1+P5+P6+P7+P8, P2+P6+P9+P10+P11, P3+P7+P10+P12+P13, P4+P8+P11+P13+P14
Roles = 6:
Takes 21 people in the following games
P0+P1+P2+P3+P4+P5, P1+P6+P7+P8+P9+P10, P2+P7+P11+P12+P13+P14, P3+P8+P12+P15+P16+P17, P4+P9+P13+P16+P18+P19, P5+P10+P14+P17+P19+P20
Roles = 7:
Takes 28 people in the following games
P0+P1+P2+P3+P4+P5+P6, P1+P7+P8+P9+P10+P11+P12, P2+P8+P13+P14+P15+P16+P17, P3+P9+P14+P18+P19+P20+P21, P4+P10+P15+P19+P22+P23+P24, P5+P11+P16+P20+P23+P25+P26, P6+P12+P17+P21+P24+P26+P27
Assuming that n ≥ 78 or so, the following simple hill climbing algorithm with periodic restarts will return a solution.
The algorithmic idea is to initialize games where each player plays each role exactly once, then drive the number of conflicts to zero (where a conflict is a player playing two roles in a single game, or two players meeting each other more than once) by choosing two random games and a random role and swapping the players involved. We restart every 107 steps because that seems to work well in practice.
Doubtless we could do a little better with constraint programming.
#include <algorithm>
#include <array>
#include <cstdlib>
#include <iostream>
#include <random>
#include <vector>
constexpr int r = 7;
int main() {
int n;
std::cin >> n;
if (n <= r * (r - 1)) {
return EXIT_FAILURE;
}
std::uniform_int_distribution<int> uniform_game(0, n - 1);
std::uniform_int_distribution<int> uniform_role(0, r - 1);
std::random_device device;
std::default_random_engine engine(device());
while (true) {
std::vector<std::array<int, r>> games(n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < r; j++) {
games[i][j] = (i + j) % n;
}
}
int badness = 0;
std::vector<std::vector<int>> pair_counts(n, std::vector<int>(n, 0));
auto count = [&badness, &games, &pair_counts](int i, int j, int increment) {
for (int k = 0; k < r; k++) {
if (k == j) continue;
auto [a, b] = std::minmax(games[i][j], games[i][k]);
badness -= pair_counts[a][b] > (a != b ? 2 : 0);
pair_counts[a][b] += increment;
badness += pair_counts[a][b] > (a != b ? 2 : 0);
}
};
for (int i = 0; i < n; i++) {
for (int j = 0; j < r; j++) {
count(i, j, 1);
}
}
for (long t = 0; t < 10000000; t++) {
int i1;
int i2;
do {
i1 = uniform_game(engine);
i2 = uniform_game(engine);
} while (i1 == i2);
int j = uniform_role(engine);
auto swap_players = [&]() {
count(i2, j, -2);
count(i1, j, -2);
std::swap(games[i1][j], games[i2][j]);
count(i1, j, 2);
count(i2, j, 2);
};
int old_badness = badness;
swap_players();
if (old_badness < badness) {
swap_players();
} else if (badness < old_badness) {
std::cerr << badness << '\n';
}
if (badness <= 0) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < r; j++) {
if (j) std::cout << ' ';
std::cout << games[i][j];
}
std::cout << '\n';
}
return EXIT_SUCCESS;
}
}
}
}
Sample output:
21 38 61 75 77 2 22
70 31 75 7 15 59 69
28 52 29 73 59 23 40
61 45 16 65 35 15 55
12 72 44 45 46 14 10
57 1 3 38 11 6 49
20 6 7 26 0 74 18
54 73 67 58 6 55 75
73 77 63 36 3 0 45
37 57 55 28 34 43 7
17 46 36 66 16 7 48
74 9 24 22 17 73 15
36 50 4 69 28 65 6
59 62 12 32 24 20 51
38 8 59 17 10 19 39
6 53 21 70 13 71 56
55 33 49 59 5 27 36
15 71 33 54 43 18 29
60 36 8 40 71 51 67
19 49 9 34 45 53 60
41 26 73 21 72 35 19
14 64 42 15 57 63 62
44 11 23 27 9 16 21
2 7 68 9 63 52 54
35 18 2 3 60 64 17
29 17 50 41 31 61 57
47 10 32 25 75 28 35
30 48 64 6 32 39 44
46 22 71 35 20 31 11
43 76 41 11 47 60 14
56 2 1 33 74 10 37
51 41 39 0 33 70 34
32 5 18 31 23 76 68
65 43 53 8 27 46 73
63 44 26 5 70 24 28
62 75 66 71 44 3 41
16 69 54 13 22 41 5
58 19 52 57 36 22 70
42 25 22 44 55 56 76
4 30 35 51 76 13 9
72 13 17 62 40 77 43
53 16 10 68 50 58 64
64 47 70 55 38 40 20
50 59 14 48 1 9 71
40 63 19 76 69 1 12
24 35 69 56 68 57 27
67 34 15 72 56 66 32
68 3 46 37 25 21 59
77 54 47 19 4 44 31
76 67 38 46 52 50 33
25 15 77 1 51 26 23
3 61 40 4 53 5 74
45 51 74 29 48 68 47
75 0 57 23 30 8 72
13 27 28 14 19 67 2
9 20 72 42 65 33 3
49 58 48 20 41 37 77
22 12 37 67 39 47 53
26 42 11 61 37 36 13
1 60 5 39 29 75 46
48 40 65 10 54 34 26
23 66 60 50 42 54 24
8 74 13 63 49 32 50
27 29 58 12 7 38 42
7 4 45 24 64 25 8
71 24 30 47 2 49 16
31 28 56 60 12 48 0
10 23 62 49 67 69 61
34 21 31 30 58 62 1
66 70 27 18 61 30 25
0 37 76 64 66 29 65
69 39 43 2 26 45 58
39 65 25 74 62 11 52
5 56 20 52 14 17 30
33 68 6 77 8 12 66
11 55 51 53 18 72 63
52 32 0 43 21 42 4
18 14 34 16 73 4 38
Eventually I think I solved this problem.
I explain it here to help any other person facing such a problem.
I used two "classical" algorithms.
try-and-error to get a first configuration of low quality, with iterations that tries first those players with fewest intearctions and which are already in more games
hill-climibing to improve quality of configuration (making swaps between a conflicting and not conflicting player, or two conflicting players if all players are conflicting) and selecting randomly, keeping the result of swap only if it increases quality - quality is worst number of conflicts (2 usually) and number of occurences)
I reach the following conclusion :
always a solution above 100
never a solution below 90
Thanks for all support you provided !

how to sort the numbers 1, ... n lexicographically without converting the numbers to string?

Assume I have input n how I can print this sequence without convert the number to string?
i.e:-
n = 100
Output :- 1 10 100 11 12 13 14 15 16 17 18 19 2 20 ...
n = 15 Output :- 1 10 11 12 13 14 15 2 3 4 5 6 7 8 9
n = 20 Output :- 1 10 11 12 13 15 15 16 17 18 19 2 20 3 4 5 6 7 8 9
What is the main factor here?
My initial solution that will print the 1's followed by 0's or 2's followed by 0's
int n = 100;
for (int i = 1; i <= n; i++) {
int x = i;
while (x <= n) {
System.out.println(x);
x *= 10;
}
}
I figured out the solution:-
You can call it with initial k = 1
for example printnum(15, 1
void printnums(int n, int k) {
if (k > n) {
return;
}
for (int i = 0; i < 10; i++) {
if (k <= n) {
System.out.println(k);
k *= 10;
printnums(n, k);
k /= 10;
k++;
if (k % 10 == 0) return;
}
}
}
I don't know if there are more optimized one?
Here a condensed solution in Python based on the OP's own answer:
def genRangeLexiSorted(n, k=1):
for i in range(k, min(k+10-k%10, n+1)):
yield i
for j in genRangeLexiSorted(n, 10*i):
yield j
def printnums(n):
print(*list(genRangeLexiSorted(n)))
Then the calls
printnums(1)
printnums(9)
printnums(11)
printnums(20)
printnums(100)
give the following outputs:
1
1 2 3 4 5 6 7 8 9
1 10 11 2 3 4 5 6 7 8 9
1 10 11 12 13 14 15 16 17 18 19 2 20 3 4 5 6 7 8 9
1 10 100 11 12 13 14 15 16 17 18 19 2 20 21 22 23 24 25 26 27 28 29 3 30 31 32 33 34 35 36 37 38 39 4 40 41 42 43 44 45 46 47 48 49 5 50 51 52 53 54 55 56 57 58 59 6 60 61 62 63 64 65 66 67 68 69 7 70 71 72 73 74 75 76 77 78 79 8 80 81 82 83 84 85 86 87 88 89 9 90 91 92 93 94 95 96 97 98 99

Split data in 2 groups in Matlab with both groups having all unique ids

I am trying to split a data set into 2 groups such that both groups have all unique ids present at least once. The data set is something like
01 02 03 04 05 06 07
07 05 08 09 10 11 12
01 04 07 13 08 14 15
06 10 11 12 08 01 02
13 14 10 01 07 03 02
15 01 03 04 10 13 11
11 12 03 05 07 14 15
06 05 10 13 01 09 14
I am trying to use Matlab to split it in 2 roughly equal groups such that both groups have at least one row where the unique ids (in this case 01 - 15) are present at least once. Will appreciate any help in getting this done.
The data has to be divided in a way that entire row has to belong to either group 1 or group 2. I am looking at my output to be 2 matrices such that
01 02 03 04 05 06 07
07 05 08 09 10 11 12
01 04 07 13 08 14 15
06 10 11 12 08 01 02
and
13 14 10 01 07 03 02
15 01 03 04 10 13 11
11 12 03 05 07 14 15
06 05 10 13 01 09 14
are the 2 output groups.
01 02 03 04 05 06 07 is row 1. 07 05 08 09 10 11 12 is row 2 and so on. Each row has 7 ids. There are 8 different rows. I want to divide it in 2 groups such that both groups will have 5/4 rows each (minor variations dont matter). Position of ids in each row cant be changed. Each row has to be sent as whole into group 1 or 2 but the row structure (position of each id in that row) has to remain intact. All unique ids need to be present in both groups.
script
clear;clc
A = [01 02 03 04 05 06 07;
07 05 08 09 10 11 12;
01 04 07 13 08 14 15;
06 10 11 12 08 01 02;
13 14 10 01 07 03 02;
15 01 03 04 10 13 11;
11 12 03 05 07 14 15;
06 05 10 13 01 09 14];
% find unique elements and rows containing them
UniqElem = unique(A);
NUniqElem = length(UniqElem);
UniqIndex = struct('UniqElem', cell(NUniqElem,1), ...
'UniqRows', cell(NUniqElem, 1), 'RowCount', cell(NUniqElem, 1));
for ii = 1:NUniqElem
t1 = UniqElem(ii);
t2 = find(any(A==t1,2));
UniqIndex(ii).UniqRows = t2;
UniqIndex(ii).UniqElem = t1;
UniqIndex(ii).RowCount = length(t2);
end
clear('t1','t2')
% find all possible combinations to make the first group
Combs1 = testf(UniqIndex);
Combs2 = struct('Combination', Combs1, ...
'Unique', {true});
for ii = 1:(length(Combs2)-1)
if Combs2(ii).Unique
CurrentComb = Combs2(ii).Combination;
for jj = (ii+1):length(Combs2)
if Combs2(jj).Unique && ...
all(ismember(CurrentComb,Combs2(jj).Combination))
Combs2(jj).Unique = false;
end
end
end
end
Combs3 = Combs2([Combs2.Unique]);
Combs4 = struct('Grp1', {Combs3.Combination}, 'Grp2', []);
AllRows = 1:size(A,1);
for ii = 1:length(Combs4)
Combs4(ii).Grp2 = AllRows(~ismember(AllRows, Combs4(ii).Grp1));
end
Combs5 = struct('Grp1', [], 'Grp2', []);
for ii = 1:length(Combs4)
if all(ismember(UniqElem, unique(A([Combs4(ii).Grp2], :))))
Combs5(end+1) = Combs4(ii);
end
end
Combinations = Combs5;
for ii = 1:length(Combinations)
fprintf('Solution %d of %d \n', ii, length(Combinations))
CurrentComb = Combinations(ii);
fprintf('Group 1 \n')
for jj = 1:length(CurrentComb.Grp1)
fprintf('R%2d: %s \n', CurrentComb.Grp1(jj), ...
num2str(A(CurrentComb.Grp1(jj), :), '%-4d') )
end
fprintf('Group 2 \n')
for jj = 1:length(CurrentComb.Grp2)
fprintf('R%2d: %s \n', CurrentComb.Grp2(jj), ...
num2str(A(CurrentComb.Grp2(jj), :), '%-4d') )
end
fprintf('\n')
end
function
function Comb = testf(UniqRowIn)
if length(UniqRowIn) == 1
Comb = num2cell(UniqRowIn.UniqRows)';
else
t2 = testf(UniqRowIn(2:end));
t1 = UniqRowIn(1).UniqRows;
Comb = cell(0);
for ii = 1:length(t2)
CurrentComb = t2{ii};
if isempty(intersect(CurrentComb, t1))
for jj = 1:length(t1)
Comb{end+1,1} = sort([CurrentComb, t1(jj)]);
end
else
Comb{end+1,1} = CurrentComb;
end
end
end
end
output
Solution 1 of 12
Group 1
Group 2
Solution 2 of 12
Group 1
R 1: 1 2 3 4 5 6 7
R 2: 7 5 8 9 10 11 12
R 3: 1 4 7 13 8 14 15
Group 2
R 4: 6 10 11 12 8 1 2
R 5: 13 14 10 1 7 3 2
R 6: 15 1 3 4 10 13 11
R 7: 11 12 3 5 7 14 15
R 8: 6 5 10 13 1 9 14
Solution 3 of 12
Group 1
R 3: 1 4 7 13 8 14 15
R 4: 6 10 11 12 8 1 2
R 5: 13 14 10 1 7 3 2
R 8: 6 5 10 13 1 9 14
Group 2
R 1: 1 2 3 4 5 6 7
R 2: 7 5 8 9 10 11 12
R 6: 15 1 3 4 10 13 11
R 7: 11 12 3 5 7 14 15
Solution 4 of 12
Group 1
R 3: 1 4 7 13 8 14 15
R 4: 6 10 11 12 8 1 2
R 6: 15 1 3 4 10 13 11
R 8: 6 5 10 13 1 9 14
Group 2
R 1: 1 2 3 4 5 6 7
R 2: 7 5 8 9 10 11 12
R 5: 13 14 10 1 7 3 2
R 7: 11 12 3 5 7 14 15
Solution 5 of 12
Group 1
R 3: 1 4 7 13 8 14 15
R 4: 6 10 11 12 8 1 2
R 7: 11 12 3 5 7 14 15
R 8: 6 5 10 13 1 9 14
Group 2
R 1: 1 2 3 4 5 6 7
R 2: 7 5 8 9 10 11 12
R 5: 13 14 10 1 7 3 2
R 6: 15 1 3 4 10 13 11
Solution 6 of 12
Group 1
R 1: 1 2 3 4 5 6 7
R 3: 1 4 7 13 8 14 15
R 7: 11 12 3 5 7 14 15
R 8: 6 5 10 13 1 9 14
Group 2
R 2: 7 5 8 9 10 11 12
R 4: 6 10 11 12 8 1 2
R 5: 13 14 10 1 7 3 2
R 6: 15 1 3 4 10 13 11
Solution 7 of 12
Group 1
R 1: 1 2 3 4 5 6 7
R 2: 7 5 8 9 10 11 12
R 5: 13 14 10 1 7 3 2
R 6: 15 1 3 4 10 13 11
Group 2
R 3: 1 4 7 13 8 14 15
R 4: 6 10 11 12 8 1 2
R 7: 11 12 3 5 7 14 15
R 8: 6 5 10 13 1 9 14
Solution 8 of 12
Group 1
R 2: 7 5 8 9 10 11 12
R 4: 6 10 11 12 8 1 2
R 5: 13 14 10 1 7 3 2
R 6: 15 1 3 4 10 13 11
Group 2
R 1: 1 2 3 4 5 6 7
R 3: 1 4 7 13 8 14 15
R 7: 11 12 3 5 7 14 15
R 8: 6 5 10 13 1 9 14
Solution 9 of 12
Group 1
R 4: 6 10 11 12 8 1 2
R 5: 13 14 10 1 7 3 2
R 6: 15 1 3 4 10 13 11
R 8: 6 5 10 13 1 9 14
Group 2
R 1: 1 2 3 4 5 6 7
R 2: 7 5 8 9 10 11 12
R 3: 1 4 7 13 8 14 15
R 7: 11 12 3 5 7 14 15
Solution 10 of 12
Group 1
R 1: 1 2 3 4 5 6 7
R 2: 7 5 8 9 10 11 12
R 6: 15 1 3 4 10 13 11
R 7: 11 12 3 5 7 14 15
Group 2
R 3: 1 4 7 13 8 14 15
R 4: 6 10 11 12 8 1 2
R 5: 13 14 10 1 7 3 2
R 8: 6 5 10 13 1 9 14
Solution 11 of 12
Group 1
R 4: 6 10 11 12 8 1 2
R 6: 15 1 3 4 10 13 11
R 7: 11 12 3 5 7 14 15
R 8: 6 5 10 13 1 9 14
Group 2
R 1: 1 2 3 4 5 6 7
R 2: 7 5 8 9 10 11 12
R 3: 1 4 7 13 8 14 15
R 5: 13 14 10 1 7 3 2
Solution 12 of 12
Group 1
R 1: 1 2 3 4 5 6 7
R 2: 7 5 8 9 10 11 12
R 5: 13 14 10 1 7 3 2
R 7: 11 12 3 5 7 14 15
Group 2
R 3: 1 4 7 13 8 14 15
R 4: 6 10 11 12 8 1 2
R 6: 15 1 3 4 10 13 11
R 8: 6 5 10 13 1 9 14
>>

Pascal reading text file, bad number format

I created text file using pascal, wrote to that text file some lines with numbers
and now I'm trying to read the first line of text file and pascal giving me error BAD NUMBER FORMAT.
Here's code:
program Text_files;
{
procedure CreateFile(f1:string);
var f:text;
x,x1,n:integer;
begin
assign(f,f1);
rewrite(f);
n:=1;
for x1:= 1 to 5 do
begin
for x:= 1 to 20 do
begin
write(f,n,' ');
n:=n+1;
end;
writeln(f);
end;
close(f);
end;
}
procedure ReadFile(f1:string);
var f:text;
n:integer;
begin
assign(f,f1);
reset(f);
while not eoln(f) do
begin
read(f,n);
write(n,' ');
end;
close(f);
end;
begin
//CreateFile('NewFile.txt');
ReadFile('NewFile.txt');
Readln;
end.
I tried to change n variable to string type and it worked i read the first line of text file, but I want that read data to be integer type. What is the problem?
NewFile.txt DATA:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
You can not read numbers when you open a text file. Even if you'd try to read it as a file of records it wouldn't work because those records don't have the same length.
1 is just on byte in size and 100 is three bytes. Additionally you have the spaces and probably EOLs.
So, you would have to change the
procedure CreateFile(f1:string);
var f:file of integer;
x,x1,n:integer;
begin
assign(f,f1);
rewrite(f);
n:=1;
for x1:= 1 to 100 do
begin
write(f,x1);
end;
close(f);
end;
And then
procedure ReadFile(f1:string);
var f:file of integer;
n:integer;
begin
assign(f,f1);
reset(f);
while not eof(f) do
begin
read(f,n);
write(n,' ');
end;
close(f);
end;
begin
CreateFile('NewFile.txt');
ReadFile('NewFile.txt');
end.
This should solve your problem. But it has a drawback: the file-contents are not human readable. If that matters to you for some reason you have to do it the hard way. I.e. you keep your version of CreateFile and rewrite only ReadFile
procedure ReadFile(f1:string);
procedure getNumsFromString(s:string);
var
sTmp: string;
iPos: integer;
i : integer;
begin
repeat
iPos=pos(#32,s);
sTmp=copy(s,1,iPos);
s=copy(s,iPos+1,length(s));
i=strtoInt(sTmp);
write(i,#32);
until (length(s)=0);
end;
var f:text;
s:string;
begin
assign(f,f1);
reset(f);
while not eof(f) do
begin
read(f,s);
getNumsFromString(s);
writeln();
end;
close(f);
end;
the code is untested but you get the idea. Hope it helps
Procedure Reading;
Var
n: longint;
Begin
assign(input,'NewFile.txt');
reset(input);
While not EoF{EoLn} do
begin
read(n);
write(n,' ');
end;
End;

ruby array of arrays saving data across all insances of array, euler 18

for the following code
#!/usr/bin/ruby -w
nums = File::read("euler18nums.txt"); #opens file with number array inside.
parts = nums.split(' '); #save it into instance of an array.
t = [] #dimension one of the array of arrays.
s = [] #dimension two.
j=0 #iteration variable.
k=0 #iteration variable.
n=1 #iteration variable.
parts.collect do |i| #itterates through the array that i have my data in.
s[k] = i.to_i #converts strings to int and save into 2nd dimension.
k+=1
if k == n #number of itterations increase by one every time function is called.
t[j] = s
n+=1 #saves second dimension of array into the first,-
k=0 #-and this is where my problem is because it saves into and overwrites-
test=0 #-of the array that i have saved.
while test != n #this is a test statement to print out the array so far-
print t[test], "\n" #-every time a new array is saved into it
test+=1
end
j+=1
end
end
print t #prints out at the end, this is always just the last-
#-array printed out fifteen times
whenever i save s into t[j] it saves into and overwrites all instances of t that have been created so far, am i misunderstanding ruby arrays, i assume that t[5] = s and wouldn't affect t[4] or t[3] ect. is there a way to do this in which ruby will only save the array for a specific instance or do i need to go back to C++ for this?
the txt file for this is
75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
also found at http://projecteuler.net/problem=18
Looks like every element of t will be exactly the same s array, the final t will look something like this:
t[0] ---\
t[1] ----+--> s
t[2] ---/
When you do this:
t[j] = s
all you're doing is assigning a reference to an array to t[j], you're not making a copy of the s array, you're just making another reference to it. The behavior is exactly the same as in C or C++ if s was an array or pointer.
I'm not sure what you're trying to accomplish with t[j] = s but you probably want to assign a copy of s to t[j]:
t[j] = s.dup
The problem is not "over-writing", it is that you only ever create one second-dimension array (which you point s to). So you are writing pointers to the same object into each t[j]
Move s = [] to the point in the code that you wish to start a new second-dimension array.
If you want to keep existing numbers in the array so far, do something like
s = s.clone
. . . which will (shallow) copy existing contents of array to a new one, and point s at it.

Resources