numbers

Sum consecutive numbers in a list. Python

妖精的绣舞 提交于 2020-01-13 10:19:08
问题 i'm trying to sum consecutive numbers in a list while keeping the first one the same. so in this case 5 would stay 5, 10 would be 10 + 5 (15), and 15 would be 15 + 10 + 5 (30) x = [5,10,15] y = [] for value in x: y.append(...) print y [5,15,30] 回答1: y = [sum(x[:i+1]) for i in range(len(x))] 回答2: You want itertools.accumulate() (added in Python 3.2). Nothing extra needed, already implemented for you. In earlier versions of Python where this doesn't exist, you can use the pure python

How to represent a number in base 2^32?

陌路散爱 提交于 2020-01-13 09:09:44
问题 If I have some base 10 or base 16 number, how do I change it into base 2^32? The reason I'm trying to do this, is for implementing BigInt as suggested by other members here .. Why to use higher base for implementing BigInt? Will it be the same as integer(base 10) till 2^32? What will happen after it? 回答1: You are trying to find something of the form a0 + a1 * (2^32) + a2 * (2^32)^2 + a3 * (2^32)^3 + ... which is exactly the definition of a base-2 32 system, so ignore all the people that told

Is there a way in Objective-C to take a number and spell it out?

若如初见. 提交于 2020-01-12 21:56:30
问题 I'm looking for a way to take a number (say 5 or 207), spell it out, and returns its ordinal form as a string (five or two hundred seven). All I could find was code that takes a number and returns its ordinal suffix (st, nd, rd, th) and the parts of English grammar guides that say you should spell out ordinals. I'm looking for a way to get a spelled out number with its ordinal suffix (fifth or two hundred seventh). 回答1: Update : I came across this answer today, suggesting the use of the use

Regex pattern for capital letters and numbers only, with possible 'List'

只谈情不闲聊 提交于 2020-01-11 19:59:24
问题 What is the regex to match words that have the pattern: Number or Capital in any order * 3 (+possible 'List' on the end) For example, OP3 G6H ZZAList 349 127List are all valid, whereas a3G P-0List HYiList def YHr are all invalid. 回答1: You can use the regex: ^[A-Z0-9]{3}(?:List)?$ Explanation: ^ : Start anchor [A-Z0-9] : Char class to match any one of the uppercase letter or digit {3} : Quantifier for previous sub-regex (?:List) : A literal 'List' enclosed in non-capturing paranthesis ? : To

Add trailing zeros to an integer without converting to string in JS?

老子叫甜甜 提交于 2020-01-11 14:33:31
问题 I'm looking to add decimals to the end of my integer. As an example: 15 => 15.00 The problem with methods like toFixed is that it will convert it into a string. I've tried to use parseFloat() and Number() on the string, but it'll convert it back to an integer with no decimals. Is this possible? If not, can someone explain to me the logic behind why this isn't possible? EDIT: Welp the intent was to display the number as a number, but from the going consensus, it looks like the way the only way

How do I write tests correctly using unittest?

独自空忆成欢 提交于 2020-01-11 14:04:53
问题 I am trying to figure out how to write unit tests for functions that I have written in Python - here's the code written below: def num_buses(n): import math """ (int) -> int Precondition: n >= 0 Return the minimum number of buses required to transport n people. Each bus can hold 50 people. >>> num_buses(75) 2 """ bus = int() if(n>=0): bus = int(math.ceil(n/50.0)) return bus I am attempting to write test code but they are giving me fail results - here's code I started with: import a1 import

迭代器方法

感情迁移 提交于 2020-01-11 12:44:31
不生成新数组的迭代器方法 这些方法不产生任何新数组,相反,他们要么对于数组中的每个元素执行某种操作,要么返回一个值。 forEach()方法 该方法接受一个函数作为参数,对数组中的每个元素使用该函数。 function square ( num ) { console . log ( num , num * num ) ; } var nums = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] ; nums . forEach ( square ) ; 该程序的输出为: 1 1 2 4 3 9 4 16 5 25 6 36 7 49 every()方法 该方法接受一个返回值为布尔类型的函数,对数组中的每个元素使用该函数。如果对于所有的元素,该函数均返回true,则该方法返回true。 function isEven ( num ) { return num % 2 == 0 ; } var nums = [ 2 , 4 , 6 , 8 , 10 ] ; var even = nums . every ( isEven ) ; if ( even ) { console . log ( "all numbers are even" ) ; } else { console . log ( "not all numbers are even" ) ; } 输出为: all

Formatting numbers, excluding trailing zeroes

风流意气都作罢 提交于 2020-01-11 07:52:06
问题 first time SO user :) I know that I can format a number like this: format-number($value, '###,###.00') But I would like to remove the dot and the zeroes if $value is zero. So, 37368 -> 37,368 2.666667 -> 2.66 Is this possible using just number formatting (doesn't seem like it) or do I have to do something along the lines of if (int(value) == value ) {...} 回答1: format-number($value, '###,###.##') 回答2: xpath 2.0 contains conditional logic if..then..else but in the more likely case that you're

How to read numbers in python from csv file?

六眼飞鱼酱① 提交于 2020-01-11 02:26:07
问题 I have a csv file and I have to compute the mean for some of the columns. That's how I did: file=csv.reader(open('tab.csv','r')) n=[] for row in file: n.append(row[8]) So I have a list of string : n=['','','1.58'...] How can I convert these to float? I tried with : n_values=np.array(n) n_values[n=='']='0' values=n_values.astype(np.float) np.mean(values) But the mean is not correct because I should skip the empty strings not counting. Thank for your help! 回答1: Just cast as you append: n.append

How to check if a int var contains a specific number

情到浓时终转凉″ 提交于 2020-01-10 03:57:07
问题 How to check if a int var contains a specific number I cant find a solution for this. For example: i need to check if the int 457 contains the number 5 somewhere. Thanks for your help ;) 回答1: 457 % 10 = 7 * 457 / 10 = 45 45 % 10 = 5 * 45 / 10 = 4 4 % 10 = 4 * 4 / 10 = 0 done Get it? Here's a C implementation of the algorithm that my answer implies. It will find any digit in any integer. It is essentially the exact same as Shakti Singh's answer except that it works for negative integers and