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 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, and Plarry's composition, will output every rd word.
Sample input:
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!
Easy Math Editor
This discussion board is a place to discuss our Daily Challenges and the math and science related to those challenges. Explanations are more than just a solution — they should explain the steps and thinking strategies that you used to obtain the solution. Comments should further the discussion of math and science.
When posting on Brilliant:
*italics*
or_italics_
**bold**
or__bold__
paragraph 1
paragraph 2
[example link](https://brilliant.org)
> This is a quote
\(
...\)
or\[
...\]
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
Here is a python2 solution below:
In C:
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
One-liner Python, but clearly isn't readable. Basically,
range(int(input()))
makes a loop ofn
iterations wheren
is the number in the input, then[input() for _ in ...]
creates a list of the words, iteratingn
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, andprint(...)
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).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)
You can practice here Hackerrank
JavaScript
here is the answer:
Ruby solution:
lines = ["7","This", "Is", "Seven","Words", "Or", "More] 0.step(lines.length-1,2) {|index| puts lines [index]}
python, using the standard library
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.In JAVA :
MATLAB
Log in to reply
the 2nd Input Number should have been Input Word. copy/paste mistake
This is in Microsoft Small Basic :)
Haskell solution:
-- 31 characters. I win!
Why are people learning C++ when Haskell is obviously more awesome? :D
PHP:
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)
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:
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 (); }