Numeric to Alphabetic Organization

-2

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.

Soren

Posted 2016-09-05T16:17:54.353

Reputation: 285

Question was closed 2016-09-05T22:13:23.090

will all files in the dir be of the form "some_letters_that_are_always_the_sameNumber.txt"? – Maltysen – 2016-09-05T16:19:23.553

Yes, it will have a pattern of PrefixNumber.Extension @Maltysen – Soren – 2016-09-05T16:20:48.580

will be ever have three length codes? – Maltysen – 2016-09-05T16:37:50.920

Will the directory only have plain files or can it e.g. have subdirectories which need to be ignored or recursed into ? – Ton Hospel – 2016-09-05T17:10:58.867

Anyways, I'm not sure I like a challenge that needs to do system commands and especially filename parsing (e.g. is \\ or / the separator, can files start with . etc.) . An input that is a list of simple filenames with an output that are the corresponding translated names seems nicer to me – Ton Hospel – 2016-09-05T17:14:54.293

Can there be holes in the sequence of numbers ? Can numbers have extra leading zeros ? Can the prefix contain digits ? Does the sequence always start with 0 ? – Ton Hospel – 2016-09-05T17:19:19.960

Yes, there will be varied length codes (remember, I have 20479 files). The prefix and extension will only contain alphabetical characters. There are no subdirectories or different patterned files. – Soren – 2016-09-05T17:37:28.407

Will the numeric suffix have leading zeroes? If you sort the numbers between 1 and 100 alphabetically you get [1, 10, 100, 11, 12 ..], but if they have leading zeroes you get [001, 002, 003 ..] – BlackCap – 2016-09-05T18:07:06.610

@BlackCap they do not – Soren – 2016-09-05T18:10:07.803

Since there are different OS in the world, can we not write a program or function which takes a list of strings in the format you defined and outputs a list of new names in the same order as the input list such that the output list is in alphabetic order? Also can we use digits and lowercase letters in our new names (since standard alphabetic sorting would be 0<9<A<Z<a<z)? – Jonathan Allan – 2016-09-05T18:52:30.610

1@Jonathan Allan only letters, not numbers. Lowercase and uppercase are allowed though. – Soren – 2016-09-05T18:53:41.023

2This needs a spec. At present we have to guess what it might be from two examples. – Peter Taylor – 2016-09-05T21:20:32.850

Imagine a hyperdictionary but culled to only have the amount of "words" as the numbers. Once I run the perl example I will have more examples – Soren – 2016-09-05T22:02:11.950

Answers

1

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@

Ton Hospel

Posted 2016-09-05T16:17:54.353

Reputation: 14 114

1

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)

BlackCap

Posted 2016-09-05T16:17:54.353

Reputation: 3 576

Maybe I am on a Quantum Computer :^) no lol I wish though – Soren – 2016-09-05T18:51:36.123

@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 .. – BlackCap – 2016-09-05T19:05:30.007

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! – Soren – 2016-09-05T19:48:19.913

1@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 – BlackCap – 2016-09-05T21:20:15.707