Sponsored Content
Top Forums Shell Programming and Scripting Script to find the available number possibilities Post 302939040 by drl on Saturday 21st of March 2015 12:26:29 PM
Old 03-21-2015
Hi.

Something similar using a CPAN module, adapted from the examples:
Code:
#!/usr/bin/env perl

# @(#) p3	Demonstrate module Math/Combinatorics.

use Math::Combinatorics;

my ( $min, $max ) = ( 1, 60 );
my @a1       = $min .. $max;
my @n        = @a1;
my $n        = 6;
my $combinat = Math::Combinatorics->new(
  count => $n,
  data  => [@n],
);

print "combinations of $n of [$min,$max]\n";
while ( my @combo = $combinat->next_combination ) {
  print join( ' ', @combo ) . "\n";
}

print "\n";

exit(0);

producing:
Code:
$ ./p3 | head -60 | specimen
Edges: 5:0:5 of 60 lines in file "-"
combinations of 6 of [1,60]
1 2 3 4 5 6
1 2 3 4 5 7
1 2 3 4 5 8
1 2 3 4 5 9
   ---
1 2 3 4 5 60
1 2 3 4 6 7
1 2 3 4 6 8
1 2 3 4 6 9
1 2 3 4 6 10

For an environment like:
Code:
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian 5.0.8 (lenny, workstation) 
perl 5.10.0
head (GNU coreutils) 6.10
specimen (local) 1.17

The module also can do permutations. See https://metacpan.org/pod/Math::Combinatorics for more information.

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

What can you do with unix what are the possibilities

Where do i get unix at (1 Reply)
Discussion started by: shinobikil
1 Replies

2. Shell Programming and Scripting

awk script to find the number of files

awk script to find the number of files in a directory with their date less than 15-oct-2006 please help (4 Replies)
Discussion started by: uni_ajay_r
4 Replies

3. Shell Programming and Scripting

script to find the average number or files?

Anyone has a script or command in UNIX that can take 4 to five different numbers and calculate the average? (2 Replies)
Discussion started by: bbbngowc
2 Replies

4. UNIX for Dummies Questions & Answers

Script to find the number of tab delimiters in a line

Hi, I need to find the number of tab delimiters in the first line of a file.So using word=`head -1 files.txt` I have extracted the first line of file into a variable word.It has 20 tab delimted columns.So can anyone help me in finding the number of delimiters? I am using csh and I am a... (5 Replies)
Discussion started by: poornimajayan
5 Replies

5. Shell Programming and Scripting

Script to find the average of a given column and also for specified number of rows??

Hi friends I have 100 files in my directory. Each file look like this.. Temp1 Temp2 Temp3 MAS 1 2 3 MAS 4 5 6 MAS 7 8 9 Delhi 10 11 12 Delhi 13 14 15 Delhi 16 17 ... (4 Replies)
Discussion started by: ks_reddy
4 Replies

6. Shell Programming and Scripting

Script to find the average of a given column and also for specified number of rows?

Hi Friends, In continuation to my earlier post https://www.unix.com/shell-programming-scripting/99166-script-find-average-given-column-also-specified-number-rows.html I am extending my problem as follows. Input: Column1 Column2 MAS 1 MAS 4 ... (2 Replies)
Discussion started by: ks_reddy
2 Replies

7. Shell Programming and Scripting

Find command possibilities

Using find command is the below possible ? find /home/abcd/****/efgh where the "****" can be any name/character. Thanks, Vasanth. (2 Replies)
Discussion started by: skcvasanth
2 Replies

8. Shell Programming and Scripting

Bash script to find the number of files and identify which ones are 0 bytes.

I am writing a bash script to find out all the files in a directory which are empty. I am running into multiple issues. I will really appreciate if someone can please help me. #!/bin/bash DATE=$(date +%m%d%y) TIME=$(date +%H%M) DIR="/home/statsetl/input/civil/test" ... (1 Reply)
Discussion started by: monasharma13
1 Replies

9. Shell Programming and Scripting

Shell script to search a pattern in a directory and output number of find counts

I need a Shell script which take two inputs which are 1) main directory where it has to search and 2) pattern to search within main directory all files (.c and .h files) It has to print number of pattern found in main directory & each sub directory. main dir --> Total pattern found = 5 |... (3 Replies)
Discussion started by: vivignesh
3 Replies

10. Shell Programming and Scripting

awk to find number in a field then print the line and the number

