counting

Counting regex pattern matches in one line using sed or grep?

十年热恋 提交于 2019-12-03 05:23:55
I want to count the number of matches there is on one single line (or all lines as there always will be only one line). I want to count not just one match per line as in echo "123 123 123" | grep -c -E "123" # Result: 1 Better example: echo "1 1 2 2 2 5" | grep -c -E '([^ ])( \1){1}' # Result: 1, expected: 2 or 3 You could use grep -o then pipe through wc -l : $ echo "123 123 123" | grep -o 123 | wc -l 3 Maybe you should convert spaces to newlines first: $ echo "1 1 2 2 2 5" | tr ' ' $'\n' | grep -c 2 3 Maybe below: echo "123 123 123" | sed "s/123 /123\n/g" | wc -l ( maybe ugly, but my bash fu

Counting coprimes in a sequence

心不动则不痛 提交于 2019-12-03 03:09:52
Having a sequence of n <= 10^6 integers, all not exceeding m <= 3*10^6, I'd like to count how many coprime pairs are in it. Two numbers are coprime if their greatest common divisor is 1. It can be done trivially in O(n^2 log n), but this is obviously way to slow, as the limit suggests something closer to O(n log n). One thing than can be done quickly is factoring out all the numbers, and also throwing out multiple occurences of the same prime in each, but that doesn't lead to any significant improvement. I also thought of counting the opposite - pairs that have a common divisor. It could be

Conway's Game of Life, counting neighbors [closed]

断了今生、忘了曾经 提交于 2019-12-02 22:06:11
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 6 years ago . I have an error somewhere in my code, I think I am entering an infinite loop. Basically I get a matrix and two indexes, i,j, and i need to count how many

Google Interview Puzzle [closed]

廉价感情. 提交于 2019-12-02 18:17:30
Given n dice, each of 'a' sides and a sum b, return the number of ways in which the sum b can be obtained. How can you reduce the time complexity and space complexity? This was asked in a Google interview and I am unsure of the answer. This is asking you to find the number of ways to write b as a sum of n positive integers. The answer is the number of compositions of b into n parts, which is (b-1 choose n-1) . Now if we take into account the constraint that the size of the parts is limited to a , the problem gets a little more interesting. I recommend using generating functions for this. The

character count minus HTML characters C#

余生颓废 提交于 2019-12-02 12:18:50
I'm trying to figure out a way to count the number of characters in a string, truncate the string, then returns it. However, I need this function to NOT count HTML tags. The problem is that if it counts HTML tags, then if the truncate point is in the middle of a tag, then the page will appear broken. This is what I have so far... public string Truncate(string input, int characterLimit, string currID) { string output = input; // Check if the string is longer than the allowed amount // otherwise do nothing if (output.Length > characterLimit && characterLimit > 0) { // cut the string down to the

Count occurence of multiple numbers in vector one by one

拟墨画扇 提交于 2019-12-02 11:48:28
问题 I have two vectors a <- c(1, 5, 2, 1, 2, 3, 3, 4, 5, 1, 2) b <- (1, 2, 3, 4, 5, 6) I want to know how many times each element in b occurs in a. So the result should be c(3, 3, 2, 1, 2, 0) All methods I found like match() , == , %in% etc. are not suited for entire vectors. I know I can use a loop over all elements in b, for (i in 1:length(b)) { c[I] <- sum(a==b, na.rm=TRUE) } but this is used often and takes to long. That's why I'm looking for a vectorized way, or a way to use apply() . 回答1:

Conway's Game of Life, counting neighbors [closed]

我与影子孤独终老i 提交于 2019-12-02 11:47:35
I have an error somewhere in my code, I think I am entering an infinite loop. Basically I get a matrix and two indexes, i,j, and i need to count how many neighbors around [i][j] have a value of 1 or 2. this is my code: int number_of_living_neighbors(matrix mat,int i, int j, int n,int m) { int counter=0,row_index=0,column_index=0; for(row_index=i-1;row_index<=i+1;row_index++) { for(column_index=j-1;column_index=j+1;column_index++) { if((row_index>=0)&&(row_index<n)&&(column_index>=0)&&(column_index<m)) { if((mat[row_index][column_index]==1)||(mat[row_index][column_index]==2)) counter++; if((row

How do I count the number of files read into this list?

て烟熏妆下的殇ゞ 提交于 2019-12-02 08:32:00
问题 I have a bunch of csv files that I am reading into a list like this: f <- list.files(pattern="201\\d{5}\\.csv") Is there any way to count how many files that I am reading in? 回答1: Yes, length(f) will give you the number of file names in f . 来源: https://stackoverflow.com/questions/18175979/how-do-i-count-the-number-of-files-read-into-this-list

How to apply the concept of counting occurrences on strings variables in C++

喜夏-厌秋 提交于 2019-12-02 05:44:11
following program ca calculate the frequency of ints in an array how to apply this concept on string variable because a string is also an array on the back end using namespace std; int counter[10]={0,0,0,0,0,0,0,0,0,0}; int arr [9][9],x; int main() { srand(time(NULL)); cout<<"enter the array \n"; for(int i=0;i<9;i++){ for(int j=0;j<9;j++){ arr[i][j]=rand()%10; } } for(int i=0;i<9;i++){ for(int j=0;j<9;j++){ cout<<arr[i][j]<<" "; } cout<<endl; } for(int i=0;i<9;i++){ for(int j=0;j<9;j++){ counter[arr[i][j]]++; } } for(int j=0;j<10;j++){ cout<<j<<" : "<< counter[j]<<endl; } return 0; } Here is

How do I count the number of files read into this list?

拈花ヽ惹草 提交于 2019-12-02 05:28:08
I have a bunch of csv files that I am reading into a list like this: f <- list.files(pattern="201\\d{5}\\.csv") Is there any way to count how many files that I am reading in? Yes, length(f) will give you the number of file names in f . 来源: https://stackoverflow.com/questions/18175979/how-do-i-count-the-number-of-files-read-into-this-list