Hanwool Codes RSS Tag Admin Write Guestbook
전체 글 (45)
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)
2023-02-17 04:32:12

Text Wrap | HackerRank

 

Text Wrap | HackerRank

Wrap the given text in a fixed width.

www.hackerrank.com

 

Goal

 

You are given a string S and width W.

Your task is to wrap the string S into a paragraph of width W.

 

Solution

 

You need to use textwrap module in python.

In textwrap, the function "wrap" is to wrap the string to the specified maximum width by breaking it into multiple lines.

The resulting lines are stored as a list in s_wrap_list. 

'\n'.join() function to return a string by joining the elements of the list with a newline character.

 

import textwrap

def wrap(string, max_width):
    
    s_wrap_list = textwrap.wrap(string, max_width)
    
    return '\n'.join(s_wrap_list)

if __name__ == '__main__':
    string, max_width = input(), int(input())
    result = wrap(string, max_width)
    print(result)
2023-02-16 05:42:04

String Validators | HackerRank

 

String Validators | HackerRank

Identify the presence of alphanumeric characters, alphabetical characters, digits, lowercase and uppercase characters in a string.

www.hackerrank.com

 

Goal

 

Find out if the give string S contains : alphanumeric characters, alphabetical characters, digits, lowercase and uppercaser characters.

 

Solution

 

You can use built-in string validation methods to solve this solution.

The script initializes a boolean variable is_pass to False, which will be used to determine whether or not a given test has passed.

The first loop in the script iterates over each character in the input string s.

For each character, it checks whether or not it is alphanumeric using the isalnum() method.

If it is, the script prints "True", sets is_pass to True, and breaks out of the loop.

If the loop completes without finding an alphanumeric character, the script prints "False".

The next four loops work similarly, but check for alphabetical characters(isalpha()), digits(isdigit()), lowercase characters(islower()), and uppercase characters(isuppper()), respectively.

After each loop, the script checks whether or not is_pass is still False.

If it is, this means that the test has not been passed, so the script prints "False".

 

# String Validators

if __name__ == '__main__':
    s = input()
    
    is_pass = False
    
    for c in s:
        if c.isalnum():
            print("True")
            is_pass = True
            break
            
    if(is_pass == False):
        print("False")
        
    is_pass = False
    
    for c in s:
        if c.isalpha():
            print("True")
            is_pass = True
            break
            
    if(is_pass == False):
        print("False")
        
    is_pass = False
        
    for c in s:
        if c.isdigit():
            print("True")
            is_pass = True
            break
            
    if(is_pass == False):
        print("False")
        
    is_pass = False
        
    for c in s:
        if c.islower():
            print("True")
            is_pass = True
            break
            
    if(is_pass == False):
        print("False")
        
    is_pass = False
        
    for c in s:
        if c.isupper():
            print("True")
            is_pass = True
            break
        
    if(is_pass == False):
        print("False")


Hanwool Codes. Designed by 코딩재개발.