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 »
Challenges

Post History

66%
+2 −0
Challenges How many umbrellas to cover the beach?

Python 3.8+, 219 bytes Short version: from itertools import combinations as c def m(w): for k in range(n:=len(w)): for y in c(range(n),k+1): s=[0]*n for x in y: for r in range(...

posted 1y ago by Arpad Horvath‭  ·  edited 1y ago by Arpad Horvath‭

Answer
#4: Post edited by user avatar Arpad Horvath‭ · 2023-11-11T11:22:26Z (about 1 year ago)
Adding import
  • Python 3.8+,192 bytes
  • Short version without import:
  • ```python
  • def m(w):
  • for k in range(n:=len(w)):
  • for xs in combinations(range(n),k+1):
  • s=[0]*n
  • for x in xs:
  • for r in range(w[x]):
  • s[max(0,x-r)]=s[min(n-1,x+r)]=1
  • if all(s):return k+1
  • ```
  • Longer version with the tests.
  • ```python
  • from itertools import combinations
  • def umbrellas(values):
  • n = len(values)
  • for k in range(n):
  • for xs in combinations(range(n), k+1):
  • shadow = [False] * n
  • for x in xs:
  • for r in range(values[x]):
  • shadow[max(0, x-r)] = shadow[min(n-1, x+r)] = True
  • if all(shadow):
  • return k + 1
  • known_results = [
  • ("214141131", 2),
  • ("92132421", 1),
  • ("11111111", 8),
  • ("513131116", 2),
  • ]
  • for nums, result in known_results:
  • res = min_shield([int(ch) for ch in nums])
  • print(nums, result, res)
  • ```
  • Python 3.8+, 219 bytes
  • Short version:
  • ```python
  • from itertools import combinations as c
  • def m(w):
  • for k in range(n:=len(w)):
  • for y in c(range(n),k+1):
  • s=[0]*n
  • for x in y:
  • for r in range(w[x]):
  • s[max(0,x-r)]=s[min(n-1,x+r)]=1
  • if all(s):return k+1
  • ```
  • Longer version with the tests.
  • ```python
  • from itertools import combinations
  • def umbrellas(values):
  • n = len(values)
  • for k in range(n):
  • for xs in combinations(range(n), k+1):
  • shadow = [False] * n
  • for x in xs:
  • for r in range(values[x]):
  • shadow[max(0, x-r)] = shadow[min(n-1, x+r)] = True
  • if all(shadow):
  • return k + 1
  • known_results = [
  • ("214141131", 2),
  • ("92132421", 1),
  • ("11111111", 8),
  • ("513131116", 2),
  • ]
  • for nums, result in known_results:
  • res = min_shield([int(ch) for ch in nums])
  • print(nums, result, res)
  • ```
#3: Post edited by user avatar Arpad Horvath‭ · 2023-11-10T17:35:15Z (about 1 year ago)
  • Python 3.8+,192 bytes
  • Short version without import:
  • ```python
  • def m(w):
  • for k in range(n:=len(w)):
  • for xs in combinations(range(n),k+1):
  • s=[0]*n
  • for x in xs:
  • for r in range(w[x]):
  • s[max(0,x-r)]=s[min(n-1,x+r)]=1
  • if all(s):return k+1
  • ```
  • Longer version with the tests.
  • ```python
  • from itertools import combinations
  • def umbrellas(values):
  • n = len(values)
  • for k in range(n):
  • for xs in combinations(range(n), k+1):
  • shadow = [False] * n
  • for x in xs:
  • for r in range(values[x]):
  • shadow[max(0, x-r)] = shadow[min(n-1, x+r)] = True
  • if all(shadow):
  • return k + 1
  • known_results = [
  • ("214141131", 2),
  • ("92132421", 1),
  • ("11111111", 8),
  • ("513131116", 2),
  • ]
  • def get_list(string):
  • return [int(ch) for ch in string]
  • for nums, result in known_results:
  • res = min_shield([int(ch) for ch in nums])
  • print(nums, result, res)
  • ```
  • Python 3.8+,192 bytes
  • Short version without import:
  • ```python
  • def m(w):
  • for k in range(n:=len(w)):
  • for xs in combinations(range(n),k+1):
  • s=[0]*n
  • for x in xs:
  • for r in range(w[x]):
  • s[max(0,x-r)]=s[min(n-1,x+r)]=1
  • if all(s):return k+1
  • ```
  • Longer version with the tests.
  • ```python
  • from itertools import combinations
  • def umbrellas(values):
  • n = len(values)
  • for k in range(n):
  • for xs in combinations(range(n), k+1):
  • shadow = [False] * n
  • for x in xs:
  • for r in range(values[x]):
  • shadow[max(0, x-r)] = shadow[min(n-1, x+r)] = True
  • if all(shadow):
  • return k + 1
  • known_results = [
  • ("214141131", 2),
  • ("92132421", 1),
  • ("11111111", 8),
  • ("513131116", 2),
  • ]
  • for nums, result in known_results:
  • res = min_shield([int(ch) for ch in nums])
  • print(nums, result, res)
  • ```
#2: Post edited by user avatar Arpad Horvath‭ · 2023-11-10T17:33:45Z (about 1 year ago)
standard format
  • Python 3.8+ **192 bytes**
  • Short version without import:
  • ```python
  • def m(w):
  • for k in range(n:=len(w)):
  • for xs in combinations(range(n),k+1):
  • s=[0]*n
  • for x in xs:
  • for r in range(w[x]):
  • s[max(0,x-r)]=s[min(n-1,x+r)]=1
  • if all(s):return k+1
  • ```
  • Longer version with the tests.
  • ```python
  • from itertools import combinations
  • def umbrellas(values):
  • n = len(values)
  • for k in range(n):
  • for xs in combinations(range(n), k+1):
  • shadow = [False] * n
  • for x in xs:
  • for r in range(values[x]):
  • shadow[max(0, x-r)] = shadow[min(n-1, x+r)] = True
  • if all(shadow):
  • return k + 1
  • known_results = [
  • ("214141131", 2),
  • ("92132421", 1),
  • ("11111111", 8),
  • ("513131116", 2),
  • ]
  • def get_list(string):
  • return [int(ch) for ch in string]
  • for nums, result in known_results:
  • res = min_shield([int(ch) for ch in nums])
  • print(nums, result, res)
  • ```
  • Python 3.8+,192 bytes
  • Short version without import:
  • ```python
  • def m(w):
  • for k in range(n:=len(w)):
  • for xs in combinations(range(n),k+1):
  • s=[0]*n
  • for x in xs:
  • for r in range(w[x]):
  • s[max(0,x-r)]=s[min(n-1,x+r)]=1
  • if all(s):return k+1
  • ```
  • Longer version with the tests.
  • ```python
  • from itertools import combinations
  • def umbrellas(values):
  • n = len(values)
  • for k in range(n):
  • for xs in combinations(range(n), k+1):
  • shadow = [False] * n
  • for x in xs:
  • for r in range(values[x]):
  • shadow[max(0, x-r)] = shadow[min(n-1, x+r)] = True
  • if all(shadow):
  • return k + 1
  • known_results = [
  • ("214141131", 2),
  • ("92132421", 1),
  • ("11111111", 8),
  • ("513131116", 2),
  • ]
  • def get_list(string):
  • return [int(ch) for ch in string]
  • for nums, result in known_results:
  • res = min_shield([int(ch) for ch in nums])
  • print(nums, result, res)
  • ```
#1: Initial revision by user avatar Arpad Horvath‭ · 2023-11-10T17:31:30Z (about 1 year ago)
Python 3.8+ **192 bytes**

Short version without import:

```python
def m(w):
 for k in range(n:=len(w)):
  for xs in combinations(range(n),k+1):
   s=[0]*n
   for x in xs:
    for r in range(w[x]):
     s[max(0,x-r)]=s[min(n-1,x+r)]=1
   if all(s):return k+1
```

Longer version with the tests.

```python
from itertools import combinations


def umbrellas(values):
    n = len(values)
    for k in range(n):
        for xs in combinations(range(n), k+1):
            shadow = [False] * n
            for x in xs:
                for r in range(values[x]):
                    shadow[max(0, x-r)] = shadow[min(n-1, x+r)] = True
            if all(shadow):
                return k + 1

known_results = [
    ("214141131", 2),
    ("92132421", 1),
    ("11111111", 8),
    ("513131116", 2),
]

def get_list(string):
    return [int(ch) for ch in string]

for nums, result in known_results:
    res = min_shield([int(ch) for ch in nums])
    print(nums, result, res)
```