Replace pattern with the number of matches so far

I want to replace a n-th pattern match in an expression with n. This is a very simple task, though it appears very hard to find an elegant implementation.

For example, with input

inp = {x, y, z, x, y, x, x, z} 

I wish to compute, replacing pattern x,

{1, y, z, 2, y, 3, 4, z} 

Preferably, I’d like to access the index n in the replacement rule. E.g. something like

Func[inp, x :> Symbol[m<>ToString@#]& ]  >>> {m1, y, z, m2, y, m3, m4, z} 

How can I achieve this? It’s trivial using Count and a For loop, but is very un-stylistic.

How do I join getting one row from the left table, no matter how many matches i get from the right table?

I have two tables – one is a data table and the other is a mapping table. I want to join them together, but only preserve the data from the right table. However, it is possible that the match table may contains multiple records that match to a single record in the right table. I cannot use a DISTINCT because there may be identical rows in the right table, and I want to preserve the same number of rows from the right-table in the result set.

Here is a sample of the data I am working with:

       DataTable                           MappingTable +-----+-----+-----+-----+           +------+------+------+------+ | ID1 | ID2 | ID3 | ID1 |           | ID1  | ID2  | ID3  | ID1  | +-----+-----+-----+-----+           +------+------+------+------+ |  1  |  1  |  1  |  1  |           |  1   | NULL | NULL | NULL | |  1  |  1  |  1  |  1  |           | NULL | NULL | NULL |  1   | |  2  |  1  |  1  |  1  |           |  3   |  3   | NULL | NULL | |  3  |  1  |  1  |  3  |           +------+------+------+------+ |  4  |  1  |  1  |  4  | |  2  |  2  |  1  |  1  | |  3  |  2  |  1  |  3  | |  3  |  3  |  1  |  3  | |  2  |  1  |  0  |  1  | |  2  |  1  |  0  |  1  | |  4  |  3  |  2  |  3  | +-----+-----+-----+-----+ 

Below is the join I am using. I wrote a custom function to handle the NULL-matching behavior, which I am including here as well.

SELECT * FROM DataTable P JOIN MappingTable M ON dbo.fNullMatchCheckIntS(P.ID1,M.ID1,0,1) = 1     AND dbo.fNullMatchCheckIntS(P.ID2,M.ID2,0,1) = 1     AND dbo.fNullMatchCheckIntS(P.ID3,M.ID3,0,1) = 1     AND dbo.fNullMatchCheckIntS(P.ID4,M.ID4,0,1) = 1 

CREATE FUNCTION dbo.fNullMatchCheckIntS (     @Value1 INT     ,@Value2 INT     ,@AutoMatchIfValue1IsNull BIT     ,@AutoMatchIfValue2IsNull BIT )     RETURNS BIT AS  BEGIN      DECLARE @Result BIT = 0      SELECT         @AutoMatchIfValue1IsNull = ISNULL(@AutoMatchIfValue1IsNull,0)         ,@AutoMatchIfValue2IsNull = ISNULL(@AutoMatchIfValue2IsNull,0)      IF         (@AutoMatchIfValue1IsNull = 1 AND @Value1 IS NULL)         OR (@AutoMatchIfValue2IsNull = 1 AND @Value2 IS NULL)         OR @Value1 = @Value2         OR (@Value1 IS NULL AND @Value2 IS NULL)     BEGIN         SET @Result = 1     END      RETURN @Result END 

The problem with the way the join works is that the first two rows in the DataTable match on the first two rows in the MappingTable, giving me four identical records in the result, but I only want 2. I know that I could add an identity column to the DataTable and then use DISTINCT or PARTITION to get the result I am looking for, but I would like to avoid that route if possible.

EDIT: I figured out a way to do this using EXISTS, but it looks a little ugly in my opinion. Still interested in other answers if anyone has an idea. Thanks!

SELECT * FROM DataTable D WHERE EXISTS (     SELECT D.ID1, D.ID2, D.ID3, D.ID4     FROM MappingTable M      WHERE dbo.fNullMatchCheckIntS(D.ID1,M.ID1,0,1) = 1         AND dbo.fNullMatchCheckIntS(D.ID2,M.ID2,0,1) = 1         AND dbo.fNullMatchCheckIntS(D.ID3,M.ID3,0,1) = 1         AND dbo.fNullMatchCheckIntS(D.ID4,M.ID4,0,1) = 1 ) 

How do I call a system like a grammar, but where a rule has to be applied to all matches at once?

For example, given rules $ \{ a \to x, a \to y \}$ and input $ aa$ , I am usually allowed to derive strings $ \{ xx, xy, yx, yy \}$ . I would like to restrict this to only performing “consistent” rewrites, so that the language would be like $ \{ xx, yy \}$ . It is evidently possible to synchronize rewrites in distant parts of a sentence within the usual formal grammar setting, but I wonder if this possibility is better explored under a different name or in a different arrangement.

I notice that context-sensitive grammars pose trouble with this “consistency” condition. For example, given a ruleset $ \{ aa \to x\}$ and initial string $ aaa$ , I am not sure if I should allow anything to be derived. Then again, it is entirely possible that only some rules, and specifically some context-free rules, may be enhanced with consistency.

I am rather sure the system I have in mind defines a language, and even that I could with some thinking propose a formal way to rewrite a given grammar so that some select context free rules are made consistent. But I wonder if this is actually widely known under some other name.

How to fetch different record when nothing matches in where conditions in the same query?

I am using ORACLE 12C database.

There are two components – Agreement and Case.

Agreement related information stores on Agreement_line and Object Table. Common column between these tables are Trans_id and agr_line_no.

Similarly, Case related information stores in Cla_case and Cla_event table. Common column between these tables is Cla_event_no.

I want to fetch details of case whose incident_date is falling between Cover_start_date and Cover_end_date of Agreement_line table. Incident_date is column of Cla_event table

I am getting desired output from the below query :

select cc.cla_case_no, al.poly_no,al.agr_line_no, al.agr_line_seq_no, al.product_line_id, al.trans_id, al.cover_start_date, ce.incident_date, al.cover_end_date, o.n15, cc.name_id_no, o.object_no, o.seq_no, CASE WHEN o.n15 = cc.name_id_no      THEN ' '      ELSE 'Joint Ins'    END    AS NAME_ID_CHECK from cla_case cc, cla_event ce, agreement_line al, object o where cc.cla_case_no = 9353152 and ce.cla_event_no = cc.cla_event_no and AL.COVER_START_DATE <= CE.INCIDENT_DATE and al.cover_end_date >= ce.incident_date and al.poly_no = 410385376 and al.product_line_id  = 'BHEDS'  and al.agr_line_no = o.agr_line_no and al.trans_id = o.trans_id 

But now I also need to fetch latest record of provided poly_no when the incident_date is not falling between cover_start_date and cover_end_date

I tried below query :

SELECT cc.cla_case_no,        al.poly_no,        al.agr_line_no,        al.agr_line_seq_no,        al.product_line_id,        al.trans_id,        al.cover_start_date,        ce.incident_date,        al.cover_end_date,        o.n15,        cc.name_id_no,        o.object_no,        o.seq_no,        CASE WHEN o.n15 = cc.name_id_no THEN ' ' ELSE 'Secondary insured' END           AS NAME_ID_CHECK,        ' ' "INCIDENT_DATE_CHECK"   FROM cla_case cc,        cla_event ce,        agreement_line al,        object o  WHERE     cc.cla_case_no = 9353152        AND ce.cla_event_no = cc.cla_event_no        AND AL.COVER_START_DATE <= CE.INCIDENT_DATE        AND al.cover_end_date >= ce.incident_date        AND al.poly_no = 410385376        AND al.product_line_id = 'BHEDS'        AND al.agr_line_no = o.agr_line_no        AND al.trans_id = o.trans_id UNION SELECT cc.cla_case_no,        al2.poly_no,        al2.agr_line_no,        al2.agr_line_seq_no,        al2.product_line_id,        al2.trans_id,        al2.cover_start_date,        ce.incident_date,        al2.cover_end_date,        o2.n15,        cc.name_id_no,        o2.object_no,        o2.seq_no,        CASE WHEN o.n15 = cc.name_id_no THEN ' ' ELSE 'Secondary insured' END           AS NAME_ID_CHECK,        'Incident date not falling between cover start date and cover end date '           "INCIDENT_DATE_CHECK"   FROM agreement line al2, object o2 where al2.poly_no not exists ( SELECT al.poly_no FROM cla_case cc, cla_event ce, agreement_line al where cc.cla_case_no = 9353152 and ce.cla_event_no = cc.cla_event_no and AL.COVER_START_DATE <= CE.INCIDENT_DATE and al.cover_end_date >= ce.incident_date and al.poly_no = 410385376 and al.product_line_id  = 'BHEDS' ) and al2.poly_no = 410385376 and al2.product_line_id = 'BHEDS' and al2.newest = 'Y' and  al2.agr_line_no = o2.agr_line_no and al2.trans_id = o2.trans_id 

I am getting error saying “Command not properly ended”. Could anyone please point out what I am doing wrong and is there any other alternative way instead of using UNION ?

A deterministic finite state automata for finding all (potentially overlapping) regular expression matches?

I was working on a bioinformatics practice problem named Finding a Protein Motif on rosalind.info. In essence, I was given a particular regular expression N[^P](S|T)[^P] and is asked to find all matches.

Solving that problem is not the goal here, I have a ‘working’ solution here. In essence, I manually designed a state machine that can find all matches for that regular expression.

And here is a ‘visualization’ of the state machine, to make it clear how it is manually designed.

digraph G {     0 [label="0,''"]     1 [label="1,'N'"]     2 [label="2,'NN'"]     3 [label="3,'NS|NX'"]     4 [label="4,'NNS'"]     5 [label="5,'NSS|NXS'"]     0 -> 1 [label="N"]     0 -> 0 [label="P"]     0 -> 0 [label="S"]     0 -> 0 [label="X"]     1 -> 2 [label="N"]     1 -> 0 [label="P"]     1 -> 3 [label="S"]     1 -> 3 [label="X"]     2 -> 2 [label="N"]     2 -> 0 [label="P"]     2 -> 4 [label="S"]     2 -> 3 [label="X"]     3 -> 1 [label="N"]     3 -> 0 [label="P"]     3 -> 5 [label="S"]     3 -> 0 [label="X"]     4 -> 1 [label="N(accept)"]     4 -> 0 [label="P"]     4 -> 5 [label="S(accept)"]     4 -> 0 [label="X(accept)"]     5 -> 1 [label="N(accept)"]     5 -> 0 [label="P"]     5 -> 0 [label="S(accept)"]     5 -> 0 [label="X(accept)"] } 

The classical theory allows us to convert a regular expression to a non-deterministic finite-state automaton and then convert it to a deterministic finite-state automaton through subset construction. In particular, subset construction guarantees that if there exists an accepting computation, then the deterministic finite-state automaton would also accept.

Let say I have a regular expression that matched twice, the corresponding deterministic finite-state automaton would accept after the first match, but then it doesn’t know what to do in order to set it to the right state for detecting overlapping matches. I guess I could start one character after the beginning of the first match, which in the worst case would probably lead to quadratic time, as we could imagine with {.*} on {{{{{}

In the worst case, I expect quadratic time (e.g. {{{{{}}}}}}), but it would be great if the timing is output-sensitive, for I believe a good deal of cases aren’t quadratic in output size.

It would be great if my state machine used to find all matches can be generalized (apparently sometimes it need linear space, not just a single state) or automatically designed. Do we know if there are existing theories for that?

No engines matches when importing accounts

Importing accounts to this engine shows “no engine matches” for all urls.

[setup]
enabled=1
default checked=0
engine type=Article
description=
dofollow=1
anchor text=1
creates own page=1
uses pages=0
multiple posts per account=1
;;; API MAIN VARIABLES
[api_url]
type=extract
default=http://gsapi.local:9090
static=1
[api_link_id]
type=extract
front=”link_id”:
back=}
static=1
[api_target_url]
type=extract
default=%targethost%
static=1
;;; API REQUIRED VARIABLES
[api_engine_name]
type=extract
default=test
static=1
;;; NORMAL VARIABLES
[URL]
type=url
[Anchor_Text]
type=text
alternate data=%spinfile-generic_anchor_text.dat%
[Article]
type=memo
allow html=1
must be filled=1
hint=The full article comes here.
auto modify=0
auto add anchor url=2
auto add anchor url content=%file-auto_anchor-article.dat%
custom mode=1
[Login]
type=login
must be filled=1
hint=The login for websites that need an account. Use numbers and letters only.
min length=10
upcase=0
static=1
[Password]
type=password
must be filled=1
hint=A password used for websites that need an account. Use numbers and letters only.
static=1
[Your E-Mail]
type=email
static=1

—–
[STEP1]
link type=Article
just download=1
submit success=”success”:true
submit failed=”success”:false
submit failed retry=XXXXXXXXXXXXXXXXXXXXXXXX
captcha failed=XXXXXXXXXXXXXXXXXXXXXXXX
verify submission=1
verify by=url
verify url=%api_url%/api/link/verify_redirect/%api_link_id%
verify interval=10
verify timeout=99999999999999999999
first verify=5
verify on unknown status=0
[STEP2]
modify url=%api_target_url%
just download=1
[STEP3]
modify url=%api_url%/api/link/create/%api_engine_name%
post data=engine_name=%api_engine_name%&target_url=%api_target_url%&url=%url%
form request with=XMLHttpRequest
encode post data=3
just download=1

Created POST route, but resulting in RoutingError (No Route matches POST)

I am setting up a new route “/api/v1/example_two” that I can POST to (create), however it is resulting in No route matches [POST] RoutingError

I have tried explicitly stating post, try to create the route through resources

config/routes.rb

Rails.application.routes.draw do    resources :roles, only: [:index], defaults: { format: :xml }    defaults format: :json do     scope :v1 do       resources :example_one, only: [:create, :show], param: :uuid       resources :example_two, only: [:create], param: :uuid     end   end end  

and I have a controller: app/controllers/example_two.rb

class example_two < ApplicationController   def create     ...   end end 

I expect it to return whatever is in example_two#create, however it is resulting in ActionController::RoutingError (No route matches [POST] \"/api/v1/example_two\"

Is there a way to filter out partial matches from search results on YouTube?

When searching for “JoJo’s Bizarre Adventure,” I frequently just search the keyword “jojo,” but my first sight is often greeted by a musical artist by the same name.

Instead, I’d prefer that all my YouTube results not show a single result pertaining to the artist, and instead pertaining to the anime itself.

Is there a way to a filter out specific content from the search results?