HackerRank Python: Capitalize! Solution

Table of Contents

Question

You are asked to ensure that the first and last names of people begin with a capital letter in their passports. For example, alison heck should be capitalised correctly as Alison Heck.

alison heck  – Alison Heck

Given a full name, your task is to capitalize the name appropriately.

Input Format

A single line of input containing the full name, S.

Constraints

  • 0 < len(S) < 1000
  • The string consists of alphanumeric characters and spaces.

Note: in a word only the first character is capitalized. Example 12abc when capitalized remains 12abc.

Output Format

A single line of input containing the full name, S.

Sample Input

chris alan

Sample Output

Chris Alan

Solution

				
					def solve(s):
    result=[]
    name=re.split(r'(\s+)',s)

    for i in range(len(name)):
       result.append(str(name[i]).capitalize())
       
    
    return ''.join(str(ele) for ele in result)
if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    s = input()

    result = solve(s)

    fptr.write(result + '\n')

    fptr.close()
				
			

The provided code capitalizes the first letter of each word in a given string `s` and returns the modified string. Let’s break down the code step by step:

1. `result = []`: This line initializes an empty list `result` that will store the modified words.

2. `name = re.split(r'(\s+)’, s)`: This line splits the string `s` into a list of words using regular expression `r'(\s+)’` as the separator. It splits the string at one or more whitespace characters.

3. `for i in range(len(name)):`: This loop iterates over each word in the `name` list.

4. `result.append(str(name[i]).capitalize())`: This line capitalizes the first letter of each word by using the `capitalize()` method. It converts the word to a string, capitalizes the first letter, and appends it to the `result` list.

5. `return ”.join(str(ele) for ele in result)`: This line joins all the words in the `result` list into a single string. It uses an empty string as the separator and returns the modified string.

6. `if __name__ == ‘__main__’:`: This condition checks if the code is being executed as the main program.

7. `s = input()`: This line takes user input to get the string `s`.

8. `result = solve(s)`: This line calls the `solve` function with the input string `s` and assigns the returned modified string to the variable `result`.

9. `fptr = open(os.environ[‘OUTPUT_PATH’], ‘w’)`: This line opens a file named `OUTPUT_PATH` in write mode. The `os.environ[‘OUTPUT_PATH’]` retrieves the path of the output file from the environment variables.

10. `fptr.write(result + ‘\n’)`: This line writes the modified string `result` to the output file.

11. `fptr.close()`: This line closes the output file.

Note: The code assumes that there is an environment variable named `OUTPUT_PATH` which specifies the path of the output file. If you are running the code without an output file, you can modify it to directly print the result using `print(result)` instead of writing to a file.

If you find anything wrong in this Solution, feel free to reach us in the comment section.

Sharing Is Caring:

Leave a Comment