1

I know very little on Python, but I'm quite experienced in C++. I was looking for an algorithm that would loop through the points in a hexagon pattern and found one written in Python that seems to be exactly what I need. The problem is that I have no idea how to interpret it.

Here is the Python code:

for x in [(n-abs(x-int(n/2))) for x in range(n)]:
    for y in range(n-x):
        print ' ',
    for y in range(x):
        print ' * ',
    print

I'd show you my attempts but there are like 30 different ones that all failed (which I'm sure are just my bad interpretation).

donkopotamus
  • 22,114
  • 2
  • 48
  • 60
Corey Iles
  • 155
  • 1
  • 17
  • 1
    I'm voting to close this question as off-topic because Stack Overflow is not a code conversion service. – Captain Obvlious Oct 03 '15 at 03:33
  • 1
    StackOverflow is however a place to come for help. It's not off topic at all. Trial and error has so far failed me, and when that happens I come here for help, to learn. This is a very valid question here and similar conversion questions were answered and not closed. [I do however respect you coming out clean on WHY you think it's off topic, usually people don't do that.] – Corey Iles Oct 03 '15 at 03:53
  • The console example given by that exact code was a valid hexagon though. I have no experience in Python. – Corey Iles Oct 03 '15 at 04:03
  • As it stands, the indentation on that *exact* code was incorrect ... (in c++ that's like putting the {} in the wrong places) ... that may be why you had trouble replicating it. – donkopotamus Oct 03 '15 at 04:17

2 Answers2

0

Hope this helps you.Tested both python and c++ code .Got same results.

#include <iostream>
#include <cmath> 

using namespace std;

int main() {
    // your code goes here
    int n = 10;
    int a[n];

    for(int j = 0; j < n; j++) {
       a[j] = n - abs(j - (n / 2)); 
    }

    for(int x = 0; x < n; x++) {
        for(int y = 0; y < (n - a[x]); y++) {
            std::cout << " " << std::endl;
        }
    }

    for(int k = 0; k < a[n-1]; k++) {
        std::cout << " * " << std::endl;
    }

    std::cout << "i" << std::endl;

    return 0;
}
SIRAJ V K
  • 45
  • 5
  • That's actually very similar to what I tried and failed. I'm kind of surprised how close I was. Thanks! (I can't test it at the moment but I'll go ahead and mark it since it looks promising.) – Corey Iles Oct 03 '15 at 04:23
  • Could you tell me what each the 'L's, '*'s, and 'i' stand for? – Corey Iles Oct 03 '15 at 04:28
  • I don't understand how this is anything related to the answer I need. This just prints a bunch of L's and *'s, and a single i. – Corey Iles Oct 03 '15 at 05:11
  • @CoreyIles i just replaced ' ' with 'L' just to check ,how many times its occuring.Python code prints ' ' Horizontally after that prints "*".Only difference in this code ur Pyhton code prints horizontally,but C++ code prints vertically.Check log – SIRAJ V K Oct 04 '15 at 03:34
0
auto out = ostream_iterator<const char*>(cout," ");

for(int i = 0; i != n; ++i) {
    auto width = n - abs(i - n/2);
    fill_n(out, n-width, " ");
    fill_n(out, width, " * ");
    cout << "\n";
}

Live demo.

Python live demo for reference.

Chris Drew
  • 14,926
  • 3
  • 34
  • 54