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 challeng...
For further actions, you may consider blocking this person and/or reporting abuse
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.
Amazing! You are a CSS master!
Good job! :)
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
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
The shortest one by far
It's also fast.
Ruby solution
ReasonML / OCaml
Runnable example: sketch.sh/s/RvRBbbn6iqnEIumBYs7UwE/
My solution for Swift :
Dirty Nim, planning to rewriting. :)
Erlang
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 -
Too late but here it is anyway...
PHP:
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!Just submitted my kata. <3 codewars.
Start from the middle, and go on appending stars to the top and bottom respectively.
Python:
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...
PHP
Playing with StringBuilder on C#
And Visual Basic.NET
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
A bit late, but with functional flavoured C#
No one will see this but I spent a couple of hours trying to figure it out:
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
Haskell
Let's go functional in Perl! (Tests included).
JS
Here's something with C++ (My personal favourite)
Clojure:
as pseudocode:
Here my solution: form the upper side and reuse it.
Assuming string concatenation has linear execution time, this solution should be O(middle*num). Please correct me if I'm wrong
This took me longer than it should have. But I haven't worked with R in almost 2 years. Sure you could do it more efficiently with just a loop, but I learned some stuff about concatenating matrices to strings in R this way so I call it a win.
R:
ruby diamonds? Okay!
JS
Python
C# Just two main loops to print upper and lower part. One loop to calculate no of rows
Rust
SQL (Postgres)
Extremely late to the party but here's my solution in JS:
Dart
My solution using Python :)
JavaScript
Ruby
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.
JS
C++
if (middleRow % 2 != 0 && middleRow > 0) {
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?
Progress 4GL solution:
Code in the ABL Dojo here
Python :
Ruby Language Version
output
My go exercise two
I didn't find any good solution to abs integer in go :(
Very late to the party here, but wanted to post another python solution.
Takes advantage of using string formatting to center the text, simplifying the code, also using chain from itertools to stack to range functions together.
My solution in js
JS
Now with contrast, invertability, custom strings and other completely unnecessary bells and whistles...
Nice one!
That's amazing >_<
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;
}
}
JavaScript
WOW! 😍
Ahh you are 100% correct! I was confusing myself with Natural Numbers (which 0 is NOT), but that's unrelated to even/odd!
Good call out!
Late to the game here, but here you go, a JS implementation.
Javascript
Javascript ans.