Решение на Piece table от Иво Димитров

Обратно към всички решения

Към профила на Иво Димитров

Резултати

  • 0 точки от тестове
  • 0 бонус точки
  • 0 точки общо
  • 0 успешни тест(а)
  • 8 неуспешни тест(а)

Код

package main
type editor interface {
// Insert text starting from given position.
Insert(position uint, text string) editor
// Delete length items from offset.
Delete(offset, length uint) editor
// Undo reverts latest change.
Undo() editor
// Redo re-applies latest undone change.
Redo() editor
// String returns complete representation of what a file looks
// like after all manipulations.
String() string
}
type DataRow struct {
origin bool
offset int
length int
}
type MyEditor struct {
origin string
add string
table []DataRow
}
func (receiver *MyEditor) String() string {
return receiver.origin
}
func (receiver *MyEditor) Insert(position uint, text string) MyEditor {
var result MyEditor
var stringUntilPosition string
stringUntilPosition = receiver.origin[:position]
var stringAfterPosition string
stringAfterPosition = receiver.origin[position:]
result.origin = stringUntilPosition + text + stringAfterPosition
var size int = len(receiver.table)
result.table = make([]DataRow, size+2)
for i, _ := range receiver.table {
result.table[i] = receiver.table[i]
}
var row DataRow
row.origin = false
row.offset = receiver.table[size-2].offset + len(receiver.add)
row.length = receiver.table[size-2].length + len(text)
result.table[size] = row
row.origin = true
row.offset = receiver.table[size-1].offset + len(receiver.origin)
row.length = receiver.table[size-1].length + len(text)
result.table[size+1] = row
return result
}

Лог от изпълнението

# _/tmp/d20181107-53-11e69j1 [_/tmp/d20181107-53-11e69j1.test]
./solution_test.go:6:10: undefined: NewEditor
./solution_test.go:19:10: undefined: NewEditor
./solution_test.go:27:10: undefined: NewEditor
./solution_test.go:35:10: undefined: NewEditor
./solution_test.go:44:10: undefined: NewEditor
./solution_test.go:51:10: undefined: NewEditor
./solution_test.go:60:10: undefined: NewEditor
./solution_test.go:67:10: undefined: NewEditor
FAIL	_/tmp/d20181107-53-11e69j1 [build failed]
# _/tmp/d20181107-53-11e69j1 [_/tmp/d20181107-53-11e69j1.test]
./solution_test.go:6:10: undefined: NewEditor
./solution_test.go:19:10: undefined: NewEditor
./solution_test.go:27:10: undefined: NewEditor
./solution_test.go:35:10: undefined: NewEditor
./solution_test.go:44:10: undefined: NewEditor
./solution_test.go:51:10: undefined: NewEditor
./solution_test.go:60:10: undefined: NewEditor
./solution_test.go:67:10: undefined: NewEditor
FAIL	_/tmp/d20181107-53-11e69j1 [build failed]
# _/tmp/d20181107-53-11e69j1 [_/tmp/d20181107-53-11e69j1.test]
./solution_test.go:6:10: undefined: NewEditor
./solution_test.go:19:10: undefined: NewEditor
./solution_test.go:27:10: undefined: NewEditor
./solution_test.go:35:10: undefined: NewEditor
./solution_test.go:44:10: undefined: NewEditor
./solution_test.go:51:10: undefined: NewEditor
./solution_test.go:60:10: undefined: NewEditor
./solution_test.go:67:10: undefined: NewEditor
FAIL	_/tmp/d20181107-53-11e69j1 [build failed]
# _/tmp/d20181107-53-11e69j1 [_/tmp/d20181107-53-11e69j1.test]
./solution_test.go:6:10: undefined: NewEditor
./solution_test.go:19:10: undefined: NewEditor
./solution_test.go:27:10: undefined: NewEditor
./solution_test.go:35:10: undefined: NewEditor
./solution_test.go:44:10: undefined: NewEditor
./solution_test.go:51:10: undefined: NewEditor
./solution_test.go:60:10: undefined: NewEditor
./solution_test.go:67:10: undefined: NewEditor
FAIL	_/tmp/d20181107-53-11e69j1 [build failed]
# _/tmp/d20181107-53-11e69j1 [_/tmp/d20181107-53-11e69j1.test]
./solution_test.go:6:10: undefined: NewEditor
./solution_test.go:19:10: undefined: NewEditor
./solution_test.go:27:10: undefined: NewEditor
./solution_test.go:35:10: undefined: NewEditor
./solution_test.go:44:10: undefined: NewEditor
./solution_test.go:51:10: undefined: NewEditor
./solution_test.go:60:10: undefined: NewEditor
./solution_test.go:67:10: undefined: NewEditor
FAIL	_/tmp/d20181107-53-11e69j1 [build failed]
# _/tmp/d20181107-53-11e69j1 [_/tmp/d20181107-53-11e69j1.test]
./solution_test.go:6:10: undefined: NewEditor
./solution_test.go:19:10: undefined: NewEditor
./solution_test.go:27:10: undefined: NewEditor
./solution_test.go:35:10: undefined: NewEditor
./solution_test.go:44:10: undefined: NewEditor
./solution_test.go:51:10: undefined: NewEditor
./solution_test.go:60:10: undefined: NewEditor
./solution_test.go:67:10: undefined: NewEditor
FAIL	_/tmp/d20181107-53-11e69j1 [build failed]
# _/tmp/d20181107-53-11e69j1 [_/tmp/d20181107-53-11e69j1.test]
./solution_test.go:6:10: undefined: NewEditor
./solution_test.go:19:10: undefined: NewEditor
./solution_test.go:27:10: undefined: NewEditor
./solution_test.go:35:10: undefined: NewEditor
./solution_test.go:44:10: undefined: NewEditor
./solution_test.go:51:10: undefined: NewEditor
./solution_test.go:60:10: undefined: NewEditor
./solution_test.go:67:10: undefined: NewEditor
FAIL	_/tmp/d20181107-53-11e69j1 [build failed]
# _/tmp/d20181107-53-11e69j1 [_/tmp/d20181107-53-11e69j1.test]
./solution_test.go:6:10: undefined: NewEditor
./solution_test.go:19:10: undefined: NewEditor
./solution_test.go:27:10: undefined: NewEditor
./solution_test.go:35:10: undefined: NewEditor
./solution_test.go:44:10: undefined: NewEditor
./solution_test.go:51:10: undefined: NewEditor
./solution_test.go:60:10: undefined: NewEditor
./solution_test.go:67:10: undefined: NewEditor
FAIL	_/tmp/d20181107-53-11e69j1 [build failed]

