I've been given this string:
'V--V----V--P--VV---V------V--P--V--V'
I need to define a function that counts the longest streak of '-' in order to find the cost of making a fence. In the case given, it would be 6.
def cost(wall): #calculate the cost of building a wall with fences. the cost will be the longest streak of fences*10
fence_count = 0
streak = 0
for fence in wall:
if fence == '-':
fence_count = 1
elif fence == fence 1:
fence_count = 1
else:
streak = fence_count
fence_count = 0
return streak*10
wall = input()
c = cost(wall)
print(c)
TypeError: can only concatenate str (not "int") to str
Is there a built-in function in Python that can do this?
CodePudding user response:
Issue
I commented and fixed your code to some extend:
fence_count = 0 # the count can only increase
streak = 0 # direct factor on costs (a fence streak of 1 costs 10)
# count fences (denoted by hyphen char '-')
# iterate through all chars (here variable fence) in a string (here wall)
for fence in wall:
if fence == '-': # if fence then increase counter
fence_count = 1
# What if fence_count is now higher than the previous streak ?
elif fence_count > streak:
# then we have a new maximum as (new) longest streak
else: # else restart counting (e.g. if 'V' or 'P')
streak = fence_count # save counter as streak (only if longer than any streak before?)
fence_count = 0 # reset counter
# the longest streak was found
return streak*10
You cannot add or concatenate (using
operator) a character (of type str
) in fence
to a number (of type int
) in numerical literal 1
.
Thus the runtime-error
TypeError: can only concatenate str (not "int") to str
here:
elif fence == fence 1: # error raised here
fence_count = 1
What should the increase and comparison do here?
Alternative solution using regex-split
Using a regex-split, then sort by length descending and finally printing the first (which is the max streak):
import re
line = 'V--V----V--P--VV---V------V--P--V--V'
segments = re.split('[^-]', line) # split by all non-hyphen chars like V or P
print(segments) # print the resulting 12 streaks (some empty)
# ['', '--', '----', '--', '--', '', '---', '------', '--', '--', '--', '']
segments.sort(key=lambda s: len(s), reverse=True) # sorts in-place by length descending
print(segments) # the longest at first after sorting
# ['------', '----', '---', '--', '--', '--', '--', '--', '--', '', '', '']
print(len(segments[0])) # print the length of the longest streak
# 6
See also:
CodePudding user response:
You can use max()
with a map()
function that counts the length of each run of dashes:
from itertools import groupby
max(map(lambda x:sum(1 for c in x[1]) if x[0] == '-' else 0, groupby(data)))
This outputs:
6
CodePudding user response:
Similar idea with groupby
- but in 2 steps: first to find the consecutive range
of interesting item, then do 2) len(max(counts)) to get answer.
counts = [list(g) for k, g in groupby(s, key=lambda x: x in ('VP')) if not
key]
print(counts) # see what you got
print(len(max(counts))) # 6
CodePudding user response:
Another variant using regular expressions, find all substrings that match -
(i.e. one or more -
), map
those to their len
and get the max
:
>>> line = 'V--V----V--P--VV---V------V--P--V--V'
>>> max(map(len, re.findall("- ", line)))
6