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!
Discussion
CSS
Some people will claim that I am cheating with this one; and they are probably right... but it was fun to develop and it kind of works (although only with odd numbers π). The idea is:
Here there is a working demo on Codepen:
Wow amazing one again! I don't think it's cheating at all, just a creative solution to the problem!
Thanks!
Yesterday's solution was definitely more cheating than this one.
Good job! :)
Amazing! You are a CSS master!
After @andrewbrown shamed us all yesterday for not having test cases, I decided I needed to step up my game and went full TDD with this one!
Rust Solution:
+1 for Rust
+1 for TDD
Nice!
Hey! Also JavaScript
This is my favorite JS answer to this challenge π I like how you used the repeater, keeps things clean & compact.
Haskell!
Go (with bonus diamond of diamonds) playground link
Woah that's cool! I wanted to see what your diamond of diamonds looked like!
Thought I'd paste in the medium sized one, and let people go to the playground for the big one!
JS β€οΈ
BASH
Ruby solution
Ruby
I didnβt follow the fine print so Iβm not sure this totally fits the spec now that Iβm reading more carefully.
Iβll be more careful tomorrow π
Python
Refactored slightly, used range function on the initial passed in value, instead of adding
1
, but I came across your solution which both checked for negative numbers and how to elegantly print the row in one line. I also through in a check for even values at the beginning.I would like to add:
1) Making
str.center
do some of the dirty work2) Using ranges (and
chain
) to iterate up and down instead of keeping a state onnum
It might be more readable to have two loops instead of using
chain
but I likechain
ing more because I don't need to repeat theprint
Is this fast too?
I wouldn't worry about it too much, since
print
requires IO, it is by far the bottleneck.For the rest of the utilities that I used:
range
,chain
, andstr.center
: they are all implemented inC
if you are using the standardCPython
(should be fast).To avoid the IO, let's compare the two functions as generators of strings (I replaced the
print
withyield (('*' * i).center(n))
for both my implementation and Nicks:Seems like mine is slower than Nicks.
However, on my machine, a single print statement takes about 4.17us, which is almost as long as
diamond(11)
takes without any prints!WOW! π
The shortest one by far
It's also fast.
My solution for Swift :
ReasonML / OCaml
Runnable example: sketch.sh/s/RvRBbbn6iqnEIumBYs7UwE/
PHP
Gave it a try in Python. Not exactly as required since the diamond is printed on-the-fly, but I think it shows the main idea to create every line one after the other. Could also be stored in a string and then returned...
Just submitted my kata. <3 codewars.
Start from the middle, and go on appending stars to the top and bottom respectively.
Python:
Javascript
You can change the characters which are considered blank and full to create different effects.
normal output -
with blank="*" and full="@" -
If you mess with the fill/blank values, you get some nice art -
Did this in rust too:
On invalid input, it returns -
Dirty Nim, planning to rewriting. :)
Too late but here it is anyway...
PHP:
Erlang
My solution using Python :)
JavaScript
Ruby
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
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.
Here is my simple diamond solution with Python:
Sorry for that (JS)
one line functional python, why I dunno.
deviated from the spec in one specific way where I don't return null if a an even number or negative number is provided but instead throw an error.
I feel however this gives more information to the user.
Another π
Python