2
\$\begingroup\$

I have this code to weave the split_input ['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'] together. However I feel like this can be done more efficient (especially the weave function) but I have no clue how to improve it without importing any modules.

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

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

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

    def weave(self, row2):
        result = Class1([])
        for i in range(len(self.row)):
            if i < len(self.row):
                result.append(self.row[i])
            if i < len(row2.row):
                result.append(row2.row[i])
        if len(row2.row) > len(self.row):
            result.extend(row2.row[len(self.row):])
        return result

def get_route(split_input):
    rows = []
    for i in range(len(split_input)):
        rows += [Class1(split_input[i].split())]
    previous_row = rows[0]

    for row in rows[1:]:
        previous_row = previous_row.weave(row)
    return previous_row


woven_rows = get_route(split_input)
print woven_rows

Do you have any good advice?

The output of the code is one list. First, the first and second list are weaved into one list. Then this list is weaved with the third list etc.

\$\endgroup\$
3
  • \$\begingroup\$ When I print print(woven_rows.row) it shows ['5,4', '5,5', '9,8', '7,8', '7,6', '6,5', '6,3', '6,4', '4,5', '3,2', '8,7', '9,6', '4,3']. If that's the final result - why it should be namely like that and in that order? \$\endgroup\$ Dec 3, 2019 at 16:31
  • 2
    \$\begingroup\$ So, weaving [1, 2, 3, 4] with ["a", "b", "c", "d"] should produce [1, "a", 2, "b", 3, "c", 4, "d"], and then you somehow weave that with ["!", "@", "#", "$"], although I have no idea how that should look? Can you give some easier example in- and output? \$\endgroup\$
    – Graipher
    Dec 3, 2019 at 17:38
  • \$\begingroup\$ @Boris Please keep answers inside answers. Answers only have to state one improvement, so yes your comment is fine as an answer. Thank you. \$\endgroup\$
    – Peilonrayz
    Dec 4, 2019 at 9:02

1 Answer 1

1
\$\begingroup\$

I'm not sure what Class1 is for, but you can weave 2 lists together using a list comprehension to build the first part of the list. Then add on the rest of the longer list. Like so:

def weave2(seq1, seq2):
    result = [item for pair in zip(seq1, seq2) for item in pair]

    if len(seq1) < len(seq2):
        result += seq2[len(seq1):]
    else:
        result += seq1[len(seq2):]

    return result

To weave more than two lists, weave the first two together, then weave in the third list, and so on. Like so:

def weave(*args):
    args = iter(args)
    result = next(args)

    for arg in args:
        result = weave2(result, arg)

    return result
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.