HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Weaving rows of numbers

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
rowsnumbersweaving

Problem

I want to write a cleaner version of this code. It's about two different rows consisting of numbers that have to be weaved. I have already asked this question at this link, but I have translated my code in English so that it's maybe easier to help me improve it.

It's for an assignment for a course I'm currently following:

Traveler, if riches are to be achieved, the solution only has to be weaved.
5,4 4,5 8,7
Weave 6,3 3,2 9,6 4,3
Weave 7,6
Weave 9,8
Weave 5,5 7,8 6,5 6,4


I have to start with the coordinates on the first row, weave the coordinates of the second row through the first row, then weave the coordinates of the third row through the resulting row etc. Note, if two rows of unequal length have to be woven, the two rows are woven as far as possible. After the elements of the shortest row have been exhausted, the row is completed by adding the remaining elements of the longest row.

The input is this (my code already returns the desired output):

5,4 4,5 8,7=6,3 3,2 9,6 4,3=7,6=9,8=5,5 7,8 6,5 6,4


The output has to be like this:

6,4
6,5
10,8
8,8
8,6
7,5
7,3
7,4
5,5
4,2
9,7
10,6
5,3


Coordinate.py

class Coordinate:
def __init__(self, x_coord, y_coord):
self.x_coord = x_coord
self.y_coord = y_coord

def calculate_new_coord(self):
self.x_coord += 1
self.y_coord += 0
return self.x_coord, self.y_coord


CoordinateRow.py

class CoordinateRow:
def __init__(self, row):
self.row = row

def append(self, coord):
self.row += [coord]

def extend(self, row):
for coordinate in row:
self.row += [coordinate]

def weave(self, weaveRow):
result = CoordinateRow([])
for i in range(len(self.row)):
if i len(self.row): #Als 2e rij(te weaven door 1e rij) groter dan 1e rij(bestaande rij)
result.extend(weaveRow.row[len(self.row):]) #voeg het verschil toe aan result
return result


Excerpt of the main code

`fil

Solution

There are some things, which can be improved.

For one, the weave method can be rewritten using itertools, see this SO post for examples.
The idea is to combine elements via iterators (with function similar to zip - see izip_longest), chain and filter out Nones, which were possibly added from missing counterparts.

Then, this pattern:

rows = []
for i in range(len(splits_bestand)):
    rows += [CoordinateRow(splits_bestand[i].split())]


can be replaced with more compact comprehension:

rows = [CoordinateRow(sb.split()) for sb in splits_bestand]


Also, Python list has extend method, which can be used directly in .extend method, like: self.row.extend(row). Similarly for append method. If you will need those at all after using itertools.

Code for previous raw can also be easily rewritten with itertools. Left as an exercise.

Code Snippets

rows = []
for i in range(len(splits_bestand)):
    rows += [CoordinateRow(splits_bestand[i].split())]
rows = [CoordinateRow(sb.split()) for sb in splits_bestand]

Context

StackExchange Code Review Q#148903, answer score: 2

Revisions (0)

No revisions yet.