Dissect a word and arrange all its letters in alphabetical order

-5

0

Question

Write a program to accept one string on the console only and print back its letters arranged in alphabetical order. The string must only have the following characters:

0123456789abcdefghijklmnopqrstuvwxyz

Rules

The ascending alphabetical order is defined in the given string.

All repeated characters must be printed.

If the string contains characters other than the ones mentioned above, it must display "Invalid input".

This is code-golf, so shortest answer in bytes wins.

Standard loopholes are forbidden.

A null string "" will output "Invalid input".

Your program must be executable and not a function or snippet.

Boilerplate is not required.

Examples

"dragon"--> adgnor
"skoda3ferrari"--> 3aadefikorrrs
"ferret"--> eefrrt
"werewolf92"--> 29eeflorww
"@hog"--> Invalid input
"gTa5"--> Invalid input
"  l "--> Invalid input
""--> Invalid input

Edit: The double quotes are given for clarification.

May the force be with you. Good luck!

Riley Jones

Posted 2020-01-08T10:09:45.533

Reputation: 49

3Any particular reason for the input being restricted to the console? Why not allow a function taking an argument? – Adám – 2020-01-08T10:15:22.707

What if my function is executable without boilerplate? – Adám – 2020-01-08T10:16:05.553

What is boilerplate? – Riley Jones – 2020-01-08T10:16:30.243

Now your post is self-contradictory: as an argument vs not a function – Adám – 2020-01-08T10:17:00.370

Boilerplate is additional code necessary to actually run one's main code. – Adám – 2020-01-08T10:17:22.567

Input will be restricted to the console. Boilerplate is not required. – Riley Jones – 2020-01-08T10:18:09.097

@a'_' No, unless it has characters outside the set. – Adám – 2020-01-08T10:40:36.147

Suggested additional test cases: "ABC" and "" – Adám – 2020-01-08T10:42:07.113

Invalid input for "" and also and whitespace – Riley Jones – 2020-01-08T10:44:42.547

2@RileyJones Why is "" invalid? It has no characters outside the set. – Adám – 2020-01-08T10:46:51.613

Just so. No specific reason. – Riley Jones – 2020-01-08T10:47:50.033

@RileyJones Since that is an additional rule which cannot be inferred from the other rules, you should be explicit about it. Do not rely on a test case to specify the challenge. – Adám – 2020-01-08T10:49:55.590

Does "console input" included passing the string as argument? – he77789 – 2020-01-08T11:43:49.547

no, only console input – Riley Jones – 2020-01-08T12:10:38.743

Is the Invalid input mandatory to be with titlecased Invalid and lowercase input, or is Invalid Input or invalid input or INVALID INPUT valid as well? Several answers are using Invalid Input right now. – Kevin Cruijssen – 2020-01-08T13:31:04.857

11Hi! I've downvoted this post as it goes against several site conventions (allowable I/O methods, allowed to be programs or functions, and input parsing) without really providing justification for those decisions (e.g., input parsing as a core component of the challenge). – AdmBorkBork – 2020-01-08T13:32:34.297

2Hi Riley, thank you for your challenge. Please do stick around, and answer a few challenges to get to know the site better. It would be a good idea to post your next challenge to the sandbox (see link at top right of screen) for review before it goes live. @AdmBorkBork I applaud you for explaining the reason for your downvote, and I don't disagree with your reasons. On the other hand I think all of us need to be a bit more welcoming and lenient with new contributors lest they are put off using the site in future. – Level River St – 2020-01-09T20:16:38.420

Answers

6

05AB1E, 16 15 bytes

-1 byte thanks to Kevin Cruijssen

