标签: counting

代码高尔夫7月版第4期:计算前十个发生的单词

鉴于以下总统名单可以在最小的计划中进行前十个字数:

输入文件

    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

以 …

code-golf counting text-files

11
推荐指数
5
解决办法
1111
查看次数

计算0到N之间的K数

问题:

我见过这样的问题:

  1. count the number of 0s between 0 and N?
  2. count the number of 1s between 0 and N?
  3. count the number of 2s between 0 and N?
  4. ......

这些类型的问题与要求查找Ks (i.e. K=0,1,2,...,9)数字范围中显示的总数非常相似[0, N].

例:

  • 输入: K=2, N=35
  • 输出: 14
  • 细节: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)

algorithm numbers range counting time-complexity

11
推荐指数
2
解决办法
1万
查看次数

如何计算php中包含零的数字位数

我正在计算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)

让我知道如何解决它.

php numbers count counting

10
推荐指数
2
解决办法
4万
查看次数

这段代码如何计算1位数?

我找到以下代码来计算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)

c c++ algorithm bit-manipulation counting

10
推荐指数
2
解决办法
3591
查看次数

分组和计数以获得贴近

我想每计数country次数的数量statusIS open和的次数statusclosed.然后计算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)

grouping r counting dataframe

10
推荐指数
1
解决办法
254
查看次数

计算python中字典中某个值的出现次数?

如果我有这样的事情:

D = {'a': 97, 'c': 0 , 'b':0,'e': 94, 'r': 97 , 'g':0}
Run Code Online (Sandbox Code Playgroud)

如果我想要例如将"0"的出现次数计算为一个值而不必迭代整个列表,那么这是否可能?如何?

python dictionary counting python-3.x

10
推荐指数
4
解决办法
2万
查看次数

什么是计算组合的Julia函数(n选择k)?

我正在寻找Julia中的(希望内置)函数来计算组合的数量

nChooseK

我显然可以使用阶乘法来实现自己,但我几乎可以肯定有人已经对此感到担忧.

counting combinatorics julia

10
推荐指数
2
解决办法
3866
查看次数

将索引添加到相等值的连续运行

制作计数器索引比使用循环有更快的方法吗?在相等值的连续运行中,索引应该相同.我发现循环非常慢,特别是当数据如此之大时.

为了说明,这是输入和所需的输出

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)

请注意,连续运行具有不同的索引.例如,查看值的所需索引24

我的低效代码是这样的:

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)

indexing performance loops r counting

9
推荐指数
3
解决办法
457
查看次数

如何获取具有特定元素的列表数量?

我有一个列表列表,看起来像

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)

我想知道是否有更好的方法来实现我的目标.

python list counting python-3.x

9
推荐指数
4
解决办法
1148
查看次数

做循环与Java中的循环计数

在计数方面,应该使用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!!");

java for-loop counting do-while

9
推荐指数
1
解决办法
415
查看次数