Skip to content

Feature/first tweet python solution -> main#16

Open
alangnt wants to merge 3 commits intocodedex-io:mainfrom
alangnt:feature/first-tweet-python-solution
Open

Feature/first tweet python solution -> main#16
alangnt wants to merge 3 commits intocodedex-io:mainfrom
alangnt:feature/first-tweet-python-solution

Conversation

@alangnt
Copy link
Contributor

@alangnt alangnt commented Mar 21, 2026

Hi Codédex team,

This PR creates a folder for today's First Tweet 🐦 challenge, adds a working Python solution, and updates the README to add a link for the challenge.

Here's the code :

def tweet_balance(tweet):
  # first we split the tweet and initialize the char count
  split_tweet = tweet.split(" ")
  char_count = 0

  # for each word in the split tweet
  for word in split_tweet:
    # if @ add 20
    if word.startswith("@"):
      char_count += 20
    # if url add 23
    elif word.startswith("http://") or word.startswith("https://"):
      char_count += 23
    else:
      # for each character
      for char in word:
        # if ascii value at least 0x1F000 it means it's an emoji
        if ord(char) >= 0x1F000:
          char_count += 2
        else:
          char_count += 1

    # space after each word
    char_count += 1

  # no trailing space after last word
  char_count -= 1

  return (140 - char_count)

Here's how it works :

  • first it splits the tweets into words using the split(" ") function so that each word is separated with a space
  • then we define a char_count variable that will hold the number of characters from our tweet
  • for each word in the split tweet, if the word starts with @, it's a mention so we only add 20, if it starts with either http:// or https:// it means it's an url so we only add 23
  • otherwise, for each character of the word, if it's ASCII value is at least 0x1F000 that means it's an emoji so we add 2, otherwise it's a simple character and we add 1
  • once the loop on the word is over, we add 1 for the space
  • once the whole loop is over, we remove 1 to avoid counting an unnecessary space after the last word's loop

Have a nice day !

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant