On Wed, Jul 20, 2011 at 7:32 AM, Baishampayan Ghose <b.gh...@gmail.com> wrote:
> On Wed, Jul 20, 2011 at 11:04 AM, Payam Mahmoudian
> <pa...@mahmoudian.info> wrote:
>> It's appreciated if you could help me to convert following C code into 
>> clojure:
>>
>> float qTR(char *s1, char *s2)
>> {
>>    int    p,pl,q=0,r,s1l=strlen(s1),s2l=strlen(s2);
>>
>>    for (p=0;p<s1l;p++)
>>        for (pl=1;pl<=s1l-p;pl++)
>>            for (r=0;r<s2l-pl+1;r++)
>>                if (!memcmp(s1+p,s2+r,pl)) q += pl;
>>
>>    
>> return((((s1l*q)/(s1l*(s1l+1)*(s1l+2)/6.0))+((s2l*q)/(s2l*(s2l+1)*(s2l+2)/6.0)))/(s1l+s2l));
>> }
>>
>> Actually, I have a performance issue O(n^3), so I want to know if I
>> could use clojure concurrency feature in this case.
>
> Welcome to Clojure! Ideally, you should have posted a new question
> instead of replying to an existing post. It messes up threads in the
> discussion, you know.
>
> Coming to your problem, it'd be great if you explain the problem and
> your proposed solution in English (or pseudo-code). It's hard for
> someone to convert imperative code written in C to functional Clojure
> by just looking at existing code.

At a glance, I can tell you that it's seeing if two strings s1 and s2
have any substring in common -- for example "zoboomafoo" and "foobar"
contain a common substring "foo". For each possible substring position
as indicated by an offset into each string and a length at least 1 and
not enough to run off the end of either string, it adds the length to
a running total q if the corresponding substrings *differ*. So q ends
up smaller the more and larger common substrings turn up.

After that, it does some complicated math with the strings' lengths and q. :)

The OP may want to read up in the Knuth-Morris-Pratt algorithm to
discover the substrings more efficiently. Then q can be computed from
what it would be if the strings had no single letter in common by
subtracting for each substring n+2(n-1)+3(n-2)+ ... + n(1) where n is
that substring's length. There's probably a more efficient way to
compute q, or even to compute the return value, though.

-- 
Protege: What is this seething mass of parentheses?!
Master: Your father's Lisp REPL. This is the language of a true
hacker. Not as clumsy or random as C++; a language for a more
civilized age.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to