鉴于以下总统名单可以在最小的计划中进行前十个字数:
输入文件
Washington
Washington
Adams
Jefferson
Jefferson
Madison
Madison
Monroe
Monroe
John Quincy Adams
Jackson
Jackson
Van Buren
Harrison
DIES
Tyler
Polk
Taylor
DIES
Fillmore
Pierce
Buchanan
Lincoln
Lincoln
DIES
Johnson
Grant
Grant
Hayes
Garfield
DIES
Arthur
Cleveland
Harrison
Cleveland
McKinley
McKinley
DIES
Teddy Roosevelt
Teddy Roosevelt
Taft
Wilson
Wilson
Harding
Coolidge
Hoover
FDR
FDR
FDR
FDR
Dies
Truman
Truman
Eisenhower
Eisenhower
Kennedy
DIES
Johnson
Johnson
Nixon
Nixon
ABDICATES
Ford
Carter
Reagan
Reagan
Bush
Clinton
Clinton
Bush
Bush
Obama
以 …
我见过这样的问题:
count the number of 0s between 0 and N?count the number of 1s between 0 and N?count the number of 2s between 0 and N?这些类型的问题与要求查找Ks (i.e. K=0,1,2,...,9)数字范围中显示的总数非常相似[0, N].
例:
K=2, N=3514 2s之间的列表[0,35]:2, 12, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 32,注意22将被计为两次(22包含两个2s)每种方案都有解决方案(如果您搜索它,则可用).通常,O(log N)需要时间来通过递归地考虑最高位来解决这些问题,等等.计算0到N之间2的数量的一个例子可以通过以下过程解决(从这里借用):
// Take n = …Run Code Online (Sandbox Code Playgroud) 我正在计算PHP中的数字位数.我只想计算数据库的数字值.在第一个数字有零意味着,它不会作为计数的数字.
例如:
12 ==number of count value is 2
122 ==number of count value is 3
Run Code Online (Sandbox Code Playgroud)
我可以通过function.here我的函数实现这一点.
function count_digit($number)
{
return strlen((string) $number);
}
$number = 12312;
echo count_digit($number); // 5
Run Code Online (Sandbox Code Playgroud)
但我需要为该数字添加零$num = 0012312;(零填充).
012 == number of count value is 3
0133 == number of count value is 4
Run Code Online (Sandbox Code Playgroud)
让我知道如何解决它.
我找到以下代码来计算1-bits给定整数的二进制表示的数量.谁能解释它是如何工作的?如何为这样的任务选择位掩码?谢谢.
int count_one(int x)
{
x = (x & (0x55555555)) + ((x >> 1) & (0x55555555));
x = (x & (0x33333333)) + ((x >> 2) & (0x33333333));
x = (x & (0x0f0f0f0f)) + ((x >> 4) & (0x0f0f0f0f));
x = (x & (0x00ff00ff)) + ((x >> 8) & (0x00ff00ff));
x = (x & (0x0000ffff)) + ((x >> 16) & (0x0000ffff));
return x;
}
Run Code Online (Sandbox Code Playgroud) 我想每计数country次数的数量statusIS open和的次数status为closed.然后计算closerate每个country.
数据:
customer <- c(1,2,3,4,5,6,7,8,9)
country <- c('BE', 'NL', 'NL','NL','BE','NL','BE','BE','NL')
closeday <- c('2017-08-23', '2017-08-05', '2017-08-22', '2017-08-26',
'2017-08-25', '2017-08-13', '2017-08-30', '2017-08-05', '2017-08-23')
closeday <- as.Date(closeday)
df <- data.frame(customer,country,closeday)
Run Code Online (Sandbox Code Playgroud)
添加status:
df$status <- ifelse(df$closeday < '2017-08-20', 'open', 'closed')
customer country closeday status
1 1 BE 2017-08-23 closed
2 2 NL 2017-08-05 open
3 3 NL 2017-08-22 closed
4 4 NL 2017-08-26 closed
5 5 BE 2017-08-25 closed …Run Code Online (Sandbox Code Playgroud) 如果我有这样的事情:
D = {'a': 97, 'c': 0 , 'b':0,'e': 94, 'r': 97 , 'g':0}
Run Code Online (Sandbox Code Playgroud)
如果我想要例如将"0"的出现次数计算为一个值而不必迭代整个列表,那么这是否可能?如何?
我正在寻找Julia中的(希望内置)函数来计算组合的数量
我显然可以使用阶乘法来实现自己,但我几乎可以肯定有人已经对此感到担忧.
制作计数器索引比使用循环有更快的方法吗?在相等值的连续运行中,索引应该相同.我发现循环非常慢,特别是当数据如此之大时.
为了说明,这是输入和所需的输出
x <- c(2, 3, 9, 2, 4, 4, 3, 4, 4, 5, 5, 5, 1)
Run Code Online (Sandbox Code Playgroud)
期望的结果:
c(1, 2, 3, 4, 5, 5, 6, 7, 7, 8, 8, 8, 9)
Run Code Online (Sandbox Code Playgroud)
请注意,非连续运行具有不同的索引.例如,查看值的所需索引2和4
我的低效代码是这样的:
group[1]<-1
counter<-1
for (i in 2:n){
if (x[i]==x[i-1]){
group[i]<-counter
}else{
counter<-counter+1
group[1]<-counter}
}
Run Code Online (Sandbox Code Playgroud) 我有一个列表列表,看起来像
listOfLists = [
['a','b','c','d'],
['a','b'],
['a','c'],
['c','c','c','c']
]
Run Code Online (Sandbox Code Playgroud)
我想计算具有特定元素的列表的数量.例如,我的输出应该是
{'a':3,'b':2,'c':3,'d':1}
Run Code Online (Sandbox Code Playgroud)
如您所见,我不需要元素的总数.在这种情况下"c",尽管其总计数为5,但输出为3,因为它仅出现在3个列表中.
我正在使用计数器来获取计数.同样可以在下面看到.
line_count_tags = []
for lists in lists_of_lists:
s = set()
for element in lists:
s.add(t)
lines_count_tags.append(list(s))
count = Counter([count for counts in lines_count_tags for count in counts])
Run Code Online (Sandbox Code Playgroud)
所以,当我打印计数时,我明白了
{'a':3,'c':3,'b':2,'d':1}
Run Code Online (Sandbox Code Playgroud)
我想知道是否有更好的方法来实现我的目标.
在计数方面,应该使用do-while循环还是for循环?因为这:
class Main {
public static void main(String[] args) {
int times = 1;
do {
System.out.println("I have printed " + times + " times.");
times++;
} while (times < 6);
}
}
Run Code Online (Sandbox Code Playgroud)
似乎与此完全相同:
class Main {
public static void main(String[] args) {
for (int times = 1; times < 6; times++) {
System.out.println("I have printed " + times + " times.");
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是速度的差异吗?偏爱?情况?个人怪癖?某种"Java社会禁忌"?我不知道.似乎可以用于有效计数,只需要更多.两者都打印完全相同的东西.
System.out.println("Many thanks!!");