How do I know if two things can see each other? - python-3.x

How can I know if there are two 'L's which can see each other vertically or horizontally? This is a grid-based question. This is the code I currently have, but it doesn't seem to be functioning.
def sight(board):
lst = []
for line in board:
linelst = []
for char in line.strip():
linelst.append(char)
lst.append(linelst)
counter=-1
count=-1
for i in lst:
count+=1
counter = -1
for item in i:
counter+=1
if item == 'L':
numlstings = ['0','1','2','3','4']
for inter in range(1, len(lst)):
for inte in range(1,len(i)):
try:
if lst[count+inte][counter] == 'L' and count+inte > -1 and count+inte < len(lst) and count+inte > count:
return 'unhappy'
if lst[count-inte][counter] == 'L' and count-inte > -1 and count-inte < len(lst) and count-inte < count:
return 'unhappy'
if lst[count][counter+inter] == 'L' and counter+inter > -1 and counter+inter < len(i) and counter+inter<counter:
return 'unhappy'
if lst[count][counter-inter] == 'L' and counter-inter > -1 and counter-inter < len(i) and counter-inter>counter:
return 'unhappy'
if lst[count+inte][counter] == 'X' or lst[count+inte][counter] in numlstings:
break;
if lst[count-inte][counter] == 'X' or lst[count-inte][counter] in numlstings:
break;
if lst[count][counter+inter] == 'X' or lst[count][counter+inter] in numlstings:
break;
if lst[count][counter-inter] == 'X' or lst[count][counter-inter] in numlstings:
break;
except IndexError:
break;
return 'happy'
I want this code to return unhappy if it can see another 'L' in the same row or column, without the 'X' or the numbers intruding it. Otherwise, if it can't see an L it should return happy.
One such board is:
'......X.L.
.X..X...X.
L........L
X.....L...
....XX..X.
...LXX....
.X......L.
L.........
.......LXL
..LXL.....'.strip().split('\n')
This should return unhappy, but with my code it is returning happy.
There are also different boards which I have to test out.

Related

Character Countdown

I'm trying to create a function. Function; it will simply be designed to increase the last letter sequence from its position in the alphabet or letter list.
import time
def CountDown(text,reply=3):
abc = list("ABCDEFGHIJ")
c = 1
text_list = list(text)
while 1:
Index = abc.index(text_list[-c])
if not list(filter(lambda a: a!=abc[-1], text_list)):
return "".join(text_list)
if text_list[-c] == abc[-1]:
text_list[-c] = abc[0]
c += 1
continue
else:
s=1
while 1:
text_list[-c] = abc[(Index+s) if (Index+s)<len(abc) else 0]
if text_list.count(abc[(Index+s) if (Index+s)<len(abc) else 0])+1<reply:
break
s+=1
text_list[-c] = abc[(Index+s) if (Index+s)<len(abc) else 0]
return "".join(text_list)
if __name__ == "__main__":
code="ABHD"
while 1:
code=CountDown(code)
time.sleep(0.5)
print(code)
OUTPUT:
ABHE
ABHF
ABHG
ABHI
ABHJ
ABIA
ABIC
ABID
ABIE
ABIF
ABIG
ABIH
ABIJ
ABJA
ABJC
ABJD
ABJE
ABJF
ABJG
ABJH
ABJI
....(idling)
The code doesn't give an output after a while. I think there is something wrong.
How can I fix this code sample?

Is there a way i can avoid getting "None" when i print out my function?

