counting

Counting substring that begin with character 'A' and ends with character 'X'

扶醉桌前 提交于 2019-12-11 07:17:55
问题 PYTHON QN: Using just one loop, how do I devise an algorithm that counts the number of substrings that begin with character A and ends with character X ? For example, given the input string CAXAAYXZA there are four substrings that begin with A and ends with X , namely: AX , AXAAYX , AAYX , and AYX . For example: >>>count_substring('CAXAAYXZA') 4 回答1: Since you didn't specify a language, im doing c++ish int count_substring(string s) { int inc = 0; int substring_count = 0; for(int i = 0;i < s

Count the amount of vowels in a sentence and display the most frequent

北城余情 提交于 2019-12-11 02:46:51
问题 This is my code so far for counting vowels. I need to to scan through a sentence, count and compare the vowels and then display the top occurring vowels. from collections import Counter vowelCounter = Counter() sentence=input("sentence") for word in sentence: vowelCounter[word] += 1 vowel, vowelCount= Counter(vowel for vowel in sentence.lower() if vowel in "aeiou").most_common(1)[0] Does anyone have a better way to do this ? 回答1: IMO, long lines are best avoided for the sake of clarity: #!

Simple way to count occurrences of String and sort by them

喜你入骨 提交于 2019-12-11 01:39:07
问题 I am looking at a redundant list of Strings, e.g. { "One", "One", "One", "Two", "Three", "Three" } What is the best way to count the occurrences, then create a non-redundant list of the strings, sorted by the number of occurrences? The result I want is a List like this: { "One", "Three", "Two" } 回答1: You can use the trick in the most voted answer of this question about how to sort the map by its values. Here is an example implementation (I have added generics to the comparator): you add the

How to stop nil values in a list being counted

只谈情不闲聊 提交于 2019-12-11 00:56:34
问题 I have a function which is returning "Match" if true and nil else. I need to know the number of values in the list which "Match" (hence using nil as the other value in my function). someList = {"gdj", nil, "jdis"} print(#someList) --> 3 My origination question is here if there is something I should be returning other than nil! 回答1: # operator is defined in a rather weird way; it will count until one of non-nil elements. In your case, it's entirely possible that it could return 1 as well. This

what is Counting Semaphore?

社会主义新天地 提交于 2019-12-10 20:22:21
问题 Hi I did get how the Counting Semaphore works? Please help me in understanding. As per my understanding if we set count as 3, then process can use 3 threads to access the resource. so, here just 3 threads have access on the resource. When 1 thread leaves the other waiting thread comes in. If my understanding is correct, these 3 thread can corrupt shared data too. Then what is use of it? 回答1: Your observations are correct; typically a resource either needs to be restricted to one thread (e.g.

Python: Binary Counting without using inbuilt functions

北城余情 提交于 2019-12-10 18:16:22
问题 I have been having some trouble recently with creating a program that counts in binary from 1 to the chosen number. This is my code at the moment: num6 = 1 binStr = '' num5 = input('Please enter a number to be counted to:') while num5 != num6: binStr = str(num6 % 2) + binStr num6 //= 2 num6 = num6 + 1 print(binStr) For example, if I input 5, it needs to go 1, 10, 11, 100, 101. I just can't seem to get the hang of it. Any help will be appreciated, thanks. 回答1: The issue is that you're dividing

Count non-NA numbers in a column and output a data frame

纵然是瞬间 提交于 2019-12-10 16:06:15
问题 For example, I have this data frame (df): Color X1 X2 X3 X4 Red 1 1 0 2 Blue 0 NA 4 1 Red 3 4 3 1 Green 2 2 1 0 I would like to create a function that counts up the number of non-NAs in X2 as a function of color. I would like the output of this function in a new data frame named newdf. This is what I would like for output: Color X2 Red 2 Blue NA Green 1 So far, I have this code: Question <- function(Color){ Result <- rowsum((df[c("X2")] > 0) + 0, df[["X2"]], na.rm = TRUE) rowSums(Result)[

Output iterator adapter to count but not copy

大城市里の小女人 提交于 2019-12-10 14:29:35
问题 There are various STL algorithms that rely on an output iterator to store the result of the algorithm. For example, std::set_intersection will store all the common elements between two sorted ranges in an Output iterator that is then post incremented per element outputted. Sometimes, I am not interested in the actual elements but only the number of output elements. In such cases it is a waste of memory and performance to copy the elements. Is there an iterator adapter that I can use to count

Counting lattice points inside a triangle

我怕爱的太早我们不能终老 提交于 2019-12-10 08:55:08
问题 I've points for a big triangle lets call it a, b, c. (a = (x, y) and so on). Now I want to count the number of integral points inside the area enclosed by this triangle, so I first looked at Pick's theorem. The second approach that I considered was generating a list of points bounded by max, min of the triangle, and then checking if each point lies inside the triangle. I used the Barycentric coordinates method to do this. It works however my triangle is pretty huge and my program is basically

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

对着背影说爱祢 提交于 2019-12-09 05:12:15
问题 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 回答1: You could use grep -o then pipe through wc -l : $ echo "123 123 123" | grep -o 123 | wc -l 3 回答2: Maybe you should convert spaces to newlines first: $ echo "1 1 2 2 2 5" | tr '