Here's to all the new programmers! I, too, am quite new to programming and want to help you guys be better at programming. I will post programming tasks from time to time and ask you guys to try and code something that can solve this. If you manage to, congratulations! If you don't, I will post the answer (mostly in C++ cause that's my dominant language) after a few days so you can analyze it.

For this task:

Plarry the Dinosaur needs to write a composition of $n$ lines. Being an evil teacher, even after Plarry has written his composition, now the teacher wants Plarry to output every third word! Being a lazy dinosaur, he wants you to help him. Make a code that given, $n$ and Plarry's composition, will output every $3$rd word.

**Sample input:**

$7$

This

Is

Seven

Words

Or

More

Than

**Sample Output:**

Seven

More

Congratz to those who have solved it, i'll post the answer 2 days later ^.^

Feel free to look at the answers below for reference, mine is quite similar to Daniel's but I feel that taking a look at the other languages would have benefits too!

No vote yet

1 vote

×

Problem Loading...

Note Loading...

Set Loading...

$</code> ... <code>$</code>...<code>."> Easy Math Editor

`*italics*`

or`_italics_`

italics`**bold**`

or`__bold__`

boldNote: you must add a full line of space before and after lists for them to show up correctlyparagraph 1

paragraph 2

`[example link](https://brilliant.org)`

`> This is a quote`

Remember to wrap math in $</span> ... <span>$ or $</span> ... <span>$ to ensure proper formatting.`2 \times 3`

`2^{34}`

`a_{i-1}`

`\frac{2}{3}`

`\sqrt{2}`

`\sum_{i=1}^3`

`\sin \theta`

`\boxed{123}`

## Comments

Sort by:

TopNewestHere is a python2 solution below:

Log in to reply

In C:

Log in to reply

## C++

Log in to reply

I don't really think the string library was required but the code pretty much works :D

Log in to reply

yes, the string library is required or else cin and cout won't work, based on my experience

Log in to reply

Log in to reply

Log in to reply

Log in to reply

Log in to reply

Log in to reply

Log in to reply

One-liner Python, but clearly isn't readable. Basically,

`range(int(input()))`

makes a loop of`n`

iterations where`n`

is the number in the input, then`[input() for _ in ...]`

creates a list of the words, iterating`n`

times, then`...[2::3]`

means taking from the 2nd item (0-based, so 1-based means from the 3rd item) and taking every third item afterwards, then`"\n".join(...)`

means joining all the items, inserting a newline character (`\n`

) between items, and`print(...)`

displays the resulting string.Let's play code golf! Can you make a program (in any language, but not one specifically designed to solve this particular problem) that solves this in as few characters as possible? The above is 62 characters (61 if you count

`\n`

as one character).Log in to reply

Here is a pascal solution below:

`var n,i,j:longint; arr:array [1..1000] of string; begin readln(n); for i:=1 to n do begin readln(arr[i]); end; for j:=1 to n do begin if (j mod 3)=0 then writeln(arr[j]); end; end.`

Log in to reply

I tried running this code and they gave me "Error - Program name is missing in main file". Could you be missing something? (Sorry not that familiar with pascal)

Log in to reply

You can practice here Hackerrank

Log in to reply

## JavaScript

Log in to reply

here is the answer:

Log in to reply

Ruby solution:

`lines = ["7","This", "Is", "Seven","Words", "Or", "More] 0.step(lines.length-1,2) {|index| puts lines [index]}`

Log in to reply

python, using the standard library

Log in to reply

Not effective Python solution but works.

Log in to reply

Incorrect, because the problem doesn't ask you to output

`"How many words:"`

,`"Enter x words.."`

, and the list of words. Your output must be precisely as required.Log in to reply

In JAVA :

Log in to reply

Log in to reply

MATLAB

Log in to reply

the 2nd Input Number should have been Input Word. copy/paste mistake

Log in to reply

This is in Microsoft Small Basic :)

Log in to reply

Haskell solution:

-- 31 characters. I win!

Why are people learning C++ when Haskell is

obviouslymore awesome? :DLog in to reply

## PHP:

Log in to reply

Thanks to everyone who tried, anyway here was my solution:

Language: C++

But i feel that Iamsudip and Daniel's answers were better to please refer to theirs (for mine i didnt need string library i think i use a diff version)

Log in to reply

Python 3 solution. (Note: this will ONLY work in Python 3 because print is treated as a function and NOT a keyword as Python 2 does):

Sample output:

Input:

Prints:

Log in to reply

Void main() { Int I,n; Char ch[10][10]; Cout<<"enter no of lines"; Cin>>n; For(I=0;I<n;I++) {Cin>>ch[I]; } For(I=0;I<n;I++) { If((i%3==0)&&(I!=0)) Cout<<ch [I]; } Getch (); }

Log in to reply