integer

Python : reducing memory usage of small integers with missing values

*爱你&永不变心* 提交于 2021-01-29 09:32:20
问题 I am in the process of reducing the memory usage of my code. The goal of this code is handling some big dataset. Those are stored in Pandas dataframe if that is relevant. Among many other data there are some small integers. As they contain some missing values (NA) Python has them set to the float64 type by default. I was trying to downcast them to some smaller int format (int8 or int16 for exemple), but I got an error because of the NA. It seems that there are some new integer type (Int64)

Unsigned Multiplication using Signed Multiplier

偶尔善良 提交于 2021-01-29 07:18:08
问题 I have been assigned with a task to perform unsigned multiplication using signed multiplier. Despite multiple attempts, I couldn't get it. Is it possible to do this? Code: #include <stdio.h> int main() { // Must be short int short int a=0x7fff; short int b=0xc000; unsigned int res1; signed int res2; //unsigned multiplier res1= (unsigned short int) a * (unsigned short int) b; //signed multiplier res2= (short int) a * (short int) b; printf("res1: 0x%x %d \n res2: 0x%x %d\n",res1,res1,res2,res2)

How to store random integers from the set of 32-bit integers in a compact data structure to rapidly check membership?

陌路散爱 提交于 2021-01-29 05:22:05
问题 I am thinking about how to organize/allocate memory and that led me to this question, which I have distilled to its essence, so it may seem out of the blue, but the memory question is too complicated and confusing and distracting, so I am not asking it here. I tried asking it earlier and got no response. So here it goes. Say you have a system where you want to check if you have some integer exists in it as fast as possible, and you want to be able to add and remove integers as fast as

“'int' object is not iterable” in while

本秂侑毒 提交于 2021-01-28 20:24:51
问题 def rect_extend(x): m, n = 1 while 1 < x: m = m + 1 n = n + 1 return m, n This simple function returns: 'int' object is not iterable error in iPython. I don't know why it does this, while function doesn't work - condition seems to be true . ( while 's condition was simplified on purpose; original code doesn't have it) 回答1: I think you want m = 1 n = 1 or m = n = 1 instead of m, n = 1 . This (sequence unpacking)[http://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences]: x, y

How to keep looping with try and catch to get correct input?

依然范特西╮ 提交于 2021-01-28 16:42:20
问题 I'm trying to make a function that checks for an integer and will keep looping until the user correctly enters an integer of 17 or higher. However, if I put in wrong input, like 'K', or '&', it will get stuck in an infinite loop. public static int getAge(Scanner scanner) { int age; boolean repeat = true; while (repeat) { try { System.out.println("Enter the soldier's age: "); age = scanner.nextInt(); repeat = false; } catch(InputMismatchException exception) { System.out.println("ERROR: You

in R, how to set and retain custom levels in factor with different labels?

邮差的信 提交于 2021-01-28 08:30:54
问题 in R, how to set and retain custom levels in factor with different labels ? That is, I want to set custom numbers in the levels of a factor, and these numerical values - integers to be retained and not converted to "1, 2, 3 etc.". I know that one solution is to set these weights as Labels, but then I will missing the "labels" of the factor. The "weighted" distance between factors is not retained. Is it possible in R, to achieve something like this, using a single variable ? For example: age_f

Signed and unsigned integers?

妖精的绣舞 提交于 2021-01-28 07:58:56
问题 Could someone please explain the two to me because I have to give an explanation of them both in my assignment. I know what a normal integer is of course and have used the following to describe it: "An integer is a whole number which can be positive, negative and zero but it cannot have decimal points." But I'm just not sure about signed and unsigned. Thanks 回答1: In most languages when you declare an integer, you are declaring a signed integer. If you want to declare an unsigned integer you

How to convert a certain string[index] to int in C#?

。_饼干妹妹 提交于 2021-01-28 07:10:12
问题 I have this code: string number = "124235245423523423", number2 = "3423525232332325423"; for(i=number.length-1;i>=0;i--){ int save = Convert.ToInt32(number[i]) + Convert.ToInt32(number2[i]); } That is not the complete code but my question is why can't I convert and access some value at a certain index of a string as an integer? Isn't there a straight forward approach to this? I have tried some things but it didn't work out. 回答1: You're looking for int.Parse . int save = int.Parse(number[3]

piecewise numpy function with integer arguments

我们两清 提交于 2021-01-27 12:14:32
问题 I define the piecewise function def Li(x): return piecewise(x, [x < 0, x >= 0], [lambda t: sin(t), lambda t: cos(t)]) And when I evaluate Li(1.0) The answer is correct Li(1.0)=array(0.5403023058681398) , But if I write Li(1) the answer is array(0) . I don't understand this behaviour. 回答1: It seems that piecewise() converts the return values to the same type as the input so, when an integer is input an integer conversion is performed on the result, which is then returned. Because sine and

How to format a flat string with integers in it in erlang?

孤街醉人 提交于 2021-01-26 23:00:32
问题 In erlang, I want to format a string with integers in it and I want the result to be flattened. But I get this: io_lib:format("sdfsdf ~B", [12312]). [115,100,102,115,100,102,32,"12312"] I can get the desired result by using the code below but it is really not elegant. lists:flatten(io_lib:format("sdfsdf ~B", [12312])). "sdfsdf 12312" Is there a better formatting strings with integers in them, so that they are flat? Ideally, using only one function? 回答1: You flatten a list using lists:flatten