Your goal is to write a program that, given a number N and a string as input, will return a string containing every Nth word of the original string, starting from the first word.

Sample inputs/outputs:

N = 2, str = "This is a sentence"

"This a"
N = 2, str = "The quick brown fox jumped over the lazy dog."

"The brown jumped the dog."
N = 7, str = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa."

"Lorem elit. massa."

You're allowed to assume that the input will always contain at least N+1 words, and that words will always be delimited by the space character  .

This is , so the shortest answer in bytes wins.

  • 1
    \$\begingroup\$ The I/O seems to be underspecified. Are we supposed to read N = 2, str = "This is a sentence" from STDIN? \$\endgroup\$ – Dennis May 21 '15 at 22:23
  • 1
    \$\begingroup\$ Should have posted first and asked questions later. This contest is already over... \$\endgroup\$ – Dennis May 21 '15 at 22:26
  • 5
    \$\begingroup\$ This is a bit too easy IMO... This just becomes a fastest gun in the west contest. \$\endgroup\$ – orlp May 21 '15 at 22:35
  • 10
    \$\begingroup\$ Also, what the hell? Don't accept an answer within 20 minutes! A contest must run for at least a week to allow other submissions. \$\endgroup\$ – orlp May 21 '15 at 22:39
  • 4
    \$\begingroup\$ @orlp: I've undone it; I thought that I should change it if another shorter one comes along. This was my first attempt at a contest, prolly should've used the sandbox first. \$\endgroup\$ – M. I. Wright May 21 '15 at 23:15

Pyth, 6


Splits on spaces, takes every Nth element, joins on spaces.

Try it online


jd          : join on spaces
  %Q        : take every Qth character (Q will have read the input with N)
    cz      : chop the other input on whitespace

CJam, 7 bytes


Test it here. This reads a number and a string (in that order) from STDIN.


q~      e# Read and eval input.
  S/    e# Split on spaces.
    %   e# Take every Nth element.
     S* e# Join the words back together with spaces.

Python, 35 characters

lambda n,s:' '.join(s.split()[::n])
  • \$\begingroup\$ It should be a string, not a list, as per the examples. \$\endgroup\$ – M. I. Wright May 21 '15 at 22:38
  • \$\begingroup\$ Whoops, that's right. Fixed. \$\endgroup\$ – Doorknob May 21 '15 at 22:50

KDB(Q), 26 bytes

{" "sv#[0N,y;" "vs x][;0]}


             " "vs x         / cut x string by space
      #[0N,y;       ]        / cut list by y length
                     [;0]    / take first of each list
 " "sv                       / combine with space
{                        }   / lambda


q){" "sv#[0N,y;" "vs x][;0]}["The quick brown fox jumped over the lazy dog.";2]
"The brown jumped the dog."

Julia, 38 bytes

(n,s)->join(split(s," ")[1:n:end]," ")

This creates an anonymous function that accepts an integer and string as input and returns the required output. To call it, give it a name, e.g. f=(n,s)->....

Ungolfed + explanation:

function f(n, s)
    # Split the string into words
    words = split(s, " ")

    # Get every nth word starting at 1
    every_nth = words[1:n:end]

    # Join into a single string separated with a space
    join(every_nth, " ")


julia> f(2, "The quick brown fox jumped over the lazy dog.")
"The brown jumped the dog."

julia> f(2, "This is a sentence.")
"This a"

Jelly, 3 bytes (language postdates challenge?)


splits the first input on spaces; m takes every nth element; then K joins on spaces again. m is missing a second argument, so it takes the second input by default.

Try it online!


12-basic, 42 bytes




Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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