Infinite loop while trying to determine the injectivity of a list [duplicate]
This question already has an answer here:
Convert from “For-loops” to “While-loops”
3 answers
For a school work, we have to create a function that determines if a list is injective, which returns True or False. 2 ways of doing the job are recommended : with 'for' loops and with 'while' loops. I managed to program with 'for' loops, but I'm having issues with 'while' loops, as i cannot figure out why mine is infinite.
I'll put my code below. I already tried getting rid of few variables that where needless, and as I modify the value of 'a' within the loop, it seems to me there is nothing wrong on paper.
def injective(x):
i=0
k=i+1
a=True
while i<len(L)-1 : #L is the name of the list
while k<len(L) :
if L[i]==L[k] :
a=False
else :
a=True
i+=1
return a
I expect to get True when the list is injective and False when not
python loops
marked as duplicate by Community♦ Dec 28 '18 at 18:25
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Convert from “For-loops” to “While-loops”
3 answers
For a school work, we have to create a function that determines if a list is injective, which returns True or False. 2 ways of doing the job are recommended : with 'for' loops and with 'while' loops. I managed to program with 'for' loops, but I'm having issues with 'while' loops, as i cannot figure out why mine is infinite.
I'll put my code below. I already tried getting rid of few variables that where needless, and as I modify the value of 'a' within the loop, it seems to me there is nothing wrong on paper.
def injective(x):
i=0
k=i+1
a=True
while i<len(L)-1 : #L is the name of the list
while k<len(L) :
if L[i]==L[k] :
a=False
else :
a=True
i+=1
return a
I expect to get True when the list is injective and False when not
python loops
marked as duplicate by Community♦ Dec 28 '18 at 18:25
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
put k+=1 inside while k<len(L) loop and i+=1 in outer while loop
– prashant rana
Dec 28 '18 at 18:04
L
isx
or something else?
– Anto Jurković
Dec 28 '18 at 18:04
converting afor
loop to awhile
loop would be an easier approach if you have that working. anyways, your innermost loop is never modifying the value ofk
so the condition never becomes false. as a tip, if you can do it in afor
loop, make sure you convert each for loop to awhile
loop by resetting and incrementing at the right levels.
– Paritosh Singh
Dec 28 '18 at 18:04
Thank you for the help I see what i forgot.
– kiki fer
Dec 28 '18 at 18:08
The title given to your question is unlikely to be useful for anybody searching for answers about a similar problem. The question not abot injectivity of a list, but about how to convert for loops to while loops. This already has answers on SO, e.g. here.
– Adrian W
Dec 28 '18 at 18:12
add a comment |
This question already has an answer here:
Convert from “For-loops” to “While-loops”
3 answers
For a school work, we have to create a function that determines if a list is injective, which returns True or False. 2 ways of doing the job are recommended : with 'for' loops and with 'while' loops. I managed to program with 'for' loops, but I'm having issues with 'while' loops, as i cannot figure out why mine is infinite.
I'll put my code below. I already tried getting rid of few variables that where needless, and as I modify the value of 'a' within the loop, it seems to me there is nothing wrong on paper.
def injective(x):
i=0
k=i+1
a=True
while i<len(L)-1 : #L is the name of the list
while k<len(L) :
if L[i]==L[k] :
a=False
else :
a=True
i+=1
return a
I expect to get True when the list is injective and False when not
python loops
This question already has an answer here:
Convert from “For-loops” to “While-loops”
3 answers
For a school work, we have to create a function that determines if a list is injective, which returns True or False. 2 ways of doing the job are recommended : with 'for' loops and with 'while' loops. I managed to program with 'for' loops, but I'm having issues with 'while' loops, as i cannot figure out why mine is infinite.
I'll put my code below. I already tried getting rid of few variables that where needless, and as I modify the value of 'a' within the loop, it seems to me there is nothing wrong on paper.
def injective(x):
i=0
k=i+1
a=True
while i<len(L)-1 : #L is the name of the list
while k<len(L) :
if L[i]==L[k] :
a=False
else :
a=True
i+=1
return a
I expect to get True when the list is injective and False when not
This question already has an answer here:
Convert from “For-loops” to “While-loops”
3 answers
python loops
python loops
asked Dec 28 '18 at 18:01
kiki ferkiki fer
33
33
marked as duplicate by Community♦ Dec 28 '18 at 18:25
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Community♦ Dec 28 '18 at 18:25
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
put k+=1 inside while k<len(L) loop and i+=1 in outer while loop
– prashant rana
Dec 28 '18 at 18:04
L
isx
or something else?
– Anto Jurković
Dec 28 '18 at 18:04
converting afor
loop to awhile
loop would be an easier approach if you have that working. anyways, your innermost loop is never modifying the value ofk
so the condition never becomes false. as a tip, if you can do it in afor
loop, make sure you convert each for loop to awhile
loop by resetting and incrementing at the right levels.
– Paritosh Singh
Dec 28 '18 at 18:04
Thank you for the help I see what i forgot.
– kiki fer
Dec 28 '18 at 18:08
The title given to your question is unlikely to be useful for anybody searching for answers about a similar problem. The question not abot injectivity of a list, but about how to convert for loops to while loops. This already has answers on SO, e.g. here.
– Adrian W
Dec 28 '18 at 18:12
add a comment |
1
put k+=1 inside while k<len(L) loop and i+=1 in outer while loop
– prashant rana
Dec 28 '18 at 18:04
L
isx
or something else?
– Anto Jurković
Dec 28 '18 at 18:04
converting afor
loop to awhile
loop would be an easier approach if you have that working. anyways, your innermost loop is never modifying the value ofk
so the condition never becomes false. as a tip, if you can do it in afor
loop, make sure you convert each for loop to awhile
loop by resetting and incrementing at the right levels.
– Paritosh Singh
Dec 28 '18 at 18:04
Thank you for the help I see what i forgot.
– kiki fer
Dec 28 '18 at 18:08
The title given to your question is unlikely to be useful for anybody searching for answers about a similar problem. The question not abot injectivity of a list, but about how to convert for loops to while loops. This already has answers on SO, e.g. here.
– Adrian W
Dec 28 '18 at 18:12
1
1
put k+=1 inside while k<len(L) loop and i+=1 in outer while loop
– prashant rana
Dec 28 '18 at 18:04
put k+=1 inside while k<len(L) loop and i+=1 in outer while loop
– prashant rana
Dec 28 '18 at 18:04
L
is x
or something else?– Anto Jurković
Dec 28 '18 at 18:04
L
is x
or something else?– Anto Jurković
Dec 28 '18 at 18:04
converting a
for
loop to a while
loop would be an easier approach if you have that working. anyways, your innermost loop is never modifying the value of k
so the condition never becomes false. as a tip, if you can do it in a for
loop, make sure you convert each for loop to a while
loop by resetting and incrementing at the right levels.– Paritosh Singh
Dec 28 '18 at 18:04
converting a
for
loop to a while
loop would be an easier approach if you have that working. anyways, your innermost loop is never modifying the value of k
so the condition never becomes false. as a tip, if you can do it in a for
loop, make sure you convert each for loop to a while
loop by resetting and incrementing at the right levels.– Paritosh Singh
Dec 28 '18 at 18:04
Thank you for the help I see what i forgot.
– kiki fer
Dec 28 '18 at 18:08
Thank you for the help I see what i forgot.
– kiki fer
Dec 28 '18 at 18:08
The title given to your question is unlikely to be useful for anybody searching for answers about a similar problem. The question not abot injectivity of a list, but about how to convert for loops to while loops. This already has answers on SO, e.g. here.
– Adrian W
Dec 28 '18 at 18:12
The title given to your question is unlikely to be useful for anybody searching for answers about a similar problem. The question not abot injectivity of a list, but about how to convert for loops to while loops. This already has answers on SO, e.g. here.
– Adrian W
Dec 28 '18 at 18:12
add a comment |
1 Answer
1
active
oldest
votes
It is because the value of k in the inner loop is not changing, I think you are missing k+=1
somewhere.
Code
def injective(x):
i=0
k=i+1
while i<len(L)-1 : #L is the name of the list
while k<len(L) :
if L[i]==L[k] :
return False
k+=1
i+=1
return True
I have taken the liberty to optimize the code a bit (it will decrease your iterations drastically for a non-injective function). You can use the same in your other program with the for loop.
1
This is similar to what I have for my for loop, thank you for the help
– kiki fer
Dec 28 '18 at 18:13
One more thing, I am not sure why you need 'len(L)-1' in the outer loop, I think you will need to check the last element also. (not sure though)
– javan.rajpopat
Dec 28 '18 at 18:15
Isn't the last element checked with the first iteration ?
– kiki fer
Dec 28 '18 at 18:19
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
It is because the value of k in the inner loop is not changing, I think you are missing k+=1
somewhere.
Code
def injective(x):
i=0
k=i+1
while i<len(L)-1 : #L is the name of the list
while k<len(L) :
if L[i]==L[k] :
return False
k+=1
i+=1
return True
I have taken the liberty to optimize the code a bit (it will decrease your iterations drastically for a non-injective function). You can use the same in your other program with the for loop.
1
This is similar to what I have for my for loop, thank you for the help
– kiki fer
Dec 28 '18 at 18:13
One more thing, I am not sure why you need 'len(L)-1' in the outer loop, I think you will need to check the last element also. (not sure though)
– javan.rajpopat
Dec 28 '18 at 18:15
Isn't the last element checked with the first iteration ?
– kiki fer
Dec 28 '18 at 18:19
add a comment |
It is because the value of k in the inner loop is not changing, I think you are missing k+=1
somewhere.
Code
def injective(x):
i=0
k=i+1
while i<len(L)-1 : #L is the name of the list
while k<len(L) :
if L[i]==L[k] :
return False
k+=1
i+=1
return True
I have taken the liberty to optimize the code a bit (it will decrease your iterations drastically for a non-injective function). You can use the same in your other program with the for loop.
1
This is similar to what I have for my for loop, thank you for the help
– kiki fer
Dec 28 '18 at 18:13
One more thing, I am not sure why you need 'len(L)-1' in the outer loop, I think you will need to check the last element also. (not sure though)
– javan.rajpopat
Dec 28 '18 at 18:15
Isn't the last element checked with the first iteration ?
– kiki fer
Dec 28 '18 at 18:19
add a comment |
It is because the value of k in the inner loop is not changing, I think you are missing k+=1
somewhere.
Code
def injective(x):
i=0
k=i+1
while i<len(L)-1 : #L is the name of the list
while k<len(L) :
if L[i]==L[k] :
return False
k+=1
i+=1
return True
I have taken the liberty to optimize the code a bit (it will decrease your iterations drastically for a non-injective function). You can use the same in your other program with the for loop.
It is because the value of k in the inner loop is not changing, I think you are missing k+=1
somewhere.
Code
def injective(x):
i=0
k=i+1
while i<len(L)-1 : #L is the name of the list
while k<len(L) :
if L[i]==L[k] :
return False
k+=1
i+=1
return True
I have taken the liberty to optimize the code a bit (it will decrease your iterations drastically for a non-injective function). You can use the same in your other program with the for loop.
edited Dec 28 '18 at 18:12
answered Dec 28 '18 at 18:04
javan.rajpopatjavan.rajpopat
1514
1514
1
This is similar to what I have for my for loop, thank you for the help
– kiki fer
Dec 28 '18 at 18:13
One more thing, I am not sure why you need 'len(L)-1' in the outer loop, I think you will need to check the last element also. (not sure though)
– javan.rajpopat
Dec 28 '18 at 18:15
Isn't the last element checked with the first iteration ?
– kiki fer
Dec 28 '18 at 18:19
add a comment |
1
This is similar to what I have for my for loop, thank you for the help
– kiki fer
Dec 28 '18 at 18:13
One more thing, I am not sure why you need 'len(L)-1' in the outer loop, I think you will need to check the last element also. (not sure though)
– javan.rajpopat
Dec 28 '18 at 18:15
Isn't the last element checked with the first iteration ?
– kiki fer
Dec 28 '18 at 18:19
1
1
This is similar to what I have for my for loop, thank you for the help
– kiki fer
Dec 28 '18 at 18:13
This is similar to what I have for my for loop, thank you for the help
– kiki fer
Dec 28 '18 at 18:13
One more thing, I am not sure why you need 'len(L)-1' in the outer loop, I think you will need to check the last element also. (not sure though)
– javan.rajpopat
Dec 28 '18 at 18:15
One more thing, I am not sure why you need 'len(L)-1' in the outer loop, I think you will need to check the last element also. (not sure though)
– javan.rajpopat
Dec 28 '18 at 18:15
Isn't the last element checked with the first iteration ?
– kiki fer
Dec 28 '18 at 18:19
Isn't the last element checked with the first iteration ?
– kiki fer
Dec 28 '18 at 18:19
add a comment |
1
put k+=1 inside while k<len(L) loop and i+=1 in outer while loop
– prashant rana
Dec 28 '18 at 18:04
L
isx
or something else?– Anto Jurković
Dec 28 '18 at 18:04
converting a
for
loop to awhile
loop would be an easier approach if you have that working. anyways, your innermost loop is never modifying the value ofk
so the condition never becomes false. as a tip, if you can do it in afor
loop, make sure you convert each for loop to awhile
loop by resetting and incrementing at the right levels.– Paritosh Singh
Dec 28 '18 at 18:04
Thank you for the help I see what i forgot.
– kiki fer
Dec 28 '18 at 18:08
The title given to your question is unlikely to be useful for anybody searching for answers about a similar problem. The question not abot injectivity of a list, but about how to convert for loops to while loops. This already has answers on SO, e.g. here.
– Adrian W
Dec 28 '18 at 18:12