6 kyu

Give me a Diamond

7,399 of 37,553jayeshcp
Description
Loading description...
Strings
ASCII Art
Fundamentals
  • Please sign in or sign up to leave a comment.
  • lotheravanti Avatar

    Diamonds are forever

  • ejini战神 Avatar

    No random tests in Java

  • jdizzydizzle Avatar

    had to turn to chatgpt to figure out how to center the stars and the solution was so clever. If you know, you know.

  • nilsmartel Avatar

    this would be much easier and less confusing, if we'd use "_" instead of " " characters.

  • albatronix Avatar

    This comment has been hidden.

  • user2076228 Avatar

    So.... Is Jamie trying to smash James's girlfriend? I am very confused. Am I Jamie?!?!? My name is not Jamie. What does James's girlfriend look like? Maybe I can be Jamie.

  • metatable Avatar
  • sidwick Avatar

    Nice Kata!

  • talerzdebi Avatar

    30 minutes spent cuz didnt see that last star should also have \n .....xd

  • MoonPresident Avatar

    My Rust solution should work, but adding the appropriately placed newline characters gives this result. This only pops up when the answer is correct - if I add a newline character at the start of the string, the test fails and no error occurs.

    memory allocation of 9223372036854775806 bytes failed

    Caused by: process didn't exit successfully: /workspace/target/debug/deps/challenge-07bd72047d917620 --show-output -Z unstable-options --format json (signal: 6, SIGABRT: process abort signal)

  • Koii Avatar

    This one really took a toll on me! TvT

  • CodingDochi Avatar

    This comment has been hidden.

  • Xros22 Avatar

    fiddly, im not sure what i learnt from this

  • SabbyDude Avatar

    getting this problem '\n\n*\n' should equal ' \n\n *\n' can anyone help?

  • iNikAnn Avatar

    I spent too much time on this 😒

  • saudiGuy Avatar

    python new test framework is required. updated in this fork

  • Webscum Avatar

    Think i am making a mistake somewhere but i don't know where, any suggestions or possible mistakes i could have made. (in C)

  • Adi Panchal Avatar

    Thank you for this kata! Really enjoyed it! Gave myself an extra challenge for doing it in C.

  • notRene Avatar

    How did you guys manage the formatting with JavaScript?

  • ATechAjay Avatar

    HELP, What does it mean?

    expected '*\n' to equal '*\n'

  • KayleighWasTaken Avatar
  • Hlopchek Avatar

    Really nice kata, but there is a problem with Swift. Some of tests fails till change return type from String to String?

  • possiphil Avatar

    For Swift: You have to manually change the return type from String to String? to solve this (Maybe this will get fixed in the future).

  • theverynvg Avatar

    i used expressions like f'{"*" * i:^{n}}' and i've got a diamond,but it won't accept my solution.I got a result that is equal to the test result

  • SacredRider02 Avatar

    Traceback (most recent call last):

    File "/workspace/default/tests.py", line 6, in

        test.assert_equals(diamond(1), "*\n")
                           ^^^^^^^^^^
    
  • Budkin Avatar

    Hello! My language is JavaScript. I got a result that is equal to the test result absolutely so I cannot understand what a problem passing the test is. I need your help.

  • davletlinar Avatar

    Noone wrote a code in the shape of diamond, though) Well, who'll take the challenge?

  • paul_merzliakov Avatar

    I think James needs to find another line of work.

  • Doug Colin Avatar

    This was a very satisfying Kata.

    It took me a LONG time to complete, but for each problem I had with my initial solution, a logical fix emerged.

    Challenging without any funky edge cases, thanks.

  • dtx319 Avatar

    I was able to complete this in Python and really enjoyed it! Great kata!

  • Kimi no Na wa Avatar

    Unhandled rejection TestError: Expected: ' \n*\n \n', instead got: ' \n\n *\n' they are identical. What is the roblem here?

  • ПавелРАЗ Avatar

    This comment has been hidden.

  • ejini战神 Avatar

    C# Function name should use PascalCase

  • Poperos1 Avatar

    This comment has been hidden.

  • NikRuchkin1 Avatar

    This comment has been hidden.

  • trashy_incel Avatar

    C translation 💎 (author gone)

  • akar-0 Avatar
  • user3815974 Avatar

    This comment has been hidden.

  • LeAlbert Avatar

    This comment has been hidden.

  • NewOne789 Avatar

    Just three sentences (about guy and his girlfriend) at the start of the kata's discription, but it make this kata more "warm" and interesting. This is better than typical "okay, dude, here the list and here the task" in most another katas.

  • RNOH Avatar

    nice kata~!

  • nayrem Avatar

    This comment has been hidden.

  • SemenTishin Avatar

    This does not work on Swift because you need to return either nil or String, but both options cannot be made in Swift. Either String or "nil"

  • haynesm1 Avatar

    I only had one problem with this task, the formatting for the result string was a little difficult to match. It initially seemed that the string was padded with spaces on both sides of the stars but it was not all of the time. It seemed as if the desired string by the tests would change. It may just be a qlitch but thought I would share.

  • TuCrG123 Avatar

    This comment has been hidden.

  • user9644768 Avatar

    Ruby 3.0 should be enabled, see this to learn how to do it

    Please organize the structure of test fixture as following (although it has been mentioned in the attached link, I'm repeated here again)

    describe "<This message should describe the categories of test groups inside this block>" do
      it "<A short message describing this test group>" do
        expect(...).to eq(...) #Assertions
        #Or Test.assert_equals(user_response, reference_response)
      end
    end
    
  • straightpath Avatar

    Please HELP, check the @verdugo's issue* below. There is a problem in Swift version of this question.

    Best

  • dogma-n Avatar

    jamie bouta swipe james' girl.

  • marklar909 Avatar

    Whew, my answer is way so much sloppier than most others. But hey, it worked!

  • DN20 Avatar

    This comment has been hidden.

  • webdev3 Avatar

    This type of problem really shows my weakness as a developer and makes me want to quit my career. I don't know how people can visualize and break down problems like these and come up with a solution. It makes me feel like a friggin idiot.

  • user9298165 Avatar

    I’m really frustrated with this.

    I either get:

    1. cannot force unwrap value of non-optional type 'String' XCTAssertEqual(diamond(3)!, " *\n***\n *\n”) or

    2. XCTAssertEqual failed: ("Optional("nil")") is not equal to ("nil") .

    The test looks like it’s force unwrapping a non-optional type ‘String’, so is the test wrong? Also, if I change that test to not force unwrap diamond(3)!, I get the second error, ("Optional("nil")") is not equal to ("nil"). Sometimes I get that error 3 times. No idea what to do. Also, all my tests work fine locally. Negative numbers, 0, 1, even numbers, and large numbers all work fine.

  • Eric-Hern Avatar

    I'm struggling to figure out how to 'center' the diamond. I'm able to output a diamond based on the input of an odd positive integer, but my diamond lines are justified on the left margin. I need to add the appropriate amount of spaces to the left side of each line (well, each line except the 'center'/ largerst line) - just need to come up with a clean way of doing it... Any suggestions? I'm using python.

  • DHoffman Avatar

    Sorry if this is just me being dumb, I'm stuck with the error: Test Failed Expected: null But was: <string.Empty>

    I am returning a null value with : return null;

    Is this not the way to do it in C#? I've tried googling but found nothing but discussion on the difference between null and String.Empty, which doesn't exactly help me.

  • PavelLazarev Avatar

    so bed tests

  • ujfasci Avatar

    Originally, as the instructions stated, I used null to return when n was either negative or even. However, only None is accepted.

  • CMEXOTPOH Avatar

    Shine on, you crazy diamond!

  • svesang Avatar

    Hi. can't really seam to get this one to work in java, when i run it on my own computer it works just fine, but when i test it here it fails, don't know if this is because i have done somthing wrong in my code or if its the test. If anybody have managed to clear it in java please let me know :D

  • nos373737 Avatar

    For what reason in the test add space before result string and in the reverse loop the same spaces but after each element?

  • nikkiricks Avatar

    Skipping based on the cliche sexism.

  • rcfroggatt786 Avatar

    Just done the JavaScript version (and passed). However the description is not showing the multiple spaces for size 5 diamond. It shows as:
    ...that is: " *\n ***\n*****\n ***\n *\n"
    which should instead be something like:
    ...that is:

    "  *\n ***\n*****\n ***\n  *\n"
    

    Thanks.

  • bucephalus Avatar

    Great Kata. I really enjoyed this one!

  • kolowert Avatar

    I think something is wrong with testing here. On my computer, all these tests pass. But here, on the server, it doesn't.

  • GianlucaBianchi Avatar

    Hello everyone, for some reason I cannot make a for loop work. Does anyone else has the same issue?

    I tried a simple loop as a test but the program clearly skips them; The solutions it gives me are totally empty, because I am adding "*" and " " inside nested for loops. The tests with 0, even numbers or negative numbers pass without problem.

    I wrote:

    for (int i = 0; i > 5; i++){System.out.println("BEANS!");}

    but no BEANS!BEANS!BEANS!BEANS!BEANS!BEANS! in the log :/

    Edit: this happened to me on other katas, but I just used a for each loop and no problems. I even tried a while loop and printed something, that works just fine...

  • zLuki Avatar

    Beautiful kata! Print the string before returning it, if you want to see them :)

  • Kimapr Avatar

    Return null/nil/None/... if the input is an even number or negative, as it is not possible to print a diamond of even or negative size.

    Challenge accepted! (even-sized diamonds are totally possible)

  • vladkrutenyuk Avatar

    bad tests for task: why should there be at the end \n ? e.g. if n = 1 must be return \n (it may be just ), if n = 3 \n\n\n but \n\n will be th same visual result it is not necessary!

  • jsuisnul Avatar

    Go translation

  • caitlinmaree Avatar

    How does the instructions say "no trailing spaces" but then I get this result from one of the tests?

    Expected: ' *\n \n**\n ***\n *\n', instead got: ' *\n \n**\n ***\n *\n'

    I originally had no spaces but the code failed. And now the test expects random extra spaces between them? Especially weird considering the previous test passes with:

    Test Passed: Value == ' \n**\n *\n'

    But the second test wants two spaces before the first astrix?

  • user5973621 Avatar

    This comment has been hidden.

  • gvilardo Avatar

    This comment has been hidden.

  • nasdda Avatar

    Not sure what this error means: expected: < *


    *[ ]> but was: < *


    *[]>

    The only difference between the 2 are the [] but I can't figure what's causing it. The program output is just as expected, and I made sure there wasn't any trailing white spaces.

  • aprettyloner Avatar

    Response received but no data was written to STDOUT or STDERR.

  • JohanWiltink Avatar

    This comment has been hidden.

  • ᛚᚨᚱᛊ ᚺᛖᚾᚱᛁᚲ Avatar

    Python: No randomized testing.

  • anter69 Avatar

    Ruby translation kumited, including updated description. Please check and approve

  • c0dr Avatar

    This comment has been hidden.

  • kamillamagna Avatar

    lol f*** you, James, I buy my own diamonds

  • user9077868 Avatar

    I might be picky here, but it says "must implement the diamond(n) method". As far as I know, method is a function that's called from a certain object or a class, but here we have a plain function.

  • nitwit Avatar

    my codes include print('something', end='') which is unavailable in python2 :/

  • renldx Avatar

    Passing every single test except ONE in RandomTest/Tests Random Diamonds, 4th from last (just before 49). I get

    XCTAssertEqual failed: ("Optional("*\n")") is not equal to ("nil") -
    which makes no sense because I return nil the second the size parameter is even or < 3. I can't see what the test case is doing so there's no way for me to understand what's going on. Using Swift 4.

  • pooh-reddy Avatar

    STDERR : Max Buffer Size Reached (1.5 MiB). I'm getting this error inspite of all the test cases being passed. Help Please?

  • zezek Avatar

    Hi there,

    I'm seeing Execution Timed Out (12000 ms) for the random test but I can easily test sizes past 1000. Is this the same issue that Fr3qu3ntFly3r had?

    Thanks!

  • Fr3qu3ntFly3r Avatar

    Hey guys,

    I have the same ServerTimeOut error, when I attempt.

    I don't know what is in random tests, but in the sample test I tested it with Int.max and it passes without a problem.

    Some pointers would be highly appreciated.

    Thanks in advance.

  • vantango Avatar

    I think this is why I give up with Swift in codewars. The tests run and pass in 6ms but the process timesout in 12000ms and fails.

    Passed: 2 Failed: 0 Errors: 1 Test Results: SolutionTest Tests From Example (pass in 4.5ms) Tests Large Diamonds (pass in 0.3ms) Completed in 6.611ms RandomTest STDERR: Process was terminated. It took longer than 12000ms to complete

  • Kotey Avatar

    Got daimonds with \n tags and it cannot accept it as vaild answer. Why? c# Ok i Got it - I need to return whole string, not write it with Console.Write(temp); My bad.

  • lamour19 Avatar

    This comment has been hidden.

  • tragicmuffin Avatar

    Interesting problem, but the description was very unclear. I had to try a couple different things and see the errors just to understand what it was asking for. At the very least, having more sample tests showing a range of desired results would make it easier to figure out what's being asked.

  • matt.southward Avatar

    The instructions indicate "The line above and below will be centered and will have 2 less *’s than the middle line." The sample test expects " *\n*** *\n" which aligns things to the right, not centered.

  • Gorillapaws Avatar

    This comment has been hidden.

  • ZerNox Avatar

    This comment has been hidden.

  • AviC Avatar

    This comment has been hidden.

  • amypellegrini Avatar

    From what I see, the tests don't fully meet the requirement. Am I suposed to add more tests?

  • cshr25 Avatar

    This comment has been hidden.

  • ZerNox Avatar

    gah ' * \n*\n * \n' should equal ' \n\n *\n'

  • williamgp Avatar

    Swift 3.1.1.... can't submit a valid solution. It times out.

  • Suncake Avatar

    This comment has been hidden.

  • Un_Boeing Avatar

    My code passes the sample tests and runs great in a playground, but when I click "Attempt" I get a Server Error: [object Object] output. Any idea what that means?

  • EugenSleptsov Avatar

    I pass all sample tests but when trying attempt tests I have an error: [object Object]

  • eric.johnson Avatar

    "The shape that will be returned from print method resembles a diamond ..."

    Like others here I spent a ton of time displaying the diamonds in the diamond function instead of returning a string that has the appropriate amount of whitespace and new line returns so the shape can be manufactured by the calling routine.

    For me, the instructions are not clear enough for beginners learning python.

  • victorep Avatar

    #java I'm passing the tests but why I run attempt I get timeout errors. I don't get it, the code does not take that long...

  • dribbles Avatar

    For those using Python, be aware that this Kata is only working for Python 2.7.6 , and therefore the functions may act differently than you are used to with Python 3! It caught me out for a while.

  • aaslam.dev Avatar

    This kata seems to have issue with running tests using Swift 3.0 All sample tests were passed. However, when you try to attempt it, it keeps producing ("Optional("nil")") is not equal to ("nil")

    I am sure, I am not returing an optional value. If that was the case, sample tests should not have been passed.

  • ajaymenon0 Avatar

    This was quite a satisfying problem to work on. The trailing spaces once was a bit of an issue since it's not clearly visible...had to keep highliting the text to check them. The trick is to write down the number of '*' and ' ' in a given line wrt to 'n' on a piece of paper to see an emerging pattern.

  • andres-uris Avatar

    This comment has been hidden.

  • shailu Avatar

    To everyone who has problems with this kata, make sure you don't have any trailing spaces.

  • Aziraphale81 Avatar

    I spent probably far too much time on this, but it was great practice. I didn't have any problem with the description, just with overcoming my own mental hurdles.

  • Overlord-R Avatar

    This comment has been hidden.

  • MacSmo Avatar

    This Kata is a not worth effort and time. Even I print correct diamond I can't pass those test cases.

  • Gendalf_beliy Avatar

    This comment has been hidden.

  • akulchhillar Avatar

    This comment has been hidden.

  • croceflamenco Avatar

    This comment has been hidden.

  • MikeHathaway Avatar

    I'm consistently able to generate arbitrarily large diamonds, however, I'm failing most of the test cases. I've attempted to prepend a new line at the end before the final string return, and still not getting the proper result. Has anyone had other issues with seemingly perfect diamonds failing the tests?

  • benjicwood Avatar

    rubbish task details, don't need the stupid story

  • Adi2Point0 Avatar

    The tests don't pass even if I get correct output. Am I missing something?

    ✘ Expected: <pre>    *
       ***
      *****
     *******
    *********
     *******
      *****
       ***
        *
    </pre>instead got :<pre>    *    
       ***   
      *****  
     ******* 
    *********
     ******* 
      *****  
       ***   
        *    
    </pre>
    

    Another one

    ✘ Expected: <pre>         *
            ***
           *****
          *******
         *********
        ***********
       *************
      ***************
     *****************
    *******************
     *****************
      ***************
       *************
        ***********
         *********
          *******
           *****
            ***
             *
    </pre>instead got :<pre>         *         
            ***        
           *****       
          *******      
         *********     
        ***********    
       *************   
      ***************  
     ***************** 
    *******************
     ***************** 
      ***************  
       *************   
        ***********    
         *********     
          *******      
           *****       
            ***        
             *         
    </pre>
    
  • elDucUrraca Avatar

    This comment has been hidden.

  • steveo42 Avatar

    This comment has been hidden.

  • ben-hatcher Avatar

    Can this kata get a less "girls like diamonds herp" prompt?

  • Vvkmnn Avatar

    This comment has been hidden.

  • samiralibabic Avatar

    This comment has been hidden.

  • RedTriangle53 Avatar

    The task asks you specifically to print the diamond, but the tests only accept return. I spent hours trying to find what I did wrong before realizing that the author of the kata simply just lied. That's not acceptable. Boo.

  • Shadows of Time Avatar

    This comment has been hidden.

  • Mdubs Avatar

    I have a Python solution to this that works with any number I pass to it, but when I submit to this Kata, I'm getting this error:

    ✘ None should equal ' *\n***\n *\n'

    What does this mean?

  • blackblitz Avatar

    This comment has been hidden.

  • Enteleform Avatar

    This comment has been hidden.

  • candyapplecorn Avatar

    I wrote code which correctly produced diamonds, but the test kept failing because apparently my diamond wasn't the same as the desired diamonds. The 'expected' and 'actual' output seemed exactly the same and it took me over an hour to figure out the difference; the test expects a newline appended at the end of diamonds.

    This shouldn't be a requirement. It's not listed in the requirements and the tests don't really show this either. I wasted an hour tracking this down. I want my hour back.

    It doesn't help that the 'actual' test output always has a space character prepended, further confusing me/the programmer.

  • bladez Avatar

    This comment has been hidden.

  • ceocookie Avatar

    Hi its killing me. But why does if not n: return None not satisfy the test case: '' should return None?

  • ravvi Avatar

    I'm able to print diamonds in Eclipse, but I don't know how to return my printed output as a single String like it wants me to. Can anyone offer any hints?

  • Sarci1 Avatar

    I have attempted to submit but get a failure on the 4th test: '' should equal None. I though this might be saying that an empty string for n (i.e. an invalid input) should return None but I got that working and it tested just fine. Could anyone offer insight into what that failure might indicate that they are testing against?

  • ghudson Avatar

    Can someone explain what the square brackets mean in the test cases?


    expected:< *


    [] *

    but was:< *


    [*** ] *


    I'm not sure why my square brackets have three stars in them, but the example doesn't. Also very confused as to where these brackets are comine from. Any assistance would be greatly appreciated!

  • xartx Avatar

    This comment has been hidden.

  • mordoss Avatar

    My diamonds are good, but test still fails. Maybe problem is in outputed string. That should exactly match, not diamonds. Example for string: " \n**\n *\n". So, no space characters on a end of every row. Am I right?

  • rusizzle Avatar

    I am getting the proper diamonds but still failing. Can someone help?

  • tekkies Avatar

    Not sure about the use of platform-specific line ending '\n' - IMHO we should be encouraging the use of techniques like StringBuilder.AppendLine() instead.

  • joyzyfer Avatar

    Where is the error here?

    Expected: *


    • , instead got: *

  • RobbyTassy Avatar

    Can anyone help me understand why I keep getting error TypeError: Object has no method 'repeat' on here? When I run the code on repl.it, it works fine and meets requirements. I've read others have the issue, but the solution is a spoiler so I can't see it. Please help :)

  • rhymefororange Avatar

    Can somebody explain why this doesn`t pass? // * //*** // * None should equal ' \n**\n *\n' It actually draws a diamond.

  • ryanq214 Avatar

    if anyone is having problems with the final tests even though they look the same you apparently have to add one more "\n" to the end. That's all i did and then it passed the final tests. Also my code wouldn't pass the test case because the first test expected result comes out like this //* //*** //* when it's supposed to be centered. so i just went straight to the submit hiddent tests

  • Vanya_16 Avatar

    Why n = 1 should display *


    • this?
  • kn2322 Avatar

    This comment has been hidden.

  • dragzaras Avatar

    This comment has been hidden.

  • oatco Avatar

    if you are getting diamonds but failed just few tcs? make sure you return null for even and Negative numbers also

  • tangtangs Avatar

    I can print out a perfect diamond shape with * characters, but it says I failed.

  • wainage Avatar

    Getting diamonds but not passing the tests? Remember to include the final '\n' in your result!

  • baniplus Avatar

    This comment has been hidden.

  • fanqie Avatar

    This comment has been hidden.

  • NetLagina Avatar

    This comment has been hidden.

  • sofianm Avatar

    (Python) After 3 successful tests I'm getting: Traceback: in in diamond MemoryError

    Can anyone help me figure out what that means?

  • nwr Avatar

    This comment has been hidden.

  • Night Idol Avatar

    This comment has been hidden.

  • bstenzel Avatar

    This comment has been hidden.

  • albarralnunez Avatar

    This comment has been hidden.

  • ZozoFouchtra Avatar

    JS translation kumited.

    (Description modified; 'print' renamed 'diamond' as in Python version)

  • ChristianECooper Avatar

    This comment has been hidden.

  • Mythic Avatar

    This comment has been hidden.

  • g964 Avatar

    This comment has been hidden.

  • Panddha Avatar

    This comment has been hidden.