A
A
Amalat2021-11-18 09:56:46
Python
Amalat, 2021-11-18 09:56:46

How to make a turtle move?

There is a function to which one argument is an integer t>0.
you need to repeat the following operation t times: let's say that at the previous step we got the string G. We need to add the letter X to it, and then add G again, but, firstly, read from the end, and, secondly, in which the letters R replaced by the letters Z and vice versa. For example, with t=1, XXZ is obtained as X + X + Z, with t=2, XXZXXZZ = XXZ + X + XZZ

I get it like this, but this is most likely wrong:

def f(t, d):
    if t == 0:
        pass
    else:
        f(t - 1, 'X')
        print(d, end='')
        f(t - 1, 'Z')

f(4, 'X')


I also could not connect the turtle library to make it move according to the received rules.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-11-18
@Vindicar

#подготавливаем таблицу замены символов
transform_table = str.maketrans( { 'X': 'Z', 'Z': 'X' } )
#функция, производящая один проход преобразования строки
#input[::-1] - это строка, развернутая наоборот
#метод translate() заменяет символы согласно указанной таблице замен
def transform_once(input: str) -> str:
  return input + 'X' + input[::-1].translate(transform_table)
#функция, производящая несколько проходов преобразования строки
def transform_multiple(input: str, t: int) -> str:
  for _ in range(t):
    input = transform_once(input)
  return input

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question