4
\$\begingroup\$

Challenge

An ascii string is made 'dank' when exactly one space is inserted between any two consecutive letters and all capital letters are replaced with lowercase ones. In this challenge I would like you to write a program that takes a string (not necessarily 'dank') as input and outputs a 'dank' version of that string.

Specification

Your program should take as input an ascii string of arbitrary length and output another ascii string.

A string is classed as dank when it contains only spaces and lowercase letters such that there is exactly one space between any two consecutive letters. To make a string dank you'll have to strip all non-alphabetical characters, replace capital letters with their lowercase counterparts, then insert spaces between all the characters.

Example

"aesthetic" > "a e s t h e t i c"
"How are you?" > "h o w a r e y o u"
"drop // top" > "d r o p t o p"

Winning criterion

Code golf so the shortest code in bytes wins.

g o o d l u c k

\$\endgroup\$
10
  • 1
    \$\begingroup\$ Highly related \$\endgroup\$
    – Leaky Nun
    Jun 3, 2017 at 18:27
  • 1
    \$\begingroup\$ Hey! I would try putting your challenges on the Sandbox first so that people who have been around for years can recognize them as dupes before you post them :) \$\endgroup\$
    – Stephen
    Jun 3, 2017 at 18:37
  • 5
    \$\begingroup\$ Differences: the string has to be converted to lowercase, all non-alphabetical characters have to be removed, there's no need for a trailing space. I personally would not close this as a dupe. \$\endgroup\$ Jun 3, 2017 at 18:38
  • 2
    \$\begingroup\$ Oh, OK, you should probably clarify that in the post. Welcome to the site, btw! Not sure why this is getting so many downvotes, it's a pretty good first challenge... \$\endgroup\$ Jun 3, 2017 at 20:52
  • 4
    \$\begingroup\$ @xnor - could you explain your reasoning? I do agree that the target is very similar, but am not 100% sure that they are duplicates (see ETH's differences). However, a reopen vote from me would (I believe) hammer it back open (not the most productive thing to do). \$\endgroup\$ Jun 3, 2017 at 21:14

13 Answers 13

2
\$\begingroup\$

05AB1E, 6 5 bytes

-1 byte thanks to Erik

láSðý

Explanation:

l        Convert to lowercase
 á       Remove all non-alphabetic characters
  S      Get characters as array
   ðý    Join by spaces

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ á is a builtin to do AuA«Ã, so láSðý or álSðý should suffice...yet the challenge turned out to be a dupe. \$\endgroup\$ Jun 3, 2017 at 18:46
1
\$\begingroup\$

Python 3.5, 46 49 47 bytes

print(*[i for i in input().lower()if'`'<i<'{'])

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Jelly, 6 bytes

ŒlfØaK

A monadic link taking and returning lists of characters (or a program that prints the result).

Try it online!

How?

ŒlfØaK - Main link: list of characters, s
Œl     - convert to lowercase (vectorises)
   Øa  - yield lowercase alphabet: "abcdefghijklmnopqrstuvwxyz"
  f    - filter keep
     K - join with spaces
       - if running as a program implicit print
\$\endgroup\$
1
\$\begingroup\$

Decimal, 92 bytes

91D31030012090D415D56112032D41D625230012122D416D5291D5230012097D417D5291D52301212032D301291D

Whew, that was fun. Thanks for the challenge - it helped me develop Decimal into a fully working language. (While writing this, I realized a few flaws. Just bugs and such.)

Ungolfed:

91D
 310 300    ; input to stack, duplicate
 12090D     ; push CHAR 'Z' to stack
 415D       ; compare <, pop values used, push result

 5          ; if truthy
  61        ;  put result into memory
  12032D    ;  push CHAR 32 to stack
  41D       ;  math +, pop values used, push result
  62        ;  pop memory to stack
 5          ; endif

 2          ; pop

 300        ; duplicate DSI
 12122D     ; push CHAR 'z' to stack
 416D       ; compare >, pop values used, push result
 5          ; if truthy
  291D      ; pop, restart
 5          ; endif

 2          ; pop

 300        ; duplicate DSI
 12097D     ; push CHAR 'a' to stack
 417D       ; compare <, pop values uesd, push result
 5          ; if truthy
  291D      ; pop, restart
 5          ; endif

 2          ; pop

 3012       ; print, pop
 12032D3012 ; push CHAR ' ', print, pop
91D

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Japt, 16 5 + 1 (-S flag) = 6 bytes

Thanks to obakaron for saving 10 bytes

;v fE

Try it online!

\$\endgroup\$
7
  • \$\begingroup\$ What if you put the v first? ;) \$\endgroup\$ Jun 3, 2017 at 18:38
  • \$\begingroup\$ Also, you can replace the "[^A-Z]"'i with "%l" to save a few bytes \$\endgroup\$ Jun 3, 2017 at 18:40
  • \$\begingroup\$ @ETHproductions if he puts the v first, trump wins the election. \$\endgroup\$ Jun 3, 2017 at 18:44
  • \$\begingroup\$ @cairdcoinheringaahing like that would change anything... :P \$\endgroup\$ Jun 3, 2017 at 18:59
  • \$\begingroup\$ @ETHproductions what do you mean? The election is happening tomorrow. Time difference, man... \$\endgroup\$ Jun 3, 2017 at 19:00
0
\$\begingroup\$

Perl 6, 18 bytes

{m:g/<:L>/.Str.lc}

m:g/<:L>/ produces a list of all of the letters in the input string; .Str converts the list into a string with a space between each element; and .lc converts the string to lower case.

\$\endgroup\$
0
\$\begingroup\$

Pyth, 6 bytes

jd@Gr0

Try it here!

\$\endgroup\$
0
\$\begingroup\$

shortC, 40 bytes

c;AO;~c;c=G)!isalpha(c)?:R"%c ",tolower(c

Conversions in this program:

  • A -> int main(int argc, char **argv){
  • O -> for(
  • G -> getchar()
  • R -> printf(
  • Auto-inserted closing ));}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

PHP, 68 Bytes

<?=chunk_split(preg_replace("#[^a-z]#","",strtolower($argn)),1," ");

Try it online!

PHP, 70 Bytes

<?=join(" ",str_split(preg_replace("#[^a-z]#","",strtolower($argn))));

Try it online!

or

<?=preg_replace(["#[^a-z]#","#.(?=.)#"],["","$0 "],strtolower($argn));

Try it online!

\$\endgroup\$
0
\$\begingroup\$

C, 65 bytes

c;f(){for(;~c;c=getchar())!isalpha(c)?:printf("%c ",tolower(c));}

How it works:

  • c declares an integer c, and sets it to 0 by default.
  • for(;~c; declares a loop that will only continue if performing a bitwise NOT on c is true. NOT will return false only if c is EOF (meaning there is no more input).
  • c=getchar() reads a character from input into c.
  • !isalpha(c)?: checks if c is in the alphabet. If so...
  • printf("%c ",tolower(c)); prints the character in lowercase, then a space.

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Retina, 14 bytes

T`Llp`ll_
\B
 

Try it online! This is so simple I don't even need a header to run multiple testcases. All uppercase letters are lowercased, lowercase letters are also lowercased for consistency, and everything else is deleted. Then spaces are inserted between every pair of letters.

\$\endgroup\$
0
\$\begingroup\$

JavaScript (ES6), 53 bytes

s=>[...s.toLowerCase().replace(/[^a-z]/g,'')].join` `

Sometimes the straightforward way is the shortest...

Here are a few successively more fun versions using array comprehensions (only works in Firefox 30+):

s=>[for(x of s.toLowerCase())if('`'<x&x<'{')x].join` `
s=>[for(x of s)if(parseInt(x,36)>9)x.toLowerCase()].join` `
s=>[for(x of s)if((x=parseInt(x,36))>9)x.toString(36)].join` `

If we can assume the input contains at least one letter, this will work for 42:

s=>s.toLowerCase().match(/[a-z]/g).join` `
\$\endgroup\$
0
\$\begingroup\$

Python 2, 55 50 53 50 bytes

-3 bytes thanks to ETHproductions.

lambda s:' '.join(i for i in s.lower()if'`'<i<'{')

Try it online!

\$\endgroup\$
2
  • 3
    \$\begingroup\$ Can you not do if"`"<i<"{", since i is already lowercase? \$\endgroup\$ Jun 3, 2017 at 20:44
  • \$\begingroup\$ @ETHproductions I should really take a closer look at the ASCII table... Thanks! \$\endgroup\$ Jun 3, 2017 at 20:45

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