EaglePB2's Competitive Programming Writeups
  • Home Page
  • Why Python?
  • Why 10^9 + 7?
  • General Formatting Title
  • 🇲🇾CodeNection
    • 2021
      • Closed Cateogry
        • Attend Talks
        • Distant Relatives
        • Concert
        • Mamak
        • Fair Contest
      • Open Preliminary Round
        • f(Aibohphobia)^-1
        • Did they cheat?
        • Semester Breaks
      • Open Final Round
        • Ways 1
        • Circular Campus
        • A joke
        • 🥰Last year when life was better
        • Thank You Pizza
    • 2023
      • Test Round
        • Codey and Alphabetical Scoring
        • Codey and Binary Guesser
      • Preliminary Round
        • Codey and CodeNection
        • Codey and Hide-and-Seek
        • Codey and Math
        • Codey and Textbooks
        • Codey and Money
        • Codey and Team Selection
        • Codey and Painted Tree
        • Codey and Number Grid
        • Codey and Crimes
      • Final Round
        • Codey and CodeNection 2
        • Codey and Connection
        • Codey and Schedule
        • Codey and School Supplies
        • Codey and Zombies
        • Codey and Sightseeing
        • Codey and Apples
        • Codey and Facto
        • Codey and Zoey
    • 2024
      • Test Round
        • Codey and Sunday
        • Codey and Takoyaki
      • Preliminary Round
        • Codey and CodeNection
        • Codey and Pebbles
        • Codey and Spam
        • Codey and Coins
        • Codey and Rectangles
        • Codey and Manuscript
        • Codey and Recipes
        • Codey and Toy Kingdom
        • Codey and Peaks
      • Final Round
        • Codey and Exit
        • Codey and Gardening
        • Codey and Symbol
        • Codey and Rectangles 2
        • Codey and Jutsu
        • Codey and Toy Kingdom 2
        • Codey and Speeches
  • ABaKoDa
    • 2023
      • Round 1
        • Problem Letters
        • Problem Statistics
        • Rankings Order
        • Rankings Search
      • Round 2
        • Abakoda Letters
        • Borrowed Words
        • Kensorship
        • Duel Languages
  • Meta Coding Competitions
    • 2011
      • Qualification Round
        • Double Squares
        • Peg Game
        • Studious Student
      • Round 1A
        • Diversity Number
        • Turn on the Lights
        • Wine Tasting
      • Round 1B
        • Chess 2
        • Diminishing Circle
        • Slot Machine Hacker
      • Round 1C
        • N-Factorful
        • Polynomial Factoring
        • Risky Slide
      • Round 2
        • Bonus Assignments
        • Scott's New Trick
        • Studious Student II
      • Final Round
        • Alien Game
        • Party Time
        • Safest Place
  • EaglePB2's Special
    • Hong Kong Identity card
    • Cycle Prefix Averages
    • Word Squares
Powered by GitBook
On this page
  • Question
  • Input Format
  • Constraints
  • Output Format
  • Sample Inputs:
  1. CodeNection
  2. 2023
  3. Test Round

Codey and Alphabetical Scoring

https://www.hackerrank.com/contests/codenection-2023-test/challenges/challenge-1-250

Question

A has score of 1, B has score of 2, ... Z has score of 26. Given a string s, find the score of the string.

Input Format

The first line contains a string s.

Constraints

1≤∣s∣≤30001 \le |s| \le 30001≤∣s∣≤3000
  • It is guaranteed that all the letters in string s are uppercase alphabetical letters.

Output Format

Output the total score of the string.

Sample Inputs:

Input

ABC

Output

6

Solution 1 - Dictionary Lookup

Create a dictionary which stores all the values of A to Z:

alphabet = {
    "A": 1
    "B": 2
    ...
    "Z": 26
}

Then, receive the input, break them into char arrays, lookup for numbers, add onto one variable, print that variable, and done.

Here's the solution:

alphabet = {
    "A": 1, 
    "B": 2, 
    "C": 3, 
    "D": 4, 
    "E": 5, 
    "F": 6, 
    "G": 7, 
    "H": 8, 
    "I": 9, 
    "J": 10, 
    "K": 11, 
    "L": 12, 
    "M": 13, 
    "N": 14, 
    "O": 15, 
    "P": 16, 
    "Q": 17, 
    "R": 18, 
    "S": 19, 
    "T": 20, 
    "U": 21, 
    "V": 22, 
    "W": 23, 
    "X": 24, 
    "Y": 25, 
    "Z": 26
}

user_input = input().strip().upper()
print(sum(alphabet[char] for char in input_string if char in alphabet))

Realisticly, the actual competition won't be that easy to compress the code into 1 line. It's ok if you prefer traditional, easier to understand method to write the code. If test case passes, then no issue about that.

Solution 2 - Char Calculation

If you want to save some time onto other questions, you can look through ASCII value for each character instead.

Given A is 65, all you need to do is convert the words into chars, convert them into ASCII value, then subtract 64 to all of them. Finally adds them to solve it.

Here's the full code:

letter_values = {chr(i): i - 64 for i in range(65, 91)}

def calculate_total_value(input_string):
    return sum(letter_values[char] for char in input_string if char in letter_values)

user_input = input().strip().upper()
print(calculate_total_value(user_input))

Personally, I don't recommend this method as this one easily caught AI detection. In competitive programming which you can't get any help with, this code itself is too optimized that almost nobody would believe it was done without referring information.

PreviousTest RoundNextCodey and Binary Guesser

Last updated 2 months ago

🇲🇾