18
\$\begingroup\$

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.

Riker
7,9084 gold badges39 silver badges73 bronze badges
asked Apr 29, 2017 at 16:13
\$\endgroup\$
10
  • \$\begingroup\$ Related \$\endgroup\$ Commented Apr 29, 2017 at 16:38
  • \$\begingroup\$ Can we take input and output as strings, or as arrays of digits? \$\endgroup\$ Commented Apr 29, 2017 at 16:39
  • \$\begingroup\$ @ETHproductions Clarified. \$\endgroup\$ Commented Apr 29, 2017 at 16:40
  • 19
    \$\begingroup\$ A few test cases would be nice. \$\endgroup\$ Commented Apr 29, 2017 at 18:15
  • 1
    \$\begingroup\$ Now 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. \$\endgroup\$ Commented Apr 30, 2017 at 2:30

27 Answers 27

21
\$\begingroup\$

Python 3, (削除) 40 (削除ここまで) 39 bytes

1 byte thanks to Jonathan Allan.

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

Try it online!

answered Apr 29, 2017 at 16:21
\$\endgroup\$
1
  • 7
    \$\begingroup\$ You can drop the 3, since find will return -1 when an item is not found. \$\endgroup\$ Commented Apr 29, 2017 at 17:51
17
\$\begingroup\$

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
answered Apr 29, 2017 at 16:24
\$\endgroup\$
7
  • \$\begingroup\$ 13žsRvy†J for 9 bytes \$\endgroup\$ Commented Apr 29, 2017 at 16:29
  • \$\begingroup\$ @LeakyNun: Oh yeah, duplicates doesn't matter. Thanks :) \$\endgroup\$ Commented Apr 29, 2017 at 16:31
  • 3
    \$\begingroup\$ Can you use žq instead of 13žs? \$\endgroup\$ Commented Apr 29, 2017 at 17:56
  • \$\begingroup\$ @Adnan It doesn't seem to work. \$\endgroup\$ Commented Apr 29, 2017 at 18:01
  • 2
    \$\begingroup\$ @Adnan: Yes of course. I didn't realize there was another pi constant :) \$\endgroup\$ Commented Apr 29, 2017 at 18:05
14
\$\begingroup\$

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?!?).

answered Apr 29, 2017 at 18:00
\$\endgroup\$
6
  • \$\begingroup\$ Woohoo, this even beats 05AB1E! Edit: it doesn't beat 05AB1E, and I don't wanna steal :( \$\endgroup\$ Commented Apr 29, 2017 at 18:00
  • 3
    \$\begingroup\$ I 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. \$\endgroup\$ Commented Apr 29, 2017 at 18:16
  • 3
    \$\begingroup\$ @LeakyNun and you can even save the backtick: ox+.n0 \$\endgroup\$ Commented Apr 29, 2017 at 22:14
  • \$\begingroup\$ OK, disregard the first comment, thanks to LeakyNun and Jakube, I again beat 05AB1E, I hope for good this time. \$\endgroup\$ Commented Apr 30, 2017 at 8:06
  • 1
    \$\begingroup\$ That's a beautiful amount of implicit input. \$\endgroup\$ Commented May 13, 2017 at 6:27
8
\$\begingroup\$

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
answered Apr 29, 2017 at 16:43
\$\endgroup\$
11
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Apr 29, 2017 at 16:46
  • \$\begingroup\$ Does Jelly not have a builtin for pi? \$\endgroup\$ Commented Apr 29, 2017 at 16:46
  • \$\begingroup\$ @mathjunkie but Jelly is not very efficient on string manipulation \$\endgroup\$ Commented Apr 29, 2017 at 16:47
  • \$\begingroup\$ @mathjunkie Yes, but the manipulations to the list take too many bytes \$\endgroup\$ Commented Apr 29, 2017 at 16:47
  • \$\begingroup\$ "ṀSṪw’ will give you 3145926870. \$\endgroup\$ Commented Apr 29, 2017 at 16:49
8
\$\begingroup\$

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
answered Apr 29, 2017 at 16:42
\$\endgroup\$
7
\$\begingroup\$

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.

answered Apr 29, 2017 at 19:26
\$\endgroup\$
7
\$\begingroup\$

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
answered Apr 29, 2017 at 18:24
\$\endgroup\$
2
  • \$\begingroup\$ ...and there I was searching for squares - 3820009 (sqrt of 14592468760081) is still 3 digits in base 250. \$\endgroup\$ Commented Apr 29, 2017 at 18:25
  • \$\begingroup\$ The in your explanation is misplaced. \$\endgroup\$ Commented Apr 30, 2017 at 9:56
7
\$\begingroup\$

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

answered Apr 29, 2017 at 17:33
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Yay, beats MATL :) \$\endgroup\$ Commented Apr 29, 2017 at 17:53
  • \$\begingroup\$ tio.run/nexus/cjam#@19UHZCgnFyr8v@/oYGRsYmpmbmFJQA \$\endgroup\$ Commented May 1, 2017 at 19:02
  • \$\begingroup\$ @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. \$\endgroup\$ Commented May 2, 2017 at 11:12
6
\$\begingroup\$

C (clang), (削除) 85 (削除ここまで) 73 bytes

*z=L"9150236874"-48;s(*a,*b){return z[*a]-z[*b];}f(*t,n){qsort(t,n,4,s);}

Try it online!

answered Apr 30, 2017 at 8:49
\$\endgroup\$
4
  • \$\begingroup\$ @ceilingcat, wow, it took my small mind forever to figure out what you did there!! \$\endgroup\$ Commented Sep 24, 2021 at 16:08
  • \$\begingroup\$ 69, different IO \$\endgroup\$ Commented Sep 26, 2021 at 8:21
  • \$\begingroup\$ @l4m2 Nice!! I'll update my answer; when I get back to my laptop. -2 bytes \$\endgroup\$ Commented Sep 26, 2021 at 9:08
  • \$\begingroup\$ @l4m2 65 bytes \$\endgroup\$ Commented Sep 26, 2021 at 9:11
5
\$\begingroup\$

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

answered Apr 29, 2017 at 16:42
\$\endgroup\$
1
  • \$\begingroup\$ I've added a 69 byte solution. Maybe we can get it down to 66 byte together ;) \$\endgroup\$ Commented May 2, 2017 at 14:13
4
\$\begingroup\$

Ruby, 50 bytes

n=gets;"3145926870".each_char{|c|$><<c*n.count(c)}
answered Apr 29, 2017 at 16:24
\$\endgroup\$
4
\$\begingroup\$

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!

answered May 1, 2017 at 0:01
\$\endgroup\$
3
\$\begingroup\$

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'
answered Apr 29, 2017 at 17:23
\$\endgroup\$
2
\$\begingroup\$

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.

answered Apr 30, 2017 at 22:25
\$\endgroup\$
4
  • \$\begingroup\$ Where's the 3? \$\endgroup\$ Commented May 1, 2017 at 13:44
  • 1
    \$\begingroup\$ @Paul it's not neccessary, as it returns -1 if the element isn't found. \$\endgroup\$ Commented May 1, 2017 at 13:47
  • \$\begingroup\$ 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#. \$\endgroup\$ Commented May 18, 2017 at 13:13
  • \$\begingroup\$ @TheLethalCoder I updated it to C# Interactive. The using isn't neccesaary in the interactive, as it's included automatically. \$\endgroup\$ Commented May 18, 2017 at 13:16
2
\$\begingroup\$

PHP, (削除) 66 (削除ここまで) 65 bytes

Saved 1 byte thanks to Titus.

while(~$d=_3145926870[++$i])echo preg_filter("/[^$d]/",'',$argn);
answered Apr 30, 2017 at 12:25
\$\endgroup\$
0
2
\$\begingroup\$

