#String
_string = '12345 54321 hello!'
print('STRING: ', _string[3])
#List
_list = [12345, 54321, 'hello!']
print('LIST: ', _list[2])
# Tuple
# Same as lists except they are immutable(cannot be changed once created).
tup = tuple(123, 'hello')
_tuple = 12345, 54321, 'hello!'
print('TUPLE: ', tup)
#Set: unordered collection with no duplicate elements.
#Supports mathematical operations:**
#- Union, Intersection, Difference and symmetric difference.
_set = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
print('orange' in _set)
Indexed by keys:
_dictionary = {'jack': 4098, 'sape': 4139}
print('Dict', _dictionary['jack'])
dictionary['Alex'] = 31
print(_dictionary)
def func1(arg1,
arg2=5,
arg3=['one', 'two'],
*arguments,
**keywordarguments):
return
def func1(arg1, arg2=5, arg3=['one', 'three']):
print('function func1: ', arg1, arg2, arg3[0])
df = func1('hello function')
print(df)
class Func1:
def __init__(self, arg1, arg2=5, arg3=['class1', 'class2']):
print('class Func1: ', arg1, arg2, arg3[0])
df2 = Func1('hello class')
print(df2)
class TwoNumCalculator():
def __init__(self, n1=1, n2=1):
self.n1 = n1
self.n2 = n2
def multiply(self, n1, n2):
return n1 * n2
def add(self, n1, n2):
return n1 + n2
clas = TwoNumCalculator()
print(clas.multiply(1, 1))
print(clas.add(1, 1))
def mult(n1, n2):
return n1 * n2
def ad(n1, n2):
return n1 + n2
print(mult(1, 1))
print(ad(1, 1))
my_string = '12345'
my_list = [1, 2, 3, 4, 5]
my_tup = 1, 2, 3, 4, 5
user_input = input('..........: ')
user_input2 = input('...: ')
my_list = ['water', 'wool', 'pick-axe', 'dirt']
add_to_my_list = my_list.append(user_input)
add_to_my_list = my_list.append(user_input2)
print(user_input)
for i in range(0, 10, 2):
print(i)
print(type(i))
my_list = list(range(1, 10, 1))
print(my_list)
class BasicCalc:
def __init__(self, n1, n2):
self.n1 = n1
self.n2 = n2
def multiply_nums(self):
return self.n1 * self.n2
bc = BasicCalc(2, 3)
print(bc.multiply_nums())
bc2 = BasicCalc(6, 3)
bc3 = BasicCalc(9, 3)
sent = 'Hello World I am Great'
length = int(len(sent) / 2)
print(int(length))
for i in enumerate(sent):
df = i[length]
print(i)
def person(name):
return name
func2 = person('alex')
# print('def person: ', func2)
'''
Terms:
1. Instantiate or 'Class instantiation'
2. method: just a function inside a class
3. attribute: is in this case self.name
4. __init__ is short for Initialization: The thing you want done everytime.
5. Inheritance: When a class gets all the methods from another class.
'''
class Person:
def __init__(self, name):
self.name = name
class Student(Person):
def printGrade(self, grade):
print(f'{self.name} is in grade {grade}')
# print("%s is in grade %d" % (self.name, grade))
class Teacher(Person):
def printSubject(self, subject):
print(f'{self.name} teaches subject {subject}')
# print("%s teaches %s" % (self.name, subject))
print(Teacher('alex').printSubject('Python Programming Class'))
# student = Student(input("Name: "))
# teacher = Teacher(input("Name: "))
# student.printGrade(11)
# teacher.printSubject("Math")
sentence = input("Enter the most random sentence you can think of: ")
sentenceArr = sentence.split(" ")
print(sentenceArr)
word = input("Enter a word you want to search for in the sentence: ")
index = sentenceArr.index(word)
Make a program that gets a sentence from the user and a word to search for in that sentence
Tell the user the position of the word in the sentence (ex. 1st/2nd/3rd/...)
sentence = input("Enter the most random sentence you can think of: ")
sentenceArr = sentence.split(" ")
word = input("Enter a word you want to search for in the sentence: ")
index = sentenceArr.index(word)
ordinal = "th"
if index == 0:
ordinal = "st"
elif index == 1:
ordinal = "nd"
elif index == 2:
ordinal = "rd"
print("'%s' is the %d%s word in the sentence" % (word, index + 1, ordinal))
num_seconds = int(input())
print(num_seconds // 3600, num_seconds // 60)
hours_1 = int(input())
minutes_1 = int(input())
seconds_1 = int(input())
hours_2 = int(input())
minutes_2 = int(input())
seconds_2 = int(input())
print((hours_2 - hours_1) * 3600 + (minutes_2 - minutes_1) * 60 + seconds_2 - seconds_1)
qwertyuiop = int(input())
print((qwertyuiop % 100) // 10)
Given a month (an integer from 1 to 12) and a day in it (an integer from 1 to 31) in the year 2017, print the month and the day of the next day to it.
month = 3
day = 30
feb = 28
april = 4
june = 6
sept = 9
nov = 11
if day == 30 and month == april or month == june or month == sept or month == nov:
month += 1
day = 1
elif day == 28 and month == feb:
month += 1
day = 1
elif day == 31:
month += 1
day = 1
print(month)
print(day)
a = int(input())
b = int(input())
for x in range(a, b+1):
print(x)
n = int(input())
for i in range(1, n+1):
for j in range(1, i+1):
print(j, end="")
print("\n")
mystring = input()
print(mystring[2])
print(mystring[-2])
print(mystring[:5])
print(mystring[:-2])
print(mystring[::2])
print(mystring[1::2])
print(mystring[::-1])
print(mystring[::-2])
print(len(mystring))
Given a string consisting of words separated by spaces. Determine how many words it has. To solve the problem, use the method count.
Example input
Hello world
Example Output
2
print(len(input().split()))
u = str(input())
a = len(u)
b = a//2
if a % 2 == 0:
c = (u[:int(b)])
d = (u[int(b):])
print(d + c)
elif(a % 2 == 1):
e = u[a//2]
c = (u[:int(b)])
d = (u[int(b+1):])
print(d + c + e)
a = list(map(str, input().split(" ")))
print(a[1], a[0])
# or
# split string input
s = str(input()).split(' ')
# Print a string:
print(s[1], s[0])
j = input()
a = ""
b = ""
l = 0
for i in range(0, len(j)):
if j[i] == "f":
a += str(i)
break
else:
l += 1
if l == len(j):
a += str(-1)
for i in range(len(j)-1, -1, -1):
if j[i] == "f":
b += str(i)
break
if b == a:
b = ""
print(a, b)
Given a string that may contain a letter p. Print the index of the second occurrence of p. If the letter p occurs only once, then print -1, and if the string does not contain the letter p, then print -2.
Example input #1
appropriate
Example output #1
2
Example input #2
spare
Example output #2
-1
Example input #3
reason
Example output #3
-2
s = input()
if 'p' in s:
if s.find('p') == s.rfind('p'):
print(-1)
else:
print(s.find('p') + s[s.find('p') + 1:].find('p') + 1)
else:
print(-2)
Given a string in which the letter h occurs at least twice. Remove from that string the first and the last occurrence of the letter h, as well as all the characters between them.
Example input
In the hole in the ground there lived a hobbit
Example Output
In tobbit
s = input()
print(s[:s.find('h')] + s[s.rfind('h') + 1:])
Given a string in which the letter h occurs at least twice, reverse the sequence of characters enclosed between the first and last occurrences of it.
Example input
In the hole in the ground there lived a hobbit
Example output
In th a devil ereht dnuorg eht ni eloh ehobbit
s = input()
first_pos, last_pos = s.find('h') + 1, s.rfind('h')
left, middle, right = s[:first_pos], s[first_pos:last_pos], s[last_pos:]
print(left + middle[::-1] + right)
Given a string. Replace in this string all the numbers 1 by the word one.
Example input
1+1=2
Example output
one+one=2
print(input().replace('1', 'one'))
Given a string, delete all the characters @ from this string.
Example input
Bilbo.Baggins@bagend.hobbiton.shire.me
Example output
Bilbo.Bagginsbagend.hobbiton.shire.me
print(input().replace('@', ''))
Given a string in which the letter h occurs at least twice, replace every occurrence of the letter h by the letter H, except for the first and the last ones.
Example input
In the hole in the ground there lived a hobbit
Example output
In the Hole in tHe ground tHere lived a hobbit
s = input()
first_pos, last_pos = s.find('h') + 1, s.rfind('h')
left, middle, right = s[:first_pos], s[first_pos:last_pos], s[last_pos:]
print(left + middle.replace('h', 'H') + right)
Given a string, delete all its characters whose indices are divisible by 3.
Example input
Python
Example output
yton
s = input()
t = ''
for i in range(len(s)):
if i % 3 != 0:
t += s[i]
print(t)
Given a sequence of non-negative integers, where each number is written in a separate line.
Determine the length of the sequence.
The sequence ends with 0.
Print the length of the sequence (not counting the 0). The numbers following the number 0 should be omitted.
Example input
1
7
9
0
5
Example output
3
a = int(input())
length = 0
while a != 0:
a = int(input())
length += 1
print(length)
Given a sequence of non-negative integers, where each number is written in a separate line. The sequence ends with 0. Print the sum of the sequence.
Example input
1
7
9
0
Example output
17
a = int(input())
sum_of_sequence = 0
while a != 0:
sum_of_sequence += a
a = int(input())
print(sum_of_sequence)
Given a sequence of non-negative integers, where each number is written in a separate line. The sequence ends with 0. Print the average of the sequence.
Example input
10
30
0
Example output
20.0
a = int(input())
length = 0
sum_of_sequence = 0
while a != 0:
sum_of_sequence += a
length += 1
a = int(input())
print(sum_of_sequence / length)
Given a sequence of non-negative integers, where each number is written in a separate line. The sequence ends with 0. Print the maximum of the sequence.
Example input
1
2
3
2
1
0
Example output
3
maximum = a = int(input())
while a != 0:
a = int(input())
if a >= maximum:
maximum = a
print(maximum)
Given a sequence of non-negative integers, where each number is written in a separate line.
The sequence ends with 0.
Print the index of the first maximum of the sequence.
Example Input:
1
7
9
5
0
Example Output:
3
maximum = a = int(input())
maximum_index = 1
i = 1
while a != 0:
a = int(input())
i += 1
if a > maximum:
maximum = a
maximum_index = i
print(maximum_index)
Given a sequence of non-negative integers, where each number is written in a separate line.
The sequence ends with 0. Print the number of even elements of the sequence.
Example Input:
2
1
4
0
Example Output:
2
a = int(input())
num_even = 0
while a != 0:
if a % 2 == 0:
num_even += 1
a = int(input())
print(num_even)
Given a sequence of non-negative integers, where each number is written in a separate line.
The sequence ends with 0.
Print the number of elements of the sequence that are greater than their neighbors above.
Example Input:
1
2
3
4
5
0
Example Output:
4
prev = next = int(input())
counter = 0
while next != 0:
if prev < next:
counter += 1
prev, next = next, int(input())
print(counter)
Given a sequence of distinct non-negative integers, where each number is written in a separate line.
The sequence ends with 0.
Print the second largest element in this sequence.
It is guaranteed that the sequence has at least two elements.
Example Input
1
7
9
0
Example Output:
7
maximum = int(input())
second_maximum = int(input())
if second_maximum > maximum:
second_maximum, maximum = maximum, second_maximum
a = -1
while a != 0:
a = int(input())
if a > maximum:
second_maximum, maximum = maximum, a
elif a > second_maximum:
second_maximum = a
print(second_maximum)
Given a sequence of non-negative integers, where each number is written in a separate line.
The sequence ends with 0.
Find how many elements of this sequence are equal to its largest element.
Example Input #1:
1
7
9
0
Example Output #1:
1
Example Input #2
1
3
3
1
0
Example Output #2
2
maximum = int(input())
counter = 1
a = -1
while a != 0:
a = int(input())
if a > maximum:
maximum, counter = a, 1
elif a == maximum:
counter += 1
print(counter)
Given a sequence of non-negative integers, where each number is written in a separate line.
The sequence ends with 0.
Determine the length of the widest fragment where all the elements are equal to each other.
Example Input:
1
7
7
9
1
0
Example Output:
2
next = int(input())
length = 1
max_length = 1
while next != 0:
prev, next = next, int(input())
if prev == next:
length += 1
else:
length = 1
max_length = max(length, max_length)
print(max_length)
Fibonacci numbers are the numbers in the integer sequence starting with 1, 1 where every number after the first two is the sum of the two preceding ones:
1, 1, 2, 3, 5, 8, 13, 21, 34, ...
Given a positive integer n, print the nth Fibonacci number.
Example input
6
Example Output:
8
prev, next = 1, 1
n = int(input())
for i in range(n - 2):
prev, next = next, prev + next
print(next)
Fibonacci numbers are the numbers in the integer sequence starting with 1, 1 where every number after the first two is the sum of the two preceding ones:
1, 1, 2, 3, 5, 8, 13, 21, 34, ...
Given a positive integer, determine if it's the nth Fibonacci number for some n. If it is, print such n, otherwise print -1.
Example Input:
8
Example Output:
6
prev, next = 1, 1
index = 2
possible_fib = int(input())
while possible_fib > next:
prev, next = next, prev + next
index += 1
if possible_fib == next:
print(index)
else:
print(-1)
Given two integers - the number of rows m and columns n of m×n 2d list - and subsequent m rows of n integers, followed by one integer c. Multiply every element by c and print the result.
Example input
3 4
11 12 13 14
21 22 23 24
31 32 33 34
2
Example output
22 24 26 28
42 44 46 48
62 64 66 68
m, n = [int(s) for s in input().split()]
a = [[int(j) for j in input().split()] for i in range(m)]
c = int(input())
for i in range(m):
for j in range(n):
a[i][j] *= c
for line in a:
print(*line)
Given two integers - the number of rows m and columns n of m×n 2d list - and subsequent m rows of n integers, find the maximal element and print its row number and column number. If there are many maximal elements in different rows, report the one with smaller row number. If there are many maximal elements in the same row, report the one with smaller column number.
Example input
3 4
0 3 2 4
2 3 5 5
5 1 2 3
(maximal element is highlighted)
Example output
1 2
m, n = [int(s) for s in input().split()]
a = [[int(j) for j in input().split()] for i in range(m)]
max_value, max_i, max_j = a[0][0], 0, 0
for i in range(m):
for j in range(n):
if a[i][j] > max_value:
max_value, max_i, max_j = a[i][j], i, j
print(max_i, max_j)
Given an integer n, create a two-dimensional array of size n×n according to the following rules and print it:
On the main diagonal put 0.
On the diagonals adjacent to the main put 1.
On the next adjacent diagonals put 2, and so forth.
Example input
5
Example output
0 1 2 3 4
1 0 1 2 3
2 1 0 1 2
3 2 1 0 1
4 3 2 1 0
n = int(input())
a = [[0] * n for i in range(n)]
for i in range(n):
for j in range(n):
a[i][j] = abs(i - j)
for line in a:
print(*line)
Given an integer n, create a two-dimensional array of size n×n according to the following rules and print it:
On the antidiagonal put 1.
On the diagonals above it put 0.
On the diagonals below it put 2.
Example input
4
Example output
0 0 0 1
0 0 1 2
0 1 2 2
1 2 2 2
n = int(input())
a = [[0] * n for i in range(n)]
for i in range(n):
for j in range(n):
if i + j + 1 < n:
a[i][j] = 0
elif i + j + 1 == n:
a[i][j] = 1
else:
a[i][j] = 2
for line in a:
print(*line)
Given two integers - the number of rows m and columns n of m×n 2d list - and subsequent m rows of n integers, followed by two non-negative integers i and j less than n, swap the columns i and j of 2d list and print the result.
Example input
3 4
11 12 13 14
21 22 23 24
31 32 33 34
0 1
Example output
12 11 13 14
22 21 23 24
32 31 33 34
m, n = [int(s) for s in input().split()]
a = [[int(j) for j in input().split()] for i in range(m)]
col1, col2 = [int(s) for s in input().split()]
for i in range(m):
a[i][col1], a[i][col2] = a[i][col2], a[i][col1]
for line in a:
print(*line)
Given an odd positive integer n, produce a two-dimensional array of size n×n. Fill each element with the character "." . Then fill the middle row, the middle column and the diagonals with the character "*". You'll get an image of a snow flake. Print the snow flake in n×n rows and columns and separate the characters with a single space.
Example input
7
Example output
* . . * . . *
. * . * . * .
. . * * * . .
* * * * * * *
. . * * * . .
. * . * . * .
* . . * . . *
n = int(input())
a = [['.'] * n for i in range(n)]
for i in range(n):
for j in range(n):
if i == j or i + j + 1 == n or i == n // 2 or j == n // 2:
a[i][j] = '*'
for line in a:
print(*line)
Given two positive integers n and m, create a two-dimensional array of size n×m and populate it with the characters "."and "*" in a chequered pattern. The top left corner should have the character "." .
Example input
6 8
Example output
. * . * . * . *
* . * . * . * .
. * . * . * . *
* . * . * . * .
. * . * . * . *
* . * . * . * .
n, m = [int(s) for s in input().split()]
a = [['.' if (i + j) % 2 == 0 else '*']
for i in range(n)
for j in range(m)]
for line in a:
print(*line)
Given a list of integers, count how many distinct numbers it has. This task can be solved in a single line.
Example input
1 2 3 2 1
Example output
3
print(len(set(input().split())))
Given two lists of numbers, count how many numbers of the first one occur in the second one.
Example input
1 3 2
4 3 2
Example output
2
print(len(set(input().split()) & set(input().split())))
Statement
Given two lists of numbers, find all the numbers that occur in both the first and the second list.
Print them in ascending order.
Example Input:
1 3 2
4 3 2
Example Output:
2 3
first = set([int(s) for s in input().split()])
second = set([int(s) for s in input().split()])
print(*sorted(first & second))
Given a sequence of numbers, scan them from left to right and for each number print YES if this number was already seen or NO if it appears for the first time.
Example input
1 2 3 2 3 4
Example output
NO
NO
NO
YES
YES
NO
a = [int(s) for s in input().split()]
seen = set()
for i in a:
if i in seen:
print('YES')
else:
print('NO')
seen.add(i)
Augustus and Beatrice play the following game.
Augustus thinks of a secret integer number from 1 to n.
Beatrice tries to guess the number by providing a set of integers.
Augustus answers YES if his secret number exists in the provided set, or NO, if his number does not exist in the provided set.
Then after a few questions Beatrice, totally confused, asks you to help her determine Augustus's secret number.
Given the positive integer n in the first line, followed by the a sequence Beatrice's guesses, series of numbers seperated by spaces and Agustus's responses, or Beatrice's plea for HELP.
When Beatrice calls for help, provide a list of all the remaining possible secret numbers, in ascending order, separated by a space.
Example input
10
1 2 3 4 5
YES
2 4 6 8 10
NO
HELP
Example output
1 3 5
n = int(input())
possible = set(range(n))
while True:
line = input()
if line == 'HELP':
break
guess = set([int(s) for s in line.split()])
if input() == 'YES':
possible &= guess
else:
possible -= guess
print(*sorted(possible))
b = int(input())
dicti={}
for i in range(0,b):
a = list(map(str, input().split(" ")))
if a[0] not in dicti:
dicti.update({a[0]:int(a[1])})
else:
dicti.update({a[0]:int(a[1]) + dicti[a[0]]})
del a[:]
for i in sorted(dicti):
print(i, dicti[i])
The text is given in a single line. For each word of the text count the number of its occurrences before it.
Example input
one two one two three two four three
Example output
0 0 1 1 0 2 0 1
text = input().split()
times_seen = {}
for word in text:
if word not in times_seen:
times_seen[word] = 0
print(times_seen[word], end=' ')
times_seen[word] += 1
You are given a dictionary consisting of word pairs.
Every word is a synonym of the other word in its pair.
All the words in the dictionary are different.
After the dictionary there's one more word given. Find a synonym for it.
Example input
3
Hello Hi
Bye Goodbye
List Array
Goodbye
Example output
Bye
synonyms = {}
for i in range(int(input())):
w1, w2 = input().split()
synonyms[w1] = w2
synonyms[w2] = w1
print(synonyms[input()])
The first line contains the number of records. After that, each entry contains the name of the candidate and the number of votes they got in some state.
Count the results of the elections: sum the number of votes for each candidate.
Print candidates in the alphabetical order.
Example Input:
5
McCain 10
McCain 5
Obama 9
Obama 8
McCain 1
Example Output:
McCain 16
Obama 17
n = int(input())
votes_total = {}
for i in range(n):
candidate, num_votes = input().split()
if candidate not in votes_total:
votes_total[candidate] = 0
votes_total[candidate] += int(num_votes)
for candidate in sorted(votes_total):
print(candidate, votes_total[candidate])
Given the text: the first line contains the number of lines, then given the lines of words. Print the word in the text that occurs most often. If there are many such words, print the one that is less in the alphabetical order.
Example Input:
2
apple orange banana
banana orange
Example Output:
banana
words_count = {}
for i in range(int(input())):
words = input().split()
for word in words:
if word not in words_count:
words_count[word] = 0
words_count[word] += 1
max_frequency = max(words_count.values())
for word in sorted(words_count):
if words_count[word] == max_frequency:
print(word)
break
import operator
b = int(input())
dicti = {}
l = []
for i in range(0, b):
a = list(map(str, input().split(" ")))
for i in range(0, len(a)):
if a[i] not in dicti:
dicti.update({a[i]:1})
else:
dicti.update({a[i]:dicti[a[i]]+1})
maxv = max(dicti.values())
l = [k for k, v in dicti.items() if v == maxv]
l.sort()
l.append(max(dicti.items(), key = operator.itemgetter(1))[0])
print(l[0])
The virus attacked the filesystem of the supercomputer and broke the control of access rights to the files. For each file there is a known set of operations which may be applied to it:
write W,
read R,
execute X.
The first line contains the number N — the number of files contained in the filesystem. The following N lines contain the file names and allowed operations with them, separated by spaces. The next line contains an integer M — the number of operations to the files. In the last M lines specify the operations that are requested for files. One file can be requested many times.
You need to recover the control over the access rights to the files. For each request your program should return OK if the requested operation is valid or Access denied if the operation is invalid.
Example input
4
helloworld.exe R X
pinglog W R
nya R
goodluck X W R
5
read nya
write helloworld.exe
execute nya
read pinglog
write pinglog
Example Output:
OK
Access denied
Access denied
OK
OK
ACTION_TO_RIGHT = {
'read': 'R',
'write': 'W',
'execute': 'X',
}
file_rights = {}
for _ in range(int(input())):
filename, *rights = input().split()
file_rights[filename] = set(rights)
for _ in range(int(input())):
action, filename = input().split()
if ACTION_TO_RIGHT[action] in file_rights[filename]:
print('OK')
else:
print('Access denied')
Given a list of countries and cities of each country, then given the names of the cities. For each city print the country in which it is located.
Example Input:
2
USA Boston Pittsburgh Washington Seattle
UK London Edinburgh Cardiff Belfast
3
Cardiff
Seattle
London
Example Output:
UK
USA
UK
city_country = {}
for _ in range(int(input())):
country, *cities = input().split()
for city in cities:
city_country[city] = country
for _ in range(int(input())):
print(city_country[input()])
Given a number n, followed by n lines of text, print all words encountered in the text, one per line, with their number of occurrences in the text.
The words should be sorted in descending order according to their number of occurrences, and all words within the same frequency should be printed in lexicographical order.
Hint. After you created a dictionary of the words and their frequencies, you would like to sort it according to the frequencies.
This can be achieved if you create a list whose elements are lists of two elements: the number of occurrences of a word and the word itself.
For example, [[2, 'hi'], [1, 'what'], [3, 'is']]. Then the standard list sort will sort a list of lists, with the lists compared by the first element, and if these are equal, by the second element.
This is nearly what is required.
Example Input:
9
hi
hi
what is your name
my name is bond
james bond
my name is damme
van damme
claude van damme
jean claude van damme
Example Output:
damme 4
is 3
name 3
van 3
bond 2
claude 2
hi 2
my 2
james 1
jean 1
what 1
your 1
frequency = {}
for _ in range(int(input())):
for word in input().split():
if word not in frequency:
frequency[word] = 0
frequency[word] += 1
for i in sorted(set(frequency.values()), reverse=True):
for word in sorted([word for word in frequency if frequency[word] == i]):
print(word, i)
One day, going through old books in the attic, a student Bob found an English-Latin dictionary.
By that time he spoke English fluently, and his dream was to learn Latin. So finding the dictionary was just in time.
Unfortunately, full-fledged language studying process requires also another type of dictionary: Latin-English.
For lack of a better way he decided to make a second dictionary using the first one.
As you know, the dictionary consists of words, each of which contains multiple translations.
For each Latin word that appears anywhere in the dictionary, Bob has to find all its translations (that is, all English words, for which our Latin word is among its translations), and take them and only them as the translations of this Latin word.
Help him to create a Latin-English.
The first line contains a single integer N — the number of English words in the dictionary - followed by N dictionary entries. Each entry is contained on a separate line, which contains first the English word, then a hyphen surrounded by spaces and then comma-separated list with the translations of this English word in Latin.
All the words consist only of lowercase English letters. The translations are sorted in lexicographical order.
The order of English words in the dictionary is also lexicographic.
Print the corresponding Latin-English dictionary in the same format. In particular, the first word line should be the lexicographically minimal translation of the Latin word, then second in that order, etc.
The English words inside each line should also be sorted lexicographically.
Example Input:
3
apple - malum, pomum, popula
fruit - baca, bacca, popum
punishment - malum, multa
la_en = {}
for _ in range(int(input())):
en_word, la_words = input().split(' - ')
for la_word in la_words.split(', '):
if la_word not in la_en:
la_en[la_word] = []
la_en[la_word].append(en_word)
print(len(la_en))
for la_word in sorted(la_en):
print(la_word, '-', ', '.join(sorted(la_en[la_word])))