Hanwool Codes RSS Tag Admin Write Guestbook
Python HackerRank (2)
2023-02-19 16:11:52

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()
2023-02-18 21:28:11

String Formatting | HackerRank

 

String Formatting | HackerRank

Print the formatted decimal, octal, hexadecimal, and binary values for $n$ integers.

www.hackerrank.com

 

Goal

 

given an integer, n, print the following values for each integer i from 1 to n:

1. Decimal
2. Octal
3. Hexadecimal (capitalized)
4. Binary

 

Each value should be space-padded to match the width of the binary value of number and the values should be separated by a single space.

 

 

Solution

 

In this script, we have a function called print_formatted which takes an integer number as input.

The function then sets the maximum width of the binary representation of number and initializes an empty string called text.

The function then uses a for loop to iterate through the range from 1 to number.

Inside the loop, it adds the right-justified string of the current number in decimal, octal, hexadecimal, and binary format to the text string.

The right-justified strings are created using the rjust method which takes an integer argument indicating the minimum width of the resulting string.

By adding 1 to the max_width variable, we create a space between the formatted strings.

After constructing the text string for each value of i, the function prints it to the console and then resets the text string to an empty string.

 

# String Formatting

def print_formatted(number):
    # your code goes here
    binary_max = str(bin(number))
    max_width = len(binary_max[2:])
    text = ""
    
    for i in range(1, number + 1):
    
        text += str(i).rjust(max_width)
        text += str(oct(i))[2:].rjust(max_width + 1)
        text += str(hex(i))[2:].upper().rjust(max_width + 1)
        text += str(bin(i))[2:].rjust(max_width + 1)
        
        print(text)
        text = ""
        

if __name__ == '__main__':

    n = int(input())
    print_formatted(n)


Hanwool Codes. Designed by 코딩재개발.