Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

occurence of repeated numbers group

by satya02 (Initiate)
on Jul 19, 2018 at 13:42 UTC ( [id://1218823]=perlquestion: print w/replies, xml ) Need Help??

satya02 has asked for the wisdom of the Perl Monks concerning the following question:

Please help me in finding occurence of repeated numbers group.

Example input:

1 2 5 9

1 2 5 10

4 5 10 12

5 9 10 11

output:-

1 2 repated 2 times

2 5 repeated 2 times

5 10 repeated 3 times

Replies are listed 'Best First'.
Re: occurence of repeated numbers group
by choroba (Cardinal) on Jul 19, 2018 at 13:57 UTC
    Using List::PowerSet:
    #!/usr/bin/env perl use warnings; use strict; use feature qw{ say }; use List::PowerSet qw{ powerset }; my %groups; while (<DATA>) { my @numbers = split; for my $ps (@{ powerset(@numbers) }) { next if @$ps < 2; $groups{"@$ps"}++; } } for my $group (keys %groups) { next if $groups{$group} == 1; say "$group repeated $groups{$group} times"; } __DATA__ 1 2 5 9 1 2 5 10 4 5 10 12 5 9 10 11

    Output:

    5 9 repeated 2 times 1 2 repeated 2 times 1 5 repeated 2 times 5 10 repeated 3 times 1 2 5 repeated 2 times 2 5 repeated 2 times
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: occurence of repeated numbers group
by tybalt89 (Monsignor) on Jul 19, 2018 at 14:20 UTC
    #!/usr/bin/perl # https://perlmonks.org/?node_id=1218823 use strict; use warnings; my %dups; local $_ = do { local $/; <DATA> }; /\b(\d+)\b.*?\b(\d+)\b(?{$dups{"$1 $2"}++})(*FAIL)/; $dups{$_} > 1 and print "$_ repeated $dups{$_} times\n" for sort keys +%dups; __DATA__ 1 2 5 9 1 2 5 10 4 5 10 12 5 9 10 11

    Outputs:

    1 2 repeated 2 times 1 5 repeated 2 times 2 5 repeated 2 times 5 10 repeated 3 times 5 9 repeated 2 times
Re: occurence of repeated numbers group
by hippo (Bishop) on Jul 19, 2018 at 13:51 UTC

    It is unclear where you are having problems. Is it:

    1. You have code which runs but doesn't do what you want - in which case, show your code.
    2. You have code which doesn't run - in which case, show your code and the precise text of the error message.
    3. You have an algorithm but are having trouble turning it into code - in which case, show your algorithm.
    4. You have no algorithm.
      Thank you for reply. I dont have code. I'm learning Perl and was able to count no of times a digit repeated in file , and now trying to do group (:D.
Re: occurence of repeated numbers group
by talexb (Chancellor) on Jul 19, 2018 at 14:15 UTC

    What you need to do is called 'software development', and it consists of two steps:

    1. Develop the logic to solve the problem you are facing; and
    2. Write software that replicates the logic you've developed.
    Are you stuck on step 1, or on step 2?

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Re: occurence of repeated numbers group
by BrowserUk (Patriarch) on Jul 19, 2018 at 19:32 UTC

    Assuming "repeated number groups" rather than pairs, and without modules so you can see the logic:

    #! perl -slw use strict; use Data::Dump qw[ pp ]; my %dups; while( <DATA> ) { my @nums = split ' ', $_; for my $l ( 2 .. @nums ) { ++$dups{ join ' ', @nums[ $_ .. $_+$l-1 ] } for 0 .. @nums - $ +l; } } #pp \%dups; $dups{ $_ } > 1 and print "$dups{ $_ } x '$_'" for sort keys %dups; __DATA__ 1 2 5 9 1 2 5 10 4 5 10 12 5 9 10 11

    Produces:

    C:\test>1218823.pl 2 x '1 2' 2 x '1 2 5' 2 x '2 5' 2 x '5 10' 2 x '5 9'

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
    In the absence of evidence, opinion is indistinguishable from prejudice. Suck that fhit
Re: occurence of repeated numbers group
by Anonymous Monk on Jul 19, 2018 at 13:54 UTC

    looks like 12 repates 3 times.

      Thank you. 12 is not '1 2'. I want to know how many times the same pattern ( group ) is repeated.

      1 2 5 9

      1 2 5 10

      4 5 10 12

      5 9 10 11

      output:-

      1 2 repated 2 times

      2 5 repeated 2 times

      5 10 repeated 3 times

      5 9 repated 2 times

      Thanks

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1218823]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (7)
As of 2024-04-23 13:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found