I am trying to calculate the avarage grade of a subject. but when i print the function i get printed None.
And i do not know how to fix it.
Ive tried returning the value instead then printing the function, but it wont work.
def karakterKonversjon(bokstav):
if bokstav == 'A':
return 6
if bokstav == 'B':
return 5
if bokstav == 'C':
return 4
if bokstav == 'D':
return 3
if bokstav == 'E':
return 2
if bokstav == 'F':
return 1
def konversjonTilBokstav(tall):
if tall == 6:
return 'A'
if tall == 5:
return 'B'
if tall == 4:
return 'C'
if tall == 3:
return 'D'
if tall == 2:
return 'E'
if tall == 1:
return 'F'
def beregnSnitt():
nummer_karakter = 0
suM = 0
for i in emner:
if emner[i] != "":
tall_karakter = eksamen.karakterKonversjon(emner[i])
suM += (tall_karakter * studiepoeng)
suM /= totalPoeng
rundetSvar = eksamen.normal_round(suM)
eksamen.konversjonTilBokstav(rundetSvar)
print(rundetSvar)
def normal_round(suM):
if (float (suM) < 5):
print(math.floor(suM))
else:
print(math.ceil(suM))
THe result i am expecting is
4
C
But i am getting
4
None
I made a few modifications to your code:
(I assume you are importing math in the eksamen file)
def karakterKonversjon(bokstav): # make this function more efficient
atof = ['A','B','C','D','E','F']
for i in range(len(atof)):
if bokstav.upper() == atof[i]:
return len(atof) - i
def konversjonTilBokstav(tall): # make this function more efficient
atof = ['A','B','C','D','E','F']
for i in range(1,7):
if tall == i:
return atof[len(atof)-i]
def beregnSnitt():
nummer_karakter = 0
suM = 0
for i in range(len(emner)): # if enmer == "enmer"(for example) you cannot reference enmer['e'] without raising an error
if emner[i] != "":
tall_karakter = eksamen.karakterKonversjon(emner[i])
suM += (tall_karakter * studiepoeng)
suM /= totalPoeng
rundetSvar = eksamen.normal_round(suM)
eksamen.konversjonTilBokstav(rundetSvar)
print(rundetSvar)
def normal_round(suM):
if (float (suM) < 5):
print(math.floor(suM))
else:
print(math.ceil(suM))
apart from the differences i made, your code should work well however i cannot test without knowing what enmer is (i assume it's a string) or what studiepoeng is.
the function konversjonTilBokstav did work fine for me. But what I have included in the code snippet should be less spaghetti code

Strange behaviour when adding values to elements of a python list

I'm trying to create a game which randomly chooses a "room" layout from a set and then puts it in 1 of 4 positions on the screen. My code to achieve this is:
room_spawns = [[randint(0, 5)] for a in range(4)]
for i in range(0, 4):
these_segments = []
these_shapes = []
if i == 1:
modifier = [800, 0]
elif i == 2:
modifier = [0, 448]
elif i == 3:
modifier = [800, 448]
else:
modifier = [0, 0]
if room_spawns[i] == 0:
these_segments = room_1_segments
these_shapes = room_1_shapes
elif room_spawns[i] == 1:
these_segments = room_2_segments
these_shapes = room_2_shapes
elif room_spawns[i] == 2:
these_segments = room_3_segments
these_shapes = room_3_shapes
elif room_spawns[i] == 3:
these_segments = room_4_segments
these_shapes = room_4_shapes
elif room_spawns[i] == 4:
these_segments = room_5_segments
these_shapes = room_5_shapes
elif room_spawns[i] == 5:
these_segments = room_6_segments
these_shapes = room_6_shapes
for z in range(0, len(these_segments)):
these_segments[z][0] += modifier[0]
these_segments[z][1] += modifier[1]
for x in range(0, len(these_shapes)):
these_shapes[x][0][0] += modifier[0]
these_shapes[x][0][1] += modifier[1]
these_shapes[x][1][0] += modifier[0]
these_shapes[x][1][1] += modifier[1]
segments.append(these_segments)
shapes.extend(these_shapes)
However, whenever a room which is the same as one chosen previously is chosen, its value is increased by the modifier value more than once and as such doesn't appear on screen. Can anybody please explain why this happens, and how it could be fixed.
Edit: After further research and debugging, for some reason, when adding the modifier value to these_segments and these_shapes, that same value is also added to the identical values already in segments and shapes, as seen here. It appears as though the values of room_2_segments themselves have been modified. Does anybody know why this is and how it could be prevented?
Notice that room_spawns is a list with length of one (it contains one boolean). Thus, room_spawns[0] will return that boolean, but for i>0 you get an IndexError.
Also, note that [randint(0, 5)] in range(4) will always return False because [randint(0,5)] is not an integer, it's a list with only one element. And since room_spawns is a list with this boolean, it will always be equal to [False].
Maybe you want room_spawns to be a list with more than one element?

python function project, histo function that return o

histogram(xs): Given a list of integers named xs, return a string which, when printed, shows a histogram corresponding to the integers. Use small "o" as the display symbol.
Assume: xs is a list of non-negative integers; xs could be empty.
Restrictions: none currently stated.
histogram([3,10,0,5]) → 'ooo\noooooooooo\n\nooooo'
histogram([]) → ''
This is what I tried:
def histogram(xs):
last = len(xs)
histo = ''
for number in xs:
while number > 0:
histo += 'o'
number -= 1
last -= 1
if last == 0:
if histo == None:
return ''
return histo
histo += '\n'
I may have had your indentation wrong when I edited your question. I had to guess because I was pulling it out of the comment. Also, you might have had an indentation problem. Anyways, this seems to work (It's your original code with proper indentation):
def histogram(xs):
last = len(xs)
histo = ''
for number in xs:
while number > 0:
histo += 'o'
number -= 1
last -= 1
if last == 0:
if histo == None:
return ''
return histo
histo += '\n'
Here's a more compact solution:
def histogram(xs):
output = ""
for number in xs:
output += "o" * number + "\n"
return output

Python3: Why does list.append('string') appends to the element of the list not to list itself?

I am trying to parse equations as prepared text (eg:"0.035*Vp-31.5"),
extract decimal numbers,
save them to a list and replace them by 'X' in the equation.
Then return everything ...
Strange output:
('0.035*Vp-31.5', ['0.03531.5'])
It is supposed to be:
('X*Vp-X', ['0.035','31.5'])
def const(eqNo):
temp = ''
args = []
eq = eq_s[eqNo]
for i in range(len(eq)):
if eq[i].isdigit():
temp+=eq[i]
elif eq[i] == '.':
temp+=eq[i]
elif eq[i].isdigit == False:
if len(temp) != 0:
args.append(temp)
temp = ''
else:
if len(temp) != 0:
args.append(temp)
for j in args:
eq.replace(j,'X',1)
return eq, args
The error is with this
elif eq[i].isdigit == False:
you forgot parenthesis after isdigit, it should be
elif eq[i].isdigit() == False:
Cheers!

Resources