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.
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/
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.
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')
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.