Choose k not-adjacent elements out of N [closed] - combinations

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Suppose we have a line of dots, there are totally N of them, we are to choose k dots such that each pairs of dots are not neighbors. How many possibilities there are in total?
For example, if we have N = 3 dots, we want to choose 2 dots that aren't adjacent, then there's only 1 possibility.

First and last dots have N-2 options to choose from, as you can't select the same dot or its neighbor. All the other 'inside' dots have N-3 options, as you can't select the same and one adjacent dot on each side.
If you would sum them all up, you would get twice as many options (because of duplicates). So N-2 dots have N-3 options, but you have to divide it by 2.
The result would be K = (N-2) + (N-2)*(N-3)/2

Related

How to avoid unrelated data from postgresql search [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to get the data to contain keyword of both "LED" and "car"
select count ( * ) from test_eu where eng_discription ~ '.* led .* AND .* car .*';
When I search PostgreSQL with the above code, results include those unrelated data like
so-called cardboard
carefully installed
In order to avoid this, I thought both sides of the searching keyword contain space " " solve this problem.
regex of space is
\s
so I made this code
select count ( * ) from test_eu where eng_discription ~ '\sled\s and \scar\s';
but still does not work.
How should I modify my code?
Assuming you want to check for the presence of both LED and car, anywhere in the description column, you could try:
SELECT COUNT(*) AS cnt
FROM test_eu
WHERE eng_discription ~* '\yled\y' AND eng_discription ~* '\ycar\y';

Regex (Bigquery) get specific values from STRING [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have the STRING - TX1234XT batch 44, 1111ABCDEF
TX1234XT (Can be different length)
batch 44 (number can be different length)
ABCDEF (can be a different length, but always have 1111 at the start)
What I need is to generate two columns:
BatchNumber Name
44 1111ABCDEF
1 1111SAMPLE
999 1111Example
Starting point:
First is done:
REGEXP_EXTRACT(reference, r'1111[a-zA-Z0-9_.+-]+') AS Name
Second
- REGEXP_REPLACE(REGEXP_EXTRACT(reference, r'batch [0-9_.+-]+'),r'batch ','') AS BatchNumber
SORTED ^_^
I don't really know Google Big Query, but if you want to extract the batch number and the value at the end, you could go with this regular expression:
/^.*?batch\s*(\d+),\s*(1111.+)$/
(\d+) will capture your batch id.
(1111.+) will capture the value starting with 1111.
Example here: https://regex101.com/r/SJXmIV/2

How to process a string? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
The first line of input will be 2 integers, h and w, which is the height(h) and width(w) of the rectangular area of sky you will be counting stars from. You may assume their values are below 1000.
The following h lines will have w characters per line, either x (Clear Sky) or * (star).
Sample Input
5 5
x***x
xxx*x
x*xxx
xxx*x
*xxxx
Sample output
7
How can I process the string?
Declare a variable to count the stars with an appropriate data type.
Iterate (loop) over the string to check each character for equality with '*'. If this is the case then increment your star counter.
Width and height are not required. If you want to constrain a larger field to the width and height provided, you can use a column (character# in a line) and a row (line#) counter and keep track where in the file you are.
After each character in your string increment (add 1) your column counter. After each line ('\n'-character denotes a new line) increment your row counter and reset the column counter. if your current column or row exceed the provided width or height, then ignore any '*'-characters until you are within bounds again or the string ends.
I leave the actual code for you as an excercise.
Helpful links:
Strings
Operators
Conditionals
If you have trouble with the input part, you should consider looking elsewhere than stackoverflow.

Replace the words "can't, don't" by "can not, do not" using python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I need to replace words like "{can't, don't, won't }" by "{can not, do not, would not}" using python
The problem is:
"can't" can be detected by checking suffix "n't", so we can replace "n't" by "not"
But how can we transform "ca" to "can" as when we split "can't" it should be transformed to "can not"?
Since the rules of English are large and sometimes inconsistent, your best bet is probably just to set up full word maps rather than trying to figure out on the fly which letters are represented by the apostrophe.
In other words, a dictionary with values like:
can't -> can not
don't -> do not
won't -> will not
:
oughtn't -> ought not

Regex - How to create a regex to check two strings with different length but one depends on the other [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
If there is a string like this "abcdefghijklm.com 80 /abcdefgh.php" where the domainname followed by http port and the sub string is first 8 digits of the domain name always and followed by ".php" (the sub-string character will change to 6 OR 8 OR 5 at times but however all those length would contain same characters of domain name and endswith .php
more examples like this,
xyzklmopqr.com 80 xyzklm.php
lkjhgfdsaq.com 80 lkjhg.php
mjuyhnbgtr.com 80 mjuyhnbg.php
This works and you can easily change the numbers
(\w{5,6}|\w{8})\w*\.com 80 \1\.php
It's a little simpler than the other guy's solution
The following should work:
(((\w{5})\w?)\w{2}?)\w*\.com 80 (\1|\2|\3)\.php
Note that this works for the specific lengths you mentioned in your question (5, 6, and 8), not for any generic length substring.
Example: http://www.rubular.com/r/NwCcihN6o6
I would try ([a-z]{6})\S* 80 \1\.php
That would work for your 6 case, you can change the number as needed for your other cases.