Python Bin, Sort, Recusion and Index numbers
up vote
1
down vote
favorite
I'm trying to find the index of a number in an array(list) but sometimes it falls into the if statement (if upper < lower) even though it's not a true statement.
def recurse(lower,upper,result,target):
if upper < lower:
print("Error, Upper is less then Lower")
return -999
midpoint = (lower + upper)//2
guess = result[midpoint]
if guess == target:
return midpoint
else:
if guess > target:
upper = midpoint -1
return recurse(lower,upper,result,target)
else:
lower = midpoint +1
return recurse(lower,upper,result,target)
result5 = recurse(0,30,result,215)
print("The index of that number is : ",result5)
I have formed a random generation array and have 30 numbers generated between 201 and 300
def randomList(array):
array =
for x in range(29):
r = random.randint(201,300)
array.append(r)
return array
result = randomList(result)
print("Random list of 30 numbers between 201-300",result)
I have pasted the list result [282, 215, 204, 295, 236, 232, 229, 218, 295, 214, 287, 295, 270, 212, 276, 255, 205, 204, 212, 228, 230, 265, 278, 204, 272, 226, 292, 284, 290]
My goal for the recuse function is to display the index of the number I send in the target parameter. Sometimes it actually works and will tell me the index number, but sometimes it doesn't. I'm pretty stumped. I'm sorry for formatting errors.
The result from sending the target number 215 is:
The index of that number is : 1
Which is completely acurate and works, but the problem arises when I find another number, lets say 282, it should give me 0. But it gives me my return value of -999. Or when I do 272, same problem, return -999. It only works on some values. Anyways I'd appreciate any light that can be shed on this problem. Thank you.
python function if-statement recursion
add a comment |
up vote
1
down vote
favorite
I'm trying to find the index of a number in an array(list) but sometimes it falls into the if statement (if upper < lower) even though it's not a true statement.
def recurse(lower,upper,result,target):
if upper < lower:
print("Error, Upper is less then Lower")
return -999
midpoint = (lower + upper)//2
guess = result[midpoint]
if guess == target:
return midpoint
else:
if guess > target:
upper = midpoint -1
return recurse(lower,upper,result,target)
else:
lower = midpoint +1
return recurse(lower,upper,result,target)
result5 = recurse(0,30,result,215)
print("The index of that number is : ",result5)
I have formed a random generation array and have 30 numbers generated between 201 and 300
def randomList(array):
array =
for x in range(29):
r = random.randint(201,300)
array.append(r)
return array
result = randomList(result)
print("Random list of 30 numbers between 201-300",result)
I have pasted the list result [282, 215, 204, 295, 236, 232, 229, 218, 295, 214, 287, 295, 270, 212, 276, 255, 205, 204, 212, 228, 230, 265, 278, 204, 272, 226, 292, 284, 290]
My goal for the recuse function is to display the index of the number I send in the target parameter. Sometimes it actually works and will tell me the index number, but sometimes it doesn't. I'm pretty stumped. I'm sorry for formatting errors.
The result from sending the target number 215 is:
The index of that number is : 1
Which is completely acurate and works, but the problem arises when I find another number, lets say 282, it should give me 0. But it gives me my return value of -999. Or when I do 272, same problem, return -999. It only works on some values. Anyways I'd appreciate any light that can be shed on this problem. Thank you.
python function if-statement recursion
1
Think about what your code assumes. If your guess is greater than your target, then the code says that none of the values further to the right of your guess cannot be equal to the target. When is this true? Does this hold for your input array? What kind of input arrays can this function work on properly?
– eozd
Nov 19 at 21:38
Ok, I'm trying to understand what you're saying. Maybe if they were in ascending order ?
– Dotexe
Nov 19 at 21:45
Yes, you are in the right track. I haven't checked your code in detail, so there might be mistakes in it; but the function would only work if the input array is in ascending order in the first place. Otherwise, you don't have any guarantees for the argument that the element you are looking for cannot be in the subarray you discard.
– eozd
Nov 19 at 21:48
Okay, well I'll give it a shot to order them, but I was thinking since it's just checking the index number, what should the value of the index have anything to do with it other then making sure it matches the Target Parameter
– Dotexe
Nov 19 at 21:55
I am not exactly sure what you are saying; but I got the impression that you think this function actually checks every element of the input array, which is not the case. If your input array has N elements, this function only checks logN of them, and the other N - logN elements are not compared against your target.
– eozd
Nov 19 at 21:58
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm trying to find the index of a number in an array(list) but sometimes it falls into the if statement (if upper < lower) even though it's not a true statement.
def recurse(lower,upper,result,target):
if upper < lower:
print("Error, Upper is less then Lower")
return -999
midpoint = (lower + upper)//2
guess = result[midpoint]
if guess == target:
return midpoint
else:
if guess > target:
upper = midpoint -1
return recurse(lower,upper,result,target)
else:
lower = midpoint +1
return recurse(lower,upper,result,target)
result5 = recurse(0,30,result,215)
print("The index of that number is : ",result5)
I have formed a random generation array and have 30 numbers generated between 201 and 300
def randomList(array):
array =
for x in range(29):
r = random.randint(201,300)
array.append(r)
return array
result = randomList(result)
print("Random list of 30 numbers between 201-300",result)
I have pasted the list result [282, 215, 204, 295, 236, 232, 229, 218, 295, 214, 287, 295, 270, 212, 276, 255, 205, 204, 212, 228, 230, 265, 278, 204, 272, 226, 292, 284, 290]
My goal for the recuse function is to display the index of the number I send in the target parameter. Sometimes it actually works and will tell me the index number, but sometimes it doesn't. I'm pretty stumped. I'm sorry for formatting errors.
The result from sending the target number 215 is:
The index of that number is : 1
Which is completely acurate and works, but the problem arises when I find another number, lets say 282, it should give me 0. But it gives me my return value of -999. Or when I do 272, same problem, return -999. It only works on some values. Anyways I'd appreciate any light that can be shed on this problem. Thank you.
python function if-statement recursion
I'm trying to find the index of a number in an array(list) but sometimes it falls into the if statement (if upper < lower) even though it's not a true statement.
def recurse(lower,upper,result,target):
if upper < lower:
print("Error, Upper is less then Lower")
return -999
midpoint = (lower + upper)//2
guess = result[midpoint]
if guess == target:
return midpoint
else:
if guess > target:
upper = midpoint -1
return recurse(lower,upper,result,target)
else:
lower = midpoint +1
return recurse(lower,upper,result,target)
result5 = recurse(0,30,result,215)
print("The index of that number is : ",result5)
I have formed a random generation array and have 30 numbers generated between 201 and 300
def randomList(array):
array =
for x in range(29):
r = random.randint(201,300)
array.append(r)
return array
result = randomList(result)
print("Random list of 30 numbers between 201-300",result)
I have pasted the list result [282, 215, 204, 295, 236, 232, 229, 218, 295, 214, 287, 295, 270, 212, 276, 255, 205, 204, 212, 228, 230, 265, 278, 204, 272, 226, 292, 284, 290]
My goal for the recuse function is to display the index of the number I send in the target parameter. Sometimes it actually works and will tell me the index number, but sometimes it doesn't. I'm pretty stumped. I'm sorry for formatting errors.
The result from sending the target number 215 is:
The index of that number is : 1
Which is completely acurate and works, but the problem arises when I find another number, lets say 282, it should give me 0. But it gives me my return value of -999. Or when I do 272, same problem, return -999. It only works on some values. Anyways I'd appreciate any light that can be shed on this problem. Thank you.
python function if-statement recursion
python function if-statement recursion
edited Nov 20 at 1:01
eozd
8941515
8941515
asked Nov 19 at 21:22
Dotexe
82
82
1
Think about what your code assumes. If your guess is greater than your target, then the code says that none of the values further to the right of your guess cannot be equal to the target. When is this true? Does this hold for your input array? What kind of input arrays can this function work on properly?
– eozd
Nov 19 at 21:38
Ok, I'm trying to understand what you're saying. Maybe if they were in ascending order ?
– Dotexe
Nov 19 at 21:45
Yes, you are in the right track. I haven't checked your code in detail, so there might be mistakes in it; but the function would only work if the input array is in ascending order in the first place. Otherwise, you don't have any guarantees for the argument that the element you are looking for cannot be in the subarray you discard.
– eozd
Nov 19 at 21:48
Okay, well I'll give it a shot to order them, but I was thinking since it's just checking the index number, what should the value of the index have anything to do with it other then making sure it matches the Target Parameter
– Dotexe
Nov 19 at 21:55
I am not exactly sure what you are saying; but I got the impression that you think this function actually checks every element of the input array, which is not the case. If your input array has N elements, this function only checks logN of them, and the other N - logN elements are not compared against your target.
– eozd
Nov 19 at 21:58
add a comment |
1
Think about what your code assumes. If your guess is greater than your target, then the code says that none of the values further to the right of your guess cannot be equal to the target. When is this true? Does this hold for your input array? What kind of input arrays can this function work on properly?
– eozd
Nov 19 at 21:38
Ok, I'm trying to understand what you're saying. Maybe if they were in ascending order ?
– Dotexe
Nov 19 at 21:45
Yes, you are in the right track. I haven't checked your code in detail, so there might be mistakes in it; but the function would only work if the input array is in ascending order in the first place. Otherwise, you don't have any guarantees for the argument that the element you are looking for cannot be in the subarray you discard.
– eozd
Nov 19 at 21:48
Okay, well I'll give it a shot to order them, but I was thinking since it's just checking the index number, what should the value of the index have anything to do with it other then making sure it matches the Target Parameter
– Dotexe
Nov 19 at 21:55
I am not exactly sure what you are saying; but I got the impression that you think this function actually checks every element of the input array, which is not the case. If your input array has N elements, this function only checks logN of them, and the other N - logN elements are not compared against your target.
– eozd
Nov 19 at 21:58
1
1
Think about what your code assumes. If your guess is greater than your target, then the code says that none of the values further to the right of your guess cannot be equal to the target. When is this true? Does this hold for your input array? What kind of input arrays can this function work on properly?
– eozd
Nov 19 at 21:38
Think about what your code assumes. If your guess is greater than your target, then the code says that none of the values further to the right of your guess cannot be equal to the target. When is this true? Does this hold for your input array? What kind of input arrays can this function work on properly?
– eozd
Nov 19 at 21:38
Ok, I'm trying to understand what you're saying. Maybe if they were in ascending order ?
– Dotexe
Nov 19 at 21:45
Ok, I'm trying to understand what you're saying. Maybe if they were in ascending order ?
– Dotexe
Nov 19 at 21:45
Yes, you are in the right track. I haven't checked your code in detail, so there might be mistakes in it; but the function would only work if the input array is in ascending order in the first place. Otherwise, you don't have any guarantees for the argument that the element you are looking for cannot be in the subarray you discard.
– eozd
Nov 19 at 21:48
Yes, you are in the right track. I haven't checked your code in detail, so there might be mistakes in it; but the function would only work if the input array is in ascending order in the first place. Otherwise, you don't have any guarantees for the argument that the element you are looking for cannot be in the subarray you discard.
– eozd
Nov 19 at 21:48
Okay, well I'll give it a shot to order them, but I was thinking since it's just checking the index number, what should the value of the index have anything to do with it other then making sure it matches the Target Parameter
– Dotexe
Nov 19 at 21:55
Okay, well I'll give it a shot to order them, but I was thinking since it's just checking the index number, what should the value of the index have anything to do with it other then making sure it matches the Target Parameter
– Dotexe
Nov 19 at 21:55
I am not exactly sure what you are saying; but I got the impression that you think this function actually checks every element of the input array, which is not the case. If your input array has N elements, this function only checks logN of them, and the other N - logN elements are not compared against your target.
– eozd
Nov 19 at 21:58
I am not exactly sure what you are saying; but I got the impression that you think this function actually checks every element of the input array, which is not the case. If your input array has N elements, this function only checks logN of them, and the other N - logN elements are not compared against your target.
– eozd
Nov 19 at 21:58
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53382828%2fpython-bin-sort-recusion-and-index-numbers%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53382828%2fpython-bin-sort-recusion-and-index-numbers%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Think about what your code assumes. If your guess is greater than your target, then the code says that none of the values further to the right of your guess cannot be equal to the target. When is this true? Does this hold for your input array? What kind of input arrays can this function work on properly?
– eozd
Nov 19 at 21:38
Ok, I'm trying to understand what you're saying. Maybe if they were in ascending order ?
– Dotexe
Nov 19 at 21:45
Yes, you are in the right track. I haven't checked your code in detail, so there might be mistakes in it; but the function would only work if the input array is in ascending order in the first place. Otherwise, you don't have any guarantees for the argument that the element you are looking for cannot be in the subarray you discard.
– eozd
Nov 19 at 21:48
Okay, well I'll give it a shot to order them, but I was thinking since it's just checking the index number, what should the value of the index have anything to do with it other then making sure it matches the Target Parameter
– Dotexe
Nov 19 at 21:55
I am not exactly sure what you are saying; but I got the impression that you think this function actually checks every element of the input array, which is not the case. If your input array has N elements, this function only checks logN of them, and the other N - logN elements are not compared against your target.
– eozd
Nov 19 at 21:58