-5
\$\begingroup\$

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.

\$\endgroup\$
8
  • 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, 2015 at 22:23
  • 1
    \$\begingroup\$ Should have posted first and asked questions later. This contest is already over... \$\endgroup\$
    – Dennis
    May 21, 2015 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, 2015 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, 2015 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\$
    – user39326
    May 21, 2015 at 23:15

7 Answers 7

7
\$\begingroup\$

Pyth, 6

jd%Qcz

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

Try it online

Explanation

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

CJam, 7 bytes

q~S/%S*

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

Explanation

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.
\$\endgroup\$
3
\$\begingroup\$

Python, 35 characters

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

KDB(Q), 26 bytes

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

Explanation

             " "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

Test

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

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, " ")
end

Examples:

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"
\$\endgroup\$
1
\$\begingroup\$

Jelly, 3 bytes (language postdates challenge?)

ḲmK

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!

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

12-basic, 42 bytes

?JOIN(INPUT$().SPLIT(" ")STEP INPUT()," ")

Try

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.