Thema: Probleme mit iterable skip list |
|
Hallo, im Folgenden versuche ich vergeblich die Funktionen len und get_node zu programmieren in einer itereable Skip List. Len zu bestimmen über die Anzahl der Einträge ist problematisch, da die Skip List verzweigt und mehr Items da sind als Einträge eingetragen werden. Folgender Code ist zu berücksichtigen;
code: |
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:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
|
# A program to implement a skip list.
# TODO: implement the `getNode` function, which is a prerequisite for __getitem__ and __setitem__
# TODO: implement the `length`
import random
import math as np
index_s = None
class Node:
"""
An item in the skip list holding the data itself and pointers to
the successors of the item at all levels.
"""
def __init__(self, data, max_lvl):
self.data = data
self.successors = [None] * (max_lvl + 1)
# By convention, the width is 0 if no successor is present.
# Otherwise it specifies the deviation of the indices between
# this item and the succeeding item in a level.
self.widths = [0] * (max_lvl + 1)
def __str__(self):
return f'Node(data={self.data}, \
successors={list(map(lambda n: n.data if n else None, self.successors))}, \
widths={self.widths})'
class SkipList:
"""
A class modelling a skip list and providing common list operations.
"""
def __init__(self, max_lvl, p):
self.max_lvl = max_lvl
self.head = None
self.p = p
self.self_length = 0
def insert(self, data, index = None):
"""
Insert a new element into the list.
If index isn't specified, or the index exceeds the highest index
in the list, the item is inserted at the tail of the list.
"""
if self.head == None:
self.head = Node(data, self.max_lvl)
return self.head
else:
n = Node(data, self.max_lvl)
if index is not None and index < 1:
# item will be inserted before current head
n.successors = [self.head] * (self.max_lvl + 1)
n.widths = [1] * (self.max_lvl + 1)
self.head = n
else:
current = self.head
# find the item in each level that will preceed the newly inserted item
to_update = [[None, 0]] * (self.max_lvl + 1)
i = 0
for level in range(self.max_lvl, -1, -1):
while current.successors[level] and \
(index == None or current.widths[level] + i < index):
i = i + current.widths[level]
current = current.successors[level]
to_update[level] = (current, i)
# choose the max level in that a pointer to the new item should be added
max_lvl = 0
while random.random() < self.p and max_lvl < self.max_lvl:
max_lvl = max_lvl + 1
# insert the new item and update its predecessors
for level in range(self.max_lvl + 1):
pred, pred_index = to_update[level]
if level <= max_lvl:
n.successors[level] = pred.successors[level]
new_pred_width = i - pred_index + 1
if pred.successors[level]:
n.widths[level] = pred.widths[level] - new_pred_width + 1
pred.widths[level] = new_pred_width
pred.successors[level] = n
else:
pred.widths[level] = pred.widths[level] + 1
index_s = index
self.self_length += 1
return n
def get_node(self, k):
"""
x = self.head
pos = 0
for i in range(self.max_lvl, -1, -1):
while pos + x.widths[i] < k:
pos = pos + x.widths[i]
x = x.successors[i]
if x == None:
return None
else:
return x
"""
node = self.head
k += 1
for level in reversed(range(self.max_lvl)):
while node.widths[level] <= k:
k -= node.widths[level]
node = node.successors[level]
return node
"""
Returns the node of this skip list at the given index or None if no
item exist for this index.
"""
# TODO your code here
def __getitem__(self, index):
"""Overwrites reading with the array operator `list[i]`."""
n = self.get_node(index)
if n:
return n.data
else:
raise IndexError
def __setitem__(self, index, value):
"""Overwrites writing with the array operator `list[i]`."""
n = self.get_node(index)
if n:
n.data = value
else:
raise IndexError
def updateList(self, elem):
update = [None]*self.max_lvl
x = self.head
for i in reversed(range(self.max_lvl)):
while x.successors[i] != None and x.successors[i].data < elem:
x = x.successors[i]
update[i] = x
return update
def __len__(self):
"""Return the amount of items in this list."""
# TODO your code here
return self.self_length
def __str__(self):
result = ""
if self.head:
for i in range(self.max_lvl, -1, -1):
str = f"lvl {i}: {self.head.data}"
current = self.head
while current.successors[i]:
str += f" -{current.widths[i]}> {current.successors[i].data}"
current = current.successors[i]
if result != "":
result += "\n"
result += str
return result
if __name__ == 'builtins' or __name__ == '__main__':
l = SkipList(4, .5)
l.insert(15)
l.insert(4)
l.insert(2)
l.insert(5)
l.insert(10)
l.insert(11)
l.insert(56)
l.insert(200, 0)
l.insert(400, 7)
print("Skip List structure:")
print(l) |
|
|
|
Thema: Neuer Ansatz zum Neunerpuzzle |
|
Also die Nullpointerausnahmen kommen daher, dass ein Knoten erst erzeugt werden muss, ehe ihm null zugewiesen wird! Das habe ich jetzt behoben, und mein Programm läuft. Allerdings kommt er immer wieder auf die Ausgangslage zurück und bricht dann ab. In der Rekursion muss also noch etwas geändert werden! Kann mir jemand weiterhelfen? Hier der neue Code:
code: |
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:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
|
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package FU_2;
import java.util.ArrayList;
/**
*
* @author Richard Kunstwadl
*/
public class L_E2_3 {
int[][] ziel = {{1, 2, 3}, {4, 5, 6}, {7, 8, 0}};
int[][] ausgang = {{1, 2, 3}, {8, 0, 5}, {4, 7, 6}};
Node root = new Node();
public Node g_erster(int[][] matrix, Node n) {
int generiert = 0;
for (int i = 1; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (matrix[i][j] == 0) {
int zwischen = matrix[i - 1][j];
matrix[i - 1][j] = 0;
matrix[i][j] = zwischen;
i = 3;
j = 3;
generiert = 1;
System.out.println("Neue Matrix errechnet");
ausgabe(matrix);
}
}
}
if (generiert == 1) {
boolean doppelt = false;
while (n != null) {
if (n.content == matrix) {
doppelt = true;
Node neu = new Node();
neu = null;
return neu;
} else {
n = n.parent;
}
}
System.out.println("Ein neuer Knoten wird generiert " + matrix);
Node neu = new Node();
neu.parent = n;
neu.content = matrix;
return neu;
}
Node neu = new Node();
neu = null;
return neu;
}
public Node g_zweiter(int[][] matrix, Node n) {
int generiert = 0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
if (matrix[i][j] == 0) {
int zwischen = matrix[i + 1][j];
matrix[i + 1][j] = 0;
matrix[i][j] = zwischen;
i = 3;
j = 3;
generiert = 1;
System.out.println("Neue Matrix errechnet");
ausgabe(matrix);
}
}
}
if (generiert == 1) {
boolean doppelt = false;
while (n != null) {
if (n.content == matrix) {
doppelt = true;
Node neu = new Node();
neu = null;
return neu;
} else {
n = n.parent;
}
}
System.out.println("Ein neuer Knoten wird generiert " + matrix);
Node neu = new Node();
neu.parent = n;
neu.content = matrix;
return neu;
}
Node neu = new Node();
neu = null;
return neu;
}
public Node g_dritter(int[][] matrix, Node n) {
int generiert = 0;
for (int i = 0; i < 3; i++) {
for (int j = 1; j < 3; j++) {
if (matrix[i][j] == 0) {
int zwischen = matrix[i][j - 1];
matrix[i][j - 1] = 0;
matrix[i][j] = zwischen;
i = 3;
j = 3;
generiert = 1;
System.out.println("Neue Matrix errechnet");
ausgabe(matrix);
}
}
}
if (generiert == 1) {
boolean doppelt = false;
while (n != null) {
if (n.content == matrix) {
doppelt = true;
Node neu = new Node();
neu = null;
return neu;
} else {
n = n.parent;
}
}
System.out.println("Ein neuer Knoten wird generiert " + matrix);
Node neu = new Node();
neu.parent = n;
neu.content = matrix;
return neu;
}
Node neu = new Node();
neu = null;
return neu;
}
public Node g_vierter(int[][] matrix, Node n) {
int generiert = 0;
for (int i = 1; i < 3; i++) {
for (int j = 0; j < 2; j++) {
if (matrix[i][j] == 0) {
int zwischen = matrix[i][j + 1];
matrix[i][j + 1] = 0;
matrix[i][j] = zwischen;
i = 3;
j = 3;
generiert = 1;
System.out.println("Neue Matrix errechnet");
ausgabe(matrix);
}
}
}
if (generiert == 1) {
boolean doppelt = false;
while (n != null) {
if (n.content == matrix) {
doppelt = true;
Node neu = new Node();
neu = null;
return neu;
} else {
n = n.parent;
}
}
System.out.println("Ein neuer Knoten wird generiert " + matrix);
Node neu = new Node();
neu.parent = n;
neu.content = matrix;
return neu;
}
Node neu = new Node();
neu = null;
return neu;
}
public Node insert(Node n) {
if (n != null) {
Node v = new Node();
n.eins = v;
n.eins = g_erster(n.content, n);
if (n.eins != null && !abbruch(n.eins)) {
insert(n.eins);
}
if (abbruch(n.eins)) {
return n.eins;
}
Node w = new Node();
n.zwei = w;
n.zwei = g_zweiter(n.content, n);
if (n.zwei != null && !abbruch(n.zwei)) {
insert(n.zwei);
}
if (abbruch(n.zwei)) {
return n.zwei;
}
Node x = new Node();
n.drei = x;
n.drei = g_dritter(n.content, n);
if (n.drei != null && !abbruch(n.drei)) {
insert(n.drei);
}
if (abbruch(n.drei)) {
return n.drei;
}
Node y = new Node();
n.vier = y;
n.vier = g_vierter(n.content, n);
if (n.vier != null && !abbruch(n.vier)) {
insert(n.vier);
}
if (abbruch(n.vier)) {
return n.vier;
}
}
return null;
}
public boolean abbruch(Node n) {
if (n != null && n.content == ziel) {
return true;
} else {
return false;
}
}
public void ausgabe(int[][] matrix){
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
System.out.println("____________________");
System.out.println();
}
public static void main(String[] args) {
// TODO code application logic here
L_E2_3 p = new L_E2_3();
p.root.content = p.ausgang;
p.root.parent = new Node();
p.root.parent = null;
Node v = new Node();
v = p.root;
Node resultat = new Node();
resultat = p.insert(p.root);
while (resultat != null) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(resultat.content[i][j] + " ");
}
System.out.println();
}
System.out.println("____________________");
System.out.println();
resultat = resultat.parent;
}
}
}
|
|
|
|
Thema: Neuer Ansatz zum Neunerpuzzle |
|
Hallo, jetzt habe ich es mit Bäumen versucht. Die Idee ist die Ausgangsstellung in root einzustellen, und dann nach up, down, Right und left zu verzweigen! So entsteht ein Baum mit einer maximalen Verzweigung von vier. Aber im Programm wird eine nullpointerexception geworfen! Das muss wohl irgendwie damit zusammenhängen, dass ich null da ansetze, wo keine Verzweigung mehr erfolgt! Hier der Code:
code: |
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:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
|
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package FU_2;
import java.util.ArrayList;
/**
*
* @author Richard Kunstwadl
*/
public class L_E2_3 {
int[][] ziel = {{1, 2, 3}, {4, 5, 6}, {7, 8, 0}};
int[][] ausgang = {{1, 2, 3}, {8, 0, 5}, {4, 7, 6}};
Node root = new Node();
public Node g_erster(int[][] matrix, Node n) {
int generiert = 0;
for (int i = 1; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (matrix[i][j] == 0) {
int zwischen = matrix[i - 1][j];
matrix[i - 1][j] = 0;
matrix[i][j] = zwischen;
i = 3;
j = 3;
generiert = 1;
}
}
}
if (generiert == 1) {
boolean doppelt = false;
while (n != null) {
if (n.parent.content == matrix) {
doppelt = true;
return null;
} else {
n = n.parent;
}
}
Node neu = new Node();
neu.parent = n;
neu.content = matrix;
return neu;
}
return null;
}
public Node g_zweiter(int[][] matrix, Node n) {
int generiert = 0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
if (matrix[i][j] == 0) {
int zwischen = matrix[i + 1][j];
matrix[i + 1][j] = 0;
matrix[i][j] = zwischen;
i = 3;
j = 3;
generiert = 1;
}
}
}
if (generiert == 1) {
boolean doppelt = false;
while (n != null) {
if (n.parent.content == matrix) {
doppelt = true;
return null;
} else {
n = n.parent;
}
}
Node neu = new Node();
neu.parent = n;
neu.content = matrix;
return neu;
}
return null;
}
public Node g_dritter(int[][] matrix, Node n) {
int generiert = 0;
for (int i = 0; i < 3; i++) {
for (int j = 1; j < 3; j++) {
if (matrix[i][j] == 0) {
int zwischen = matrix[i][j-1];
matrix[i][j-1] = 0;
matrix[i][j] = zwischen;
i = 3;
j = 3;
generiert = 1;
}
}
}
if (generiert == 1) {
boolean doppelt = false;
while (n != null) {
if (n.parent.content == matrix) {
doppelt = true;
return null;
} else {
n = n.parent;
}
}
Node neu = new Node();
neu.parent = n;
neu.content = matrix;
return neu;
}
return null;
}
public Node g_vierter(int[][] matrix, Node n) {
int generiert = 0;
for (int i = 1; i < 3; i++) {
for (int j = 0; j < 2; j++) {
if (matrix[i][j] == 0) {
int zwischen = matrix[i ][j+1];
matrix[i][j+1] = 0;
matrix[i][j] = zwischen;
i = 3;
j = 3;
generiert = 1;
}
}
}
if (generiert == 1) {
boolean doppelt = false;
while (n != null) {
if (n.parent.content == matrix) {
doppelt = true;
return null;
} else {
n = n.parent;
}
}
Node neu = new Node();
neu.parent = n;
neu.content = matrix;
return neu;
}
return null;
}
public Node insert(Node n) {
if (n != null) {
n.eins = g_erster(n.content, n);
if (n.eins != null && !abbruch(n.eins)){
insert (n.eins);
}
if (abbruch(n.eins)) return n.eins;
n.zwei = g_zweiter(n.content, n);
if (n.zwei != null && !abbruch(n.zwei)){
insert (n.zwei);
}
if (abbruch(n.zwei)) return n.zwei;
n.drei = g_dritter(n.content, n);
if (n.drei != null && !abbruch(n.drei)){
insert (n.drei);
}
if (abbruch(n.drei)) return n.drei;
n.vier = g_vierter(n.content, n);
if (n.vier != null && !abbruch(n.vier)){
insert (n.vier);
}
if (abbruch(n.vier)) return n.vier;
}
return null;
}
public boolean abbruch(Node n){
if (n.content==ziel) { return true;} else {return false;}
}
public static void main(String[] args) {
// TODO code application logic here
L_E2_3 p = new L_E2_3();
p.root.content=p.ausgang;
p.root.parent=null;
Node v = new Node();
v = p.root;
Node resultat = new Node();
resultat= p.insert(v);
while (resultat.parent!=null){
for (int i=0; i<3; i++){
for (int j=0; j<3; j++){
System.out.print(resultat.content[i][j]+ " ");
}
System.out.println();
}
System.out.println("____________________");
System.out.println();
}
resultat=resultat.parent;
}
}
|
|
Es fehlt noch die Nodeklasse:
code: |
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
|
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package FU_2;
/**
*
* @author Richard Kunstwadl
*/
public class Node {
Node eins;
Node zwei;
Node drei;
Node vier;
Node parent;
int[][] content;
}
|
|
|
|
Thema: Neunerpuzzle |
|
Meine erste Fehlersuche ergab, dass ein Stein zuerst verschoben wird, und dann in einer anderen Rekursion wieder zurückgeschoben wird, wodurch keine weitere Rekursion mehr erfolgt. Wie kann ich das vermeiden?
code: |
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:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
|
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package fu_hagen;
import java.util.ArrayList;
/**
*
* @author Richard Kunstwadl
*/
public class FU_Hagen {
int[][] ziel = {{1, 2, 3}, {4, 5, 6}, {7, 8, 0}};
int[][] ausgang = {{1, 2, 3}, {8, 0, 5}, {4, 7, 6}};
ArrayList<int[][]> resultat = new ArrayList<int[][]>();
ArrayList<int[][]> korb = new ArrayList<int[][]>();
public boolean up(int[][] k) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i > 0 && k[i - 1][j] == 0) {
return true;
}
}
}
return false;
}
public boolean down(int[][] k) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i < 2 && k[i + 1][j] == 0) {
return true;
}
}
}
return false;
}
public boolean left(int[][] k) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (j < 2 && k[i][j] == 0) {
return true;
}
}
}
return false;
}
public boolean right(int[][] k) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (j > 0 && k[i][j] == 0) {
return true;
}
}
}
return false;
}
public void starter(int[][] z, int[][] a) {
ArrayList<int[][]> korb_s = new ArrayList<int[][]>();
rekursion(z, a, korb_s);
}
public void rekursion(int[][] z, int[][] a, ArrayList<int[][]> korb) {
boolean ende = true;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (z[i][j] != a[i][j]) {
ende = false;
i = 3;
j = 3;
System.out.println("Falsches Ende");
}
}
}
if (ende == true) {
resultat = korb;
System.out.println("jetzt wird abgebrochen " + a[2][2]);
return;
};
int[][] bi = a;
System.out.println("a an der Stelle 0,1 ist " + a[0][1]);
if (up(a)) {
for (int i = 1; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i > 0 && a[i - 1][j] == 0) {
int zwischen = a[i][j];
a[i][j] = 0;
a[i - 1][j] = zwischen;
System.out.println("Jetzt in up");
if (korb.indexOf(a) == -1) {
System.out.println("Wird hinzugefügt?");
System.out.println("a an der Stelle 1,1 ist " + a[1][1]);
korb.add(a);
rekursion(z, a, korb);
}
}
}
}
}
bi = a;
if (down(a)) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
if (i > 0 && a[i + 1][j] == 0) {
int zwischen = a[i][j];
a[i][j] = 0;
a[i + 1][j] = zwischen;
System.out.println("a an der Stelle 1,1 ist " + a[1][1]);
if (korb.indexOf(a) == -1) {
System.out.println("Wird hinzugefügt? down");
korb.add(a);
rekursion(z, a, korb);
}
}
}
}
}
bi = a;
if (left(a)) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
if (j < 2 && a[i][j] == 0) {
int zwischen = a[i][j + 1];
a[i][j] = zwischen;
a[i][j + 1] = 0;
System.out.println("a an der Stelle 1,1 ist " + a[1][1]);
if (korb.indexOf(a) == -1) {
System.out.println("Wird hinzugefügt? left");
korb.add(a);
rekursion(z, a, korb);
}
}
}
}
}
if (right(a)) {
for (int i = 0; i < 3; i++) {
for (int j = 1; j < 3; j++) {
if (j > 0 && a[i][j] == 0) {
int zwischen = a[i][j - 1];
a[i][j] = zwischen;
a[i][j - 1] = 0;
System.out.println("a an der Stelle 1,1 ist " + a[1][1]);
if (korb.indexOf(a) == -1) {
System.out.println("Wird hinzugefügt? right");
korb.add(a);
rekursion(z, a, korb);
}
}
}
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
FU_Hagen p = new FU_Hagen();
ArrayList<int[][]> a_korb = new ArrayList<int[][]>();
p.starter(p.ziel, p.ausgang);
for (int[][] ausgabe : p.resultat) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(ausgabe[i][j] + " ");
}
System.out.println("");
}
System.out.println("___________________");
}
}
}
|
|
|
|
Thema: Neunerpuzzle |
|
Der Code beinhaltet, dass unendliche Schleifen eingezogen werden. Ich habe versucht das zu verbessern, aber das Problem ist, dass immer die Anfangskonstellation ausgegeben wird!
code: |
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:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
|
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package fu_hagen;
import java.util.ArrayList;
/**
*
* @author Richard Kunstwadl
*/
public class FU_Hagen {
int[][] ziel ={{1,2,3},{4,5,6},{7,8,0}};
int[][] ausgang = {{1,2,3},{8,0,5},{4,7,6}};
public boolean up (int[][] k) {
for (int i= 0; i< 3; i++){
for (int j = 0; j< 3; j++){
if (i>0 && k[i-1][j] == 0){
return true;
}
}
}
return false;
}
public boolean down (int[][] k) {
for (int i= 0; i< 3; i++){
for (int j = 0; j< 3; j++){
if (i<2 && k[i+1][j] == 0){
return true;
}
}
}
return false;
}
public boolean left (int[][] k) {
for (int i= 0; i< 3; i++){
for (int j = 0; j< 3; j++){
if (j < 2 && k[i][j] == 0){
return true;
}
}
}
return false;
}
public boolean right (int[][] k) {
for (int i= 0; i< 3; i++){
for (int j = 0; j< 3; j++){
if (j > 0 && k[i][j] == 0){
return true;
}
}
}
return false;
}
public ArrayList<int[][]> starter (int[][] z, int[][] a){
ArrayList<int[][]> korb_s = new ArrayList<int[][]>();
return rekursion(z,a,korb_s);
}
public ArrayList<int[][]> rekursion(int[][] z, int[][] a, ArrayList<int[][]> korb){
boolean ende = true;
for (int i=0; i<3; i++){
for (int j= 0; j< 3; j++){
if ( z[i][j] != a[i][j] )
{ ende = false;
i = 3; j = 3;
}
}
}
if (ende == true) return korb;
int[][] bi= a;
if (up(bi)){
for (int i= 1; i< 3; i++){
for (int j = 0; j< 3; j++){
if (i>0 && bi[i-1][j] == 0){
int zwischen = bi[i][j];
bi[i][j]=0;
bi[i-1][j]=zwischen;
System.out.println("Jetzt in up");
if (korb.indexOf(bi)== -1){
System.out.println("Wird hinzugefügt?");
korb.add(bi);
return rekursion(z,bi,korb);
}
}
}
}
}
if (down(bi)){
for (int i= 0; i< 2; i++){
for (int j = 0; j< 3; j++){
if (i>0 && bi[i+1][j] == 0){
int zwischen = bi[i][j];
bi[i][j]=0;
bi[i+1][j]=zwischen;
if (korb.indexOf(bi)== -1){
korb.add(bi);
return rekursion(z,bi,korb);
}
}
}
}
}
if (left(bi)){
for (int i= 0; i< 3; i++){
for (int j = 0; j< 2; j++){
if (j < 2 && bi[i][j] == 0){
int zwischen = bi[i][j+1];
bi[i][j]=zwischen;
bi[i][j+1]=0;
if (korb.indexOf(bi)== -1){
korb.add(bi);
return rekursion(z,bi,korb);
}
}
}
}
}
if (right(bi)){
for (int i= 0; i< 3; i++){
for (int j = 1; j< 3; j++){
if (j > 0 && bi[i][j] == 0){
int zwischen = bi[i][j-1];
bi[i][j]=zwischen;
bi[i][j-1]=0;
if (korb.indexOf(bi)== -1){
korb.add(bi);
return rekursion(z,bi,korb);
}
}
}
}
}
return korb;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
FU_Hagen p = new FU_Hagen();
ArrayList<int[][]> a_korb= new ArrayList<int[][]>();
a_korb= p.starter(p.ziel, p.ausgang);
for (int[][] ausgabe : a_korb){
for (int i=0; i<3; i++){
for (int j=0; j<3; j++){
System.out.print(ausgabe[i][j] + " ");
}
System.out.println("");
}
System.out.println("___________________");
}
}
}
|
|
|
|
Thema: Neunerpuzzle |
|
Hallo, es gilt ein 3 mal 3 Feld mit 8 Zahlen und der 0 in eine richtige Reihenfolge zu bringen. Das Feld am Anfang ist anfang, dass Zielfeld ist ziel. Wie kann ich eine Folge vom anfang bis zum ziel erzeugen? Hier mein Vorschlag:
code: |
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:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
|
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package fu_hagen;
import java.util.ArrayList;
/**
*
* @author Richard Kunstwadl
*/
public class FU_Hagen {
int[][] ziel ={{1,2,3},{4,5,6},{7,8,0}};
int[][] ausgang = {{1,2,3},{8,0,5},{4,7,6}};
public boolean up (int[][] k) {
for (int i= 0; i< 3; i++){
for (int j = 0; j< 3; j++){
if (i>0 && k[i-1][j] == 0){
return true;
}
}
}
return false;
}
public boolean down (int[][] k) {
for (int i= 0; i< 3; i++){
for (int j = 0; j< 3; j++){
if (i<2 && k[i+1][j] == 0){
return true;
}
}
}
return false;
}
public boolean left (int[][] k) {
for (int i= 0; i< 3; i++){
for (int j = 0; j< 3; j++){
if (j < 2 && k[i][j] == 0){
return true;
}
}
}
return false;
}
public boolean right (int[][] k) {
for (int i= 0; i< 3; i++){
for (int j = 0; j< 3; j++){
if (j > 0 && k[i][j] == 0){
return true;
}
}
}
return false;
}
public ArrayList<int[][]> starter (int[][] z, int[][] a){
ArrayList<int[][]> korb_s = new ArrayList<int[][]>();
return rekursion(z,a,korb_s);
}
public ArrayList<int[][]> rekursion(int[][] z, int[][] a, ArrayList<int[][]> korb){
if (korb.size()==0){
korb = new ArrayList<int[][]>();
}
boolean ende = true;
for (int i=0; i<3; i++){
for (int j= 0; j< 3; j++){
if ( z[i][j] != a[i][j] )
{ ende = false;
i = 3; j = 3;
}
}
}
if (ende == true) return korb;
int[][] bi=a;
if (up(bi)){
for (int i= 0; i< 3; i++){
for (int j = 0; j< 3; j++){
if (i>0 && bi[i-1][j] == 0){
int zwischen = bi[i][j];
bi[i][j]=0;
bi[i-1][j]=zwischen;
korb.add(bi);
rekursion(z,bi,korb);
}
}
}
}
bi = a;
if (down(bi)){
for (int i= 0; i< 3; i++){
for (int j = 0; j< 3; j++){
if (i>0 && bi[i+1][j] == 0){
int zwischen = bi[i][j];
bi[i][j]=0;
bi[i+1][j]=zwischen;
korb.add(bi);
rekursion(z,bi,korb);
}
}
}
}
bi = a;
if (left(bi)){
for (int i= 0; i< 3; i++){
for (int j = 0; j< 3; j++){
if (j <2 && bi[i][j] == 0){
int zwischen = bi[i][j+1];
bi[i][j]=zwischen;
bi[i][j+1]=0;
korb.add(bi);
rekursion(z,bi,korb);
}
}
}
}
bi = a;
if (right(bi)){
for (int i= 0; i< 3; i++){
for (int j = 0; j< 3; j++){
if (j > 0 && bi[i][j] == 0){
int zwischen = bi[i][j-1];
bi[i][j]=zwischen;
bi[i][j-1]=0;
korb.add(bi);
rekursion(z,bi,korb);
}
}
}
}
return korb;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
FU_Hagen p = new FU_Hagen();
ArrayList<int[][]> a_korb= new ArrayList<int[][]>();
a_korb= p.starter(p.ziel, p.ausgang);
for (int[][] ausgabe : a_korb){
for (int i=0; i<3; i++){
for (int j=0; j<3; j++){
System.out.print(ausgabe[i][j] + " ");
}
}
System.out.println("___________________");
}
}
}
|
|
|
|
Thema: Einfacher Javacode funktioniert niccht |
|
Habe den Fehler schon gefunden; nicht == bei der Zuweisung und Felder werden a[i][j] angesprochen! Schon lange nicht mehr programmiert, daher der Fehler!
|
|
Thema: Einfacher Javacode funktioniert niccht |
|
Die Vergleichung zweier Werte ist schwieriger als ich dachte: Was ist falsch an folgendem Code, den mein Compiler nicht akzeptiert?!
code: |
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:
|
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package fu_hagen;
import java.util.ArrayList;
/**
*
* @author Richard Kunstwadl
*/
public class FU_Hagen {
int[][] ziel ={{1,2,3},{4,5,6},{7,8,0}};
int[][] ausgang = {{1,2,3},{8,0,5},{4,7,6}};
public void rekursion(int[][] z, int[][] a, ArrayList<int[][]> korb){
if (korb.size()==0){
korb = new ArrayList<int[][]>();
}
boolean ende = true;
for (int i=0; i<3; i++){
for (int j= 0; j< 3; j++){
if ( z[i,j] != a[i,j] )
{ ende == false;
}
}
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
}
}
|
|
|
|
Thema: Figur in x3d |
|
Ein erster Ansatz:
code: |
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:
|
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE X3D PUBLIC "ISO//Web3D//DTD X3D 3.3//EN" "http://www.web3d.org/specifications/x3d-3.3.dtd">
<X3D profile='Immersive' version='3.3' xmlns:xsd='http://www.w3.org/2001/XMLSchema-instance' xsd:noNamespaceSchemaLocation='http://www.web3d.org/specification s/x3d-3.3.xsd'>
<Scene>
<group>
<shape>
<Appearance>
<Material DEF="BarColor" diffuseColor="0 0 1"
specularColor="0.5 0.5 0.5" transparency="0.2">
<IS>
<connect nodeField="diffuseColor" protoField="barColor"/>
</IS>
</Material>
</Appearance>
<Box DEF="bar" size="2 2 2"/>
</shape>
<shape>
<Appearance>
<Material DEF="PointerColor"
diffuseColor="1 1 1" specularColor="0.5 0.5 0.5">
</Material>
</Appearance>
<Box DEF="bar" size="2 2 0.1"/>
</shape>
</group>
</Scene>
</X3D>
|
|
|
|
Thema: Figur in x3d |
|
Die Aufgabe lautet, dass man eine Box aufrecht stehend, wobei nur die Kanten zu sehen sind, zu programmieren in der eine Ebene eingezogen ist, die die Höhe in der Box zeigt, und diese Ebene soll in der Box nach oben und unten verschoben werden können. Finde keine Beispiele im Netz!
|
|
Thema: Zerlegung einer Zahl in Primfaktoren |
|
Ich möchte eine Zahl in Primfaktoren zerlegen. Allerdings funktioniert folgendes nicht:
code: |
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:
|
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication2;
import java.util.ArrayList;
/**
*
* @author
*/
public class EA6_Aufgabe_2_1 {
public static boolean isPrim(double value) {
if (value <= 2) {
return (value == 2);
}
for (double i = 2.0; i * i <= value; i++) {
if ((value % i == 0) && ((int)value == value)) {
return false;
}
}
return true;
}
public ArrayList<Double> zerlege(int n){
ArrayList<Double> z = new ArrayList();
for (int i= 3; i< (int) Math.sqrt(n); i++){
if (isPrim(i)){
if (isPrim((n*1.0)/(i*1.0))){
z.add(i*1.0);
z.add(n*1.0/i);
}
}
}
return z;
}
public static void main(String[] args) {
EA6_Aufgabe_2_1 ea6 = new EA6_Aufgabe_2_1();
ArrayList<Double> ergebnis = new ArrayList();
ergebnis = ea6.zerlege(3599);
for (double i: ergebnis) System.out.println("Ich habe gefunden " + i);
}
}
|
|
|
|
Thema: Erzeugung eines Sockets und Schichtenmodell |
|
Frage: Bei der Erzeugung eines Sockets müssen eine IP-Adresse und eine Portnummer übergeben werden. Ist die Verwendung der Portnummer und der IP-Adresse hier problematisch, angesichts des Prinzips des Schichtenmodells?
Mögliche Antwort: Probleme können bspw. bei der Textkodierung auftreten. Wird nur ein Bytestrom übertragen gibt es keine Probleme. Bei der Kommunikation mit Sockets sind viele Fehlerquellen möglich, z.B. ein nicht antwortender Server oder eine nicht vorhandene Verbindung mit dem Internet.
Frage: Wie viele Sockets benötigt der Server-Prozess bei der verbindungsorientierten Kommunikation, wenn er n Verbindungen gleichzeitig unterstützt und jede Verbindung einen eigenen Socket verwendet?
Mögliche Antwort: keine Idee
|
|
Thema: Steine aufklauben |
|
Vielleicht kann ich mein Anliegen durch einen regelbasierten Ansatz klar machen:
Ein Stein ist gefunden wenn:
not(Feld.h) und not(Feld.Ö) und not(Feld.B) und not(Feld.N) ==> klaube Stein auf.
Da aber nach Feld.B nicht traversiert wird, reduziert sich diese Regel auf:
not(Feld.h) und not(Feld.Ö) und not(Feld.N) ==> klaube Stein auf.
Die Logik des Sachverhaltes wirkt sich so auf den Code aus. Da der Code den Regelbestandteil nur indirekt repräsentiert, spreche ich davon, dass es sich um ein Unbewußtes des Codes handelt.
Nochmals danke für deine Programmierungshinweise.
|
|
Thema: Steine aufklauben |
|
Ich beschäftige mich mit der Psychoanalyse des Programmierens. Da ist es von Interesse, was als eine Form des Unbewußten in Programmen aufgefaßt werden kann. Das ist also keine Frage, sondern nur ein Anstoß für Interessierte.
|
|
Thema: Steine aufklauben |
|
Mal eine Perspektive auf den Code:
code: |
1:
2:
3:
|
if (feld[x][y]!="H" && feld[x][y]!="Ö" && feld[x][y]!="N"){
gefunden.add(feld[x][y]);
} |
|
reagiert auf die Redundanz:
code: |
1:
2:
3:
|
if (feld[x][y]!="H" && feld[x][y]!="Ö" && feld[x][y]!="N" && feld[x][y] !="B"){
gefunden.add(feld[x][y]);
} |
|
In diesem Sinne hat der Code ein Unbewußtes.
|
|
|