Posts: 10
Threads: 1
Joined: Oct 2016
Oct-20-2016, 07:30 PM
(This post was last modified: Oct-20-2016, 07:38 PM by malling.)
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
Posts: 3,458
Threads: 101
Joined: Sep 2016
Sweet. So what's your code look like so far?
Posts: 10
Threads: 1
Joined: Oct 2016
Oct-20-2016, 07:51 PM
(This post was last modified: Oct-20-2016, 08:09 PM by Yoriz.)
1 2 3 4 |
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.
Posts: 1,298
Threads: 38
Joined: Sep 2016
Oct-20-2016, 08:01 PM
(This post was last modified: Oct-20-2016, 08:11 PM by Yoriz.)
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
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Posts: 3,458
Threads: 101
Joined: Sep 2016
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 :)
Posts: 10
Threads: 1
Joined: Oct 2016
Oct-20-2016, 08:08 PM
(This post was last modified: Oct-20-2016, 08:44 PM by malling.)
1 2 3 4 5 6 |
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...
Posts: 3,458
Threads: 101
Joined: Sep 2016
"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.
Posts: 10
Threads: 1
Joined: Oct 2016
Oct-20-2016, 08:53 PM
(This post was last modified: Oct-20-2016, 10:20 PM by Yoriz.)
I think i came up with something. I basicly just repeated the steps from previous task. the apply(left, right, state)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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)
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
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)
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.
Posts: 3,458
Threads: 101
Joined: Sep 2016
Oct-20-2016, 09:50 PM
(This post was last modified: Oct-20-2016, 10:20 PM by Yoriz.)
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...
1 |
flatten = ''.join(flatten)
|
Posts: 10
Threads: 1
Joined: Oct 2016
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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)
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
|