Greater than less than greater than something fishy

48

3

Given a length N string of less-than and greater-than signs (<, >), insert the integers 0 through N at the start and end and in between each pair of signs such that all the inequalities are satisfied. Output the resulting string. If there are multiple valid outputs, output any one (and just one) of them.

For example

<<><><<

has 7 characters so all the numbers from 0 to 7 inclusive must be inserted. A valid output is

2<3<4>1<5>0<6<7

because all the inequalities taken one at a time

2<3
3<4
4>1
1<5
5>0
0<6
6<7

are true.

If desired, the output may have spaces surrounding the signs, e.g. 2 < 3 < 4 > 1 < 5 > 0 < 6 < 7.

The shortest code in bytes wins.

Test Cases

The first line after an empty line is the input and the next line(s) are each valid output examples.

[empty string]
0

<
0<1

>
1>0

<<
0<1<2

<>
1<2>0

><
1>0<2
2>0<1

>>
2>1>0

<<<
0<1<2<3

><>
1>0<3>2

>><
3>2>0<1
3>1>0<2
2>1>0<3

>>>
3>2>1>0

>>><<<
3>2>1>0<4<5<6
6>3>1>0<2<4<5
4>2>1>0<3<5<6
4>3>1>0<2<5<6

<<><><<
2<3<4>1<5>0<6<7

>><><>>
7>6>0<5>1<4>3>2

<<<<<<<<<<<<<<
0<1<2<3<4<5<6<7<8<9<10<11<12<13<14

>><<<<><>><><<
6>5>4<7<8<9<10>3<11>2>1<12>0<13<14
14>5>4<7<8<9<10>3<11>2>1<12>0<13<6

Calvin's Hobbies

Posted 2016-09-06T21:46:52.400

Reputation: 84 000

4Will there always be a valid output? – mbomb007 – 2016-09-06T21:51:09.363

3@mbomb007 Yes. There always is at least one. – Calvin's Hobbies – 2016-09-06T21:52:25.813

23I want to see someone program this in ><>! That would be awesome (and ironic I guess?) – Soren – 2016-09-06T23:25:07.370

This was a really fun but simply challenge, thanks op – Shaun Wild – 2016-09-07T15:00:14.553

Not sure if you're listing all valid outputs or just some example possible outputs, but if the first, test case <> can also be 0<2>1 as possible valid output. – Kevin Cruijssen – 2020-01-16T11:23:31.160

Answers

32

Retina, 20 bytes

Byte count assumes ISO 8859-1 encoding.


$.'
S`>
%O#`\d+
¶
>

Try it online! (The first line enables a linefeed-separated test-suite.)

Explanation

A simple way to find a valid permutation is to start by inserting the numbers from 0 to N in order, and then to reverse the numbers surrounding each substring of >s. Take <><<>>><< as an example:

0<1>2<3<4>5>6>7<8<9
  ---   -------      these sections are wrong, so we reverse them
0<2>1<3<7>6>5>4<8<9

Both of those tasks are fairly simple in Retina, even though all we can really work with are strings. We can save an additional byte by inserting the numbers from N down to 0 and reversing the sections surrounding < instead, but the principle is the same.

Stage 1: Substitution


$.'

We start by inserting the length of $' (the suffix, i.e. everything after the match) into every possible position in the input. This inserts the numbers from N down to 0.

Stage 2: Split

S`>

We split the input around > into separate lines, so each line is either an individual number or a list of numbers joined with <.

Stage 3: Sort

%O#`\d+

