Friday, February 24, 2023
HomeSoftware EngineeringMethods to Create a Hashtag Generator in Python

Methods to Create a Hashtag Generator in Python


The problem

Write a hashtag generator operate that takes a string and returns a #HashCodeString of it.

The hashtag generator ought to carry out the next duties.

Comply with these guidelines:

  • It should begin with a hashtag (#).
  • All phrases will need to have their first letter capitalized.
  • If the ultimate result’s longer than 140 chars it should return false.
  • If the enter or the result’s an empty string it should return false.

Examples:

"    Howdy     World   "                  =>  "#HelloWorld"
""                                        =>  false

The answer in Python code

Possibility 1:

def generate_hashtag(s):
    output = "#"
    for phrase in s.break up():
        output += phrase.capitalize()
    return False if (len(s) == 0 or len(output) > 140) else output

Possibility 2:

def generate_hashtag(s):
    ans = '#'+ str(s.title().change(' ',''))
    return s and never len(ans)>140 and ans or False

Possibility 3:

generate_hashtag=lambda d:(lambda b:d>''<b==b[:139]and'#'+b)(d.title().change(' ',''))

Take a look at instances to validate our answer

check.describe("Fundamental checks")
check.assert_equals(generate_hashtag(''), False, 'Anticipated an empty string to return False')
check.assert_equals(generate_hashtag('Do We've A Hashtag')[0], '#', 'Expeted a Hashtag (#) at the start.')
check.assert_equals(generate_hashtag('c i n'), '#CIN', 'Ought to capitalize first letters of phrases even when single letters.')
check.assert_equals(generate_hashtag('this  is  good'), '#ThisIsNice', 'Ought to take care of pointless center areas.')
check.assert_equals(generate_hashtag('Looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong Cat'), False, 'Ought to return False if the ultimate phrase is longer than 140 chars.')
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments

situs slot gacor provider terbaik agen toto slot terpercaya 2023 agen toto togel terpercaya 2023 situs toto togel pasaran resmi terbaik bandar toto macau pasaran resmi toto togel bandar toto slot gacor 4d 2023 bo togel online pasaran terlengkap sepanjang masa bo toto slot terlengkap sepanjang masa situs toto togel 2023 bet 100 perak daftar toto slot dan toto togel 2023 bermain toto togel dengan bet hanya 100 perak daftar toto slot bonus new member terpercaya bermain toto slot pelayanan 24 jam nonstop agen slot gacor 4d hadiah terbesar bandar toto slot provider terbaik toto slot gacor 4d hingga toto togel toto togel pasaran resmi terpercaya bo togel online terbaik 2023 agen togel online terbesar 2023 situs togel online terpercaya 2023 bo togel online paling resmi 2023 toto togel pasaran togel hongkong resmi situs slot online pasti gacor agen slot online anti rungkad bo slot online deposit tanpa potongan situs toto togel dan toto slot bonus new member situs toto slot gacor 4d bo toto slot gacor 4d bo toto slot gacor dari toto togel 4d bo toto slot 4d terpercaya bo toto slot terpercaya toto macau resmi dari toto togel 4d agen togel terbesar dan situs toto slot terpercaya bandar toto togel dan slot online 2023 bo slot gacor terbaik sepanjang masa winsortoto winsortoto bo toto togel situs toto situs toto togel terpercaya situs toto slot terpercaya situs slot gacor 4d terbaik sepanjang masa agen toto togel dan situs toto slot terpercaya situs toto togel dan agen toto slot terpercaya bandar toto togel tersedia pasaran toto macau resmi agen toto togel bet 100 perak deposit 10rb ltdtoto