2

I am trying to create a program in Python that creates a snowflake based on the input of a number. Below is my code:

n = int(input())
a = [["."] * n] * n

temp = n/2
start_point = 0
mid_point = int(temp)
end_point = n - 1

for i in range(n):
    if i > mid_point + 1:
        start_point -= 1
        end_point += 1

    for j in range(n):
        
        if (j == start_point) or (j == mid_point) or (j == end_point) or (i == mid_point):
            a[i][j] = "*"
        else:
            a[i][j] = "."
    
    
    if i < mid_point - 1:
        start_point += 1
        end_point -= 1


for row in a:
    print(' '.join([str(elem) for elem in row]))

For example, if the input is '5' the output should look like:

* . * . *
. * * * .
* * * * *
. * * * .
* . * . *

However, my output looks like:

. * * * .
. * * * .
. * * * .
. * * * .
. * * * .

I was sure that my code was correct so I rewrote it in Java as:

public class Snowflake {
    
    public static void createSnowflake(int n) {
        String[][] array = new String[n][n];

        float temp = (float) (n/2);
        System.out.println(temp);
        int start_point = 0;
        int mid_point = (int) (temp);
        System.out.println(mid_point);
        int end_point = n - 1;

        for(int i = 0; i < n; i++) {
            if(i > mid_point+1) {
                start_point--;
                end_point++;
            }
            for(int j = 0; j < n; j++) {
                if((j == start_point) || (j == mid_point) || (j == end_point) || (i == mid_point)) {
                    array[i][j] = "*";
                }
                else {
                    array[i][j] = ".";
                }
            }
            
            if(i < mid_point-1) {
                start_point++;
                end_point--;
            }
        }
        
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < n; j++) {
                System.out.print(array[i][j]);
            }
            System.out.print("\n");
        }
    }

    public static void main(String[] args) {
        createSnowflake(5);
    }
}

And it worked as expected. To my eyes the underlying logic is exactly the same, and yet the Java code works and the Python code doesn't. Could someone help me find where I've made a mistake in the Python syntax or how my Java code somehow differs from it?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 2
    Does this answer your question? [List changes unexpectedly after assignment. How do I clone or copy it to prevent this?](https://stackoverflow.com/questions/2612802/list-changes-unexpectedly-after-assignment-how-do-i-clone-or-copy-it-to-prevent) – norok2 Dec 17 '20 at 23:02
  • No, I'm afraid it doesn't even attempt to as it addresses something different. – Joel Pearce Dec 17 '20 at 23:17
  • 2
    I believe that @norok2 was trying to say that you shouldn’t create `a` like that for this purpose(if you read the explanation on the question he showed). – willcrack Dec 18 '20 at 00:06
  • Try `a=[[“.” for i in range(n)] for j in range(n)]` – willcrack Dec 18 '20 at 00:09
  • somewhat related: [matplotlib snippet to plot a fractal snowflake](https://matplotlib.org/3.3.2/gallery/lines_bars_and_markers/fill.html?highlight=fill) – Pierre D Dec 18 '20 at 01:24

1 Answers1

2

If you change the creation of a to:

 a= [["." for j in range(n)] for i in range(n)]

it should fix it.
This has to do with the way python copies lists.
Check the question linked on the comments to your question.

Enjoyed this question, I feel like it could only be here during this time of the year.

willcrack
  • 1,794
  • 11
  • 20