integer

Python - Check if a number is a square

孤街浪徒 提交于 2021-02-08 04:36:38
问题 I wrote a function that returns whether a number input is a square or not def is_square(n): if n<1: return False else: for i in range(int(n/2)+1): if (i*i)==n: return True else: return False I am confident that this code works. But when I did the test cases, example: test.expect( is_square( 4)) , it says that the value is not what was expected. 回答1: Your function doesn't actually work, as it will immediatley return False on the first non-square-root found. Instead you will want to modify your

Inserting commas into integers

心不动则不痛 提交于 2021-02-07 14:36:40
问题 In Android is there an easy way to insert commas into a numberical value? ie if I have an EditText with 12345 in it, how can I display this as 12,345? I think I can do it using substring and chopping it up into x number of 3 number chunks then concatenating the answer with ',' between each 3 number chunk. This would be pretty easy if length of number was constant, but as the number could be from one digit to, say 20, it makes it more complex. Just curious if there is a simple and clean way to

Inserting commas into integers

六眼飞鱼酱① 提交于 2021-02-07 14:31:16
问题 In Android is there an easy way to insert commas into a numberical value? ie if I have an EditText with 12345 in it, how can I display this as 12,345? I think I can do it using substring and chopping it up into x number of 3 number chunks then concatenating the answer with ',' between each 3 number chunk. This would be pretty easy if length of number was constant, but as the number could be from one digit to, say 20, it makes it more complex. Just curious if there is a simple and clean way to

Java map an array of Strings to an array of Integers

牧云@^-^@ 提交于 2021-02-07 12:15:24
问题 I found this code on SO to map strings to ints Arrays.stream(myarray).mapToInt(Integer::parseInt).toArray(); But how do I make it map to Integer type not the primitive int? I tried switching from Integer.parseInt to Integer.valueOf , but it seems that the mapToInt() method forces the primitive type. I have an ArrayList of arrays of Integers, so I cannot use primitive ints. 回答1: Since String and Integer are both reference types you can simply call Stream::map to transform your array. Integer[]

Java map an array of Strings to an array of Integers

↘锁芯ラ 提交于 2021-02-07 12:14:33
问题 I found this code on SO to map strings to ints Arrays.stream(myarray).mapToInt(Integer::parseInt).toArray(); But how do I make it map to Integer type not the primitive int? I tried switching from Integer.parseInt to Integer.valueOf , but it seems that the mapToInt() method forces the primitive type. I have an ArrayList of arrays of Integers, so I cannot use primitive ints. 回答1: Since String and Integer are both reference types you can simply call Stream::map to transform your array. Integer[]

Read two bytes into an integer?

我只是一个虾纸丫 提交于 2021-02-05 20:20:21
问题 I have a byte[] that I've read from a file, and I want to get an int from two bytes in it. Here's an example: byte[] bytes = new byte[] {(byte)0x00, (byte)0x2F, (byte)0x01, (byte)0x10, (byte)0x6F}; int value = bytes.getInt(2,4); //This method doesn't exist This should make value equal to 0x0110 , or 272 in decimal. But obviously, byte[].getInt() doesn't exist. How can I accomplish this task? The above array is just an example. Actual values are unknown to me. 回答1: You should just opt for the

Read two bytes into an integer?

人走茶凉 提交于 2021-02-05 20:18:11
问题 I have a byte[] that I've read from a file, and I want to get an int from two bytes in it. Here's an example: byte[] bytes = new byte[] {(byte)0x00, (byte)0x2F, (byte)0x01, (byte)0x10, (byte)0x6F}; int value = bytes.getInt(2,4); //This method doesn't exist This should make value equal to 0x0110 , or 272 in decimal. But obviously, byte[].getInt() doesn't exist. How can I accomplish this task? The above array is just an example. Actual values are unknown to me. 回答1: You should just opt for the

Algorithm or formula that can take an incrementing counter and make it appear uniquely random

百般思念 提交于 2021-02-05 11:16:09
问题 I am wondering if there is a general formula of some sort that can take a single incrementing integer, and run it through a modulus sort of thing to shift it to a random place, so as you increment the counter, its output value jumps around and appears random, yet no value is ever hit twice. Assuming some limit on the set of numbers like 16-bit integers (65536 integers), or 32-bit integers, etc.. Perhaps there is a way to spiral numbers down somehow, I don't know. The sequence would be

Convert a column of dates from ordinal numbers to the standard date format - pandas

假装没事ソ 提交于 2021-02-05 09:29:17
问题 I have to convert a column of dates from the integer/date format to the date format d-m-Y. Example: import pandas as pd col1 = [737346, 737346, 737346, 737346, 737059, 737346] col2 = ['cod1', 'cod2', 'cod3', 'cod4', 'cod1', 'cod2'] dict = {'V1' : col1, 'V2' : col2} df = pd.DataFrame.from_dict(dict) df V1 V2 0 737346 cod1 1 737346 cod2 2 737346 cod3 3 737346 cod4 4 737059 cod1 5 737346 cod2 expected: df V1 V2 0 14-10-2019 cod1 1 14-10-2019 cod2 2 14-10-2019 cod3 3 14-10-2019 cod4 4 31-12-2018

Can the input() function in Python dynamically detect the input's data type?

爷,独闯天下 提交于 2021-02-05 06:39:24
问题 So I was making a console application using Python 3.7; that heavily depends on input (wowz). The application's function is to "swap" between two integer variables' values. And that is not where the problem is at, the problem is when I try to validate the user's input by checking the data-type for the input using a couple "if statements", and no matter what the user inputs using the " input() " function; the input's data-type will always be defined as "" I just want this little piece of ART