APL (Dyalog Unicode), 13 bytes

{⍵[⍵⍋⍨14⍕しろまる1]}

Try it online!

しろまる1 Pi times 1.

14⍕ convert to a string with 14 decimal digits. This includes all unique digits except 0.

⍵⍋⍨ the indices that would sort the input string according to order given by the previous string.

⍵[...] index with those indices into the input to sort it.

answered Aug 13, 2021 at 18:29
\$\endgroup\$
2
\$\begingroup\$

05AB1E, (削除) 5 (削除ここまで) 6 bytes

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
Jo King
48.1k6 gold badges130 silver badges187 bronze badges
answered May 18, 2017 at 13:10
\$\endgroup\$
1
  • \$\begingroup\$ 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 :) \$\endgroup\$ Commented May 18, 2017 at 13:24
1
\$\begingroup\$

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
answered May 1, 2017 at 13:15
\$\endgroup\$
1
\$\begingroup\$

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").

answered May 1, 2017 at 16:44
\$\endgroup\$
1
\$\begingroup\$

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.

answered May 2, 2017 at 14:12
\$\endgroup\$
3
  • \$\begingroup\$ $c!=$d?:print$d as alternative for $c==$d&&print$d I only see in the moment \$\endgroup\$ Commented May 2, 2017 at 14:21
  • 1
    \$\begingroup\$ _3145926870 instead of `"3145926870" save 1 Byte \$\endgroup\$ Commented May 2, 2017 at 14:26
  • \$\begingroup\$ for(;(~$d=$argn[$j++])?:~$c=_3145926870[++$i+$j=0];$c!=$d?:print$d); is also a working alternative \$\endgroup\$ Commented May 2, 2017 at 14:57
1
\$\begingroup\$

Husk, 4 bytes

Ö€İπ

Try it online!

Input as a list of digits.

answered Aug 13, 2021 at 17:53
\$\endgroup\$
1
\$\begingroup\$

Pip, 10 bytes

PI.0@?_SKa

Try it online!

Explanation

 SKa Sort the command-line argument a by this key function:
 @?_ Index of first occurrence in
PI.0 Pi (3.141592653589793) concatenated with 0

If the 0 isn't added, PI@?0 returns nil, which behaves... strangely... when used as a sorting key.

answered Sep 24, 2021 at 16:30
\$\endgroup\$
1
\$\begingroup\$

Vyxal, 8 bytes

×ばつ(+=»ḟ

Try it Online!

μ Sort by

first index in

×ばつ(+=» compressed integer 145926870

answered Jun 18, 2023 at 0:06
\$\endgroup\$
1
  • \$\begingroup\$ Try it Online! for 6 bytes \$\endgroup\$ Commented Jun 18, 2023 at 0:43
0
\$\begingroup\$

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
}
answered May 1, 2017 at 18:02
\$\endgroup\$
1
  • \$\begingroup\$ you can save 1 byte by dropping '3' from the lookup. \$\endgroup\$ Commented Aug 13, 2021 at 23:34
0
\$\begingroup\$

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
answered May 2, 2017 at 14:51
\$\endgroup\$
0
\$\begingroup\$

Japt, 6 bytes

Input as a string, output as an array of digit strings

¬nUiMP

Try it

answered Aug 13, 2021 at 16:42
\$\endgroup\$
0
\$\begingroup\$

TI-Basic, 100 bytes

Input Str1
seq(expr(sub(Str1,I,1)),I,1,length(Str1→A
seq(1+sum(not(cumSum(Ans(I)={3,1,4,5,9,2,6,8,7}))),I,1,dim(Ans→B
SortD(ʟB,ʟA
sum(seq(10^(I-1)ʟA(I),I,1,dim(ʟA

Takes input as a string. Outputs a number that is stored in Ans and is displayed at the end.

answered Sep 27, 2021 at 16:22
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.