Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Meta

Post History

50%
+0 −0
Meta Default Rules: Code Golf Answer Types

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...

posted 3mo ago by trichoplax‭  ·  edited 3mo ago by trichoplax‭

Answer
#2: Post edited by user avatar trichoplax‭ · 2024-08-26T21:19:11Z (3 months ago)
List full import code to remove ambiguity
  • ## 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 by user avatar trichoplax‭ · 2024-08-26T21:16:22Z (3 months ago)
## 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.