История (1 версия и 0 коментара)

Иво обнови решението на 07.11.2018 15:41 (преди 9 месеца)

+package main
+
+type editor interface {
+ // Insert text starting from given position.
+ Insert(position uint, text string) editor
+
+ // Delete length items from offset.
+ Delete(offset, length uint) editor
+
+ // Undo reverts latest change.
+ Undo() editor
+
+ // Redo re-applies latest undone change.
+ Redo() editor
+
+ // String returns complete representation of what a file looks
+ // like after all manipulations.
+ String() string
+}
+
+type DataRow struct {
+ origin bool
+ offset int
+ length int
+}
+
+type MyEditor struct {
+ origin string
+ add string
+ table []DataRow
+}
+
+func (receiver *MyEditor) String() string {
+
+ return receiver.origin
+}
+
+func (receiver *MyEditor) Insert(position uint, text string) MyEditor {
+
+ var result MyEditor
+
+ var stringUntilPosition string
+ stringUntilPosition = receiver.origin[:position]
+
+ var stringAfterPosition string
+ stringAfterPosition = receiver.origin[position:]
+
+ result.origin = stringUntilPosition + text + stringAfterPosition
+
+ var size int = len(receiver.table)
+
+ result.table = make([]DataRow, size+2)
+
+ for i, _ := range receiver.table {
+
+ result.table[i] = receiver.table[i]
+ }
+
+ var row DataRow
+
+ row.origin = false
+ row.offset = receiver.table[size-2].offset + len(receiver.add)
+ row.length = receiver.table[size-2].length + len(text)
+ result.table[size] = row
+
+ row.origin = true
+ row.offset = receiver.table[size-1].offset + len(receiver.origin)
+ row.length = receiver.table[size-1].length + len(text)
+ result.table[size+1] = row
+
+ return result
+}