PIXELBANKv9.1.0
Menu

Score the sentiment of text using a simple lexicon-based approach.

Given a sentiment lexicon (mapping words to scores) and a text, compute the average sentiment score of all lexicon words found in the text.

Input format:

  • Line 1: Number of lexicon entries M
  • Lines 2 to M+1: word followed by its score (float)
  • Line M+2: Text to score (lowercase words)

Output: Average sentiment score of matched words, rounded to 4 decimal places. If no lexicon words are found, output 0.0.

Example:

Input:
5
good 1.0
great 1.5
bad -1.0
terrible -2.0
ok 0.0
the movie was great but the ending was bad
Output:
0.25
Reasoning:

Lexicon words found in text:

  • "great" -> 1.5
  • "bad" -> -1.0

Words not in lexicon: the, movie, was, but, ending (skipped)

Average: (1.5 + (-1.0)) / 2 = 0.5 / 2 = 0.25

Constraints:

  • Sentiment scores can be negative (negative sentiment) or positive
  • Only average over words found in the lexicon
  • If a word appears multiple times in text, count it each time
  • If no lexicon words found, return 0.0
  • Round to 4 decimal places
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
Sentiment Scorer - Easy | PixelBank