i'm having problem at (return not alternating).The error is 'inconsistent use of spaces'.Could anyone pls help me.
I tried using proper amount of tabs and spaces but still the problem persists.
def alternating(list):
listDifference = []
if list == []:
return True
alternating = True
lPos = 0
rPos = 1
#appends the difference between values to a new lis
while (lPos < len(list)-1) and (rPos < len(list)):
listDifference.append(list[lPos] - list[rPos])
lPos += 1
rPos += 1
#resets the position
lPos,rPos = 0,1
#checks whether values are alternating or not
while (lPos < len(listDifference)-1) and (rPos < len(listDifference)):
if listDifference[lPos] < 0:
if listDifference[rPos] > 0:
lPos += 1
rPos += 1
else:
return not alternating
elif listDifference[lPos] > 0:
if listDifference[rPos] < 0:
lPos += 1
rPos += 1
else:
return not alternating
return alternating
Your problem is that you are using both tabs and spaces which gives and error, soy you need to use only tabs or only spaces.
Related
When I debug my code, the value of the variables shows that they are equal to my limit condition but my while loop still works
I'm trying to stop my while loop by this limitation:
while (i + j) != len(fruits):
Even though (i+j) is equal to len(fruits), the loop still works, it doesn't break.
It should be broken when it meets my limitation.
My code is below:
from typing import List
class Solution:
def totalFruit(self, fruits: List[int]) -> int:
pointed = [0 for i in range(max(fruits) + 1)]
maX, count = 0, 0
count_type = 0
i, j = 0, 0
while (i + j) != len(fruits):
while count_type < 3:
if pointed[fruits[i + j]] == 0:
count_type += 1
if count_type < 3:
count += 1
pointed[fruits[i + j]] += 1
j += 1
elif count_type < 3 and pointed[fruits[i + j]] != 0:
pointed[fruits[i + j]] += 1
count += 1
j += 1
if count > maX: maX = count
if pointed[fruits[i]] == 1: count_type -= 2
pointed[fruits[i]] -= 1
count -= 1
i += 1
j-= 1
return maX
fruits = input()
fruits = [int(i) for i in fruits.split()]
obj = Solution()
print(obj.totalFruit(fruits))
when I debug:
enter image description here
I have an assignment for my Python course that I'm struggling with.
We are supposed to make a function that prints out binary as follows:
If the input is:
chessboard(3)
It should print out:
101
010
101
And so forth..
Its a "simple" program but I'm really new to coding.
I can produce a while loop that writes out the correct length and amount of lines but I'm struggling to produce variation between the lines.
This is what I have come up with so far:
def chessboard(n):
height = n
length = n
while height > 0:
while length > 0:
print("1", end="")
length -= 1
if length > 0:
print("0", end="")
length -= 1
height -= 1
if length == 0:
break
else:
print()
length = n
With the input:
chessboard(3)
It prints out:
101
101
101
Could someone help me figure out how I could start every other line with zero instead of one?
As I understand it, it is simple :
print("stackoverflow")
def chessboard(n):
finalSentence1 = ""
finalSentence2 = ""
for i in range(n): #we add 0 and 1 as much as we have n
if i%2 == 0: #
finalSentence1 += "1"
finalSentence2 += "0"
else:
finalSentence1 += "0"
finalSentence2 += "1"
for i in range(n): #we print as much as we have n
if i%2 == 0:
print(finalSentence1)
else:
print(finalSentence2)
chessboard(3)
returns :
stackoverflow
101
010
101
I am working on the same kind of assignment, but as we have only covered conditional statements and while loops so far, following the same logic, here is my solution:
def chessboard(size):
output_1 = ''
output_2 = ''
i = 1
j = 1
while j <= size:
while i <= size:
if i % 2 == 0:
output_1 += '1'
output_2 += '0'
i += 1
else:
output_1 += '0'
output_2 += '1'
i += 1
if j % 2 == 0:
print(output_1)
j += 1
else:
print(output_2)
j += 1
chessboard(5)
returns:
10101
01010
10101
01010
10101
def chessboard(x):
i = 0
while i < x:
if i % 2 == 0:
row = "10"*x
else:
row = "01"*x
print(row[0:x])
i += 1
startDate, stopDate, startTime, stopTime, startBehavior, and stopBehavior are all lists with the same length. I'm getting a list index out of range in line 5 (if startDate[i] != stopDate[j]) and I'm not sure why. It was working before and now it's not. I'm trying to print the specific behaviors based on the conditions in the if/elif statements.
i = 0
while (i < len(startDate)):
j = 0
while (j < len(stopDate)):
if startDate[i] != stopDate[j]:
j += 1
elif startDate[i] == stopDate[j]:
if stopTime[j] < startTime[i]:
j += 1
elif stopTime[j] > startTime[i]:
if startBehavior[i] != stopBehavior[j]:
j += 1
elif startBehavior[i] == stopBehavior[j]:
print(startBehavior[i])
print(stopBehavior[j])
print('')
i += 1
any help would be appreciated! thank you in advanced!
If you run the following snippet, it will run forever, even though i is clearly greater than 100.
i = 0
while i < 100:
while True:
i += 1
print(i)
Your code is doing something similar -- in the case that i == len(startDate) - 1; startDate[i] == stopDate[j]; stopTime[j] > startTime[i]; and startBehavior[i] == stopBehavior[j], your code will increment i so that i == len(startDate), and then, since j < len(stopDate) is still True, you will not have exited your second while loop. When you try to access startDate[i], you will get an IndexError.
This is somewhat data-dependent so it is possible that it worked before without ever having this issue.
I have written a code using recursion, but later realized that the depth is too high and does not work for higher level input values. It works completely fine without any issue.
def checkCount(first_window, index=0,reference_list=None):
reference_list = []
count = 0
while index<=len(first_window)-2:
if first_window[index] < first_window[index+1]:
index += 1
count += 1
else: break
if count != 0:
reference_list.append(int((count*(count+1))/2))
count = 0
while index <= len(first_window)-2:
if first_window[index] > first_window[index+1]:
index += 1
count += 1
else: break
if count != 0:
reference_list.append(-int((count*(count+1))/2))
if index > len(first_window)-2: return reference_list
elif first_window[index] == first_window[index+1] and index<len(first_window)-2: index += 1
reference_list = reference_list + checkCount(first_window, index, reference_list)
return reference_list
import random
import time
start = time.clock()
input_array = list(map(int,"188930 194123 201345 154243 154243".split(" ")))
input_array = random.sample(range(1,100),10)
def main():
N = len(input_array)
K = 8
if K == 1: return None
print("Input array: ",input_array)
print("First Window",input_array[:K])
print("Real Output", checkCount(input_array[:K]))
if __name__ == "__main__":main()
Now no matter how I try without recursion, it ends with an infinite loop. I have tried different ways but no progress.
One way I have tried is taking out the recursion statement and returning the referencelist + index:
def checkCount(..):
....
....
return referencelist,index
while index <= K-2:
print("While",index)
reference_list, index = checkCount(new_input, index=0, reference_list=[])
referencelist += reference_list
The application is similar to the here. But we have to deal with tons of data where recursion cannot help. Assume that the input array is greater than 100,000. I am really struck here, I do not understand what logic am I missing. Any help will be grateful.
The first_window variable is only read, and the index variable is only incremented. There is no need for recursion, a simple loop can work.
def check_count(first_window):
reference_list = []
index = 0
while index < len(first_window) - 2:
count = 0
while index <= len(first_window) - 2:
if first_window[index] < first_window[index + 1]:
index += 1
count += 1
else:
break
if count != 0:
reference_list.append(int((count * (count + 1)) / 2))
count = 0
while index <= len(first_window) - 2:
if first_window[index] > first_window[index + 1]:
index += 1
count += 1
else:
break
if count != 0:
reference_list.append(-int((count * (count + 1)) / 2))
if index < len(first_window) - 2 and first_window[index] == first_window[index + 1]:
index += 1
return reference_list
Of course, this algorithm can be optimised, for instance, we can avoid repetitions like: len(first_window) - 2.
So I'm trying to implement a ConnectFour game in python, and I'm having some trouble with counting the pieces (from a single player) that are lined up together in a row. My code:
class ConnectFour(object):
def __init__(self):
self.row=6
self.col=7
self.board = []
#initialize the board
for arow in range(self.row):
row = []
for acol in range(self.col):
row.append(None)
self.board.append(row)
#function for counting the number of the same pieces in a row
def count_it(self, row, column, step_row, step_col):
assert row >= 0 and row < 6 and column >= 0 and column < 7
assert step_row != 0 or step_col != 0
counter1 = 0
counter2 = 0
if self.board[row][column] == None:
return 0
elif self.board[row][column] == 1:
for i in range(6):
while self.board[row + (i*step_row)][column + (i*step_col)] == 1:
counter1 += 1
return counter1
else:
for i in range(6):
while self.board[row + (i * step_row)][column + (i*step_col)] == 2:
counter2 += 1
return counter2
When I input a location and "step" in my function, I would like to get the number of pieces player 1 or player 2 has lined up but when I enter:
x= ConnectFour()
x.board[5][6] = 1
x.board[4][6] = 1
x.count_it(5,6,-1,0)
I get no output.
There is no need for that while inside for: whenever the while condition is true, it will become an infinite loop since the body of that loop does not affect the condition, it just keeps incrementing a counter forever.
One approach would be a single while loop:
while self.board[row + (counter1*step_row)][column + (counter1*step_col)] == 1:
counter1 += 1
Another approach is to leave the for loop, but i and counter1 actually serve the same purpose:
for i in range(6):
if self.board[row + (i*step_row)][column + (i*step_col)] != 1:
break
counter1 += 1
In both cases, take care of array boundaries, either by some ifs, or by placing sentinels at the border of the array.