https://www.hackerrank.com/challenges/capitalize/problem
Capitalize! | HackerRank
Capitalize Each Word.
www.hackerrank.com
Goal
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.
Given a full name, your task is to capitalize the name appropriately.
Solution
The solve function takes a string s as an input and returns a new string where the first letter of each word in the original string is capitalized.
The function first initializes an empty list called new_string.
Then, it uses the split method to split the input string s into a list of individual words. The split method separates the string at each occurrence of a space character, creating a list of strings.
Next, the function iterates through each word in the list, capitalizes the first letter of the word using the capitalize() method, and adds the modified word to the new_string list.
Finally, the function joins the list of capitalized words into a single string using the join method, with a space character as the separator, and returns the resulting string.
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the solve function below.
def solve(s):
new_string = []
for name in s.split(' '):
new_string.append(name.capitalize())
return ' '.join(new_string)
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
s = input()
result = solve(s)
fptr.write(result + '\n')
fptr.close()
'software Engineering > (Python)HackerRank' 카테고리의 다른 글
[Python] HackerRank - collections.Counter() (0) | 2023.02.22 |
---|---|
[Python] HackerRank - itertools.product() (0) | 2023.02.21 |
[Python] HackerRank - String Formatting (0) | 2023.02.18 |
[Python] HackerRank - Text Wrap (0) | 2023.02.17 |
[Python] HackerRank - String Validators (0) | 2023.02.16 |