Monday, July 6, 2020

collections.Counter()-HackerRank Solution

 collections.Counter()-HackerRank Solution

 A counter is a container that stores elements as dictionary keys, and their counts are stored as dictionary values.




Sample Code

>>> from collections import Counter
>>> 
>>> myList = [1,1,2,3,4,5,3,2,3,4,2,1,2,3]
>>> print Counter(myList)
Counter({2: 4, 3: 4, 1: 3, 4: 2, 5: 1})
>>>
>>> print Counter(myList).items()
[(1, 3), (2, 4), (3, 4), (4, 2), (5, 1)]
>>> 
>>> print Counter(myList).keys()
[1, 2, 3, 4, 5]
>>> 
>>> print Counter(myList).values()
[3, 4, 4, 2, 1]

Task

 is a shoe shop owner. His shop has  number of shoes.
He has a list containing the size of each shoe he has in his shop.
There are  number of customers who are willing to pay  amount of money only if they get the shoe of their desired size.

Your task is to compute how much money  earned.

Input Format

The first line contains , the number of shoes.
The second line contains the space separated list of all the shoe sizes in the shop.
The third line contains , the number of customers.
The next  lines contain the space separated values of the  desired by the customer and , the price of the shoe.

Constraints




Output Format

Print the amount of money earned by .

Sample Input

10
2 3 4 5 6 8 7 6 5 18
6
6 55
6 45
6 55
4 40
18 60
10 50

Sample Output

200

Explanation

Customer 1: Purchased size 6 shoe for $55.
Customer 2: Purchased size 6 shoe for $45.
Customer 3: Size 6 no longer available, so no purchase.
Customer 4: Purchased size 4 shoe for $40.
Customer 5: Purchased size 18 shoe for $60.
Customer 6: Size 10 not available, so no purchase.

Total money earned =  200

Sunday, July 5, 2020

Export data from MySQL to CSV file using SQL query- SQL Hacks

Export data from MySQL to CSV

Sometimes it's required to store the data from your database table to our local machine. To do this operation we can use phpmyadmin export feature too, but what if you want to get the data from the database to your CSV file using MySQL command line. In this blog post, I will explain how to do it just by writing one query.

But before starting that make sure that you have set your global 'local_infile'=on, if not then please visit this link to know about it-https://codingclown.blogspot.com/2020/06/import-data-from-csv-file-to-mysql.html.

Step 1: Select your database where the table is stored from where you want to copy the data.

Step 2: Check whether the table is not empty using the command-
             
             Run the command: Select * from table_name;

Step 3: Type this command to copy the data from your table to your local machine.

           
Run the command:    select * into outfile "C:/ProgramData/MySQL/MySQL Server        8.0/Uploads/Book1.csv" fields terminated by "," optionally enclosed by " ' " lines                terminated by  "\n" from table_name;

Here Book1.csv is my CSV file, make sure there is no file in this location with the same name.

Saturday, July 4, 2020

Compress the String!-HackerRank Solution

Compress the String!-HackerRank Solution

You are given a string . Suppose a character '' occurs consecutively  times in the string. Replace these consecutive occurrences of the character '' with  in the string.

For a better understanding of the problem, check the explanation.


Input Format

A single line of input consisting of the string .

Output Format

A single line of output consisting of the modified string.

Constraints

All the characters of  denote integers between  and .

Sample Input

1222311

Sample Output

(1, 1)   (3, 2)   (1, 3)   (2, 1)

Explanation

First, the character  occurs only once. It is replaced by . Then the character  occurs three times, and it is replaced by  and so on.

Also, note the single space within each compression and between the compressions.


itertools.combinations_with_replacement()- HackerRank Solutions

itertools.combinations_with_replacement()- HackerRank Solutions

This tool returns  length subsequences of elements from the input iterable allowing individual elements to be repeated more than once.

Combinations are emitted in lexicographic sorted order. So, if the input iterable is sorted, the combination tuples will be produced in sorted order.



Sample Code

>>> from itertools import combinations_with_replacement
>>> 
>>> print list(combinations_with_replacement('12345',2))
[('1', '1'), ('1', '2'), ('1', '3'), ('1', '4'), ('1', '5'), ('2', '2'), ('2', '3'), ('2', '4'), ('2', '5'), ('3', '3'), ('3', '4'), ('3', '5'), ('4', '4'), ('4', '5'), ('5', '5')]
>>> 
>>> A = [1,1,3,3,3]
>>> print list(combinations(A,2))
[(1, 1), (1, 3), (1, 3), (1, 3), (1, 3), (1, 3), (1, 3), (3, 3), (3, 3), (3, 3)]

Task

You are given a string .
Your task is to print all possible size  replacement combinations of the string in lexicographic sorted order.

Input Format

A single line containing the string  and integer value  separated by a space.

Constraints


The string contains only UPPERCASE characters.

Output Format

Print the combinations with their replacements of string  on separate lines.

Sample Input

HACK 2

Sample Output

AA
AC
AH
AK
CC
CH
CK
HH
HK
KK