Skip to main content
added 13 characters in body
Source Link
GZ0
  • 2.3k
  • 7
  • 19

Here is a review of the solution.

  • ^ is xor in Python. It is not for computation of exponentials.

  • When running code outside a method / class, it is a good practice to put the code inside a main guard. See here for more explanation.

     if __name__ == "__main__":
        ...
    

    When you are providing quick answers on a forum, the guard might not always be necessary. However, if you are writing reusable code for yourself, it is better to adopt the practice.

  • By convention, _ is used to represent a don't-care variable that is unused, which won't be used after itits value is assigned, e.g. v = [[] for _ in range(4)]. It is undesirable to refer to it in an expression like answer[_].

  • The output is a pure text file. It is unnecessary to open it in binary mode (b). The original question does not suggest an append mode (a) either.

  • The q-1 index would no longer work if q is changed to a different group of values. Therefore, it is better to use enumerate in this case:

    for i, q in enumerate(RANGE_Q):
       ...
       export[i, 0] = q
       ...
    

    A better approach is to use zip:

    for q, output_row in zip(RANGE_Q, output_arr):
        ...
        output_row[0] = q
        ...
    
  • The assignments to the output numpy array can be improved, as shown in my solution below.

Here is my solution.

import numpy as np

if __name__ == "__main__":
    # Define constants
    RANGE_Q = range(1, 11)               # Range of q values
    POLY_DEG = 4                         # Degree of polynomial
    OUTPUT_PATH = "/tmp/latest_foo.csv"  # Output csv path

    # Compute roots
    roots_arr = np.array([np.roots([1, 3*q*q, 4*q, np.sqrt(q), q]) for q in RANGE_Q])
    
    # Construct output array and assign values
    output_arr = np.empty(shape=(len(RANGE_Q), POLY_DEG * 2 + 1))
    output_arr[:, 0] = RANGE_Q
    output_arr[:, 1::2] = roots_arr.real
    output_arr[:, 2::2] = roots_arr.imag

    # Save results to output file
    np.savetxt(OUTPUT_PATH, output_arr, fmt="%.4g", delimiter=',')

Here is a review of the solution.

  • ^ is xor in Python. It is not for computation of exponentials.

  • When running code outside a method / class, it is a good practice to put the code inside a main guard. See here for more explanation.

     if __name__ == "__main__":
        ...
    

    When you are providing quick answers on a forum, the guard might not always be necessary. However, if you are writing reusable code for yourself, it is better to adopt the practice.

  • By convention, _ is used to represent a don't-care variable that is unused after it is assigned, e.g. v = [[] for _ in range(4)]. It is undesirable to refer to it in an expression like answer[_].

  • The output is a pure text file. It is unnecessary to open it in binary mode (b). The original question does not suggest an append mode (a) either.

  • The q-1 index would no longer work if q is changed to a different group of values. Therefore, it is better to use enumerate in this case:

    for i, q in enumerate(RANGE_Q):
       ...
       export[i, 0] = q
       ...
    

    A better approach is to use zip:

    for q, output_row in zip(RANGE_Q, output_arr):
        ...
        output_row[0] = q
        ...
    
  • The assignments to the output numpy array can be improved, as shown in my solution below.

Here is my solution.

import numpy as np

if __name__ == "__main__":
    # Define constants
    RANGE_Q = range(1, 11)               # Range of q values
    POLY_DEG = 4                         # Degree of polynomial
    OUTPUT_PATH = "/tmp/latest_foo.csv"  # Output csv path

    # Compute roots
    roots_arr = np.array([np.roots([1, 3*q*q, 4*q, np.sqrt(q), q]) for q in RANGE_Q])
    
    # Construct output array and assign values
    output_arr = np.empty(shape=(len(RANGE_Q), POLY_DEG * 2 + 1))
    output_arr[:, 0] = RANGE_Q
    output_arr[:, 1::2] = roots_arr.real
    output_arr[:, 2::2] = roots_arr.imag

    # Save results to output file
    np.savetxt(OUTPUT_PATH, output_arr, fmt="%.4g", delimiter=',')

Here is a review of the solution.

  • ^ is xor in Python. It is not for computation of exponentials.

  • When running code outside a method / class, it is a good practice to put the code inside a main guard. See here for more explanation.

     if __name__ == "__main__":
        ...
    

    When you are providing quick answers on a forum, the guard might not always be necessary. However, if you are writing reusable code for yourself, it is better to adopt the practice.

  • By convention, _ is used to represent a don't-care variable, which won't be used after its value is assigned, e.g. v = [[] for _ in range(4)]. It is undesirable to refer to it in an expression like answer[_].

  • The output is a pure text file. It is unnecessary to open it in binary mode (b). The original question does not suggest an append mode (a) either.

  • The q-1 index would no longer work if q is changed to a different group of values. Therefore, it is better to use enumerate in this case:

    for i, q in enumerate(RANGE_Q):
       ...
       export[i, 0] = q
       ...
    

    A better approach is to use zip:

    for q, output_row in zip(RANGE_Q, output_arr):
        ...
        output_row[0] = q
        ...
    
  • The assignments to the output numpy array can be improved, as shown in my solution below.

Here is my solution.

import numpy as np

if __name__ == "__main__":
    # Define constants
    RANGE_Q = range(1, 11)               # Range of q values
    POLY_DEG = 4                         # Degree of polynomial
    OUTPUT_PATH = "/tmp/latest_foo.csv"  # Output csv path

    # Compute roots
    roots_arr = np.array([np.roots([1, 3*q*q, 4*q, np.sqrt(q), q]) for q in RANGE_Q])
    
    # Construct output array and assign values
    output_arr = np.empty(shape=(len(RANGE_Q), POLY_DEG * 2 + 1))
    output_arr[:, 0] = RANGE_Q
    output_arr[:, 1::2] = roots_arr.real
    output_arr[:, 2::2] = roots_arr.imag

    # Save results to output file
    np.savetxt(OUTPUT_PATH, output_arr, fmt="%.4g", delimiter=',')
added 5 characters in body
Source Link
GZ0
  • 2.3k
  • 7
  • 19

Here is a review of the solution.

  • ^ is xor in Python, it. It is not for exponential computation of exponentials.

  • When running code outside a method  /class class, it is a good practice to put the code inside a main guard. See here for more explanation.

     if __name__ == "__main__":
        ...
    

    When you are providing quick answers on a forum, the guard might not always be necessary. However, if you are writing reusable code for yourself, it is better to adopt the practice.

  • By convention, _ is used to represent a don't-care variable that is unused after it is assigned, e.g. v = [[] for _ in range(4)]. It is undesirable to refer to it in an expression like answer[_].

  • The output is a pure text file. It is unnecessary to open it in binary mode (b). The original question does not suggest an append mode (a) either.

  • The q-1 index would no longer work if q is changed to a different group of values. Therefore, it is better to use enumerate in this case:

    for i, q in enumerate(RANGE_Q):
       ...
       export[i, 0] = q
       ...
    

    A better approach is to use zip:

    for q, output_row in zip(RANGE_Q, output_arr):
        ...
        output_row[0] = q
        ...
    
  • The assignments to the output numpy array can be improved, as shown in my solution below.

Here is my solution.

import numpy as np

if __name__ == "__main__":
    # Define constants
    RANGE_Q = range(1, 11)               # Range of q values
    POLY_DEG = 4                         # Degree of polynomial
    OUTPUT_PATH = "/tmp/latest_foo.csv"  # Output csv path

    # Compute roots
    roots_arr = np.array([np.roots([1, 3*q*q, 4*q, np.sqrt(q), q]) for q in RANGE_Q])
    
    # Construct output array and assign values
    output_arr = np.empty(shape=(len(RANGE_Q), POLY_DEG * 2 + 1))
    output_arr[:, 0] = RANGE_Q
    output_arr[:, 1::2] = roots_arr.real
    output_arr[:, 2::2] = roots_arr.imag

    # Save results to output file
    np.savetxt(OUTPUT_PATH, output_arr, fmt="%.4g", delimiter=',')

Here is a review of the solution.

  • ^ is xor in Python, it is not for exponential computation.

  • When running code outside a method/class, it is a good practice to put the code inside a main guard. See here for more explanation.

     if __name__ == "__main__":
        ...
    

    When you are providing quick answers on a forum, the guard might not always be necessary. However, if you are writing reusable code for yourself, it is better to adopt the practice.

  • By convention, _ is used to represent a don't-care variable that is unused after it is assigned, e.g. v = [[] for _ in range(4)]. It is undesirable to refer to it in an expression like answer[_].

  • The output is a pure text file. It is unnecessary to open it in binary mode (b). The original question does not suggest an append mode (a) either.

  • The q-1 index would no longer work if q is changed to a different group of values. Therefore, it is better to use enumerate in this case:

    for i, q in enumerate(RANGE_Q):
       ...
       export[i, 0] = q
       ...
    

    A better approach is to use zip:

    for q, output_row in zip(RANGE_Q, output_arr):
        ...
        output_row[0] = q
        ...
    
  • The assignments to the output numpy array can be improved, as shown in my solution below.

