Search code examples
pythonpython-3.xmemoization

Can python recognise patterns in the printed statements while printing more than one line in a function?


I have a function which prints a bunch of lines one after another. It takes an input of different symbols and then replaces a string obj in a list with those symbols based on an if condition.

eg:

original_string = ["|-----", "|-----", "|-----", "|-----", "|-----"]

my_list = ['*', '+']


def foo(my_list):
    new_string = original_string[:]
    for i in range(len(original_string)):
        # The following if condition changes often to print different patterns.
        # I've evaluated it to give an even integer for simplicity purposes
        if i % 2 == 0:
            new_string.pop(i)
            new_string.insert(i, f"|--{my_list[0]}--")
        print(new_string)


foo(my_list)

I get the following output:

['|--*--', '|-----', '|-----', '|-----', '|-----']
['|--*--', '|-----', '|-----', '|-----', '|-----']
['|--*--', '|-----', '|--*--', '|-----', '|-----']
['|--*--', '|-----', '|--*--', '|-----', '|-----']
['|--*--', '|-----', '|--*--', '|-----', '|--*--']

My question is this: How can I can have a python program recognise what output patters were generated and take action accordingly?

WHAT I AM LOOKING FOR IS SOMETHING LIKE THIS:

This is My actual code output:

Output pattern I want to recognise:

Pattern A:
    1 |-------x------------
    2 |-----------------x--
    3 |-----------------x--
    4 |-----------------x--
    5 |-------x------------
    6 |--------------------


Actual Final Output pattern:

1 |-------3------------------------8--------10------12
2 |-------3---------5--------------8------------------
3 |-----------------5---------7---------9-----------12
4 |--2--------------5-----------------------10------12
5 |-------3---------5---------7-------------10--------
6 |-------3------------------------8--------10------12

So, if Pattern A exists within final output, I should only print position of pattern A in the output and x should be replaced by the fret position numbers as found in the actual output.

Required output would look something like this:

    1 |-------3------------
    2 |-----------------5--
    3 |-----------------5--
    4 |-----------------5--
    5 |-------3------------
    6 |--------------------

Note: One position here is 5 char. i.e, "--x--" is one position


Solution

  • If I understand the crux of this problem, your input will be a fret board with stopped positions marked (the ones with a digit holding the string). You want to identify a stored chord within this board.

    You're focusing in the wrong area. Why do you want to recognize the output form? That's a bunch of characters for human convenience. To make this a reasonable application, you have to abstract the qualities of each chord, and store that in a fashion that's easier to match to a similarly abstracted fret board.

    As you've said, the parameter of any match is the starting fret: how far do you need to "capo" the chord to get a match. Think of this as a lattice on the Cartesian plane. (0,0) is your "Capo" fret just off the "top", on an imaginary string 0 (above string 1, the high E), the upper-left corner of your diagram. Now, your example chord, Amaj, is anchored at the high-E, 1st fret, represented by five lattice points by (fret, string) pairs:

    [ (0, 1),
      (2, 2),
      (2, 3),
      (2, 4),
      (0, 5) ]
    

    Now, the translation of this to any fret f is

    [ (f+0, 1),
      (f+2, 2),
      (f+2, 3),
      (f+2, 4),
      (f+0, 5) ]
    

    ... and that is the pattern you want to find, for f in range(12), perhaps.

    Keep a list of those fret-point patterns, perhaps a dict with chord labels.

    • Write an input function to reduce a 6-line fret board to a matrix of points.
    • Write an output function to render a result into the desired 6-line form, as in your 3+5 Amaj example.
    • Write a matching function to iterate through the stored chords. For each chord, slide it from fret 0 up to your practical limit, looking for an instance where all of the points match the input board.