Hanwool Codes RSS Tag Admin Write Guestbook
전체 글 (45)
2023-02-15 04:58:23

Find a string | HackerRank

 

Find a string | HackerRank

Find the number of occurrences of a substring in a string.

www.hackerrank.com

 

Goal

 

You have to print the number of times that the substring occurs in the given string. 

String traversal will take place from left to right, not from right to left.

 

 

Solution

 

The function starts by initializing the number variable to zero, which will be used to keep track of the count of the number of times sub_string occurs in string.

Then, it calculates the length of string and stores it in the n variable.

The function then enters a loop that will iterate n times.

In each iteration, the function uses the find method to search for the first occurrence of sub_string in the new_string variable.

If the find method returns -1, it means that sub_string is not found in new_string and the loop increments the index i by 1.

If find returns a value other than -1, it means that sub_string has been found, and the function increments the number count by 1.

The new_string is then updated to start from the position just after the found sub_string.

Finally, the function returns the number count, which is the number of times sub_string occurs in string.

 

# Find a string

def count_substring(string, sub_string):
    
    number = 0
    n = len(string)
    new_string = string
    
    for i in range(0, n):
        
        result = new_string.find(sub_string)
        
        if(result == -1):
            new_string = new_string[i:]
            i += 1
            
        else:
            new_string = new_string[result + 1:]
            i = result + 1
            number += 1
    
        
    
    
    return number

if __name__ == '__main__':
    string = input().strip()
    sub_string = input().strip()
    
    count = count_substring(string, sub_string)
    print(count)
2023-02-14 06:03:58

Mutations | HackerRank

 

Mutations | HackerRank

Understand immutable vs mutable by making changes to a given string.

www.hackerrank.com

 

Goal

 

Read a given string, change the character at a given index and then print the modified string.

 

Solution

 

We need to understand slicing and indexing Strings in Python. You can check this link

 

 

#Mutations

def mutate_string(string, position, character):
    
    new_string = string[:position] + character + string[position+1:]
    
    return new_string

if __name__ == '__main__':
    s = input()
    i, c = input().split()
    s_new = mutate_string(s, int(i), c)
    print(s_new)

 

2023-02-11 04:20:10

What's Your Name? | HackerRank

 

What's Your Name? | HackerRank

Python string practice: Print your name in the console.

www.hackerrank.com

 

Goal

 

You are given the firstname and lastname of a person on two different lines. 

Your task is to read them and print the following:

Hello firstname lastname! You just delved into python.

 

Solution

 

This problem is to check you can read parameters from function and print with parameter values. 

There are several ways to output formatting

 

# What's Your Name?

#
# Complete the 'print_full_name' function below.
#
# The function is expected to return a STRING.
# The function accepts following parameters:
#  1. STRING first
#  2. STRING last
#

def print_full_name(first, last):
    # Write your code here
    
    answer = "Hello {} {}! You just delved into python.".format(first, last)
    
    print(answer)

if __name__ == '__main__':
    first_name = input()
    last_name = input()
    print_full_name(first_name, last_name)

 



Hanwool Codes. Designed by 코딩재개발.