Here is my solution.

import numpy as np

if __name__ == "__main__":
    # Define constants
    RANGE_Q = range(1, 11)               # Range of q values
    POLY_DEG = 4                         # Degree of polynomial
    OUTPUT_PATH = "/tmp/latest_foo.csv"  # Output csv path

    # Compute roots
    roots_arr = np.array([np.roots([1, 3*q*q, 4*q, np.sqrt(q), q]) for q in RANGE_Q])
    
    # Construct output array and assign values
    output_arr = np.empty(shape=(len(RANGE_Q), POLY_DEG * 2 + 1))
    output_arr[:, 0] = RANGE_Q
    output_arr[:, 1::2] = roots_arr.real
    output_arr[:, 2::2] = roots_arr.imag

    # Save results to output file
    np.savetxt(OUTPUT_PATH, output_arr, fmt="%.4g", delimiter=',')

Here is a review of the solution.

  • ^ is xor in Python. It is not for computation of exponentials.

  • When running code outside a method  / class, it is a good practice to put the code inside a main guard. See here for more explanation.

     if __name__ == "__main__":
        ...
    

    When you are providing quick answers on a forum, the guard might not always be necessary. However, if you are writing reusable code for yourself, it is better to adopt the practice.

  • By convention, _ is used to represent a don't-care variable that is unused after it is assigned, e.g. v = [[] for _ in range(4)]. It is undesirable to refer to it in an expression like answer[_].

  • The output is a pure text file. It is unnecessary to open it in binary mode (b). The original question does not suggest an append mode (a) either.

  • The q-1 index would no longer work if q is changed to a different group of values. Therefore, it is better to use enumerate in this case:

    for i, q in enumerate(RANGE_Q):
       ...
       export[i, 0] = q
       ...
    

    A better approach is to use zip:

    for q, output_row in zip(RANGE_Q, output_arr):
        ...
        output_row[0] = q
        ...
    
  • The assignments to the output numpy array can be improved, as shown in my solution below.

Here is my solution.

import numpy as np

if __name__ == "__main__":
    # Define constants
    RANGE_Q = range(1, 11)               # Range of q values
    POLY_DEG = 4                         # Degree of polynomial
    OUTPUT_PATH = "/tmp/latest_foo.csv"  # Output csv path

    # Compute roots
    roots_arr = np.array([np.roots([1, 3*q*q, 4*q, np.sqrt(q), q]) for q in RANGE_Q])
    
    # Construct output array and assign values
    output_arr = np.empty(shape=(len(RANGE_Q), POLY_DEG * 2 + 1))
    output_arr[:, 0] = RANGE_Q
    output_arr[:, 1::2] = roots_arr.real
    output_arr[:, 2::2] = roots_arr.imag

    # Save results to output file
    np.savetxt(OUTPUT_PATH, output_arr, fmt="%.4g", delimiter=',')
added 6 characters in body
Source Link
GZ0
  • 2.3k
  • 7
  • 19

Here is a review of the solution.

  • ^ is xor in Python, it is not for exponetialexponential computation.

  • When running code outside a method/class, it is a good practice to put the code inside a main guard. See here for more explanation.

     if __name__ == "__main__":
        ...
    

    When you are providing quick answers on a forum, the guard might not always be necessary. However, if you are writing reusable code for yourself, it is better to adopt the practice.

  • By convention, _ is used to represent a don't-care variable that is unused after it is assigned, e.g. v = [[] for _ in range(4)]. It is undesirable to refer to it inside a loop, such as in an expression like answer[_].

  • The output is a pure text file. It is unnecessary to open it in binary mode (b). The original question does not suggest an append mode (a) either.

  • The q-1 index would no longer work if q is changed to a different group of values. Therefore, it is better to use enumerate in this case:

    for i, q in enumerate(RANGE_Q):
       ...
       export[i, 0] = q
       ...
    

    A better approach is to use zip:

    for q, output_row in zip(RANGE_Q, output_arr):
        ...
        output_row[0] = q
        ...
    
  • The assignments to the output numpy array can be improved, as shown in my solution below.

Here is my solution.

import numpy as np

