There is this hackerrank question, where we need a rhombus of letters. As to my opinion it can be viewed as some fractal, or similar logic.
Well I solved the question, but I am obviously disappointed with the code I come up with. Actually when I started writing I knew how I would end-up and I was ready to ask a question here.
I am reluctant to share this code of mine but it is necessary, if I am to improve.
size = int(raw_input()) alphabet = "abcdefghijklmnopqrstuvwxyz" def draw(size): shape = "" # The answer tmp_size = size # defining a temporary size variable to help with the solution tmp_size -= 1 while tmp_size+1: hyphen_count = (size-tmp_size) *2 - 2 # Needed hyphen count for the left-right of new_line hypens = hyphen_count*"-" # Hyphens to be added # The new line, the strange ranges I used was to get the correct letters in place. Probably needs simplifying. new_line = "-".join([alphabet[abs(num)] for num in range(1-size,tmp_size-size+1) + range(size-tmp_size-1, size)]) # Draw one line if it is the first line if tmp_size == size - 1: shape = shape + new_line # Draw to both top and bottom if it is not the first line. else: shape = hypens + new_line + hypens + "\n" + shape + "\n" + hypens + new_line + hypens tmp_size -= 1 return shape print draw(size)
I added many comments for easier evaluation of this badly-written code. So what I ask is:
- Dear users with algorithm experience, please help me find a better method if not the code itself. (Maybe a recursion ?)
- Dear programmers, please help me implement better python.
- Dear users that know everything, please teach me everything.