Python Forum

Full Version: Increasing depth in python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
It applies all rules in rules. That's what I can get of information from the task description.

but I think it is Strings. Like letters

And yes, F should be replaced by a list of other letters, like F L F R F L F
Sweet. So what's your code look like so far?
def step(rules, state):
   for rule in rules:
       state=rule(state)
    return state
it's not correctly written here i see.... i need the tabs but they are there in my python file.
I get an error saying "Error: 'str' object is not callable."
Should i use global because that doesn't seem to work either.
When posting code, use the "Python" button (6th button from right)

When posting errors, use the "Red X" button (5th button from right)
Error:
Error: 'str' object is not callable.
You should post the entire Traceback error
The error message says the string isn't callable. Which means that code I wrote for you won't work.
Globals have nothing to do with this, so using a global won't do anything to help.
And code will be formatted correctly if you insert it into a code box.

Try again :)
def step(rules, state):
   for rule in rules:
      state=rule(state)
   return state
   
step("F",["F","R","L","F"])    
Error:
Traceback (most recent call last): File "C:\Users\Joach\Desktop\Gedit\Projekter-Latex\projekt1-2016\task8.py", line 11, in <module> step("F",["F","R","L","F"]) File "C:\Users\Joach\Desktop\Gedit\Projekter-Latex\projekt1-2016\task8.py", line 3, in step state=rule(state) TypeError: 'str' object is not callable

Im just a little stuck, i have no ideer of what to do at this point :(

Any help? or then i will seek further knowledge on other sites. Im desperate cause im the worst at programming... it seems so simple but yet i fail to do it right...
"A little stuck"? But you haven't done anything yet. We're not going to do your homework for you. The only thing you've done so far is copy the code I gave, without any modifications whatsoever. Please, put in a little effort, or the rest of the class will be extremely difficult for you.
I think i came up with something. I basicly just repeated the steps from previous task. the apply(left, right, state)

def step(rules, state):
  result=[]
  for x in state:
     if x==rules:
        result.append(state)
     else:
        result.append(x)

[inner for outer in result for inner in outer]

print(result)

#This funktion is not made yet. Dont think about it!!!
def compute(depth, rules, start):
  print()

step("F",["F","L","F","R","F","L","F"])   
Now i just need to flatten it but it didn't seem to work as planned.

Update!!!

I found out how to remove the brackets but still haven't removed 2 outer ones. any ideer?
I did this:

def step(rules, state):
   result=[]
   for x in state:
       if x==rules:
           result.append(state)
       else:
           result.append(x)
   
   flatten = [inner for outer in result for inner in outer]
   print(flatten)

#This funktion is not made yet. Dont think about it!!!
def compute(depth, rules, start):
   print()
   
step("F",["F","L","F","R","F","L","F"])
and got this

Output:
['F', 'L', 'F', 'R', 'F', 'L', 'F', 'L', 'F', 'L', 'F', 'R', 'F', 'L', 'F', 'R', 'F', 'L', 'F', 'R', 'F', 'L', 'F', 'L', 'F', 'L', 'F', 'R', 'F', 'L', 'F']
But as you see i still need to remove the last 2 at each side.
That is flat.  The only way to get any flatter is if it isn't a list anymore.  If that's your goal, right before your print, you can do...
flatten = ''.join(flatten)
Okay, so this is my last try.

Im almost done, i just need to make this "start" int function properly.

I made it this far:
def step(rules, state):
   result=[]
   for x in state:
      if x==rules:
         result.append(state)
      else:
         result.append(x)

   flatten = [inner for outer in result for inner in outer]
   print(flatten)

#this function description is to write a function compute(depth,rules,start) that uses the step function depth times to transform the list start into a list representing a fractal of depth depth.
   def compute(depth, rules, start):
      for i in range(depth):
         step("F",["F","L","F","R","F","L","F"])

compute(1,"F",["F","R"])
the list "start" is not in use right now, and i just need some hints to what i can do.
I think i came a lot longer on my own, so can you plz help me a bit here?

I've written what the function is supposed to do in the #symbol above the def compute() function.

best regards Malling
Pages: 1 2