Why do these two loops return different results?
up vote
1
down vote
favorite
I have a loop for calculating the total hamming distance between two strings.
For the input:
nums = [4,14,2]
One version of the loop:
n, ans = len(nums), 0
for i in range(32):
bit, cnt = 1<<i, 0
for num in nums:
cnt += num & bit
ans += cnt * (n-cnt)
return ans
Gives the wrong result: -84
While an almost identical loop:
n, ans = len(nums), 0
for i in range(32):
bit, cnt = 1<<i, 0
for num in nums:
cnt += (num & bit) > 0
ans += cnt * (n-cnt)
return ans
Gives the correct answer 6. I can figure out why?
What does the '> 0' in the second loop do? I tried to understand it's effect using a simple test:
>>> i = -5
>>> i += 1
>>> i
-4
>>> i = -5
>>> i += 1 > 0
>>> i
-4
and the '> 0' doesn't seem to do anything. Why are the two loops different?
python
add a comment |
up vote
1
down vote
favorite
I have a loop for calculating the total hamming distance between two strings.
For the input:
nums = [4,14,2]
One version of the loop:
n, ans = len(nums), 0
for i in range(32):
bit, cnt = 1<<i, 0
for num in nums:
cnt += num & bit
ans += cnt * (n-cnt)
return ans
Gives the wrong result: -84
While an almost identical loop:
n, ans = len(nums), 0
for i in range(32):
bit, cnt = 1<<i, 0
for num in nums:
cnt += (num & bit) > 0
ans += cnt * (n-cnt)
return ans
Gives the correct answer 6. I can figure out why?
What does the '> 0' in the second loop do? I tried to understand it's effect using a simple test:
>>> i = -5
>>> i += 1
>>> i
-4
>>> i = -5
>>> i += 1 > 0
>>> i
-4
and the '> 0' doesn't seem to do anything. Why are the two loops different?
python
4
>is greater than. In regards to your simple tests-5 += 1 > 0is the same as-5 += Truewhich evaluates to-5 += 1, the same as your first code block.
– Loocid
Nov 12 at 5:53
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a loop for calculating the total hamming distance between two strings.
For the input:
nums = [4,14,2]
One version of the loop:
n, ans = len(nums), 0
for i in range(32):
bit, cnt = 1<<i, 0
for num in nums:
cnt += num & bit
ans += cnt * (n-cnt)
return ans
Gives the wrong result: -84
While an almost identical loop:
n, ans = len(nums), 0
for i in range(32):
bit, cnt = 1<<i, 0
for num in nums:
cnt += (num & bit) > 0
ans += cnt * (n-cnt)
return ans
Gives the correct answer 6. I can figure out why?
What does the '> 0' in the second loop do? I tried to understand it's effect using a simple test:
>>> i = -5
>>> i += 1
>>> i
-4
>>> i = -5
>>> i += 1 > 0
>>> i
-4
and the '> 0' doesn't seem to do anything. Why are the two loops different?
python
I have a loop for calculating the total hamming distance between two strings.
For the input:
nums = [4,14,2]
One version of the loop:
n, ans = len(nums), 0
for i in range(32):
bit, cnt = 1<<i, 0
for num in nums:
cnt += num & bit
ans += cnt * (n-cnt)
return ans
Gives the wrong result: -84
While an almost identical loop:
n, ans = len(nums), 0
for i in range(32):
bit, cnt = 1<<i, 0
for num in nums:
cnt += (num & bit) > 0
ans += cnt * (n-cnt)
return ans
Gives the correct answer 6. I can figure out why?
What does the '> 0' in the second loop do? I tried to understand it's effect using a simple test:
>>> i = -5
>>> i += 1
>>> i
-4
>>> i = -5
>>> i += 1 > 0
>>> i
-4
and the '> 0' doesn't seem to do anything. Why are the two loops different?
python
python
asked Nov 12 at 5:50
Alex Kinman
64321227
64321227
4
>is greater than. In regards to your simple tests-5 += 1 > 0is the same as-5 += Truewhich evaluates to-5 += 1, the same as your first code block.
– Loocid
Nov 12 at 5:53
add a comment |
4
>is greater than. In regards to your simple tests-5 += 1 > 0is the same as-5 += Truewhich evaluates to-5 += 1, the same as your first code block.
– Loocid
Nov 12 at 5:53
4
4
> is greater than. In regards to your simple tests -5 += 1 > 0 is the same as -5 += True which evaluates to -5 += 1, the same as your first code block.– Loocid
Nov 12 at 5:53
> is greater than. In regards to your simple tests -5 += 1 > 0 is the same as -5 += True which evaluates to -5 += 1, the same as your first code block.– Loocid
Nov 12 at 5:53
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
accepted
num & bit is not the same as num & bit > 0;
num & bit is a number (int), while num & bit > 0 is a boolean (bool);
in python, a True boolean is 1 when used as a number;
>>> i = -5
>>> i += 2
>>> i
-3
>>> i = -5
>>> i += 2 > 0
>>> i
-4
as a rule of thumb, dont test with 1 or 0; test with a random number (2 is also bad but easy to read here);
add a comment |
up vote
1
down vote
It's because some of the num&bit are bigger than 0, so (i.e there was one 8 in my run):
>>> a=8
>>> a>0
True
>>> int(a>0)
1
It's not 8!!!
So that's why.
add a comment |
up vote
0
down vote
In line
cnt += num & bit
you are incrementing the value of cnt by 1,2,4,8 values.
while in line
cnt += (num & bit) > 0
first you are validating the statement, which returns 0 or 1 then you are increamenting value by either 0 or 1.
You can cross check it by printing cnt value in both the codes.
add a comment |
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%2f53256521%2fwhy-do-these-two-loops-return-different-results%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
num & bit is not the same as num & bit > 0;
num & bit is a number (int), while num & bit > 0 is a boolean (bool);
in python, a True boolean is 1 when used as a number;
>>> i = -5
>>> i += 2
>>> i
-3
>>> i = -5
>>> i += 2 > 0
>>> i
-4
as a rule of thumb, dont test with 1 or 0; test with a random number (2 is also bad but easy to read here);
add a comment |
up vote
1
down vote
accepted
num & bit is not the same as num & bit > 0;
num & bit is a number (int), while num & bit > 0 is a boolean (bool);
in python, a True boolean is 1 when used as a number;
>>> i = -5
>>> i += 2
>>> i
-3
>>> i = -5
>>> i += 2 > 0
>>> i
-4
as a rule of thumb, dont test with 1 or 0; test with a random number (2 is also bad but easy to read here);
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
num & bit is not the same as num & bit > 0;
num & bit is a number (int), while num & bit > 0 is a boolean (bool);
in python, a True boolean is 1 when used as a number;
>>> i = -5
>>> i += 2
>>> i
-3
>>> i = -5
>>> i += 2 > 0
>>> i
-4
as a rule of thumb, dont test with 1 or 0; test with a random number (2 is also bad but easy to read here);
num & bit is not the same as num & bit > 0;
num & bit is a number (int), while num & bit > 0 is a boolean (bool);
in python, a True boolean is 1 when used as a number;
>>> i = -5
>>> i += 2
>>> i
-3
>>> i = -5
>>> i += 2 > 0
>>> i
-4
as a rule of thumb, dont test with 1 or 0; test with a random number (2 is also bad but easy to read here);
edited Nov 12 at 6:18
answered Nov 12 at 5:57
Cyker
2,63553245
2,63553245
add a comment |
add a comment |
up vote
1
down vote
It's because some of the num&bit are bigger than 0, so (i.e there was one 8 in my run):
>>> a=8
>>> a>0
True
>>> int(a>0)
1
It's not 8!!!
So that's why.
add a comment |
up vote
1
down vote
It's because some of the num&bit are bigger than 0, so (i.e there was one 8 in my run):
>>> a=8
>>> a>0
True
>>> int(a>0)
1
It's not 8!!!
So that's why.
add a comment |
up vote
1
down vote
up vote
1
down vote
It's because some of the num&bit are bigger than 0, so (i.e there was one 8 in my run):
>>> a=8
>>> a>0
True
>>> int(a>0)
1
It's not 8!!!
So that's why.
It's because some of the num&bit are bigger than 0, so (i.e there was one 8 in my run):
>>> a=8
>>> a>0
True
>>> int(a>0)
1
It's not 8!!!
So that's why.
answered Nov 12 at 5:55
U9-Forward
11.5k2834
11.5k2834
add a comment |
add a comment |
up vote
0
down vote
In line
cnt += num & bit
you are incrementing the value of cnt by 1,2,4,8 values.
while in line
cnt += (num & bit) > 0
first you are validating the statement, which returns 0 or 1 then you are increamenting value by either 0 or 1.
You can cross check it by printing cnt value in both the codes.
add a comment |
up vote
0
down vote
In line
cnt += num & bit
you are incrementing the value of cnt by 1,2,4,8 values.
while in line
cnt += (num & bit) > 0
first you are validating the statement, which returns 0 or 1 then you are increamenting value by either 0 or 1.
You can cross check it by printing cnt value in both the codes.
add a comment |
up vote
0
down vote
up vote
0
down vote
In line
cnt += num & bit
you are incrementing the value of cnt by 1,2,4,8 values.
while in line
cnt += (num & bit) > 0
first you are validating the statement, which returns 0 or 1 then you are increamenting value by either 0 or 1.
You can cross check it by printing cnt value in both the codes.
In line
cnt += num & bit
you are incrementing the value of cnt by 1,2,4,8 values.
while in line
cnt += (num & bit) > 0
first you are validating the statement, which returns 0 or 1 then you are increamenting value by either 0 or 1.
You can cross check it by printing cnt value in both the codes.
answered Nov 12 at 6:16
Ankush Khobragade
134
134
add a comment |
add a comment |
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%2f53256521%2fwhy-do-these-two-loops-return-different-results%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
4
>is greater than. In regards to your simple tests-5 += 1 > 0is the same as-5 += Truewhich evaluates to-5 += 1, the same as your first code block.– Loocid
Nov 12 at 5:53