-2
\$\begingroup\$

I have a list of images grouped numerically (YI0.png, YI1.png...YI20479.png). I want them sorted alphabetically, like so:

YI0.png -> YIA.png
YI1.png -> YIAA.png

without my last image having 20479 As in it.

Input

A directory name.

Output

All files in that directory will be alphabetically renamed.

Constraints

  • The pattern is always Prefix Number.Extension
  • There are no subdirectories
  • All files in the directory adhere to the pattern
  • Number starts at 0
  • Prefix doesn't contain numbers

Example

Directory consisting of Pie0.txt, Pie1.txt, all the way to Pie11.txt would be renamed to PieA.txt, PieB.txt, all the way to PieL.txt.

If it goes to Pie28.txt, they would be renamed like so:

Pie0.txt -> PieA.txt
Pie1.txt -> PieAA.txt
Pie2.txt -> PieAB.txt
Pie3.txt -> PieB.txt
Pie28.txt -> PieZ.txt

Remember, this is , so the answer in the shortest bytes wins.

\$\endgroup\$
  • \$\begingroup\$ will all files in the dir be of the form "some_letters_that_are_always_the_sameNumber.txt"? \$\endgroup\$ – Maltysen Sep 5 '16 at 16:19
  • \$\begingroup\$ Yes, it will have a pattern of PrefixNumber.Extension @Maltysen \$\endgroup\$ – Soren Sep 5 '16 at 16:20
  • \$\begingroup\$ will be ever have three length codes? \$\endgroup\$ – Maltysen Sep 5 '16 at 16:37
  • 1
    \$\begingroup\$ @Jonathan Allan only letters, not numbers. Lowercase and uppercase are allowed though. \$\endgroup\$ – Soren Sep 5 '16 at 18:53
  • 2
    \$\begingroup\$ This needs a spec. At present we have to guess what it might be from two examples. \$\endgroup\$ – Peter Taylor Sep 5 '16 at 21:20
1
\$\begingroup\$

Perl, 65 bytes

Assumes the number sequence starts with 0 and has no holes (or duplicates if there are extra leading zeros). No error checking. Assumes only plain files, filenames do not start with . and the user can enter the directory.

Run with the directory as argument

alphaorg.pl directory

alphaorg.pl:

#!/usr/bin/perl
chdir pop;$a=A;@F=sort map$a++,<*>;rename$_,s/\d+/$F[$&]/r for<*>

If the directory name can't contain digits and your sytem will accept / as directory separator (unix and windows do), this 61 byte version is shorter (give directory name on STDIN here):

#!/usr/bin/perl -ln
$a=A;@F=sort map$a++,@;=<$_/*>;rename$_,s/\d+/$F[$&]/r for@
\$\endgroup\$
1
\$\begingroup\$

Haskell - 97 bytes

I probably picked the wrong language; Haskell has painfully long names for IO operations

r p=do f<-getDirectoryContents p;sequence_.zipWith renameFile(sort f).sort$permutations['A'..'Z']


(this program has O(26^26) in run-time unless you're on a quantum computer)

\$\endgroup\$
  • \$\begingroup\$ Maybe I am on a Quantum Computer :^) no lol I wish though \$\endgroup\$ – Soren Sep 5 '16 at 18:51
  • \$\begingroup\$ @moo_we_all_do You could be- IBM is handing out access to a 6 qubit quantum computer at the moment (I got it, so it can't be that hard). Unfortunately you're gonna need about 123 qubits to run this program in real time .. \$\endgroup\$ – BlackCap Sep 5 '16 at 19:05
  • \$\begingroup\$ I also now have access to it. If I had $100 dollars, I would pay you $100 to write this program in QASM. That would astound me! \$\endgroup\$ – Soren Sep 5 '16 at 19:48
  • 1
    \$\begingroup\$ @moo_we_all_do R x|-Zip(Sort x).Sort$Permutations['A'..'Z']. This is QML, a haskell inspired quantum language with a compiler written in haskell: sneezy.cs.nott.ac.uk/qml Obviusly you can't read files or anything yet, so you have to pass it the file names as a list of strings. It is in my opinion by far the best effort at a high level quantum language as of now. Don't ask me why he chose to have uppercase names for functions \$\endgroup\$ – BlackCap Sep 5 '16 at 21:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.