Within each line (%) we sort (O) the numbers (\d#) by their numerical value (#). Since we inserted the number in reverse numerical order, this reverses them.

Stage 4: Substitution

¶
>

We turn the linefeeds into > again to join everything back into a single line. That's it.

As a side note, I've been meaning to add a way to apply % to other delimiters than linefeeds. Had I already done that, this submission would have been 14 bytes, because then the last three stages would have been reduced to a single one:

%'>O#`\d+

Martin Ender

Posted 2016-09-06T21:46:52.400

Reputation: 184 808

How is that like one eigth my size? Nice work. – ThreeFx – 2016-09-06T22:14:48.973

@ThreeFx Because I don't use brute force. ;) Explanation coming in a minute. – Martin Ender – 2016-09-06T22:15:29.633

27

><>, 46 43 35 + 4 for  -s= = 39 bytes

0&l?!v:3%?\&:n1+$o!
+nf0.>&n; >l&:@

This is an implementation of xnor's algorithm in ><>.

It takes the input string on the stack (-s flag with the standard interpreter).

You can try it out on the online interpreter.

Aaron

Posted 2016-09-06T21:46:52.400

Reputation: 3 689

3ockquote>

<> seems like a fitting language for this challenge.

– anaximander – 2016-09-08T10:35:45.877

24

><>, 26 + 4 = 30 bytes

l22pirv
1+$2po>:3%::2g:n$-

Try it online! +4 bytes for the -s= flag - if just -s is okay (it would mean that the flag would need to be dropped entirely for empty input), then that would be +3 instead.

Assumes that STDIN input is empty so that i produces -1 (which it does on EOF). The program errors out trying to print this -1 as a char.

Uses the max-of-nums-so-far-for->, min-of-nums-so-far-for-< approach.

[Setup]
l22p         Place (length of stack) = (length of input) into position (2, 2) of
             the codebox. Codebox values are initialised to 0, so (0, 2) will
             contain the other value we need.
i            Push -1 due to EOF so that we error out later
r            Reverse the stack
v            Move down to the next line
>            Change IP direction to rightward

[Loop]
:3%          Take code point of '<' or '>' mod 3, giving 0 or 2 respectively
             (call this value c)
:            Duplicate
:2g          Fetch the value v at (c, 2)
:n           Output it as a number
$-1+         Calculate v-c+1 to update v
$2p          Place the updated value into (c, 2)
o            Output the '<' or '>' as a char (or error out here outputting -1)

A program which exits cleanly and does not make the assumption about STDIN is 4 extra bytes:

l22p0rv
p:?!;o>:3%::2g:n$-1+$2

Sp3000

Posted 2016-09-06T21:46:52.400

Reputation: 58 729

15

CJam, 16 bytes

l_,),.\'</Wf%'<*

Try it online!

A port of my Retina answer.

Explanation

l    e# Read input.
_,   e# Duplicate, get length N.
),   e# Get range [0 1 2 ... N].
.\   e# Riffle input string into this range.
'</  e# Split around '<'.
Wf%  e# Reverse each chunk.
'<*  e# Join with '<'.

Martin Ender

Posted 2016-09-06T21:46:52.400

Reputation: 184 808

11

Perl, 29 bytes

Includes +2 for -lp

Run with input on STDIN, e.g.

order.pl <<< "<<><><<"

Output:

0<1<7>2<6>3<4<5

order.pl:

#!/usr/bin/perl -lp
s%%/\G</?$a++:y///c-$b++%eg

Explanation

