31

10

This is a challenge of practicing golf optimization in Python -- reusable tricks and shortcuts to shave off a few characters. Many will be familiar to Python golfers and use common ideas from the Python Tips. Some of these use Python-specific features that you might not know exist unless you've seen them, so do take a look at the tips if you're stuck.

**Goal:**
There are ten problems, each a reference snippet of Python code for you to optimize and a description of the code does. Your goal is rewrite it to be shorter but still functionally-equivalent.

Your score, which you're trying to minimize, is the total length of your code all the snippets. The length of the reference snippets is 150. Tiebreaker is earliest post.

**Posting answers:** For each problem, post your code and its character count. You can post the reference snippet if you didn't find something shorter. It's intended that you don't look at others' answers when you post yours. ~~Please spoiler-tag each individual problem including the individual character counts. You can leave the total count revealed.~~ Feel free now to unspoiler your solution or post new unspoilered solutions.

**Details on legality:** Functional equivalence means the code can be replaced in a program without affecting its behavior (ignoring things like memory usage and operator precedence as part of an expression). Expressions should produce values that are equivalent by `==`

. Note that `1.0==1==True`

. Your code should have no side-effects unless otherwise stated. I don't intend the problems to be version-specific, but just in case, you can specify a Python version for each problem.

**Problem 1:** Keep iterating as long as the list `L`

has at least 7 elements

```
# 16 chars
while len(L)>=7:
```

**Problem 2**: Check whether two floats `x`

and `y`

are both positive.

```
# 11 chars
x>0 and y>0
```

**Problem 3**: If Boolean `b`

is true, remove the first element of `L`

. Otherwise, leave it unchanged.

```
# 12 chars
if b:L=L[1:]
```

**Problem 4**: Check if all elements of a non-empty list `L`

of numbers are equal. For this problem, it's OK to modify the list.

```
# 22 chars
all(x==L[0]for x in L)
```

**Problem 5**: Append a number `n`

to the end of a list `L`

only if `L`

already contains that number.

```
# 16 chars
if n in L:L+=[n]
```

**Problem 6**: Express the sign of a float `x`

: `+1`

for positive, `0`

for 0, `-1`

for negative.

```
# 20 chars
abs(x)/x if x else 0
```

**Problem 7** Continue a loop as long as the first element of `L`

, a list of Booleans, is `True`

. Also stop if `L`

is empty.

```
# 17 chars
while L and L[0]:
```

**Problem 8**: Continue a loop as long as `n`

is greater than 1. The number `n`

is guaranteed to be a positive integer.

```
# 10 chars
while n>1:
```

**Problem 9**: Check if an integer represented as a string `s`

is negative (i.e, starts with '-').

```
# 9 chars
s[0]=='-'
```

**Problem 10**: Convert a Boolean `b`

to `"Win"`

/`"Lose"`

, with `True`

->`"Win"`

and `False`

->`"Lose"`

.

```
# 17 chars
["Lose","Win"][b]
```

**Warning:** Spoilers below, don't scroll down if you want to solve these yourself.

If you just want to know the optimal score for a problem:

Problem 1:

12

Problem 2:

5

Problem 3:

7

Problem 4:

13

Problem 5:

13

Problem 6:

8

Problem 7:

12

Problem 8:

9

Problem 9:

5

Problem 10:

15

3

I'm voting to close this question as off-topic because multi-part challenges with no interaction between the parts are disallowed. http://meta.codegolf.stackexchange.com/a/8464/45941

– Mego – 2016-03-22T18:04:44.0703I voted to close, since this challenge provokes duplicate answers and effectively is many questions in one, since there is no interaction between the individual challenges whatsoever. I think that if we have such questions, there should only be one answer, which is a community wiki. – Wrzlprmft – 2014-11-18T12:18:40.780

2@Wrzlprmft: Somehow it's an interesting challenge. But after having 7 great contributions, now, I'd suggest relaxing the rules and allowing unspoilered solutions to single problems, because otherwise perfect submissions might remain hidden behind overall mediocre total scores. – Falko – 2014-11-18T16:32:37.223

@Falko I agree, feel free now to post unspoilered individual solutions and unspoiler existing solutions. – xnor – 2014-11-18T21:17:54.880

2I think this is a cool idea and that we should have golf practices for more languages. – Robbie Wxyz – 2014-11-18T21:54:29.417

2

@Wrzlprmft there is now a discussion regarding this on meta. Please voice your opinion there, for easier discussion without polluting this question's comments.

– FireFly – 2014-11-18T21:55:04.6131

If you learned about a new trick or found an idea useful, please add it to Python tips if it's not there already! That way, everyone can learn from it.

– xnor – 2014-11-18T22:55:00.077