Post History
Function Score is the bytes of the function A code golf answer can be a function. Only the bytes required to define the function are included in the score. Score includes imports and definitions...
Answer
#2: Post edited
- ## Function
- ### Score is the bytes of the function
- A code golf answer can be a function. Only the bytes required to define the function are included in the score.
- ### Score includes imports and definitions
- If the function depends on additional source code in order to work, the bytes required for that additional source code are also included in the score. For example, if a library needs to be imported, or a constant needs to be defined, doing so before the definition of the function does not avoid it being included in the score.
- ### Python example code: Fibonacci
- ```python
- PHI=(1+5**.5)/2
- def f(n):return round(PHI**n/5**.5)
- for n in range(10):
- print(f(n))
- ```
- Here the `for` loop is only used for printing output for testing, and is not required for the function to work, so its bytes are not included in the score. However, if `PHI` is not defined, the function will not work, so the bytes to define `PHI` are included in the score, even though it is defined outside the function.
- The answer would therefore be posted as
- ```python
- PHI=(1+5**.5)/2
- def f(n):return round(PHI**n/5**.5)
- ```
- In practice this could also be posted as
- ```python
- def f(n):PHI=(1+5**.5)/2;return round(PHI**n/5**.5)
- ```
- The point is that extracting the definition outside the function does not avoid the definition being counted towards the score.
- ### Python example code: Area of circle
- ```python
- from math import pi
- def a(r):return pi*r*r
- ```
The function will not work without the `import`, so it must be included in the score. The answer would therefore be posted as shown, or could also have the import included inside the function- ```python
- def a(r):from math import pi;return pi*r*r
- ```
- Either way the score is the same.
- ## Function
- ### Score is the bytes of the function
- A code golf answer can be a function. Only the bytes required to define the function are included in the score.
- ### Score includes imports and definitions
- If the function depends on additional source code in order to work, the bytes required for that additional source code are also included in the score. For example, if a library needs to be imported, or a constant needs to be defined, doing so before the definition of the function does not avoid it being included in the score.
- ### Python example code: Fibonacci
- ```python
- PHI=(1+5**.5)/2
- def f(n):return round(PHI**n/5**.5)
- for n in range(10):
- print(f(n))
- ```
- Here the `for` loop is only used for printing output for testing, and is not required for the function to work, so its bytes are not included in the score. However, if `PHI` is not defined, the function will not work, so the bytes to define `PHI` are included in the score, even though it is defined outside the function.
- The answer would therefore be posted as
- ```python
- PHI=(1+5**.5)/2
- def f(n):return round(PHI**n/5**.5)
- ```
- In practice this could also be posted as
- ```python
- def f(n):PHI=(1+5**.5)/2;return round(PHI**n/5**.5)
- ```
- The point is that extracting the definition outside the function does not avoid the definition being counted towards the score.
- ### Python example code: Area of circle
- ```python
- from math import pi
- def a(r):return pi*r*r
- ```
- The function will not work without `from math import pi`, so it must be included in the score. The answer would therefore be posted as shown, or could also have the import included inside the function
- ```python
- def a(r):from math import pi;return pi*r*r
- ```
- Either way the score is the same.
#1: Initial revision
## Function ### Score is the bytes of the function A code golf answer can be a function. Only the bytes required to define the function are included in the score. ### Score includes imports and definitions If the function depends on additional source code in order to work, the bytes required for that additional source code are also included in the score. For example, if a library needs to be imported, or a constant needs to be defined, doing so before the definition of the function does not avoid it being included in the score. ### Python example code: Fibonacci ```python PHI=(1+5**.5)/2 def f(n):return round(PHI**n/5**.5) for n in range(10): print(f(n)) ``` Here the `for` loop is only used for printing output for testing, and is not required for the function to work, so its bytes are not included in the score. However, if `PHI` is not defined, the function will not work, so the bytes to define `PHI` are included in the score, even though it is defined outside the function. The answer would therefore be posted as ```python PHI=(1+5**.5)/2 def f(n):return round(PHI**n/5**.5) ``` In practice this could also be posted as ```python def f(n):PHI=(1+5**.5)/2;return round(PHI**n/5**.5) ``` The point is that extracting the definition outside the function does not avoid the definition being counted towards the score. ### Python example code: Area of circle ```python from math import pi def a(r):return pi*r*r ``` The function will not work without the `import`, so it must be included in the score. The answer would therefore be posted as shown, or could also have the import included inside the function ```python def a(r):from math import pi;return pi*r*r ``` Either way the score is the same.