Have two counters, max starting with the string length, min starting with 0. Then at each boundary (including start and end of string) if it is just before a < put the minimum there and increase by 1, otherwise put the maximum there and decrease by 1 (at the end of the string it doesn't matter which counter you take since they are both the same)

Ton Hospel

Posted 2016-09-06T21:46:52.400

Reputation: 14 114

s{}{/\G/...} I've never seen that before, it's brilliant. – primo – 2016-09-11T02:40:23.983

This is an old challenge, but you can shave a few bytes by adding a -F switch and using 1+$#F-- instead of y///c-$b++: Try it online!

– Xcali – 2020-02-19T22:50:25.647

10

Python 2, 163 137 bytes

from random import*
def f(v):l=len(v)+1;N=''.join(sum(zip(sample(map(str,range(l)),l),v+' '),()));return N if eval(N)or len(v)<1else f(v)

Shuffles the numbers until the statement evals to True.

Try it.

atlasologist

Posted 2016-09-06T21:46:52.400

Reputation: 2 945

1This is clearly the most sensible of all answers. – moopet – 2016-09-08T08:59:38.660

10

Python 2, 67 bytes

f=lambda s,i=0:s and`i+len(s)*(s>'=')`+s[0]+f(s[1:],i+(s<'>'))or`i`

A recursive function. Satisfies each operator in turn by putting the smallest unused value x for x< and greatest for x>. The smallest unused value is stored in i and updated, and the largest unused value is inferred from i and the remaining length.

xnor

Posted 2016-09-06T21:46:52.400

Reputation: 115 687

1I think you could do (s>'=') instead of (s>='>') to save a byte? – mathmandan – 2016-09-09T02:09:54.513

@mathmandan Thanks! It's weird that < and > are not consecutive codepoints. – xnor – 2016-09-10T19:00:45.797

Agreed! But I guess I can see how it would make sense to have = between < and >. – mathmandan – 2016-09-11T17:11:37.043

8

APL, 33 bytes

⍞←(S,⊂''),.,⍨-1-⍋⍋+\0,1-2×'>'=S←⍞

⍋⍋ is unusually useful.

Explanation

⍞←(S,⊂''),.,⍨-1-⍋⍋+\0,1-2×'>'=S←⍞
                                   ⍞ read a string from stdin      '<<><><<'
                                 S←   store it in variable S
                             '>'=     test each character for eq.   0 0 1 0 1 0 0
                         1-2×         1-2×0 = 1, 1-2×1 = ¯1         1 1 ¯1 1 ¯1 1 1
                                      (thus, 1 if < else ¯1)
                       0,             concatenate 0 to the vector   0 1 1 ¯1 1 ¯1 1 1
                     +\               calculate its running sum     0 1 2 1 2 1 2 3
                   ⍋                 create a vector of indices    1 2 4 6 3 5 7 8
                                      that sort the vector in
                                      ascending order
                 ⍋                   do it again: the compound ⍋⍋ 1 2 5 3 6 4 7 8
                                      maps a vector V to another
                                      vector V', one permutation of
                                      the set of the indices of V,
                                      such that
                                            V > V  => V' > V'.
                                             i   j     i    j
                                      due to this property, V and V'
                                      get sorted in the same way:
                                          ⍋V = ⍋V' = ⍋⍋⍋V.
              -1-                     decrement by one              0 1 4 2 5 3 6 7
      ⊂''                            void character vector         ⊂''
    S,                                concatenate input string     '<<><><<' ⊂''
   (     ),.,⍨                       first concatenate each        0 '<' 1 '<' 4 '>' 2 \
                                     element of the result vector  '<' 5 '>' 3 '<' 6 '<' \
                                     with the cordisponding        7 ⊂''
                                     element in the input string,
                                     then concatenate each result
⍞←                                  write to stdout

Oberon

Posted 2016-09-06T21:46:52.400

Reputation: 2 881

3What do the Christmas trees (⍋⍋) do? – Conor O'Brien – 2016-09-06T22:58:31.683

is grade up, which returns indicies in sorted order. By doing it twice, you get 1 where the smallest number was, 2 where the next smallest number was, ect. – Zwei – 2016-09-06T23:23:24.933

@ConorO'Brien edited with a short explanation. – Oberon – 2016-09-06T23:28:08.980

Yes, very short ._. – Conor O'Brien – 2016-09-06T23:44:48.120

7

JavaScript (ES6), 74 56 bytes

s=>s.replace(/./g,c=>(c<'>'?j++:l--)+c,j=0,l=s.length)+j

Starts with the set of numbers 0...N. At each stage simply takes the greatest (l) or least (j) of the remaining numbers; the next number must by definition be less than or greater than that. Edit: Saved a massive 18 bytes thanks to @Arnauld.

Neil

Posted 2016-09-06T21:46:52.400

Reputation: 95 035

3Can you use replace? Maybe s=>s.replace(/./g,c=>(c<'>'?j++:l--)+c,j=0,l=s.length)+j – Arnauld – 2016-09-07T00:29:45.347

@Arnauld ...and I thought I was doing well to golf my first attempt (which wasn't amenable to replacement by replace) down to 74 bytes... – Neil – 2016-09-07T07:48:35.770

6

Pyth - 19 bytes

Hooray for comparison chaining!

!QZhv#ms.idQ.p`Mhl

Doesn't work online cuz of eval safety.

Maltysen

Posted 2016-09-06T21:46:52.400

Reputation: 25 023

4

2sable, 20 bytes

gUvy'<Qi¾¼ëXD<U}y}XJ

Explanation

gU                     # store input length in variable X
  v              }     # for each char in input
   y'<Qi               # if current char is "<"
        ¾¼             # push counter (initialized as 0), increase counter
          ëXD<U}       # else, push X and decrease value in variable X
                y      # push current char
                  XJ   # push the final number and join the stack

Try it online!

For N<10 this could have been 14 bytes:

ÎvyN>}J'<¡í'<ý

Emigna

Posted 2016-09-06T21:46:52.400

Reputation: 50 798

4

C#, 102 99 bytes

string f(string s){int i=0,j=s.Length;var r="";foreach(var c in s)r=r+(c<61?i++:j--)+c;return r+i;}

Ungolfed:

string f(string s)
{
    int i = 0, j = s.Length;    // Used to track the lowest and highest unused number.
    var r = "";                 // Start with the empty string.

    foreach (var c in s)        // For each character in the input string:
        r = r +                 // Append to the result:
            (c < 61             // If the current character is '<':
                ? i++           // Insert the lowest unused number,
                : j--)          // otherwise, insert the highest unused number.
            + c;                // And append the current character.

    return r + i;               // Return the result with the last unused number appended.
}

Scepheo

Posted 2016-09-06T21:46:52.400

Reputation: 466

I'm tired, so I could be missing something, but wouldn't changing the r = r + part to a compound assignment save a couple bytes? – Carcigenicate – 2016-09-07T15:28:23.203

2No - the r+ part on the right-hand side tells the compiler the entire thing is a string, so the string representation of c is used. If I used r+=, the ?: part would evaluate to an int, the ordinal value of c would be added to that and only then would it be converted to its string representation. – Scepheo – 2016-09-07T21:10:18.597

4

Jelly, 27 14 12 bytes

Port of @Martin Enders CJam solution
-2 bytes thanks to @Dennis

żJ0;µFṣ”<Uj”<

Test it at TryItOnline

How?

żJ0;Fṣ”<Uj”< - main link takes an argument, the string, e.g. ><>
 J           - range(length(input)), e.g. [1,2,3]
  0          - literal 0
   ;         - concatenate, e.g. [0,1,2,3]
ż            - zip with input, e.g. [[0],">1","<2",">3"]
    F        - flatten, list, e.g. "0>1<2>3"
      ”<  ”< - the character '<'
     ṣ       - split, e.g. ["0>1","2>3"]
        U    - upend (reverse) (implicit vectorization), e.g. ["1>0","3>2"]
         j   - join, e.g. "1>0<3>2"

Previous method was interesting mathematically, but not so golfy...

=”>U
LR!×ÇĖP€S‘
L‘ḶŒ!ị@ÇðżF

This uses the factorial base system to find an index of the permutations of [0,N] that will satisfy the equation.

Jonathan Allan

Posted 2016-09-06T21:46:52.400

Reputation: 67 804

1U vectorizes, so you don't need . żJ0; saves another byte. – Dennis – 2016-09-07T15:16:53.193

4

Java 8, 126 125 bytes

s->{int t=s.replaceAll("<","").length(),y=t-1;String r=t+++"";for(char c:s.toCharArray())r+=(c+"")+(c<61?t++:y--);return r;};

I don't think this even works hehe

Ungolfed test program

public static void main(String[] args) {
    Function<String, String> function = s -> {
        int t = s.replaceAll("<", "").length(), y = t - 1;
        String r = t++ + "";
        for (char c : s.toCharArray()) {
            r += (c + "") + (c < 61 ? t++ : y--);
        }
        return r;
    };

    System.out.println(function.apply("<<><><<"));
    System.out.println(function.apply(">>><<<"));
    System.out.println(function.apply(">>>"));
    System.out.println(function.apply("<<<"));
    System.out.println(function.apply(">><><>>"));
}

Shaun Wild

Posted 2016-09-06T21:46:52.400

Reputation: 2 329

You can change the .replaceAll to .replace and remove the parenthesis around (c+"") to save 5 bytes. – Kevin Cruijssen – 2016-09-08T14:38:51.997

@KevinCruijssen Not arsed about 5 bytes hahah. – Shaun Wild – 2016-09-09T14:33:45.197

When using a proper golfing language, 5 bytes is the difference between 5th and 2nd place. With java it's the difference between last place by far and just last place. – Shaun Wild – 2016-09-12T08:17:19.020

Java will almost always be last with code-golfing challenges, but the reason we post Java answers to begin with is for the fun of writing it as short as possible. I personally am already happy if my Java code goes from 500 to 499 in terms of byte. ;P – Kevin Cruijssen – 2016-09-12T09:21:27.980

We basically ignore all over competitors and just compete with or Java/ C# submissions etc.. – Shaun Wild – 2016-09-12T11:07:01.157

4

Clojure, 152 132 126 bytes

(defn f[s](loop[l 0 h(count s)[c & r]s a""](if c(case c\<(recur(inc l)h r(str a l c))(recur l(dec h)r(str a h c)))(str a l))))

Saved a fair number of bytes by eliminating as much whitespace as I could. I realized whitespace isn't necessary to separate a parenthesis from another character.

Basically a Clojure port of @Scepheo's answer. Works identically.

Those recur calls are killer! I suppose I could have used atoms to clean it up. The swap! calls required to use atoms added to the count :/

Thanks to @amalloy for saving me a few bytes.

Ungolfed:

(defn comp-chain [chain-str]
  (loop [l 0 ; Lowest number
         h (count chain-str) ; Highest number
         [c & cr] chain-str ; Deconstruct the remaining list
         a ""] ; Accumulator
    (if c ; If there's any <>'s left
      (if (= c \<) ; If current char is a <...
        (recur (inc l) h cr (str a l c)) ; Add l to a, and inc l
        (recur l (dec h) cr (str a h c))) ; Add h to a, and dec h
      (str a l)))) ; Add on the remaining lowest number, and return

Carcigenicate

Posted 2016-09-06T21:46:52.400

Reputation: 3 295

Welcome to the site! – James – 2016-09-07T16:34:39.090

@DJMcMayhem Thanks. Hopefully next time I'll be able to come up with my own solution instead of just porting another answer. – Carcigenicate – 2016-09-07T16:36:22.413

You can save two more spaces in the loop binding, before s and after a. You could also shave a bit by replacing the if tree with a case: (case c \< (recur ...) nil (str ...) (recur ...)). And of course cr could be a shorter name. – amalloy – 2016-09-09T04:22:39.110

@amalloy Good points, thank you. I'll update when I get on my laptop. – Carcigenicate – 2016-09-09T13:33:37.967

3

Haskell, 162 bytes

import Data.List
(a:b)%(c:d)=show c++a:b%d
_%[x]=show x
f l=map(l%).filter(l#)$permutations[0..length l]
(e:f)#(x:y:z)=(e!x)y&&f#(y:z)
_#_=0<1
'>'!x=(>)x
_!x=(<)x

This is friggin' long.

ThreeFx

Posted 2016-09-06T21:46:52.400

Reputation: 1 435

3

Perl (107 + 1 for -p) 108

for$c(split''){$s.=$i++.$c;}
for$b(split'<',$s.$i){$h[$j]=join'>',reverse split'>',$b;$j++;}
$_=join'<',@h;

Algorithm stolen from Martin Ender♦'s answer

Riley

Posted 2016-09-06T21:46:52.400

Reputation: 11 345

2

05AB1E, 13 12 bytes

õª€N'<¡íJ'<ý

Port of @MartinEnder's CJam answer, so make sure to upvote him as well!
-1 byte thanks to @Grimmy.

Try it online or verify all test cases.

Explanation:

õª            # Append an empty string to the (implicit) input-string as list,
              # which also converts the input-string to a list of characters
              #  i.e. "><>" → [">","<",">",""]
  €N          # Insert the 0-based index before each character in this list
              #  → [0,">",1,"<",2,">",3,""]
    '<¡      '# Split this list on "<"
              #  → [[0,">",1,],[2,">",3,""]]
       í      # Reverse the items of each inner list
              #  → [[1,">",0],["",3,">",2]]
        J     # Join each inner list into a string
              #  → ["1>0","3>2"]
         '<ý '# And join this list of strings with "<" delimiter
              #  → "1>0<3>2"
              # (after which the resulting string is output implicitly as result)

Kevin Cruijssen

Posted 2016-09-06T21:46:52.400

Reputation: 67 575

€NIgª => õª€N for -1: TIO. Ć€N¨ also works. – Grimmy – 2020-02-18T22:28:19.953

@Grimmy Thanks, smart golfs. – Kevin Cruijssen – 2020-02-18T22:45:51.187

2

Python 2, 176 172 bytes

It's not very short compared to the others, but I'm happy that I solved it so quickly.

from itertools import*
def f(s):
 for p in permutations(range(len(s)+1)):
    n=list(s);p=list(p);t=[p.pop()]+list(chain(*zip(n,p)));r="".join(map(str,t))
    if eval(r):return r

Try it online

Ungolfed:

from itertools import*
def f(s):
    n=list(s);R=range(len(s)+1)
    for p in permutations(R):
        p=list(p)
        r=[p.pop()]
        t=n+p
        t[::2]=n
        t[1::2]=p
        r="".join(map(str,r+t))
        if eval(r):return r

Try it online

mbomb007

Posted 2016-09-06T21:46:52.400

Reputation: 21 944

the interlace part can prolly be much shorter done through zip – Maltysen – 2016-09-06T22:44:33.150

@Maltysen Not a ton shorter, because the lists aren't the same length (I still have to pop), but it is a bit shorter. If N<10, I could make stringifying shorter. – mbomb007 – 2016-09-07T13:38:14.210

2

Ruby, 135 bytes

g=gets
puts g.nil?? 0:[*0..s=g.size].permutation.map{|a|a.zip(g.chars)*""if s.times.map{|i|eval"%s"*3%[a[i],g[i],a[i+1]]}.all?}.compact

Note: Time complexity is large (O(n!)).

cia_rana

Posted 2016-09-06T21:46:52.400

Reputation: 441

1

Javascript (node.js), 94 bytes

Try it online

s=>[[0],...s].reduce((x,v,i)=>v>'<'?[...x.map(v=>v+1),0]:[...x,i]).reduce((x,v,i)=>x+s[i-1]+v)

Ungolfed:

s =>
  [[0],...s].reduce((x, v, i) =>
    v > '<' ?
      [...x.map(v => v + 1), 0] :
      [...x, i])
    .reduce((x, v, i) => x + s[i-1] + v)

begolf123

Posted 2016-09-06T21:46:52.400

Reputation: 531

1

PHP , 190 Bytes

random shuffle till a valid solution exists

$x=range(0,$l=strlen($q=$argv[1]));while(!$b){$b=1;$t="";shuffle($x);for($i=0;$i<$l;){$t.=$x[$i].$q[$i];if(($q[$i]==">"&$x[$i]<$x[$i+1])|($q[$i]=="<"&$x[$i]>$x[1+$i++]))$b=0;}}echo$t.$x[$i];

381 Bytes get all solutions and pick one

<?php $d=range(0,strlen($q=$argv[1]));echo $q."\n";$e=[];function f($t=""){global$e,$d,$q;foreach($d as$z){preg_match_all("#\d+#",$t,$y);if(in_array($z,$y[0]))continue;$p=preg_match_all("#[<>]#",$t);$g="";if(preg_match("#\d+$#",$t,$x)){if(($q[$p]==">"&$x[0]<$z)|($q[$p]=="<"&$x[0]>$z))continue;$g=$q[$p];}strlen($q)==$p+1|!$q?$e[]=$t.$g.$z:f($t.$g.$z);}}f();echo$e[array_rand($e)];

Jörg Hülsermann

Posted 2016-09-06T21:46:52.400

Reputation: 13 026

0

Burlesque, 38 bytes

sarzPp{J'>=={PPl_}{PPg_j}x/iePp}]mPP?+

Try it online!

Using xnor's algorithm

sarz                      # Generate range [0,N_comp]
Pp                        # Push this into state stack
{
 J'>==                    # Non destructive test if is ">"
 {PPl_}{PPg_j}x/ie        # If it is, take the head of the range
                          # Else take the tail
 Pp                       # Push the remainder back to the state stack
}]m                       # Apply to each character and map result to string
PP                        # Take the remaining element from state stack
?+                        # Append it to the string

Burlesque, 61 bytes

)'.2co)PIsarzr@{2CO}m[jbcz[{**FL(&&)[+}^m:e!{~]3co{x/}^mgn}m[

