counting

Counting digits using while loop

情到浓时终转凉″ 提交于 2019-11-30 17:00:19
问题 I was recently making a program which needed to check the number of digits in a number inputted by the user. As a result I made the following code: int x; cout << "Enter a number: "; cin >> x; x /= 10; while(x > 0) { count++; x = x/10; } From what I can tell (even with my limited experience) is that it seems crude and rather unelegant. Does anyone have an idea on how to improve this code (while not using an inbuilt c++ function)? 回答1: In your particular example you could read the number as a

Counting the number of occurrences of a string within a string

青春壹個敷衍的年華 提交于 2019-11-30 15:45:04
问题 What's the best way of counting all the occurrences of a substring inside a string? Example: counting the occurrences of Foo inside FooBarFooBarFoo 回答1: One way to do is to use std::string find function: #include <string> #include <iostream> int main() { int occurrences = 0; std::string::size_type pos = 0; std::string s = "FooBarFooBarFoo"; std::string target = "Foo"; while ((pos = s.find(target, pos )) != std::string::npos) { ++ occurrences; pos += target.length(); } std::cout << occurrences

Difference between retain and copy?

岁酱吖の 提交于 2019-11-30 15:38:15
What exactly is the difference between retain and copy? what is its significance on reference counting? I know that when an object is allocated using alloc/retain, reference count goes up by one. so how about using copy? Another question relating to this is, the difference between using @property(nonatomic, retain) and @property(nonatomic,copy)? retain -- is done on the created object, it just increase the reference count. copy -- create a new object Jiraheta Answering your question to the best of my knowledge. First, What exactly is the difference between retain and copy? what is its

SQLite count, group and order by count

我只是一个虾纸丫 提交于 2019-11-30 13:08:46
问题 I have a table that looks like this: FOO BAR BAZ ----+----+---- foo1 bar1 baz1 foo2 bar3 baz2 foo3 bar1 baz3 foo4 bar1 baz4 foo5 bar3 baz5 foo6 bar1 baz6 foo7 bar2 baz7 And as a result I would like to get the count of how many times each bar appeared in the table.. so, the output I'm looking for looks like this: BAR COUNT -----+----- bar1 4 bar3 2 bar2 1 Can I do a query for something like this in SQLite? I guess it should be pretty easy, but I am not an SQL programmer by any means, and I

Function to count number of digits in string

ε祈祈猫儿з 提交于 2019-11-30 06:35:49
I was looking for a quick PHP function that, given a string, would count the number of numerical characters (i.e. digits) in that string. I couldn't find one, is there a function to do this? This can easily be accomplished with a regular expression. function countDigits( $str ) { return preg_match_all( "/[0-9]/", $str ); } The function will return the amount of times the pattern was found, which in this case is any digit. first split your string , next filter the result to only include numeric chars and then simply count the resulting elements. <?php $text="12aap33"; print count(array_filter

Count the number of Ks between 0 and N

心不动则不痛 提交于 2019-11-30 06:22:49
问题 Problem: I have seen questions like: 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? ... ... These kinds of questions are very similar of asking to find the total number that Ks (i.e. K=0,1,2,...,9) are shown in number range [0, N] . Example: Input: K=2, N=35 Output: 14 Detail: list of 2 s between [0,35] : 2, 12, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 32 , note that 22 will be counted as twice (as 22 contains two 2 s)

Difference between retain and copy?

我是研究僧i 提交于 2019-11-29 22:04:35
问题 What exactly is the difference between retain and copy? what is its significance on reference counting? I know that when an object is allocated using alloc/retain, reference count goes up by one. so how about using copy? Another question relating to this is, the difference between using @property(nonatomic, retain) and @property(nonatomic,copy)? 回答1: retain -- is done on the created object, it just increase the reference count. copy -- create a new object 回答2: Answering your question to the

How to print count of occourance of some string in the same CSV file using Python?

巧了我就是萌 提交于 2019-11-29 17:43:25
I have a CSV file in which contains one column (column1). I want to check whether the element in cell repeats and how many times(occcurance_count).And print count of occurrence in the same CSV file using Python. In the below example the "241682-27638-USD-OCOF" is not repeating so the count is one, "241942-37190-USD-DIV" is repeated twice so the count is 2 and so on. Want the output as below in CSV format column1 ,occcurance_count 1682-27638-USD-OGGCOF ,1 241682-27638-USD-OGGINT ,1 241682-27638-USD-CIGGNT ,1 241682-27638-USD-OCGGINT ,1 241942-37190-USD-GGDIV ,2 241942-37190-USD-CHYOF ,1 241942

Count the number previous items in by group in R [duplicate]

五迷三道 提交于 2019-11-29 17:02:21
This question already has an answer here: Adding a counter column for a set of similar rows in R [duplicate] 1 answer I would like to create a new variable which counts the number of previous items in a by group. Here is what I mean, taking the esoph dataset as an example. first I sort the dataset by my by group esoph$agegp, esoph$alcgp and an additional value column -esoph$ncontrols . This gives me the following dataset x<-esoph[order(esoph$agegp, esoph$alcgp, -esoph$ncontrols ), ] x agegp alcgp tobgp ncases ncontrols 1 25-34 0-39g/day 0-9g/day 0 40 2 25-34 0-39g/day 10-19 0 10 3 25-34 0-39g

Counting occurrences without using collections.Counter

巧了我就是萌 提交于 2019-11-29 13:03:49
I am trying to retrieve the most frequent and less frequent elements in a list. frequency([13,12,11,13,14,13,7,11,13,14,12,14,14]) My output is: ([7], [13, 14]) I tried it with: import collections s = [13,12,11,13,14,13,7,11,13,14,12,14,14] count = collections.Counter(s) mins = [a for a, b in count.items() if b == min(count.values())] maxes = [a for a, b in count.items() if b == max(count.values())] final_vals = [mins, maxes] But I don't want to use the collections module and try a more logic oriented solution. Can you please help me to do it without collections? You could use a try and except