Header Ad

HackerRank Capitalize! problem solution in Python

In this HackerRank Capitalize problem solution in python, You are asked to ensure that the first and last names of people begin with a capital letter in their passports. 

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

HackerRank Capitalize! solution in Python


Problem solution in Python 2 programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
import string

words = raw_input().split(' ')
for i in xrange(len(words)):
    words[i] = string.capitalize(words[i])

print ' '.join(words)


Problem solution in Python 3 programming.


# Complete the solve function below.
def solve(s):
    for x in s[:].split():
        s = s.replace(x, x.capitalize())
    return s


Problem solution in pypy programming.

def capitalize(string):
    os=""
    for idx,x in enumerate(string):
        #print (x,idx,str1[idx-1])
        os+=x.upper() if string[idx-1]==" " or idx == 0 else x
    return "".join(os)


Problem solution in pypy3 programming.

def capitalize(string):
    s=""
    space=True
    for x in string :
        if space and x!=' ':
            s=s+x.upper() 
            space=False
        elif x==' ':
            s=s+x
            space=True
        else :
            s=s+x
                    
    return s 


Post a Comment

0 Comments