Hi I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field. The source file is pipe delimited and looks something like 1|net|ABC Letr1|1530||| 1|net|EXP_1040 ABC|1121||| 1|net|EXP_TG1224|1122||| 1|net|R_North|1123|||... (5 Replies)
Discussion started by: Mudshark
5 Replies
Math::Combinatorics(3pm)				User Contributed Perl Documentation				  Math::Combinatorics(3pm)

NAME
Math::Combinatorics - Perform combinations and permutations on lists SYNOPSIS
Available as an object oriented API. use Math::Combinatorics; my @n = qw(a b c); my $combinat = Math::Combinatorics->new(count => 2, data => [@n], ); print "combinations of 2 from: ".join(" ",@n)." "; print "------------------------".("--" x scalar(@n))." "; while(my @combo = $combinat->next_combination){ print join(' ', @combo)." "; } print " "; print "permutations of 3 from: ".join(" ",@n)." "; print "------------------------".("--" x scalar(@n))." "; while(my @permu = $combinat->next_permutation){ print join(' ', @permu)." "; } output: Or available via exported functions 'permute', 'combine', and 'factorial'. use Math::Combinatorics; my @n = qw(a b c); print "combinations of 2 from: ".join(" ",@n)." "; print "------------------------".("--" x scalar(@n))." "; print join(" ", map { join " ", @$_ } combine(2,@n))," "; print " "; print "permutations of 3 from: ".join(" ",@n)." "; print "------------------------".("--" x scalar(@n))." "; print join(" ", map { join " ", @$_ } permute(@n))," "; Output: combinations of 2 from: a b c ------------------------------ a b a c b c permutations of 3 from: a b c ------------------------------ a b c a c b b a c b c a c a b c b a Output from both types of calls is the same, but the object-oriented approach consumes much less memory for large sets. DESCRIPTION
Combinatorics is the branch of mathematics studying the enumeration, combination, and permutation of sets of elements and the mathematical relations that characterize their properties. As a jumping off point, refer to: http://mathworld.wolfram.com/Combinatorics.html This module provides a pure-perl implementation of nCk, nCRk, nPk, nPRk, !n and n! (combination, multiset, permutation, string, derangement, and factorial, respectively). Functional and object-oriented usages allow problems such as the following to be solved: combine - nCk http://mathworld.wolfram.com/Combination.html "Fun questions to ask the pizza parlor wait staff: how many possible combinations of 2 toppings can I get on my pizza?". derange - !n http://mathworld.wolfram.com/Derangement.html "A derangement of n ordered objects, denoted !n, is a permutation in which none of the objects appear in their "natural" (i.e., ordered) place." permute - nPk http://mathworld.wolfram.com/Permutation.html "Master Mind Game: ways to arrange pieces of different colors in a certain number of positions, without repetition of a color". Object-oriented usage additionally allows solving these problems by calling "new()" with a frequency vector: string - nPRk http://mathworld.wolfram.com/String.html "Morse signals: diferent signals of 3 positions using the two symbols - and .". $o = Math::Combinatorics->new( count=>3 , data=>[qw(. -)] , frequency=>[3,3] ); while ( my @x = $o->next_multiset ) { my $p = Math::Combinatorics->new( data=>@x , frequency=>[map{1} @x] ); while ( my @y = $p->next_string ) { #do something } } multiset/multichoose - nCRk http://mathworld.wolfram.com/Multiset.html "ways to extract 3 balls at once of a bag with 3 black and 3 white balls". $o = Math::Combinatorics->new( count=>3 , data=>[qw(white black)] , frequency=>[3,3] ); while ( my @x = $o->next_multiset ) { #do something } EXPORT the following export tags will bring a single method into the caller's namespace. no symbols are exported by default. see pod documentation below for method descriptions. combine derange multiset permute string factorial AUTHOR
Allen Day <allenday@ucla.edu>, with algorithmic contributions from Christopher Eltschka and Tye. Copyright (c) 2004-2005 Allen Day. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. ACKNOWLEDGEMENTS
A sincere thanks to everyone for helping to make this a better module. After initial development I've only had time to accept patches and improvements. Math::Combinatorics continues to be developed and improved by the community. Contributors of note include: For adding new features: Carlos Rica, David Coppit, Carlos Segre, Lyon Lemmens For bug reports: Ying Yang, Joerg Beyer, Marc Logghe, Yunheng Wang, Torsten Seemann, Gerrit Haase, Joern Behre, Lyon Lemmens, Federico Lucifredi BUGS
/ TODO Report them to the author. * Need more extensive unit tests. * tests for new()'s frequency argment * A known bug (more of a missing feature, actually) does not allow parameterization of k for nPk in permute(). it is assumed k == n. L</permute()> for details. You can work around this by making calls to both L</permute()> and L</combine()> * Lots of really interesting stuff from Mathworld.Wolfram.com. MathWorld rocks! Expect to see implementation of more concepts from their site, e.g.: http://mathworld.wolfram.com/BellNumber.html http://mathworld.wolfram.com/StirlingNumberoftheSecondKind.html http://mathworld.wolfram.com/Word.html * Other combinatorics stuff http://en.wikipedia.org/wiki/Catalan_number http://en.wikipedia.org/wiki/Stirling_number SEE ALSO
Set::Scalar Set::Bag String::Combination (alas misnamed, it actually returns permutations on a string). http://perlmonks.thepen.com/29374.html http://groups.google.com/groups?selm=38568F79.13680B86%40physik.tu-muenchen.de&output=gplain EXPORTED FUNCTIONS
combine() Usage : my @combinations = combine($k,@n); Function: implements nCk (n choose k), or n!/(k!*(n-k!)). returns all unique unorderd combinations of k items from set n. items in n are assumed to be character data, and are copied into the return data structure (see "Returns" below). Example : my @n = qw(a b c); my @c = combine(2,@n); print join " ", map { join " ", @$_ } @c; # prints: # b c # a c # a b Returns : a list of arrays, where each array contains a unique combination of k items from n Args : a list of items to be combined Notes : data is internally assumed to be alphanumeric. this is necessary to efficiently generate combinations of large sets. if you need combinations of non-alphanumeric data, or on data C<sort {$a cmp $b}> would not be appropriate, use the object-oriented API. See L</new()> and the B<compare> option. Identical items are assumed to be non-unique. That is, calling C<combine(1,'a','a') yields two sets: {a}, and {a}. See L</next_multiset() if this is not the desired behavior. derange() Usage : my @deranges = derange(@n); Function: implements !n, a derangement of n items in which none of the items appear in their originally ordered place. Example : my @n = qw(a b c); my @d = derange(@n); print join " ", map { join " ", @$_ } @d; # prints: # a c b # b a c # b c a # c a b # c b a Returns : a list of arrays, where each array contains a derangement of k items from n (where k == n). Args : a list of items to be deranged. Note : k should really be parameterizable. this will happen in a later version of the module. send me a patch to make that version come out sooner. Notes : data is internally assumed to be alphanumeric. this is necessary to efficiently generate combinations of large sets. if you need combinations of non-alphanumeric data, or on data C<sort {$a cmp $b}> would not be appropriate, use the object-oriented API. See L</new()>, and the B<compare> option. next_derangement() Usage : my @derangement = $c->next_derangement(); Function: get derangements for @data. Returns : returns a permutation of items from @data (see L</new()>), where none of the items appear in their natural order. repeated calls retrieve all unique derangements of @data elements. a returned empty list signifies all derangements have been iterated. Args : none. factorial() Usage : my $f = factorial(4); #returns 24, or 4*3*2*1 Function: calculates n! (n factorial). Returns : undef if n is non-integer or n < 0 Args : a positive, non-zero integer Note : this function is used internally by combine() and permute() permute() Usage : my @permutations = permute(@n); Function: implements nPk (n permute k) (where k == n), or n!/(n-k)! returns all unique permutations of k items from set n (where n == k, see "Note" below). items in n are assumed to be character data, and are copied into the return data structure. Example : my @n = qw(a b c); my @p = permute(@n); print join " ", map { join " ", @$_ } @p; # prints: # b a c # b c a # c b a # c a b # a c b # a b c Returns : a list of arrays, where each array contains a permutation of k items from n (where k == n). Args : a list of items to be permuted. Note : k should really be parameterizable. this will happen in a later version of the module. send me a patch to make that version come out sooner. Notes : data is internally assumed to be alphanumeric. this is necessary to efficiently generate combinations of large sets. if you need combinations of non-alphanumeric data, or on data C<sort {$a cmp $b}> would not be appropriate, use the object-oriented API. See L</new()>, and the B<compare> option. Identical items are assumed to be non-unique. That is, calling C<permute('a','a') yields two sets: {a,a}, and {a,a}. See L</next_string() if this is not the desired behavior. CONSTRUCTOR
new() Usage : my $c = Math::Combinatorics->new( count => 2, #treated as int data => [1,2,3,4] #arrayref or anonymous array ); Function: build a new Math::Combinatorics object. Returns : a Math::Combinatorics object Args : count - required for combinatoric functions/methods. number of elements to be present in returned set(s). data - required for combinatoric B<AND> permutagenic functions/methods. this is the set elements are chosen from. B<NOTE>: this array is modified in place; make a copy of your array if the order matters in the caller's space. frequency - optional vector of data frequencies. must be the same length as the B<data> constructor argument. These two constructor calls here are equivalent: $a = 'a'; $b = 'b'; Math::Combinatorics->new( count=>2, data=>[$a,$a,$a,$a,$a,$b,$b] ); Math::Combinatorics->new( count=>2, data=>[$a,$b], frequency=>[5,2] ); so why use this? sometimes it's useful to have multiple identical entities in a set (in set theory jargon, this is called a "bag", See L<Set::Bag>). compare - optional subroutine reference used in sorting elements of the set. examples: #appropriate for character elements compare => sub { $_[0] cmp $_[1] } #appropriate for numeric elements compare => sub { $_[0] <=> $_[1] } #appropriate for object elements, perhaps compare => sub { $_[0]->value <=> $_[1]->value } The default sort mechanism is based on references, and cannot be predicted. Improvements for a more flexible compare() mechanism are most welcome. OBJECT METHODS
next_combination() Usage : my @combo = $c->next_combination(); Function: get combinations of size $count from @data. Returns : returns a combination of $count items from @data (see L</new()>). repeated calls retrieve all unique combinations of $count elements. a returned empty list signifies all combinations have been iterated. Note : this method may only be used if a B<frequency> argument is B<NOT> given to L</new()>, otherwise use L</next_multiset()>. Args : none. next_multiset() Usage : my @multiset = $c->next_multiset(); Function: get multisets for @data. Returns : returns a multiset of items from @data (see L</new()>). a multiset is a special type of combination where the set from which combinations are drawn contains items that are indistinguishable. use L</next_multiset()> when a B<frequency> argument is passed to L</new()>. repeated calls retrieve all unique multisets of @data elements. a returned empty list signifies all multisets have been iterated. Note : this method may only be used if a B<frequency> argument is given to L</new()>, otherwise use L</next_combination()>. Args : none. next_permutation() Usage : my @permu = $c->next_permutation(); Function: get permutations of elements in @data. Returns : returns a permutation of items from @data (see L</new()>). repeated calls retrieve all unique permutations of @data elements. a returned empty list signifies all permutations have been iterated. Note : this method may only be used if a B<frequency> argument is B<NOT> given to L</new()>, otherwise use L</next_string()>. Args : none. next_string() Usage : my @string = $c->next_string(); Function: get strings for @data. Returns : returns a multiset of items from @data (see L</new()>). a multiset is a special type of permutation where the set from which combinations are drawn contains items that are indistinguishable. use L</next_permutation()> when a B<frequency> argument is passed to L</new()>. repeated calls retrieve all unique multisets of @data elements. a returned empty list signifies all strings have been iterated. Note : this method may only be used if a B<frequency> argument is given to L</new()>, otherwise use L</next_permutation()>. Args : none. INTERNAL FUNCTIONS AND METHODS
sum() Usage : my $sum = sum(1,2,3); # returns 6 Function: sums a list of integers. non-integer list elements are ignored Returns : sum of integer items in arguments passed in Args : a list of integers Note : this function is used internally by combine() compare() Usage : $obj->compare() Function: internal, undocumented. holds a comparison coderef. Returns : value of compare (a coderef) count() Usage : $obj->count() Function: internal, undocumented. holds the "k" in nCk or nPk. Returns : value of count (an int) data() Usage : $obj->data() Function: internal, undocumented. holds the set "n" in nCk or nPk. Returns : value of data (an arrayref) swap() internal, undocumented. reverse() internal, undocumented. rotate() internal, undocumented. upper_bound() internal, undocumented. lower_bound() internal, undocumented. _permutation_cursor() Usage : $obj->_permutation_cursor() Function: internal method. cursor on permutation iterator order. Returns : value of _permutation_cursor (an arrayref) Args : none perl v5.10.1 2006-12-12 Math::Combinatorics(3pm)
All times are GMT -4. The time now is 03:37 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy