Sort digits by their first occurrence in pi

17

1

Given a non-negative number n, sort the digits of n by their first occurrence in pi.

Input can be taken via function cli argument, or STDIN and as a string, char[] or integer. You may output via return value, exit status or STDOUT.

Roman Gräf

Posted 2017-04-29T16:13:12.357

Reputation: 2 915

Related – Stewie Griffin – 2017-04-29T16:38:42.010

Can we take input and output as strings, or as arrays of digits? – ETHproductions – 2017-04-29T16:39:07.847

@ETHproductions Clarified. – Roman Gräf – 2017-04-29T16:40:23.197

Is the input guaranteed to be an integer? – ETHproductions – 2017-04-29T16:59:29.140

It is guaranteed to be an integer or a string representation of that integer – Roman Gräf – 2017-04-29T17:00:24.723

Will the input ever contain leading zeroes? – Erik the Outgolfer – 2017-04-29T17:31:54.470

@EriktheOutgolfer No. – Roman Gräf – 2017-04-29T17:32:33.850

19A few test cases would be nice. – Dennis – 2017-04-29T18:15:03.353

1Now that 12 answers are already present, all of which performing the same thing, if you are still unclear what is being asked, then it is not the problem of the question. – Leaky Nun – 2017-04-30T02:30:40.117

You don't really need to specify those i/o methods (the i/o formats are fine though), as they are the PPCG defaults. – Rɪᴋᴇʀ – 2017-04-30T14:47:46.620

Answers

14

Pyth, 8 6 bytes

ox+.n0

Try it here.

-1 thanks to Leaky Nun: The input will provide the 0 if it's ever needed.
Trivial -1 thanks to Jakube: Backtick not needed (ah, how did I miss that, HOW?!?).

Erik the Outgolfer

Posted 2017-04-29T16:13:12.357

Reputation: 38 134

Woohoo, this even beats 05AB1E! Edit: it doesn't beat 05AB1E, and I don't wanna steal :( – Erik the Outgolfer – 2017-04-29T18:00:24.770

3I found it. You don't need the 0 at the end. If the input has a 0, the 0 would be provided by the input; if the input does not have a 0, it won't matter. – Leaky Nun – 2017-04-29T18:16:54.333

3@LeakyNun and you can even save the backtick: ox+.n0 – Jakube – 2017-04-29T22:14:24.530

OK, disregard the first comment, thanks to LeakyNun and Jakube, I again beat 05AB1E, I hope for good this time. – Erik the Outgolfer – 2017-04-30T08:06:59.257

1That's a beautiful amount of implicit input. – isaacg – 2017-05-13T06:27:53.913

@isaacg One third of the program ox+.n0NNQ can be implicit input, so it's cut away. – Erik the Outgolfer – 2017-05-13T06:31:53.367

21

Python 3, 40 39 bytes

1 byte thanks to Jonathan Allan.

lambda s:sorted(s,key="145926870".find)

Try it online!

Leaky Nun

Posted 2017-04-29T16:13:12.357

Reputation: 45 011

6You can drop the 3, since find will return -1 when an item is not found. – Jonathan Allan – 2017-04-29T17:51:49.450

18

05AB1E, 10 9 7 bytes

Saved 1 byte thanks to Leaky Nun noting that filtering out duplicates is unnecessary.
Saved 2 bytes thanks to Adnan.

žqRvy†J

Try it online!

Explanation

žq       # push pi to 15 decimals (contains all digits but 0)
  R      # reverse
   vy    # for each char in pi
     †J  # move it's occurrences in the input to the front

Emigna

Posted 2017-04-29T16:13:12.357

Reputation: 50 798

13žsRvy†J for 9 bytes – Leaky Nun – 2017-04-29T16:29:52.187

@LeakyNun: Oh yeah, duplicates doesn't matter. Thanks :) – Emigna – 2017-04-29T16:31:21.787

3Can you use žq instead of 13žs? – Adnan – 2017-04-29T17:56:28.147

@Adnan It doesn't seem to work. – Erik the Outgolfer – 2017-04-29T18:01:50.137

@EriktheOutgolfer It works for me

– Leaky Nun – 2017-04-29T18:02:15.703

@LeakyNun Whoops, I don't know what I typed then. – Erik the Outgolfer – 2017-04-29T18:03:15.450

2@Adnan: Yes of course. I didn't realize there was another pi constant :) – Emigna – 2017-04-29T18:05:17.340

8

Japt, 10 9 bytes

8 bytes of code, +1 for the -P flag.

–!bMP+U

Try it online! Takes input as a string.

Explanation

–!bMP+'0  // Implicit input

¬          // Split the input into chars.
 ñ         // Sort each char in the resulting list by
  !b       //   its index in
    MP+U   //     Math.PI + the input.
-P         // Join the result back into a single string.
           // Implicit: output result of last expression

ETHproductions

Posted 2017-04-29T16:13:12.357

Reputation: 47 880

8

Jelly, 10 bytes

“ṀSṪw’ṾiµÞ

Try it online!

Takes input as a string of digits.

-3 bytes thanks to @ETHproductions

Explanation

“ṀSṪw’ṾiµÞ
        µ  - Separate chain into function “ṀSṪw’Ṿi and sort atom Þ.
         Þ - Sort the input by
       i   - Each digit's index in: 
“ṀSṪw’     - the literal 3145926870 ...
      Ṿ    - transformed into the list 3,1,4,5,9,2,6,8,7,0

fireflame241

Posted 2017-04-29T16:13:12.357

Reputation: 7 021

I think 3145926870 can be represented as a 4-digit base-250 string (meaning it takes up 6 bytes instead of 10), but I'm not sure how to compress it as such. – ETHproductions – 2017-04-29T16:46:23.110

Does Jelly not have a builtin for pi? – math junkie – 2017-04-29T16:46:48.093

@mathjunkie but Jelly is not very efficient on string manipulation – Leaky Nun – 2017-04-29T16:47:12.597

@mathjunkie Yes, but the manipulations to the list take too many bytes – fireflame241 – 2017-04-29T16:47:43.613

“ṀSṪw’ will give you 3145926870. – Leaky Nun – 2017-04-29T16:49:16.533

If you take input and output as strings, can you get rid of the D/ in the main link? – ETHproductions – 2017-04-29T16:53:18.303

@ETHproductions The D converts it to a list of numbers, not a string – Leaky Nun – 2017-04-29T16:54:03.920

Try it online! – ETHproductions – 2017-04-29T16:55:15.573

@ETHproductions hey, that was nice. – Leaky Nun – 2017-04-29T16:55:51.570

which allows you to do “ṀSṪw’ṾiµÞ for 10 bytes – ETHproductions – 2017-04-29T16:56:50.930

@mathjunkie The pi builtin does not go far enough to include a zero, which is needed in Jelly since it's 1-indexed. – Erik the Outgolfer – 2017-04-29T17:01:26.270

7

Jelly,  8  7 bytes

-1 byte thanks to Dennis (use any existing 0 in the input, clever.)

ØP;ṾiµÞ

Try it online!

How?

ØP;ṾiµÞ - Main link: string s (char list)
     µÞ - sort the characters, c, of s by:
    i   -   first index of c in:
ØP      -     pi yield: 3.141592653589793
  ;     -     concatenate with left: [3.141592653589793, c]
   Ṿ    -     un-evaluate: "3.141592653589793,c" (a char list with the digit character c)
                                if any c is 0 ^ it will then be to the right of all others

Jonathan Allan

Posted 2017-04-29T16:13:12.357

Reputation: 67 804

...and there I was searching for squares - 3820009 (sqrt of 14592468760081) is still 3 digits in base 250. – Jonathan Allan – 2017-04-29T18:25:28.243

The in your explanation is misplaced. – Erik the Outgolfer – 2017-04-30T09:56:33.253

@EriktheOutgolfer - thanks, adjusted. – Jonathan Allan – 2017-04-30T10:38:38.550

7

JavaScript (ES6), 54 bytes

f=
s=>[...s].sort((a,b)=>k[a]-k[b],k=`9150236874`).join``
<input oninput=o.textContent=f(this.value)><pre id=o>

Uses strings for I/O.

Neil

Posted 2017-04-29T16:13:12.357

Reputation: 95 035

6

CJam, 15 12 10 8 bytes

r{P`#c}$

Try it online!

-3: Use a string based on the P pi variable instead of a literal.
-2: Decided I don't need to uniquify at all, since finding an index takes the first occurrence anyways. -2: Thanks to jimmy23013 for an interesting approach using x mod 65536.

Explanation:

r{P`#c}$ e# Takes an input token
r        e# Take the integer as a string
 {P`#c}  e# Sorting key:
  P      e#  Push P (defaults to 3.141592653589793)
   `     e#  Convert to string representation
    #    e#  Find char's index in the string we made
         e#  A '.' will never be found in an integer, but it doesn't matter, since the shifting preserves ideal sorting.
         e#  A '0' will be indexed as -1.
     c   e#  Convert index to char
         e#  This first calculates index % 65536, and then converts to char. We need this because otherwise 0 would be indexed as -1, i.e. smallest index.
         e#  We don't need to convert back to integer, since we can use lexicographical sorting.
       $ e# Sort with key

Erik the Outgolfer

Posted 2017-04-29T16:13:12.357

Reputation: 38 134

1Yay, beats MATL :) – Erik the Outgolfer – 2017-04-29T17:53:42.517

https://tio.run/nexus/cjam#@19UHZCgnFyr8v@/oYGRsYmpmbmFJQA – jimmy23013 – 2017-05-01T19:02:06.917

@jimmy23013 Wow that's clever. It's almost like there's a builtin for int(x)%65536, and ci would even convert back to integer. – Erik the Outgolfer – 2017-05-02T11:12:30.757

5

PHP, 71 Bytes

The regex solution is shorter

for(;~$c=_3145926870[$i++];)echo str_repeat($c,substr_count($argn,$c));

or

for(;~$c=_3145926870[$i++];)echo str_pad("",substr_count($argn,$c),$c);

Online Versions

PHP, 78 Bytes

for(;~$c=$argn[$i++];)$j[strpos("3145926870",$c)].=$c;ksort($j);echo join($j);

PHP, 112 Bytes

$a=str_split($argn);usort($a,function($x,$y){return strpos($d="3145926870",$x)<=>strpos($d,$y);});echo join($a);

Online Version

Jörg Hülsermann

Posted 2017-04-29T16:13:12.357

Reputation: 13 026

I've added a 69 byte solution. Maybe we can get it down to 66 byte together ;)

– Christoph – 2017-05-02T14:13:11.727

5

C, 103 97 bytes

char*p="3145926870";s(*a,*b){return strchr(p,*a)-strchr(p,*b);}f(char*t){qsort(t,strlen(t),1,s);}

Try it online

Johan du Toit

Posted 2017-04-29T16:13:12.357

Reputation: 1 524

Thanks, @ceilingcat, MSVC does not like this at all, I suppose I should rather prototype with gcc :-) – Johan du Toit – 2017-05-02T18:56:36.233

MSVC probably won't like the fact that gcc lets you ditch char in char*p and char*t – ceilingcat – 2017-09-22T20:22:06.140

4

Ruby, 50 bytes

n=gets;"3145926870".each_char{|c|$><<c*n.count(c)}

Peter Lenkefi

Posted 2017-04-29T16:13:12.357

Reputation: 1 577

3

MATL, 14 bytes

YP99Y$uj!y=sY"

Try it online!

Explanation with an example

The symbol ; is used as row separator in matrices. So [1 2 3] is a row vector, [1; 2; 3] is a column vector, and [1 2; 3 4] is a square matrix. The latter can also be represented, for clarity, as

[1 2;
 3 4]

Consider input 2325 as an example.

YP     % Push approximation of pi as a double (predefined literal)
       % 3.14159265358979
99Y$   % Variable-precision arithmetic with 99 digits. Gives a string.
       % The input 3.14159265358979 is recognized as representing pi
       % STACK: '3.141592653589793238462 ··· 707'
u      % Unique entries, keeping order of their first appearance
       % STACK: '3.145926870'
j      % Input line as a string
       % STACK: '3.145926870', '2352'
!      % Transpose
       % STACK: '3.145926870', ['2'; '3';'5'; '2']
y      % Duplicate the second-top element in the stack
       % STACK: '3.145926870', ['2'; '3';'5'; '2'], '3.145926870'
=      % Test for equality, with broadcast. This gives a matrix with
       % all pairwise comparisons)
       % STACK: '3.145926870', [0 0 0 0 0 0 1 0 0 0 0;
       %                        1 0 0 0 0 0 0 0 0 0 0;
       %                        0 0 0 0 1 0 0 0 0 0 0;
       %                        0 0 0 0 0 0 1 0 0 0 0]
s      % Sum of each column
       % STACK: '3.145926870', [1 0 0 0 1 0 2 0 0 0 0]
Y"     % Run-length decoding. Implicitly display
       % STACK: '3522'

Luis Mendo

Posted 2017-04-29T16:13:12.357

Reputation: 87 464

3

C (gcc), 78 bytes

f(char*s){for(char*d="3145926870",*p;*d;d++)for(p=s;*p;)*p++-*d||putchar(*d);}

Try it online!

Dennis

Posted 2017-04-29T16:13:12.357

Reputation: 196 637

2

PHP, 66 65 bytes

Saved 1 byte thanks to Titus.

while(~$d=_3145926870[++$i])echo preg_filter("/[^$d]/",'',$argn);

user63956

Posted 2017-04-29T16:13:12.357

Reputation: 1 571

2

C# Interactive, 37 36 Bytes

i.OrderBy(c=>"145926870".IndexOf(c))

Actually you have to execute this in the C# interactive for proper results, but I guess this is what you meant with exit status. The variable i actually is the input variable (it can be for example a string), so it's basically the method parameter.

I think the code itself is pretty straight forward.

MetaColon

Posted 2017-04-29T16:13:12.357

Reputation: 391

Where's the 3? – Paul – 2017-05-01T13:44:44.617

1@Paul it's not neccessary, as it returns -1 if the element isn't found. – MetaColon – 2017-05-01T13:47:19.280

This is just a snippet of code though, I'm pretty sure even in interactive you have to specify why i is somewhere so it can be taken as input. Also if you're saying C# you have to include using System.Linq; into the byte count. However, if this is Interactive you should specify the language as C# Interactive not solely C#. – TheLethalCoder – 2017-05-18T13:13:34.120

@TheLethalCoder I updated it to C# Interactive. The using isn't neccesaary in the interactive, as it's included automatically. – MetaColon – 2017-05-18T13:16:08.400

2

05AB1E, 5 6 bytes (noncompeting)

Had to realise that 0 is not present in the standard length pi constant.

Σтžsyk

Try it online!

Σтžsyk
Σ      Sort by the result of code
 тžs   Push 100 digits of pi
   yk  Index of digit in pi

kalsowerus

Posted 2017-04-29T16:13:12.357

Reputation: 1 894

You should mark this non-competing as Σ is newer than the challenge. – Emigna – 2017-05-18T13:17:07.097

@Emigna marked it, thanks. But after the required fix it's not anymore shorter than the winning answer anyway ): – kalsowerus – 2017-05-18T13:18:54.457

Too bad you needed that zero for this method. It should be optimal for this language at least. Can't ask for more than that :) – Emigna – 2017-05-18T13:24:34.827

1

Java 7, 110 bytes

String c(String s){String r="";for(char i:"3145926870".toCharArray())r+=s.replaceAll("[^"+i+"]","");return r;}

Explanation:

String c(String s){                       // Method with String parameter and String return-type
  String r="";                            //  Result String
  for(char i:"3145926870".toCharArray())  //  Loop over the characters of "3145926870"
    r+=s.replaceAll("[^"+i+"]","");       //   Append the result-String with all the occurrences of the current character
                                          //  End of loop (implicit / single-line body)
  return r;                               //  Return the result-String
}                                         // End of method

Test code:

Try it here.

class M{
  static String c(String s){String r="";for(char i:"3145926870".toCharArray())r+=s.replaceAll("[^"+i+"]","");return r;}

  public static void main(String[] a){
    System.out.println(c("12345678908395817288391"));
  }
}

Output:

33311145599922688888770

Kevin Cruijssen

Posted 2017-04-29T16:13:12.357

Reputation: 67 575

1

Clojure, 38 bytes

#(sort-by(zipmap"3145926870"(range))%)

Input in string, returns a sequence of characters. zipmap creates a "dictionary" object, which can be used in a function context as well.

(f "1234")
(\3 \1 \4 \2)

If input digits were guaranteed to be unique then you could simply do #(filter(set %)"3145926870").

NikoNyrh

Posted 2017-04-29T16:13:12.357

Reputation: 2 361

1

PHP, 69 68

for(;(~$d=$argn[$j++])||~$c=_3145926870[$i+++$j=0];)$c==$d&&print$d;

Still beaten by preg_filter but I thought it was quite nice itself. Maybe someone can golf off some bytes.

Christoph

Posted 2017-04-29T16:13:12.357

Reputation: 1 489

$c!=$d?:print$d as alternative for $c==$d&&print$d I only see in the moment – Jörg Hülsermann – 2017-05-02T14:21:12.463

1_3145926870 instead of `"3145926870" save 1 Byte – Jörg Hülsermann – 2017-05-02T14:26:18.127

for(;(~$d=$argn[$j++])?:~$c=_3145926870[++$i+$j=0];$c!=$d?:print$d); is also a working alternative – Jörg Hülsermann – 2017-05-02T14:57:14.660

0

Perl 6, 34 bytes

*.comb.sort:{3145926870.index: $_}

Try it

*\       # WhateverCode lambda (this is the parameter)
.comb    # split into digits
.sort: { # sort by

  3145926870.index: $_ # its index in this number
}

Brad Gilbert b2gills

Posted 2017-04-29T16:13:12.357

Reputation: 12 713

0

k, 19 bytes

{x@<"3145926870"?x}

Explanation:

{                 } /function(x)
    "3145926870"?x  /for each x: "3145926870".index(x)
   <                /get indices with which to sort
 x@                 /sort x by those indices

zgrep

Posted 2017-04-29T16:13:12.357

Reputation: 1 291