Discussion:
370103 words scanned and 206 palindromes identified in 0.292 seconds
(too old to reply)
DFS
2021-07-28 21:50:43 UTC
Permalink
Who has a better algorithm?

=============================================================================
t0 = time.perf_counter()
for word in words:
ispal = 1;
k = len(word)-1
for j in range(k):
if word[j] != word[k]:
ispal=0
break
k-=1
if ispal==1:
if len(word) > 1:
pals+=1
t1 = time.perf_counter()
print('%d words scanned and %d palindromes identified in %.3f seconds' %
(len(words), pals, (t1 - t0)))
=============================================================================

words from
https://raw.githubusercontent.com/dwyl/english-words/master/words_alpha.txt
Snit
2021-07-28 22:25:47 UTC
Permalink
Post by DFS
Who has a better algorithm?
I just want to know your favorite palindrome.
Post by DFS
=============================================================================
t0 = time.perf_counter()
ispal = 1;
k = len(word)-1
ispal=0
break
k-=1
pals+=1
t1 = time.perf_counter()
print('%d words scanned and %d palindromes identified in %.3f seconds' %
(len(words), pals, (t1 - t0)))
=============================================================================
words from
https://raw.githubusercontent.com/dwyl/english-words/master/words_alpha.txt
--
Personal attacks from those who troll show their own insecurity. They cannot use reason to show the message to be wrong so they try to feel somehow superior by attacking the messenger.

They cling to their attacks and ignore the message time and time again.
DFS
2021-07-28 22:32:55 UTC
Permalink
Post by DFS
Who has a better algorithm?
forgot to include the counter 'pals'

=============================================================================
t0 = time.perf_counter()
pals = 0
for word in words:
ispal = 1;
k = len(word)-1
for j in range(k):
if word[j] != word[k]:
ispal=0
break
k-=1
if ispal==1:
if len(word) > 1:
pals+=1
t1 = time.perf_counter()
print('%d words scanned and %d palindromes identified in %.3f seconds' %
(len(words), pals, (t1 - t0)))
=============================================================================
Post by DFS
words from
https://raw.githubusercontent.com/dwyl/english-words/master/words_alpha.txt
I actually wrote it in C first:

370103 words scanned and 206 palindromes identified in 0.0154 seconds


=============================================================================

int findpalindromes(char *wordarr[], int wordcnt)
{
QueryPerformanceCounter(&start);
int pals = 0;
for(int i=0;i<wordcnt;i++) {
int ispal = 1;
int k = strlen(wordarr[i])-1;
for(int j=0;j<k;j++) {
if(wordarr[i][j] != wordarr[i][k]) {
ispal=0;
break;
}
k--;
}
if(ispal) {
if(strlen(wordarr[i])>1) {
pals++;
}
}
}
printf ("\n%d words scanned and %d palindromes identified in %.4f
seconds\n",wordcnt,pals,elapsedtime(start));
}
=============================================================================
J***@.
1970-01-01 00:00:00 UTC
Permalink
Re: 370103 words scanned and 206 palindromes identified in 0.292 seconds

292 Milli seconds ? I get 11.1 Milli seconds ( in UTF-16, not ASCII ).

370103 words scanned and 206 palindromes identified in 11.1 Milli seconds

Diff_Load = 1, Load_File( LocDir, L"DFS.TXT" );

int j, k, L, Bad, pals = 0 ; double Start = UpSecs ;
LoopPP( Ln, fpLines ) { j = -1, Bad = 0, k = L = szStr( B ) - 1 ;

while ( ++j < k && !( Bad = B[ j ] != B[ k-- ] ) );
pals += L && !Bad, PP += !PP[1] ; }

Sh( L"%d words scanned and %d palindromes identified in %s seconds",
PP - BB - 1, pals, Prn( UpSecs - Start ) );
Branimir Maksimovic
2021-07-29 00:41:59 UTC
Permalink
Post by J***@.
Re: 370103 words scanned and 206 palindromes identified in 0.292 seconds
292 Milli seconds ? I get 11.1 Milli seconds ( in UTF-16, not ASCII ).
370103 words scanned and 206 palindromes identified in 11.1 Milli seconds
Diff_Load = 1, Load_File( LocDir, L"DFS.TXT" );
int j, k, L, Bad, pals = 0 ; double Start = UpSecs ;
LoopPP( Ln, fpLines ) { j = -1, Bad = 0, k = L = szStr( B ) - 1 ;
while ( ++j < k && !( Bad = B[ j ] != B[ k-- ] ) );
pals += L && !Bad, PP += !PP[1] ; }
Sh( L"%d words scanned and %d palindromes identified in %s seconds",
PP - BB - 1, pals, Prn( UpSecs - Start ) );
You are comparing C with Python, not fair...
--
bmaxa now listens wir brauchen dich #6 by Faust from Ravvivando
Branimir Maksimovic
2021-07-29 00:41:26 UTC
Permalink
Post by DFS
Who has a better algorithm?
=============================================================================
t0 = time.perf_counter()
ispal = 1;
k = len(word)-1
ispal=0
break
k-=1
pals+=1
t1 = time.perf_counter()
print('%d words scanned and %d palindromes identified in %.3f seconds' %
(len(words), pals, (t1 - t0)))
=============================================================================
words from
https://raw.githubusercontent.com/dwyl/english-words/master/words_alpha.txt
Program is not complete...
--
bmaxa now listens wir brauchen dich #6 by Faust from Ravvivando
Loading...