Note the tricky case about a tweet that starts with a space but is truncated -- first letter, even following spaces and punctuation, should not be capitalized.
MAX=160LOWERCASE_LETTERS="abcdefghijklmnopqrstuvwxyz"UPPERCASE_LETTERS="ABCDEFGHIJKLMNOPQRSTUVWXYZ"deffirst_letter_idx(msg):foridxinrange(0,len(msg)):ifmsg[idx]inLOWERCASE_LETTERSormsg[idx]inUPPERCASE_LETTERS:returnidxreturnNonedefshorten(tweet):iflen(tweet)<MAX:returntweet# how many spaces we need to delete
delete_count=len(tweet)-MAX# build it backwards and then reverse when return
result=[]# idx is index in tweet, read right to left
foridxinrange(0,len(tweet))[::-1]:iftweet[idx]==' 'anddelete_count>0:iflen(result)>0andresult[-1]inLOWERCASE_LETTERS:result[-1]=result[-1].upper()delete_count-=1else:result.append(tweet[idx])result=result[::-1]result=result[:160]# special case: first letter in tweet is preceeded by a space. that letter -- as the _start_ of camel-case -- should not be capitalized. however all other letters following spaces can be capitlized.
r_fl_idx=first_letter_idx(result)t_fl_idx=first_letter_idx(tweet)ifr_fl_idxisnotNoneandresult[r_fl_idx].lower()==tweet[t_fl_idx].lower():result[r_fl_idx]=tweet[t_fl_idx]return''.join(result)deftest(result,expected):print('-'*80)print(len(result)==len(expected))print(result==expected)#print(result, len(result))
#print(expected, len(expected))
result=shorten("No one expects the Spanish Inquisition! Our chief weapon is surprise, fear and surprise; two chief weapons, fear, surprise, and ruthless efficiency! And that will be it.")expected="No one expects the Spanish Inquisition! Our chief weapon is surprise, fear and surprise; two chief weapons, fear,Surprise,AndRuthlessEfficiency!AndThatWillBeIt."test(result,expected)result=shorten("SMS messages are limited to 160 characters. It tends to be irritating, especially when freshly written message is 164 characters long. SMS messages are limited to 160 characters. It tends to be irritating, especially when freshly written message is 164 characters long.")expected="SMSMessagesAreLimitedTo160Characters.ItTendsToBeIrritating,EspeciallyWhenFreshlyWrittenMessageIs164CharactersLong.SMSMessagesAreLimitedTo160Characters.ItTendsTo"test(result,expected)result=shorten("This message is already short enough!")expected="This message is already short enough!"test(result,expected)result=shorten("ThereIsNoSpoonThereIsNoSpoonThereIsNoSpoonThereIsNoSpoonThereIsNoSpoonThereIsNoSpoonThereIsNoSpoonThereIsNoSpoonThereIsNoSpoonThereIsNoSpoonThereIsNoSpoonThereIsNoSpoonThereIsNoSpoon")expected="ThereIsNoSpoonThereIsNoSpoonThereIsNoSpoonThereIsNoSpoonThereIsNoSpoonThereIsNoSpoonThereIsNoSpoonThereIsNoSpoonThereIsNoSpoonThereIsNoSpoonThereIsNoSpoonThereI"test(result,expected)result=shorten(" dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd")expected="dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"test(result,expected)result=shorten(" dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd")expected="dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"test(result,expected)print(result)print(expected)result=shorten(" Dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd")expected="Dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"test(result,expected)print(result)print(expected)result=shorten(" !d Eddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd")expected=" !d Eddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"test(result,expected)print(result)print(expected)result=shorten(" !d Edddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddeeedddddddddddddddddddddddddddddddddddddddddddddddd")expected="!dEdddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddeeeddddddddddddddddddddddddddddddddddddddddddddddd"test(result,expected)print(result)print(expected)
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
in Python
Note the tricky case about a tweet that starts with a space but is truncated -- first letter, even following spaces and punctuation, should not be capitalized.