is there a simpler way to complete the following exercise? I am adding elements to a string, depending on three conditions
From Google Python Class
D. Verbing :
Given a string, if its length is at least 3, add 'ing' to its end. Unless it already ends in 'ing', in which case add 'ly' instead. If the string length is less than 3, leave it unchanged. Return the resulting string.
def verbing(s):
if len(s) < 3:
return s
if len(s) >= 3 and s[-3:] == 'ing':
s = s + 'ly'
return s
elif s[:-3] != 'ing':
s = s + 'ing'
return s
Test Cases
print 'verbing'
test(verbing('hail'), 'hailing')
test(verbing('runt'), 'runting')
test(verbing('swiming'), 'swimingly')
test(verbing('do'), 'do')
test(verbing('hi'), 'hi')
source https://stackoverflow.com/questions/72791792/is-there-a-simpler-way-to-complete-the-following-exercise-i-am-adding-elements
Comments
Post a Comment