if __name__ == "__main__":
    # Define constants
    RANGE_Q = range(1, 11)               # Range of q values
    POLY_DEG = 4                         # Degree of polynomial
    OUTPUT_PATH = "/tmp/latest_foo.csv"  # Output csv path

    # Compute roots
    roots_arr = np.array([np.roots([1, 3*q*q, 4*q, np.sqrt(q), q]) for q in RANGE_Q])
    
    # Construct output array and assign values
    output_arr = np.empty(shape=(len(RANGE_Q), POLY_DEG * 2 + 1))
    output_arr[:, 0] = RANGE_Q
    output_arr[:, 1::2] = roots_arr.real
    output_arr[:, 2::2] = roots_arr.imag

    # Save results to output file
    np.savetxt(OUTPUT_PATH, output_arr, fmt="%.4g", delimiter=',')

Here is a review of the solution.

  • ^ is xor in Python, it is not for exponetial computation

  • When running code outside a method/class, it is a good practice to put the code inside a main guard. See here for more explanation.

     if __name__ == "__main__":
        ...
    
  • By convention, _ is used to represent a don't-care variable, e.g. v = [[] for _ in range(4)]. It is undesirable to refer to it inside a loop, such as in answer[_].

  • The output is a pure text file. It is unnecessary to open it in binary mode (b). The original question does not suggest an append mode (a) either.

  • The q-1 index would no longer work if q is changed to a different group of values. Therefore, it is better to use enumerate in this case:

    for i, q in enumerate(RANGE_Q):
       ...
       export[i, 0] = q
       ...
    

    A better approach is to use zip:

    for q, output_row in zip(RANGE_Q, output_arr):
        ...
        output_row[0] = q
        ...
    
  • The assignments to the output numpy array can be improved, as shown in my solution below.

Here is my solution.

import numpy as np

if __name__ == "__main__":
    # Define constants
    RANGE_Q = range(1, 11)               # Range of q values
    POLY_DEG = 4                         # Degree of polynomial
    OUTPUT_PATH = "/tmp/latest_foo.csv"  # Output csv path

    # Compute roots
    roots_arr = np.array([np.roots([1, 3*q*q, 4*q, np.sqrt(q), q]) for q in RANGE_Q])
    
    # Construct output array and assign values
    output_arr = np.empty(shape=(len(RANGE_Q), POLY_DEG * 2 + 1))
    output_arr[:, 0] = RANGE_Q
    output_arr[:, 1::2] = roots_arr.real
    output_arr[:, 2::2] = roots_arr.imag

    # Save results to output file
    np.savetxt(OUTPUT_PATH, output_arr, fmt="%.4g", delimiter=',')

Here is a review of the solution.

  • ^ is xor in Python, it is not for exponential computation.

  • When running code outside a method/class, it is a good practice to put the code inside a main guard. See here for more explanation.

     if __name__ == "__main__":
        ...
    

    When you are providing quick answers on a forum, the guard might not always be necessary. However, if you are writing reusable code for yourself, it is better to adopt the practice.

  • By convention, _ is used to represent a don't-care variable that is unused after it is assigned, e.g. v = [[] for _ in range(4)]. It is undesirable to refer to it in an expression like answer[_].

  • The output is a pure text file. It is unnecessary to open it in binary mode (b). The original question does not suggest an append mode (a) either.

  • The q-1 index would no longer work if q is changed to a different group of values. Therefore, it is better to use enumerate in this case:

    for i, q in enumerate(RANGE_Q):
       ...
       export[i, 0] = q
       ...
    

    A better approach is to use zip:

    for q, output_row in zip(RANGE_Q, output_arr):
        ...
        output_row[0] = q
        ...
    
  • The assignments to the output numpy array can be improved, as shown in my solution below.

Here is my solution.

import numpy as np

if __name__ == "__main__":
    # Define constants
    RANGE_Q = range(1, 11)               # Range of q values
    POLY_DEG = 4                         # Degree of polynomial
    OUTPUT_PATH = "/tmp/latest_foo.csv"  # Output csv path

    # Compute roots
    roots_arr = np.array([np.roots([1, 3*q*q, 4*q, np.sqrt(q), q]) for q in RANGE_Q])
    
    # Construct output array and assign values
    output_arr = np.empty(shape=(len(RANGE_Q), POLY_DEG * 2 + 1))
    output_arr[:, 0] = RANGE_Q
    output_arr[:, 1::2] = roots_arr.real
    output_arr[:, 2::2] = roots_arr.imag

    # Save results to output file
    np.savetxt(OUTPUT_PATH, output_arr, fmt="%.4g", delimiter=',')
added 6 characters in body
Source Link
GZ0
  • 2.3k
  • 7
  • 19
Loading
added 155 characters in body
Source Link
GZ0
  • 2.3k
  • 7
  • 19
Loading
Source Link
GZ0
  • 2.3k
  • 7
  • 19
Loading