žKlsSåßi{ë”ͼî®

Try it online!

Grimmy

Posted 2020-01-08T10:09:45.533

Reputation: 12 521

Your Input is titlecased as well now. – Kevin Cruijssen – 2020-01-08T12:26:39.800

@KevinCruijssen Yeah, several other answers titlecase Input, so I assumed it wasn't an issue – Grimmy – 2020-01-08T12:29:27.047

1

Ah ok. Btw, you can also save a byte by changing {ОKlм‚õ¢≠i to žKlsSåßi{ë: verify all test cases.

– Kevin Cruijssen – 2020-01-08T13:28:54.447

@a'_' I had already stated that in my first comment. :) And I've asked OP about it, but no response yet. It's easy to fix in 2 bytes though, by changing the ”Í¼î® to „ͼî®.ª: try it online.

– Kevin Cruijssen – 2020-01-08T14:52:45.740

4

Python 3.8 (pre-release), 77 75 74 88 bytes

import re
print(*re.match('[0-9a-z]+$',i:=input())and sorted(i)or'Invalid input',sep='')

Try it online!

TFeld

Posted 2020-01-08T10:09:45.533

Reputation: 19 246

Fails on input that contains only numbers. Input 123 outputs Invalid input. – mypetlion – 2020-01-08T18:07:38.233

Also fails on lowercase characters outside of the given alphabet. For example ú does not output Invalid input. – mypetlion – 2020-01-08T18:11:10.280

@mypetlion Fixed – TFeld – 2020-01-09T08:18:37.910

@a'_' Fixed now – TFeld – 2020-01-09T08:18:50.347

4

APL (Dyalog Extended), 37 bytesSBCS

{0∊(≢⍵),⍵∊⎕D,⌊⎕A:'Invalid input'⋄∧⍵}⍞

Try it online!

 prompt for text input from the console

{} apply the following anonymous lambda to that:

⎕A the uppercase Alphabet

Lowercase that

⎕D, prepend the digits

⍵∊ for each character of the argument, indicate if it is a member thereof

(), prepend the following:

  ≢⍵ the tally of characters in the argument

0∊: if zero is a member thereof:

  'Invalid input' return (and implicitly print) this message

 else:

  ∧⍵ sort the argument ascending

Adám

Posted 2020-01-08T10:09:45.533

Reputation: 37 779

4

Burlesque, 37 bytes

><JJann!/^zz!=||{vv"Invalid Input"}if

Try it online!

><                    # Sort input string
JJ                    # Duplicate twice
ann!                  # Not alphanumeric
/^                    # Swap and duplicate (sorted string)
zz!=                  # Lowercased not equal to original (i.e. contains uppercase)
||                    # Or
{vv"Invalid Input"}if # If not alphanumeric or lowercase drop sorted list 
                        and output invalid input

DeathIncarnate

Posted 2020-01-08T10:09:45.533

Reputation: 916

The first Burlesque answer after 2 years! – None – 2020-01-08T14:21:51.647

2I've recently been trying to learn it for a bit of fun. I looked at it several years ago and decided I'd go for befunge instead. Finally decided to give it a go for this year's advent of code. – DeathIncarnate – 2020-01-08T14:41:30.137

4

x86-16 machine code, IBM PC-DOS, 81 80 bytes

Binary:

00000000: b40a ba4f 01cd 218b f2ad 8bd6 8acc 8be9  ...O..!.........
00000010: 938b f28b fe51 ad3c 307c 0c3c 397e 0e3c  .....Q.<0|.<9~.<
00000020: 617c 043c 7a7e 06ba 4201 59eb 103a c47c  a|.<z~..B.Y..:.|
00000030: 0286 c4ab 4e4f e2de 59e2 d688 1bb4 09cd  ....NO..Y.......
00000040: 21c3 496e 7661 6c69 6420 696e 7075 7424  !.Invalid input$

Build and test DISSECT.COM using xxd -r

Unassembled listing:

B4 0A           MOV  AH, 0AH            ; DOS read buffered input from STDIN 
BA 014F R       MOV  DX, OFFSET BUF     ; load buffer pointer    
CD 21           INT  21H                ; get STDIN input 
8B F2           MOV  SI, DX             ; buffer pointer into SI 
AD              LODSW                   ; load char count into AH 
8B D6           MOV  DX, SI             ; save start of string 
8A CC           MOV  CL, AH             ; string length into CX 
8B E9           MOV  BP, CX             ; save string length for later display 
            OUTER_LOOP: 
8B F2           MOV  SI, DX             ; reset SI to beginning of buffer 
8B FE           MOV  DI, SI             ; reset DI to beginning of buffer 
51              PUSH CX                 ; save outer loop counter 
            INNER_LOOP: 
AD              LODSW                   ; load next two chars into AH/AL
3C 30           CMP  AL, '0'            ; is char < '0' ? 
7C 0C           JL   INVALID            ; if so, not valid 
3C 39           CMP  AL, '9'            ; is char <= '9'? 
7E 0E           JLE  VALID              ; if so, valid 
3C 61           CMP  AL, 'a'            ; is char < 'a'? 
7C 04           JL   INVALID            ; if so, not valid 
3C 7A           CMP  AL, 'z'            ; is char <= 'z'? 
7E 06           JLE  VALID              ; if so, valid 
            INVALID: 
BA 0142 R       MOV  DX, OFFSET INV     ; load address of "invalid" string 
59              POP  CX                 ; discard saved counter 
EB 11           JMP  OUTPUT             ; display "invalid" string
            VALID: 
3A C4           CMP  AL, AH             ; compare chars 
7C 02           JL   NO_SWAP            ; if AL < AH, do not swap 
86 C4           XCHG AL, AH             ; otherwise, swap chars 
            NO_SWAP:
AB              STOSW                   ; store chars back to buffer 
4E              DEC  SI                 ; adjust SI to previous char 
4F              DEC  DI                 ; adjust DI to previous char 
E2 DE           LOOP INNER_LOOP         ; loop inner iterations
59              POP  CX                 ; restore outer loop counter 
E2 D6           LOOP OUTER_LOOP         ; loop outer
C6 03 24        MOV  BYTE PTR[BP+DI],'$'; add string terminator
            OUTPUT:
B4 09           MOV  AH, 09H            ; DOS display string function 
CD 21           INT  21H                ; write output to console 
C3              RET                     ; exit to DOS

INV DB "Invalid input"                  ; "invalid" message string
BUF DB '$'                               ; buffer size is 36 bytes, also string terminator

Standalone PC DOS executable. Implements a simple bubble sort algorithm.

Input via console/STDIN, output to console/STDOUT.

enter image description here

640KB

Posted 2020-01-08T10:09:45.533

Reputation: 7 149

3

GolfScript, 37 bytes

.$\58,48>123,97>+-!*"Invalid input"or

Try it online!

Explanation

Whoa. Maybe I shouldn't have done this..

.                                     # Make 2 copies of the input,
 $                                    # One is the sorted copy of the input,
  \58,48>123,97>+-!                   # Another is whether all of input is in the
                                      # lowercase alphanumeric range
                   *                  # Multiply them:
                                      #     If the input contains invalid characters,
                                      #     e.g. @hog, this multiplies the sorted copy by 0.
                                      # ----If the input is a null string,
                                      #     The input multiplies 0 by the null string, 
                                      #     which is still the null string.
                                      # ----If the input is a valid string,
                                      #     e.g. dragon, the input multiplies the
                                      #     sorted copy by 1 (because everything is
                                      #     inside the range)
                    "Invalid input"or # Do a "logical magic":
                                      #     If the input is 0 or the null string, 
                                      #         returns "Invalid input"
                                      #     Otherwise, this returns the
                                      #         sorted copy of the input.

# Further explanation of the code snippet
# \                 # Swap the stack so that an unused copy of the input emerges
#  58,              # Generate a range and drop the last: 0 to ASCII ':'
                    # (The previous character is '9')
#     48>           # Select everything that is larger than 48 (ASCII '0')
#        123,97>    # Same thing with generating 'a' to 'z'
#               +   # Join the inputs together
#                -  # Set subtraction: subtract this string from the fresh unused input
#                   # This results in everything that isn't alphanumeric
#                 ! # Negate the string. If the string has something, 
#                   # it turns to 0. Otherwise it turns to 1.
```

user85052

Posted 2020-01-08T10:09:45.533

Reputation:

But now it doesn't – Lyxal – 2020-01-09T00:37:55.940

3

Keg, 35 bytes

₳0a&᠀:(⑴|⑻-)⒃᠀⒃¬+[Invalid input|¿÷⑭

Nice to see Keg still "up to par" with Golfscript! No regrets about the bad pun

Lyxal

Posted 2020-01-08T10:09:45.533

Reputation: 5 253

3

Red, 86 bytes

func[s][a: charset[#"0"-#"9"#"a"-#"z"]either parse sort s[some a][s]["Invalid input"]]

Try it online!

Galen Ivanov

Posted 2020-01-08T10:09:45.533

Reputation: 13 815

3

Jelly, 18 bytes

ɠfƑØBŒl¤ȧṢȯ“4{ẉṄi»

Try it online!

A full program that reads its input from STDIN and outputs to STDOUT.

If the input validation and requirement to use STDIN were removed, this would be a single byte:

Nick Kennedy

Posted 2020-01-08T10:09:45.533

Reputation: 11 829

3

Perl 5 -pF//, 45 bytes

$_=/^[a-z\d]+$/?join"",sort@F:"Invalid Input"

Try it online!

Nahuel Fouilleul

Posted 2020-01-08T10:09:45.533

Reputation: 5 582

2

C++ (clang), 223 \$\cdots\$ 190 182 bytes

#import<regex>
#import<iostream>
int main(){std::string s,i="Invalid input";getline(std::cin,s);sort(begin(s),end(s));for(int c:s)s=c<48|c>122|c>57&c<97?i:s;s=s[0]?s:i;std::cout<<s;}

Try it online!

Saved 8 bytes thanks to ceilingcat!!!

A program that reads a single string from stdin (handling all invalid cases) and outputs to stdout.

Noodle9

Posted 2020-01-08T10:09:45.533

Reputation: 2 776

2

PHP, 92 bytes

$a=str_split($s=$argn);sort($a);echo preg_match('/^[a-z\d]+$/',$s)?join($a):'Invalid input';

Try it online!

640KB

Posted 2020-01-08T10:09:45.533

Reputation: 7 149

2

Charcoal, 37 bytes

≔⁺⭆χιβη¿∨¬θ⊙θ¬№ηιInvalid inputFηF№θιι

Try it online! Link is to verbose version of code. Explanation:

≔⁺⭆χιβη

Concatenate the digits with the lowercase letters.

¿∨¬θ⊙θ¬№ηι

If the input is empty or contains a character not in the digit letter string, ...

Invalid input

... then output the required error message, ...

Fη

... else for each digit and lowercase letter...

F№θιι

... output each time it appears in the input.

Neil

Posted 2020-01-08T10:09:45.533

Reputation: 95 035