Python打卡Day23
The extended part of the repository starts from this page. Previous 94 problems were collected from the repository mentioned in intro. The following problems are collected from Hackerrank and other resources from internet.All the given solutions are in python 3.
Question 95
Question
Given the participants' score sheet for your University Sports Day, you are required to find the runner-up score. You are given scores. Store them in a list and find the score of the runner-up.
If the following string is given as input to the program:
15 22 3 6 6 5Then, the output of the program should be:
15
Hints
Make the scores unique and then find 2nd best number
My Solution: Python 3
1n = int(input())
2arr = map(int, input().split())
3arr = list(set(arr))
4arr.sort()
5print(arr[-2])
1'''
2Solution by: mishrasunny-coder
3'''
4num = int(input("Enter num: "))
5L = []
6
7while True:
8 L.append(num)
9 num = int(input("Enter another: "))
10 if num == 0:
11 break
12
13L1 = list(set(L[:]))
14L2 = sorted(L1)
15print(L2)
16
17print(f'The runner up is {L2[-2]}')
1'''Solution by: KailashS3
2'''
3num = int(input())
4scores = list(map(int, input().split(' ')))
5winner = max(scores)
6lst = []
7
8if len(scores) != num:
9 print('length of score is greater than input given')
10else:
11 for score in scores:
12 if winner > score:
13 lst.append(score)
14
15runnerup = max(lst)
16print(runnerup)
Question 96
Question
You are given a string S and width W. Your task is to wrap the string into a paragraph of width.
If the following string is given as input to the program:
1ABCDEFGHIJKLIMNOQRSTUVWXYZ 24Then, the output of the program should be:
1ABCD 2EFGH 3IJKL 4IMNO 5QRST 6UVWX 7YZ
Hints
Use wrap function of textwrap module
My Solution: Python 3
1import textwrap
2
3def wrap(string, max_width):
4 string = textwrap.wrap(string,max_width)
5 string = "\n".join(string)
6 return string
7
8if __name__ == '__main__':
9 string, max_width = input(), int(input())
10 result = wrap(string, max_width)
11 print(result)
1'''Solution by: mishrasunny-coder
2'''
3import textwrap
4
5string = input()
6width = int(input())
7
8print(textwrap.fill(string,width))
1'''solution by : Prashanth
2'''
3from textwrap import wrap
4x = str(input(': '))
5w = int(input())
6z = list(wrap(x, w))
7for i in z:
8 print(i)
Question 97
Question
You are given an integer, N. Your task is to print an alphabet rangoli of size N. (Rangoli is a form of Indian folk art based on creation of patterns.)
Different sizes of alphabet rangoli are shown below:
1#size 3 2 3----c---- 4--c-b-c-- 5c-b-a-b-c 6--c-b-c-- 7----c---- 8 9#size 5 10 11--------e-------- 12------e-d-e------ 13----e-d-c-d-e---- 14--e-d-c-b-c-d-e-- 15e-d-c-b-a-b-c-d-e 16--e-d-c-b-c-d-e-- 17----e-d-c-d-e---- 18------e-d-e------ 19--------e--------
Hints
First print the half of the Rangoli in the given way and save each line in a list. Then print the list in reverse order to get the rest.
My Solution: Python 3
1
2import string
3def print_rangoli(size):
4 n = size
5 alph = string.ascii_lowercase
6 width = 4 * n - 3
7
8 ans = []
9 for i in range(n):
10 left = '-'.join(alph[n - i - 1:n])
11 mid = left[-1:0:-1] + left
12 final = mid.center(width, '-')
13 ans.append(final)
14
15 if len(ans) > 1:
16 for i in ans[n - 2::-1]:
17 ans.append(i)
18 ans = '\n'.join(ans)
19 print(ans)
20
21
22if __name__ == '__main__':
23 n = int(input())
24 print_rangoli(n)
Question 98
Question
You are given a date. Your task is to find what the day is on that date.
Input
A single line of input containing the space separated month, day and year, respectively, in MM DD YYYY format.
108 05 2015
Output
Output the correct day in capital letters.
1WEDNESDAY
Hints
Use weekday function of calender module
Solution:
1import calendar
2
3month, day, year = map(int, input().split())
4
5dayId = calendar.weekday(year, month, day)
6print(calendar.day_name[dayId].upper())
Question 99
Question
Given 2 sets of integers, M and N, print their symmetric difference in ascending order. The term symmetric difference indicates those values that exist in either M or N but do not exist in both.
Input
The first line of input contains an integer, M.The second line contains M space-separated integers.The third line contains an integer, N.The fourth line contains N space-separated integers.
14 22 4 5 9 34 42 4 11 12
Output
Output the symmetric difference integers in ascending order, one per line.
15 29 311 412
Hints
Use '^' to make symmetric difference operation.
Solution:
1if __name__ == '__main__':
2 n = int(input())
3 set1 = set(map(int,input().split()))
4
5 m = int(input())
6 set2 = set(map(int, input().split()))
7
8 ans = list(set1 ^ set2)
9 ans.sort()
10 for i in ans:
11 print(i)