Skip to content

Instantly share code, notes, and snippets.

@Bl41r
Created August 19, 2016 16:34
Show Gist options
  • Save Bl41r/69d13fa88f502fa5abdd32bac56ad831 to your computer and use it in GitHub Desktop.
Save Bl41r/69d13fa88f502fa5abdd32bac56ad831 to your computer and use it in GitHub Desktop.
code challenge 2
def smart_substr(in_str, terminate='...'):
if len(in_str) <= 30:
return in_str + terminate
else:
if in_str[30] != ' ':
return ' '.join(in_str[0:30].split()[:-1]) + terminate
else:
return in_str[0:30] + terminate
@cewing
Copy link

cewing commented Aug 19, 2016

what happens if the character at index 30 is the last character in a whole word?

@Bl41r
Copy link
Author

Bl41r commented Aug 19, 2016

In [142]: smart_substr('12345678901234567890123 567890')
Out[142]: '12345678901234567890123 567890...'

@cewing
Copy link

cewing commented Aug 20, 2016

congratulations! you win the stump the chump award

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment