Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8688165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:17:24+00:00 2026-06-12T23:17:24+00:00

I have a need for a function that will do the following thing: If

  • 0

I have a need for a function that will do the following thing:

If I have a string like this "2 1 3 6 5 4 8 7" I have to insert dashes between pairs of numbers following some rules.

The rules are simple.

Put a dash between two numbers if the first one of the pair is smaller then the one that follows it. Do all possible combinations of this and if a pair already has a dash then the space next to it can’t have a dash.

Basically my results for above string would be

2 1-3 6 5 4 8 7
2 1-3 6 5 4-8 7
2 1 3-6 5 4 8 7
2 1 3-6 5 4-8 7
2 1 3 6 5 4-8 7

I did create a function that does this but I am thinking it is pretty sluggish and I don’t want to taint your ideas with it. If possible I would like to know how you guys are thinking about this and even some pseudo code or code would be great.

EDIT 1:
here is the code I have so far

$string = "2 1 3 6 5 4 8 7";

function dasher($string){
   global $dasherarray;
   $lockcodes = explode(' ', $string);

   for($i = 0; $i < count($lockcodes) - 1; $i++){
      if(strlen($string) > 2){
         $left = $lockcodes[$i];
         $right = $lockcodes[$i+1];
         $x = $left . ' ' . $right;
         $y = $left . '-' . $right;
         if (strlen($left) == 1 && strlen($right) == 1 && (int)$left < (int)$right) {
            $dashercombination = str_replace($x, $y, $string); 
            $dasherarray[] = $dashercombination;
            dasher($dashercombination);
         }
      }
   }
   return array_unique($dasherarray);
}

foreach(dasher($string) as $combination) {
   echo $combination. '<br>';
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:17:25+00:00Added an answer on June 12, 2026 at 11:17 pm

    Perhaps this will be helpful in terms of offering different methods to parse the string.

    $str="2 1 3 6 5 4 8 7";
    $sar=explode(' ',$str);
    for($i=1;$i<count($sar);$i++)
      if($sar[$i-1]<$sar[$i])
        print substr_replace($str,'-',2*($i-1)+1,1) . "\n";
    

    Note that the code expects only single digits numbers in the string.

    Note that the code expects that the string is formatted as per your example. It would be good to add some sanity checks (collapse multiple spaces, strip/trim blanks at the beginning/end).

    We can improve upon this by finding all the spaces in the string and using them to index substrings for comparison, still assuming that only a single spaces separates adjacent numbers.

    <?php
    $str="21 11 31 61 51 41 81 71";
    $letter=' ';
    #This finds the locations of all the spaces in the strings
    $spaces = array_keys(array_intersect(str_split($str),array($letter)));
    
    #This function takes a start-space and an end-space and finds the number between them.
    #It also takes into account the special cases that we are considering the first or 
    #last space in the string
    function ssubstr($str,$spaces,$start,$end){
        if($start<0)
            return substr($str,0,$spaces[$end]);
        if($end==count($spaces))
            return substr($str,$spaces[$start],strlen($str)-$spaces[$start]);
        return substr($str,$spaces[$start],$spaces[$end]-$spaces[$start]);
    }
    
    #This loops through all the spaces in the string, extracting the numbers on either side for comparison
    for($i=0;$i<count($spaces);$i++){
        $firstnum=ssubstr($str,$spaces,$i-1,$i);
        $secondnum=ssubstr($str,$spaces,$i,$i+1) . "\n";
        if(intval($firstnum)<intval($secondnum))
            print substr_replace($str,'-',$spaces[$i],1) . "\n";
    }
    
    ?>
    

    Note the explicit conversion to integers in order to avoid lexicographic comparison.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this jquery function that works, except I need to add something. I
I have need for a function pointer that takes two arguments and returns a
I have a function that i need to call on iframe mousemove(). But i
I have a function that takes an array of objects and I need to
I have a function that returns two values in a list. Both values need
I need to write a little ruby function that does word wrapping. I have
I need help with my C code. I have a function that sets a
OK, I have a function in C++ that I need to call from JavaScript,
I have a pretty large object that I need to pass to a function
I have a C function int func( int a, int* b) that I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.