Standardize Mobile Number Using Decorators
Create Date: April 01, 2019 at 08:31 PM         | Tag: PYTHON         | Author Name: Sun, Charles |
Let's dive into decorators! You are given mobile numbers. Sort them in ascending order then print them in the standard format shown below:
+91 xxxxx xxxxx
The given mobile numbers may have , or written before the actual digit number. Alternatively, there may not be any prefix at all.
Input Format
The first line of input contains an integer , the number of mobile phone numbers.
lines follow each containing a mobile number.
Output Format
Print mobile numbers on separate lines in the required format.
Sample Input
3
07895462130
919875641230
9195969878
Sample Output
+91 78954 62130
+91 91959 69878
+91 98756 41230
Concept
Like most other programming languages, Python has the concept of closures. Extending these closures gives us decorators, which are an invaluable asset. You can learn about decorators in 12 easy steps here.
To solve the above question, make a list of the mobile numbers and pass it to a function that sorts the array in ascending order. Make a decorator that standardizes the mobile numbers and apply it to the function.
def wrapper(f):
def fun(l):
# complete the function
f("+91 {} {}".format(n[-10:-5], n[-5:]) for n in l)
return fun
@wrapper
def sort_phone(l):
print(*sorted(l), sep='\n')
if __name__ == '__main__':
l = [input() for _ in range(int(input()))]
sort_phone(l)
New Comment
Maximize It!
Create Date: April 01, 2019 at 08:15 PM         | Tag: PYTHON         | Author Name: Sun, Charles |
You are given a function . You are also given lists. The list consists of elements.
You have to pick one element from each list so that the value from the equation below is maximized:
%
denotes the element picked from the list . Find the maximized value obtained.
denotes the modulo operator.
Note that you need to take exactly one element from each list, not necessarily the largest element. You add the squares of the chosen elements and perform the modulo operation. The maximum value that you can obtain, will be the answer to the problem.
Input Format
The first line contains space separated integers and .
The next lines each contains an integer , denoting the number of elements in the list, followed by space separated integers denoting the elements in the list.
Constraints
Output Format
Output a single integer denoting the value .
Sample Input
3 1000
2 5 4
3 7 8 9
5 5 7 8 9 10
Sample Output
206
Explanation
Picking from the st list, from the nd list and from the rd list gives the maximum value equal to % = .
method 1:
from itertools import product
k, m = map(int, input().split())
n = (list(map(int, input().split()))[1:] for _ in range(k))
results = (sum(num**2 for num in numbers) % m for numbers in product(*n))
print(max(results))
method 2:
from itertools import product
k, m = map(int, input().split())
n = (list(map(int, input().split()))[1:] for _ in range(k))
results = map(lambda x: sum(i**2 for i in x) % m, product(*n))
print(max(results))
Words Score
Create Date: March 31, 2019 at 10:51 PM         | Tag: PYTHON         | Author Name: Sun, Charles |
In this challenge, the task is to debug the existing code to successfully execute all provided test files.
Consider that vowels in the alphabet are a, e, i, o, u
and y
.
Function score_words
takes a list of lowercase words as an argument and returns a score as follows:
The score of a single word is if the word contains an even number of vowels. Otherwise, the score of this word is . The score for the whole list of words is the sum of scores of all words in the list.
Debug the given function score_words
such that it returns a correct score.
Your function will be tested on several cases by the locked template code.
Input Format
The input is read by the provided locked code template. In the first line, there is a single integer denoting the number of words. In the second line, there are space-separated lowercase words.
Constraints
- Each word has at most letters and all letters are English lowercase letters
Output Format
The output is produced by the provided and locked code template. It calls function score_words
with the list of words read from the input as the argument and prints the returned score to the output.
Sample Input 0
2
hacker book
Sample Output 0
4
Explanation 0
There are two words in the input: hacker
and book
. The score of the word hacker
is because it contains an even number of vowels, i.e. vowels, and the score of book
is for the same reason. Thus the total score is .
Sample Input 1
3
programming is awesome
Sample Output 1
4
Explanation 1
There are words in the input: programming
, is
and awesome
. The score of programming
is since it contains vowels, an odd number of vowels. The score of is
is also because it has an odd number of vowels. The score of awesome
is since it contains vowels, an even number of vowels. Thus, the total score is .
def is_vowel(letter):
return letter in ['a', 'e', 'i', 'o', 'u', 'y']
def score_words(words):
score = 0
for word in words:
num_vowels = 0
for letter in word:
if is_vowel(letter):
num_vowels += 1
if num_vowels % 2 == 0:
score += 2
else:
score += 1
return score
n = int(input())
words = input().split()
print(score_words(words))