Skip to main content
Topic solved
This topic has been marked as solved and requires no further attention.
Topic: Counting command or script (Read 1737 times) previous topic - next topic
0 Members and 2 Guests are viewing this topic.

Re: Counting command or script

Reply #15
 
 @nous I tried your perl command and it worked perfectly as a Swiss knife . I just tried @replabrobin cos python looks/sounds more friendly for me atm.

The only thing is when I issue your perl command like bellow
Code: [Select]
perl -F'' -le '$c{$_}++ for @F; print %c; %c=();' t | sed 's/[^0-9]/ /g' INPUT.txt > OUTPUT.txt
the shell throws 'no such file..' but   it shoots the right counts. So that error think it's because I don't perl it properly but  for reasons i don't quite get them rn 🤷🏻‍♂️

Anyway your perl command got saved  for later use in my  read only bash history file which means iz very gut 😋 ✅

Re: Counting command or script

Reply #16
The revealing conclusion is that calculation of a string pattern does not scale as I've expected.
There's a limit to how well an interpreted script can scale; after a certain point you need parallelization.
A quick'n'dirty Perl one-liner I tested on a 7-letter x 8 million times long string, 1 line file (which is 54MB) completes in 22 seconds. That's with splitting every 7nth character and counting occurrences, which is the computationally-expensive task. The Python script takes a little longer (29 seconds) on a pre-split version of the same file. A rewrite of the Perl version with fork() or MCE gives almost linear performance increase, in my 4c/8t CPU about 7 times faster and I imagine the same would apply to Python.

Re: Counting command or script

Reply #17
The only thing is when I issue your perl command like bellow
Code: [Select]
perl -F'' -le '$c{$_}++ for @F; print %c; %c=();' t | sed 's/[^0-9]/ /g' INPUT.txt > OUTPUT.txt
the shell throws 'no such file..' but   it shoots the right counts. So that error think it's because I don't perl it properly but  for reasons i don't quite get them rn 🤷🏻‍♂️
Your INPUT.txt is redundant (and missing, hence the error), the example reads from t; try the sorted version instead which as a bonus doesn't need sed:
Code: [Select]
perl -F'' -le '$,=" "; $c{$_}++ for @F; print sort{$b<=>$a} values %c; %c=();' INPUT.txt >OUTPUT.txt

Re: Counting command or script

Reply #18

Quote
Your INPUT.txt is redundant (and missing, hence the error), the example reads from t; try the sorted version instead which as a bonus doesn't need sed:

  Aw thank you very much @nous, that one gonna land on my bash_history as well. Tnx for making it more clear to me  🤩


Re: Counting command or script

Reply #19
 Speaking about perl. Found this script which looks pretty slick. I'm trying to optimizing it for example to make it output multiple text files ,not bigger than 5MB instead of a one huge 1 GB text file or more. I need to test a 9x9 string which would require to generate 9^9 variants. Also would be cool if I can make this perl  script take some breaks so to give more air bubbles to the cpu so it will not start catch fire  lol. Script looks like this.
Code: [Select]
use Algorithm::Combinatorics qw/tuples_with_repetition/;
print join(" ", map { "[@$_]" } tuples_with_repetition([qw/A B C/],2)), "\n";
Now the output of this gives square brackets like  [A B] and in rows. Is there  a way to get rid of those brackets + shoot them directly in a column format? Tnx and sorry if I'm abusing of you guys precious time 😬

EDIT: looks like I need perl-math-combinatorics  module, omg, didn't see that coming..It's only on the AUR

Re: Counting command or script

Reply #20
Now the output of this gives square brackets like  [A B] and in rows. Is there  a way to get rid of those brackets + shoot them directly in a column format?
Perhaps?
Code: [Select]
map { [@$_] }
No idea without the full script and the combi module. The square brackets are used to create an array reference, perhaps the double quotes retain the brackets too?

Re: Counting command or script

Reply #21

 Is there any easier way to write this formula in python or perl or whatever is more easy?

 Any of those three formulas basically are the same. Looks differently but does the same thing but I have no idea how they should look in python and I don't want to ask chatgpt either  :D

Tried to find a tool that converts mathematical formula to python but it seems there is no such a thing  ::)

The formula looks like in the image bellow. Any help is greatly appreciated 👍🏻 ☮




Re: Counting command or script

Reply #22
In pure Perl:
Code: [Select]
% perl -le 'sub fact{ $f=shift; $x=1; $x *= $_ for $x++..$f; return $x; } $T=13; $S=7; print (fact($T)/fact($T-($S-1)))*$S*($S-1)/2;'
1235520
Not even sure if it's correct. Also, doesn't check T and S for sanity.

Re: Counting command or script

Reply #23

 Thank you very much I'll check it and see how it works @nous is #1 🤩

Re: Counting command or script

Reply #24

 It seems that your perl script works but it solves only first part of the formula A[T,(S-1)] but has to multiply that with the second one

  S!/2(S-2)!  Thanks it seems it works but like I said would be nice if join into the perl party 🕺🏻 the second term 💃🏽 also


Re: Counting command or script

Reply #26
The one-liner follows the 3rd formula and it does calculate S(S-1)/2.


 Idk but the result gives only 13!/7! which is 1235520 that's the first formula factor A(13,6)

 The second is   6*7/2=21   

 The final result should be 1235520 × 21 = 25.945.920

 Your script must be doing gut only the first part but silently somehow discards the second, perhaps giving it the value of 1 instead of
 what it should be 21

I've quadrupled-triple checked swear on red 🫡


Re: Counting command or script

Reply #27
You're right, for some reason it stops before the second part, I put an extra pair of parentheses to help it:
Code: [Select]
perl -le 'sub fact{ $f=shift; $x=1; $x *= $_ for $x++..$f; return $x; } $T=13; $S=7; print ((fact($T)/fact($T-($S-1)))*$S*($S-1)/2);'

Re: Counting command or script

Reply #28
 
 Bingo, now it works as intended 👍🏻. Thank you @nous the King of Perl ✅. I'm gonna be asking for another implementation but first have to sort out what this script needs so we can just feed it with a T an S and the twos amount.

Another way is just sampling three of those a b c compare results and see if we need to scale up or down our twos amount from the string

Thanks once again, this is very helpful for my project 👍🏻 🤩


Re: Counting command or script

Reply #29
I'm only a novice, you'd be surprised if you saw what a real Perl monk can do with this swiss army chainsaw...