who to pass index of a loop (for loop) in python - python-3.x

I put this pre defined part in my python code, the aim of code is to get last 2 digits and put it on cluster list based on the predefined logic in cluster (index as list[1] and variable of index in cluster(2).
Now I have problem with my loop that says there are indentation error.
def find_min(matrix=[]):
i,j=0,0
min1=[1000,0,0]
while i <len(matrix):
while j <len(matrix):
if matrix[i][j] != 0:
if min1[0]>matrix[i][j]:
min1[0]=matrix[i][j]
min1[1]=i
min1[2]=j
j+=1
j=0
i+=1
return min1[1:]
def check_min(variable,list2=[]):
i=0
min=1000
list3=list2[:]
result=0
while i<len(list3):
if variable-list3[i]>0:
result=variable-list3[i]
list3[i]=result
else:
result=list3[i]-variable
list3[i]=result
i+=1
i=0
while i<len(list3):
if min>list3[i]:
min=list3[i]
i+=1
return min
def grouped(variable,cluster=[]):
cluster1=cluster[:]
k=0
list3=[]
index=0
while k<3:
index=k
if variable in cluster1[index]:
list3.append(1)
list3.append(cluster[index])
return list1
else:
continue
K+=1
return [0,-1]
list1=[10,11,30,40,70,80]
cluster=[[0,1][10,11],[2,3],[30,40],[],[]]
Then I want to have this loop to work but it shows me idiot errors!
w, h = len(list1), len(list1)
matrix = [[None for x in range(w)] for y in range(h)]
i,j,z=0,0,0
check=0
check2=0
k,e=0,0
flag1=0
index1,index2=0,0
index_i=0
index_j=0
index_cluster_i=0
index_cluster_j=0
temp1=[]
while i <len(list1):
index1=i
while j <len(list1):
index2=j
#if i is in a cluster already
temp1=grouped(index1,cluster)
index_i=temp1[0]
index_cluster_i=temp1[1]
#if j is in a cluster already
temp1=grouped(index2,cluster)
index_j=temp1[0]
index_cluster_j=temp1[1]
if(i>j):
matrix[i][j]=0
elif (index_i==1 and index_j==1):
matrix[i][j]=0
elif(index_i==1 and index_j==0):
matrix[i][j]=check_min(list1[j],cluster[index_cluster_i])
elif(index_i==0 and index_j==0):
if list1[i]>list1[j]:
matrix[i][j]=list1[i]-list1[j]
else:
matrix[i][j]=list1[j]-list1[i]
j+=1
i+=1
error is:
unindent does not match any outer indentation level

This error is for improper indentations align each block under its statement.
Like:
if i === True:
pass
else:
pass

Related

updating value of argument passed in function

I am trying merge sort but the values in the list is not updating . I know arguments are passed by assignments .but how this code is not working
def mergeSort(arr):
#add code here
if len(arr) <2 :
return
mid = len(arr)//2
s1 = arr[:mid]
s2 = arr[mid:]
mergeSort(s1)
mergeSort(s2)
i= 0 ; j =0
while i+j < len(arr):
if i<len(s1) or j ==len(s2) and s1[i] <s2[j] :
arr[i+j] = s1[i]
i +=1
else :
arr[i+j] = s2[j]
j +=1
return arr
mergeSort(arr)
for ex arr =[5,4,3,2,1]
the output is same arr,
but this code is working fine
def mergeSort(arr):
#add code here
if len(arr)>1:
mid=len(arr)//2
l=arr[:mid]
r=arr[mid:]
mergeSort(l)
mergeSort(r)
i=j=k=0
while i<len(l) and j<len(r):
if l[i]<r[j]:
arr[k]=l[i]
i+=1
else:
arr[k]=r[j]
j+=1
k+=1
while i<len(l):
arr[k]=l[i]
i+=1
k+=1
while j<len(r):
arr[k]=r[j]
j+=1
k+=1
return arr
mergeSort(arr)
I don't know what i'm missing ? How the one is updating outer array and one not.

i keep getting "Invalid result type, int expected, <class 'NoneType'> found"

def solution(A):
# write your code in Python 3.6
count=0
for i in range(len(A)):
for j in range(len(A)):
if j!=i:
if A[i]==A[j]:
count = count+1
else:
count = count+0
if count==0:
return A[i]
pass
It looks like the purpose of this code is to loop through list A and return the first value if it does not have a duplicate.
else:
count = count+0
This is redundant, and you can merge the two if statements above it.
def solution(A):
# write your code in Python 3.6
count=0
for i in range(len(A)):
for j in range(len(A)):
if j != i and A[i] == A[j]:
count += 1
if count==0:
return A[i]
The issue is that if count never equals 0, you don't return anything.
Have you found an answer yet? I was just working on the same question on Codility. It has to do with the fact that you're only checking for a value of one. The description was very vague, but it basically comes down to also checking for odd values.
def solution(A):
# write your code in Python 3.6
count=0
for i in range(len(A)):
for j in range(len(A)):
if j != i and A[i] == A[j]:
count += 1
if count % 2 == 1:
return A[i]
return -1
https://app.codility.com/programmers/lessons/2-arrays/odd_occurrences_in_array/

Update list in for loop using list.append

The variable mult is not updated as the program runs. What is the issue with this code? The run results show me that the loop is actually working as I wanted but for the list update and final print
number = 18
for i in range(int(number/2)):
i += 1
mults = []
if number % i == 0:
mults = mults.append(i)
print(i)
elif number % i != 0:
pass
elif i == int(number/2):
print(mults)
with this other code I get the error: AttributeError: 'NoneType' object has no attribute 'append'
number = 18
mults = []
for i in range(int(number/2)):
i += 1
if number % i == 0:
mults = mults.append(i)
print(i)
elif number % i != 0:
pass
print(mults)
number = 18
mults = []
for i in range(int(number/2)):
i += 1
if number % i == 0:
mults.append(i)
print(i)
elif number % i != 0:
pass
print(mults)
Few notes, move mults outside of the for loop so you aren't over writing it every time the loop runs.
You don't need that last elif statement, just print(mults) when the for loop is done, is basically the last elif statement.
mults.append(i) is in line meaning it changes the list mults automatically and you don't need to reassign it.

Recursive function that return result from 'if' statement instead of 'else' statement

I'm pretty new in Python and I'm trying to write a simple recursive function:
def bugged_recursion(inp_value,list_index=0):
'''Define a recursive function that tag lists according to one parameter.
'''
#check if a criterion is true at position 0 in the list
if list_index == 0:
if inp_value[list_index] == 'valid':
status = 'valid inp_value'
#if the criterion is false call the function at the next index
else:
status = 'invalid inp'
list_index +=1
bugged_recursion(inp_value,list_index=list_index)
#check if a criterion is true at position 1 in the list
else:
if inp_value[list_index] == 'valid':
status = 'index {} is a valid inp_value'.format(list_index)
else:
status = 'index is never a valid inp_value'
print(status)
#return the input and its status
return (inp_value,status)
if __name__ == '__main__':
inp_value = ['invalid','invalid']
bugged_recursion(inp_value)
I don't understand why this function return the status from the if statement, instead of returning the status contained in the last else statement.
For me, the strangest is that it prints the right status at some point but won't return it.
I'm unable to understand why... I'm really curious about how I could perform this task using a recursive function.
Wow wow, how tortured this is.
def bugged_recursion(inp_value, list_index=0):
# i don't get why you compare list_index here
if list_index == 0:
# you'll get an IndexError if list_index > len(inp_value)
if inp_value[list_index] == 'valid':
status = 'valid inp_value'
else:
status = 'invalid inp'
# there is only one place where you increment list_index
# i suppose there is something wrong here
list_index +=1
# you forgot to return here
return bugged_recursion(inp_value, list_index=list_index)
else:
if inp_value[list_index] == 'valid':
status = 'index {} is a valid inp_value'.format(list_index)
else:
status = 'index is never a valid inp_value'
return (inp_value,status)
That aside, people usually tend to avoid recursion as much as possible (for example on Dive into Python).
Does this cover your needs?
def no_recursion(inp_value):
for i, val in enumerate(inp_value):
# you probably have a good reason to test the index
if i == 0:
if val == 'valid':
yield 'valid inp_value: %s' % val
else:
yield 'invalid inp: %s' % val
else:
yield 'index %s is %s valid inp_value' % (
i,
'a' if val == 'valid' else 'never'
)
print tuple(no_recursion(inp_value))
Gives: ('invalid inp: invalid', 'index 1 is never valid inp_value')

FizzBuzz with commas instead of newlines

def fizz_buzz(i):
if i % 15 == 0:
return ("FizzBuzz")
elif i % 5 == 0:
return ("Buzz")
elif i % 3 == 0:
return ("Fizz")
else:
return (i)
for i in range(1, 21):
print(fizz_buzz(i))
Where and how would do a new line command here with commas?
Trying to get an output like this: 1,2,Fizz,4,Buzz,Fizz,7,8,Fizz,Buzz,11,Fizz,13,14,FizzBuzz,16,17,Fizz,19, Buzz
but sideways and with commas.
Pass end=',' to print()
def fizz_buzz(i):
if i % 15 == 0:
return ("FizzBuzz")
elif i % 5 == 0:
return ("Buzz")
elif i % 3 == 0:
return ("Fizz")
else:
return (i)
for i in range(1, 21):
print(fizz_buzz(i), end=',')
# prints
1,2,Fizz,4,Buzz,Fizz,7,8,Fizz,Buzz,11,Fizz,13,14,FizzBuzz,16,17,Fizz,19,Buzz,
If you do not want the trailing comma, end the range at 20 and follow with print(fizz_buzz(20))
Consider making a list & joining the elements with a comma. There are some great examples here.
For example:
def fizz_buzz(i):
# your code
my_list = []
for i in range(1,21):
my_list.append( str(fizz_buzz(i)) )
print ",".join(my_list)
There are more elegant ways of doing this -- using generators &c, as in the linked answer --, but this simple code will do what you want. Note that the join() method accepts string only, hence the str() in the list.append(); alternatively you could ensure that your fizz_buzz function returns strings regardless.

Resources