integer

How to sum numbers from input?

百般思念 提交于 2020-02-26 04:16:45
问题 I am working on the following problem. Write a program that continually prompts for positive integers and stops when the sum of numbers entered exceeds 1000. But my code stop early if a negative integer is entered. The numbers will not sum. My code: x = int(input("Enter an integer:")) total = 0 sum = 0 while (0 <= x): if sum <= 1000: x += 1 sum += (int(input("Enter an integer:"))) elif sum >= 1000: break 回答1: x = 0 total = 0 sum = 0 while sum <= 1000: x = int(input("Enter an integer:")) if x

Regex for a number 1-31 Ruby

感情迁移 提交于 2020-02-23 05:33:13
问题 I am trying to match any number 1-31 (inclusively). This is the closest I have: ([1-9]|[12]\d|3[01]) But numbers like 324 are accepted. Any chance there's a regex out there that can capture just 1-31? 回答1: The following regex satisfies your condition: ^([1-9]|[12][0-9]|3[01])$ Demo here 回答2: Use a Numeric Comparison Instead Depending on what you are really trying to do, or to communicate with your code, it may make more sense to simply extract all integers and reject those outside your

Sort by Integer vs. by DateTime

萝らか妹 提交于 2020-02-16 09:36:47
问题 I'm speaking from the .NET point of view but this could extend to other languages or frameworks that use similar logic. Is it correct to assume that when sorting objects by a DateTime property, the DateTime value is converted to Ticks (i.e., long integers) for comparison purposes? And as a result, the speed of sorting by DateTime is not much, if any, slower than sorting by integers? 回答1: Yes, it compares ticks. Here is actual implementation: public int CompareTo(DateTime value) { long

C++ Fix for checking if input is an integer [duplicate]

倖福魔咒の 提交于 2020-02-15 08:02:12
问题 This question already has answers here : How to read from input file (text file) and validate input as valid integer? (2 answers) Closed 6 years ago . for example, if I enter "2a", it does not show an error nor asks the user to re-input the value. how do i fix this? while (std::cin.fail()) { std::cout << "ERROR, enter a number" << std::endl; std::cin.clear(); std::cin.ignore(256,'\n'); std::cin >> dblMarkOne; } std::cout << "" << std::endl; 回答1: Two possible solutions: First store the input

How do I get the absolute value of an integer without using Math.abs?

强颜欢笑 提交于 2020-02-02 12:17:12
问题 How do I get the absolute value of a number without using math.abs? This is what I have so far: function absVal(integer) { var abs = integer * integer; return abs^2; } 回答1: You can use the conditional operator and the unary negation operator: function absVal(integer) { return integer < 0 ? -integer : integer; } 回答2: You can also use >> (Sign-propagating right shift) function absVal(integer) { return (integer ^ (integer >> 31)) - (integer >> 31);; } Note: this will work only with integer 回答3:

How javascript treat large integers (more than 52 bits)?

只愿长相守 提交于 2020-02-01 09:39:42
问题 Consider this code (node v5.0.0) const a = Math.pow(2, 53) const b = Math.pow(2, 53) + 1 const c = Math.pow(2, 53) + 2 console.log(a === b) // true console.log(a === c) // false Why a === b is true? What is the maximum integer value javascript can handle? I'm implementing random integer generator up to 2^64. Is there any pitfall I should be aware of? 回答1: .:: JavaScript only supports 53 bit integers ::. All numbers in JavaScript are floating point which means that integers are always

How javascript treat large integers (more than 52 bits)?

北城以北 提交于 2020-02-01 09:38:08
问题 Consider this code (node v5.0.0) const a = Math.pow(2, 53) const b = Math.pow(2, 53) + 1 const c = Math.pow(2, 53) + 2 console.log(a === b) // true console.log(a === c) // false Why a === b is true? What is the maximum integer value javascript can handle? I'm implementing random integer generator up to 2^64. Is there any pitfall I should be aware of? 回答1: .:: JavaScript only supports 53 bit integers ::. All numbers in JavaScript are floating point which means that integers are always

Python how to check if input is a letter or character

ε祈祈猫儿з 提交于 2020-01-30 12:02:39
问题 How can I check if input is a letter or character in Python? Input should be amount of numbers user wants to check. Then program should check if input given by user belongs to tribonacci sequence (0,1,2 are given in task) and in case user enter something different than integer, program should continue to run. n = int(input("How many numbers do you want to check:")) x = 0 def tribonnaci(n): sequence = (0, 1, 2, 3) a, b, c, d = sequence while n > d: d = a + b + c a = b b = c c = d return d

Java - How to split a string with numbers separated by spaces into variables?

拟墨画扇 提交于 2020-01-30 04:04:36
问题 I am given the string "23 45" I need to get 2 variables (int a) with the value 23 and (int b) with value 45 回答1: if your input String name is ' in ' , then we will have : String in = "23 45"; String s[] = in.split(" "); int out[] = new int[s.length]; for(int i = 0 ; i < s.length ; i++) out[i] = Integer.parseInt(s[i]); output is now in out array 来源: https://stackoverflow.com/questions/39882367/java-how-to-split-a-string-with-numbers-separated-by-spaces-into-variables

PHP round to integer

天大地大妈咪最大 提交于 2020-01-29 10:19:32
问题 I want to round a number and I need a proper integer because I want to use it as an array key. The first "solution" that comes to mind is: $key = (int)round($number) However, I am unsure if this will always work. As far as I know (int) just truncates any decimals and since round($number) returns a float with theoretically limited precision, is it possible that round($number) returns something like 7.999999... and then $key is 7 instead of 8? If this problem actually exists (I don't know how