Try it online!

A method using actually evaluating permutations of ints, longer partially due to trying to format the final answer sensibly.

)'.     # Add a "." before each character (.> is comparison in Burlesque)
2co)PI  # Split into comparison operators
sarzr@  # Create all permutations of [0,N_comp]
{2CO}m[ # Create ngrams of length 2 ({abc} -> {ab bc}
jbcz[   # Zip with the operators
{
 **FL   # Merge to form an operation of the form {a b .> b c .<}
 (&&)[+ # Append a bitwise and
}^m     # Apply to each element
:e!     # Filter for those which when evaluated produce true
{
 ~]     # Drop last element (&&)
 3co    # Split into chunks of three
 {x/}^m # Cycle and push each block
 gn     # Delete duplicate consecutive elements
}m[     # Apply to each group

DeathIncarnate

Posted 2016-09-06T21:46:52.400

Reputation: 916

0

Haskell, 95 bytes

s=show
('<':y)!(a:b)=s a++'<':y!b
(x:y)!z=s(last z)++x:y!init z
_!(y:_)=s y
f x=x![0..length x]

Try it online!

david

Posted 2016-09-06T21:46:52.400

Reputation: 180

0

PHP, 165 156 110 bytes

$o=[$j=$i=$s=$m=0];for(;$i<strlen($n=$argn);)$o[]=$n[$i++]=='<'?++$m:--$s;for(;$j<=$i;)echo$o[$j]-$s,$n[$j++];

Try it online!

Ungolfed

<?php

$min=0;
$max=0;
$current=0;

$order = [0];

for ($i=0;$i<strlen($argn);$i++){
    $compare=$argn[$i];
    if ($compare=='<') {
        $max++;
        $current=$max;
    } else {
        $min--;
        $current=$min;
    }
    $order[] = $current;
}

for ($i=0;$i<=strlen($argn);$i++){
    echo ($order[$i]-$min);
    if ($i<strlen($argn)) echo $argn[$i];
}

Guillermo Phillips

Posted 2016-09-06T21:46:52.400

Reputation: 561

0

Excel (Ver. 1911), 199 Bytes

Using iterative calculation (max iterations set to 1024)

B2 'Input
B3 =IF(B3=4^5,1,B3+1)
B4 =B3-1
C2 =SEQUENCE(LEN(B2))
D2 =ROW(C2#:C1)-1
E2 =IF(B4,IF(C2#=B4,IF(F2#=">",@SORT(G2#,,-1),G2),E2#),-C2#)
F2 =MID(B2,C2#,1)
G2 =FILTER(D2#,NOT(MMULT(N(D2#=TRANSPOSE(E2#)),C2#/C2#)))
H2 =CONCAT(E2#:F2#,G2) ' Output

Explanation

B2 'Input
B3 =IF(B3=4^5,1,B3+1) ' Keeps track of iteration number
B4 =B3-1 ' 0 index the iteration counter
C2 =SEQUENCE(LEN(B2)) ' Generate sequence to index input string
D2 =ROW(C2#:C1)-1 ' Create sequence [0..N]
E2 =IF(B4,IF(C2#=B4,IF(F2#=">",@SORT(G2#,,-1),G2),E2#),-C2#)
      ' On each iteration check if the current char is ">" pull the largest unused number,
      ' else pull the smallest
F2 =MID(B2,C2#,1) ' Break the input string into separate rows
G2 =FILTER(D2#,NOT(MMULT(N(D2#=TRANSPOSE(E2#)),C2#/C2#))) ' Set of unused numbers
H2 =CONCAT(E2#:F2#,G2) ' Concatenate numbers, gt/lt signs, and last unused number

Test Sample

test sample

begolf123

Posted 2016-09-06T21:46:52.400

Reputation: 531