Hanwool Codes RSS Tag Admin Write Guestbook
전체 글 (45)
2023-02-25 04:51:49

DefaultDict Tutorial | HackerRank

 

DefaultDict Tutorial | HackerRank

Create dictionary value fields with predefined data types.

www.hackerrank.com

 

Goal

 

The first line contains integers, n and m separated by a space.
The next  lines contains the words belonging to group A.
The next  lines contains the words belonging to group B.

Output m lines.
The i-th line should contain the 1-indexed positions of the occurrences of the i-th word separated by spaces.

 

 

Solution

 

We can practice with defaultdict to solve this task. 

Importing the defaultdict class from the collections module 

and creating a defaultdict object d that will hold the input values.

 

Taking two inputs separated by a space and storing them in x and y.

 

Then using a for loop to iterate int(x) times, taking input values

and appending them to the 'A' key of the defaultdict object d.

 

Similarly, for int(y) times, taking input values

and appending them to the 'B' key of the defaultdict object d.

 

Taking two inputs separated by a space and storing them in x and y

 

Then using a for loop to iterate int(x) times, taking input values 

and appending them to the 'A' key of the defaultdict object d

 

Similarly, for int(y) times, taking input values and appending them to the 'B' key of the defaultdict object d.

 

# DefaultDict Tutorial

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

d = defaultdict(list)

x, y = input().split()

for i in range(int(x)):
    d['A'].append(input())


for j in range(int(y)):
    d['B'].append(input())

for i in range(len(d['B'])):
    
    value = d['B'][i]
    
    matches = [k+1 for k in range(0,len(d['A'])) if value == d['A'][k]]
    
    if len(matches) == 0:
        print("{}".format(-1))
    else:
        print(*matches,sep=' ')
2023-02-24 04:02:50

Introduction to Sets | HackerRank

 

Introduction to Sets | HackerRank

Use the set tool to compute the average.

www.hackerrank.com

 

Goal

 

Ms. Gabriel Williams is a botany professor at District College.

One day, she asked her student Mickey to compute the average of all the plants with distinct heights in her greenhouse.

 

Task : You are given array of all the plants of height. You need to compute the average.

 

Solution

 

You need to use set data structure to solve this task.

 

Here is a step-by-step explanation of how this function works:


First, the input list is converted to a set using the set() function.

This removes any duplicate values from the input list, as sets only store unique values.

The length of the resulting set is computed using the len() function and stored in the total_length variable.
The sum of the values in the set is computed using the sum() function and stored in the total_values variable.
The average is computed by dividing the total_values by the total_length, and the result is returned.

 

# Introduction to Sets

def average(array):
    # your code goes here
    set_array = set(array)
    total_length = len(set_array)
    total_values = sum(set_array)
    
    return total_values / total_length
        


if __name__ == '__main__':
    n = int(input())
    arr = list(map(int, input().split()))
    result = average(arr)
    print(result)
2023-02-23 04:14:37

itertools.permutations() | HackerRank

 

itertools.permutations() | HackerRank

Find all permutations of a given size in a given string.

www.hackerrank.com

 

Goal

 

You are given a string S.

Your task is to print all poissible permutations of size K of the string in lexicographic sorted order.

 

Solution

 

You need to use itertools.permutations(iterable[, r]) method to solve this task. 

 

The itertools module in Python provides a number of powerful functions for working with iterable objects.

One of these functions is permutations(), which generates all possible permutations of an iterable object. 

The input() function is used to read a line of input from standard input, 

and the split() method is used to split the input line into a list of strings. 

The first element in the list is the string that we want to generate permutations for, 

and the second element is the length of the permutations that we want to generate.

The permutations() function is called with two arguments: 

the first is the string that we want to generate permutations for, and the second is the length of the permutations. 

The list() function is called to convert the result to a list. The list of permutations is stored in the perm variable.

The perm list is iterated over using a for loop, and a new string is created for each permutation.

The join() method is used to concatenate the elements of the permutation into a string,

and the resulting string is appended to the results list.
Once all the permutations have been generated, 

the sorted() function is called to sort the results list in lexicographic order. 

The join() method is used to concatenate the sorted strings together with a newline character, 

and the resulting string is printed to standard output.

# itertools.permutations()
# Enter your code here. Read input from STDIN. Print output to STDOUT
from itertools import permutations

com = input().split()

perm = list(permutations(com[0], int(com[1])))

results = []

for element_set in perm:
    
    tmp = ""
    
    for element in element_set:
        tmp += element
        
    results.append(tmp)
    
print('\n'.join(sorted(results)))


Hanwool Codes. Designed by 코딩재개발.