top of page

Capitalize! With Hacker Rank

13 hours ago

1 min read

0

5

0

Another Python problem from Hacker Rank.


Given an unseen string of a person's name can you capitalise the correct letters?



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.


Input Format


A single line of input containing the full name


Constraints


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


Print the capitalized string,


Sample Input


chris alan

Sample Output


Chris Alan



Consider the below code:


The function solve takes the string s and converts it to a list x.

An empty list is initialised.

x is enumerated and if the index is 0 or the index subsequent to a space that index is included in the list y.

The indices are then passed to the x list and converted to uppercase letters if they are in the correct position.

The list is then converted to a string and returned.

Snippet below for ease and reuse.



def solve(s):

x = list(s)

y = []

for index, i in enumerate(x):

if index == 0:

y.append(index)

if i == " ":

y.append(index + 1)

for index in y:

x[index] = x[index].upper()

return ''.join(x)


13 hours ago

1 min read

0

5

0

Related Posts

Comments

Share Your ThoughtsBe the first to write a comment.
bottom of page