Bitwise And in R [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have the below function in Python which I need to convert into R
def hamming(h1, h2):
h, d = 0, h1 ^ h2
while d:
h += 1
d &= d - 1
return h
But I don't know how to handle the bitwise piece.
UPDATE:
I had to update the question since I did a mistake of not sharing what I had done.
I know about the BitWise operator but I was not getting the same answer.
I should have included my code which would have not created all these confusion.
My apologies for not been precise with the question.
I had written the function as below:
hamming <- function(h1, h2) {
h <- 0
d <- h1^h2
while (d) {
h <- h + 1
d = bitwAnd(d, d-1)
}
return(h)
}
But I seem to get different results from both the function. Don't know which line is wrong.
UPDATE: I took ^ in Python to be same in R and I was wrong. I figured out the issue.
Thanks everyone

There is a set of bitwise functions in base R. See ?bitwAnd for the one you are looking for and others available.

Related

Finding all possible combinations of sum product to reach a given target [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
There has been a solution to find the possible combination of numbers to reach a given target number. However, I have a different situation below, where a,b, and c are product types and I like to find the combination of sum products of a,b and c to reach the target total.
a = 50sqft
b = 70sqft
c = 100sqft
Total = 5000sqft
I like to find all possible combinations of numbers (integer solution) of a,b,c to get to 5000, and how can I create a python function for that?
Results :
(100a,0b,0c)=5000
(23a,5b,8c)=5000
...
...
Thanks in advance!!
I got a solution :
a=50
b=70
c=100
for i in range(101): # This si 101 here to give 100a=5000
for j in range(100):
for k in range(100):
if i*a + j*b + k*c == 5000:
print('({}a,{}b,{}c)=5000'.format(i,j,k))

python coding from matlab [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have this code in MATLAB and I am trying to convert it in Python.
A=[1,-0.75,0.25]
yc(1:45)=-2;
y(1:6)=0
u(1:6)=0
[lig,col]=size(A);
alpha(1)=1;
alpha(2)=A(2)-1;
if(col>2)
for i=3:col
alpha(i)=A(i)-A(i-1);
end ;
end;
alpha(col+1)=-A(col);
I don't know how to convert it in python thnx for helping me
It would be better if your code would have been a minimal example of what you are trying to do. You are defining variables that are not even used. But here's a more or less literal translation. Note that you probably want to preallocate alpha (both in Matlab and Python)
import numpy as np
A = np.array([1.0, -.75, .25])
yc = -2 * np.ones(45)
y = np.zeros(6)
u = np.zeros(6)
col = A.size
alpha = np.array([1, A[1] - 1])
if col > 2:
for i in range(2, col):
alpha = np.append(alpha, A[i] - A[i-1])
alpha = np.append(alpha, -A[col-1])

How to code a parameter with 3 values for three conditions [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a parameter that is called f.. I don't know how to code it, should I make it as a list or it needs an if loop
f = 1 if the indicator i exists in list 1
f = -1 if the indicator i exists in list 2
f = O otherwise
I will use the parameter f in a constraint with the right side equals to f
Any help?
Should any further clarification is required I am ready.
f
constraint
You can do this in one line in python:
f = 1 if i in list1 else -1 if i in list2 else 0

python math wrong answer (not decimals) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Why in gods name is it 57.5
compiler output
input return strings, thus 2 + 3 is 23 (the default behaviour for + with strings is to concatenate them), then the casting to int turns this into an actual 23, so finally you get 5 * 23 / 2 == 115 / 2 == 57.5.
To solve this, cast each parameter to int before doing the math operations:
int(h) * (int(a)+int(b)) / 2
You concatenate two string, then turn the result into an int. If you do int(a) + int(b) it will work properly.

Why doesn't this python code run infinite times? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
a = 3
for i in range(a):
print(a, end = ' ')
a +=1
It just produces the output as:
3 4 5
I don't understand this because since a is being incremented each time, the loop should run forever.Or is it that the iterable range is generated only once?
You are confusing c/c++/c# for syntax with python.
In c/c++/c# you have a conditional inside the for syntax:
for (var i= 0; i<100;i++) # this is checked each time you come back here, incrementing
# the i will skip some runs and change how often the for is done
Pythons for is more a foreach:
for i in range(3):
==>
foreach(var k in new []{0,1,2}) # it takes a fresh "k" out every time it comes here
{ ... }
if you modify k it will still only run 3 times.
It has to do with how the program source code is interpreted. range(a) will be executed before the body of the loop, producing an iterable object which yields 3, 4, 5. a will be modified later but it will not affect range(a) cause it has already been executed. The following will do what you want, but it's kind of a silly program now:
a = 3
i = a
while i < a:
print(a, end = ' ')
a += 1
i += 1

Categories