Welcome to Day 2 of our challenge series. Today, you’ll be using some clean and polished code to create a clean and polished diamond.
Our challenge comes from user @jayeshcp on CodeWars.
Your task is to return a string that displays a diamond shape on the screen using asterisk (“*”) characters.
The shape that the print method will return should resemble a diamond. A number provided as input will represent the number of asterisks printed on the middle line. The line above and below will be centered and will have two less asterisks than the middle line. This reduction will continue for each line until a line with a single asterisk is printed at the top and bottom of the figure.
Return
null
if input is an even number or a negative number.Note: JS and Python students must implement
diamond()
method and returnNone
(Py) ornull
(JS) for invalid input.
Bonus points awarded for experimenting with any extra features.
Good luck!
Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge for a future post? Email yo+challenge@dev.to with your suggestions!
Latest comments (85)
Implemented this in rust with some tests. I am still learning and got a lot of experimentation done. Idk how much it matters in this case but I tried pre-allocating capacities for any vectors or strings.
At first I used modulo to get uneven numbers in the for loop but I got stuck doing it.
Fond a solutuion here in the comments of a guy using abs-values, that inspired me to instead only create the diamond up to its mid-row and then reflect it around that row.
Could maybe improve and use iterators instead of for-loop, idk?
with.capacity(), seems to take usize only, some improvement could be done here, for example I convert to usize for each capacity call.
Adding the padding at the end of a line isnt really neccessary?
No one will see this but I spent a couple of hours trying to figure it out:
Javascript
Javascript ans.
Couldnt find java one here. so uploading mine (suggestions are welcome)
if (base % 2 != 0 && base>0) {
int spaceDiff = (base / 2);
int star, row, order = 2;
// upper and middle
for (row = 0; row < base; row++) {
for (int space = spaceDiff; space > order - 2; space--) {
System.out.print(" ");
}
++order;
for (star = 0; star <= row; star++) {
System.out.print("");
}
System.out.println();
++row;
}
//bottom
order = spaceDiff;
for (row = base - 2; row > 0; row--) {
for (int space = order; space < spaceDiff + 1; space++) {
System.out.print(" ");
}
--order;
for (star = row; star > 0; star--) {
System.out.print("");
}
System.out.println();
--row;
}
}
one line functional python, why I dunno.
Sorry for that (JS)
Here is my simple diamond solution with Python:
JS
Powershell
It will automatically center to the specified line width, and requires that the line be at least as wide as the diamond itself.
David F.
Just used some for loops to print all of the asterisks, after doing a check for over 0 and odd, and added some css to make the shape of a diamond.
Codepen
The CSS making the asterisks look like a diamond
Ruby
My solution using Python :)
Dart
My go exercise two
I didn't find any good solution to abs integer in go :(