|
2 | 2 |
|
3 | 3 | ### **Question** |
4 | 4 |
|
5 | | ->***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.*** |
| 5 | +>***You are given words. Some words may repeat. For each word, output its number of occurrences. The output order should correspond with the input order of appearance of the word. See the sample input/output for clarification.*** |
6 | 6 |
|
7 | 7 | >***If the following string is given as input to the program:*** |
8 | 8 | >``` |
9 | | ->5 |
10 | | ->2 3 6 6 5 |
| 9 | +>4 |
| 10 | +>bcdef |
| 11 | +>abcdefg |
| 12 | +>bcde |
| 13 | +>bcdef |
11 | 14 | >``` |
12 | 15 | >***Then, the output of the program should be:*** |
13 | 16 | >``` |
14 | | ->5 |
| 17 | +>3 |
| 18 | +>2 1 1 |
15 | 19 | >``` |
| 20 | +
|
16 | 21 | ### Hints |
17 | | -> ***Make the scores unique and then find 2nd best number*** |
| 22 | +> ***Make a list to get the input order and a dictionary to count the word frequency*** |
18 | 23 |
|
19 | 24 | ---------------------- |
20 | 25 | **My Solution: Python 3** |
21 | 26 | ```python |
22 | 27 | n = int(input()) |
23 | | -arr = map(int, input().split()) |
24 | | -arr = list(set(arr)) |
25 | | -arr.sort() |
26 | | -print(arr[-2]) |
| 28 | +
|
| 29 | +word_list = [] |
| 30 | +word_dict = {} |
| 31 | +
|
| 32 | +for i in range(n): |
| 33 | + word = input() |
| 34 | + if word not in word_dict: |
| 35 | + word_list.append(word) |
| 36 | + word_dict[word] = word_dict.get(word, 0) + 1 |
| 37 | +
|
| 38 | +print(len(word_list)) |
| 39 | +for word in word_list: |
| 40 | + print(word_dict[word], end=' ') |
27 | 41 | ``` |
28 | 42 | --------------------- |
29 | 43 |
|
30 | | -# Question 96 |
| 44 | +# Question 101 |
31 | 45 |
|
32 | 46 | ### **Question** |
33 | 47 |
|
34 | | ->***You are given a string S and width W. |
35 | | -Your task is to wrap the string into a paragraph of width.*** |
| 48 | +>***You are given a string.Your task is to count the frequency of letters of the string and print the letters in descending order of frequency.*** |
36 | 49 |
|
37 | 50 | >***If the following string is given as input to the program:*** |
38 | 51 | >``` |
39 | | ->ABCDEFGHIJKLIMNOQRSTUVWXYZ |
40 | | ->4 |
| 52 | +>aabbbccde |
41 | 53 | >``` |
42 | 54 | >***Then, the output of the program should be:*** |
43 | 55 | >``` |
44 | | ->ABCD |
45 | | ->EFGH |
46 | | ->IJKL |
47 | | ->IMNO |
48 | | ->QRST |
49 | | ->UVWX |
50 | | ->YZ |
| 56 | +>b 3 |
| 57 | +>a 2 |
| 58 | +>c 2 |
51 | 59 | >``` |
52 | 60 |
|
53 | 61 | ### Hints |
54 | | -> ***Use wrap function of textwrap module*** |
| 62 | +> ***Count frequency with dictionary and sort by Value from dictionary Items*** |
55 | 63 |
|
56 | 64 | ---------------------- |
57 | 65 |
|
58 | 66 | **My Solution: Python 3** |
59 | 67 | ```python |
60 | | -import textwrap |
61 | | -
|
62 | | -def wrap(string, max_width): |
63 | | - string = textwrap.wrap(string,max_width) |
64 | | - string = "\n".join(string) |
65 | | - return string |
66 | | -
|
67 | | -if __name__ == '__main__': |
68 | | - string, max_width = input(), int(input()) |
69 | | - result = wrap(string, max_width) |
70 | | - print(result) |
| 68 | +word = input() |
| 69 | +dct = {} |
| 70 | +for i in word: |
| 71 | + dct[i] = dct.get(i,0) + 1 |
| 72 | +
|
| 73 | +dct = sorted(dct.items(),key=lambda x: (-x[1],x[0])) |
| 74 | +for i in dct[:3]: |
| 75 | + print(i[0],i[1]) |
71 | 76 | ``` |
72 | 77 | --------------------- |
73 | 78 |
|
74 | | -# Question 97 |
75 | 79 |
|
| 80 | +# Question 102 |
76 | 81 | ### **Question** |
77 | 82 |
|
78 | | ->***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.)*** |
79 | | -
|
80 | | ->***Different sizes of alphabet rangoli are shown below:*** |
81 | | ->``` |
82 | | ->#size 3 |
83 | | -> |
84 | | ->----c---- |
85 | | ->--c-b-c-- |
86 | | ->c-b-a-b-c |
87 | | ->--c-b-c-- |
88 | | ->----c---- |
89 | | -> |
90 | | ->#size 5 |
91 | | -> |
92 | | ->--------e-------- |
93 | | ->------e-d-e------ |
94 | | ->----e-d-c-d-e---- |
95 | | ->--e-d-c-b-c-d-e-- |
96 | | ->e-d-c-b-a-b-c-d-e |
97 | | ->--e-d-c-b-c-d-e-- |
98 | | ->----e-d-c-d-e---- |
99 | | ->------e-d-e------ |
100 | | ->--------e-------- |
101 | | ->``` |
102 | | -### Hints |
103 | | ->***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.*** |
104 | | -
|
105 | | ----------------------- |
106 | | -**My Solution: Python 3** |
107 | | -```python |
108 | | -
|
109 | | -import string |
110 | | -def print_rangoli(size): |
111 | | - n = size |
112 | | - alph = string.ascii_lowercase |
113 | | - width = 4 * n - 3 |
114 | | -
|
115 | | - ans = [] |
116 | | - for i in range(n): |
117 | | - left = '-'.join(alph[n - i - 1:n]) |
118 | | - mid = left[-1:0:-1] + left |
119 | | - final = mid.center(width, '-') |
120 | | - ans.append(final) |
121 | | -
|
122 | | - if len(ans) > 1: |
123 | | - for i in ans[n - 2::-1]: |
124 | | - ans.append(i) |
125 | | - ans = '\n'.join(ans) |
126 | | - print(ans) |
127 | | -
|
128 | | -
|
129 | | -if __name__ == '__main__': |
130 | | - n = int(input()) |
131 | | - print_rangoli(n) |
132 | | -``` |
133 | | ---------------------- |
134 | | - |
135 | | - |
136 | | -# Question 98 |
137 | | - |
138 | | -### **Question** |
139 | | - |
140 | | ->***You are given a date. Your task is to find what the day is on that date.*** |
| 83 | +>***Write a Python program that accepts a string and calculate the number of digits and letters.*** |
141 | 84 |
|
142 | 85 | **Input** |
143 | | ->***A single line of input containing the space separated month, day and year, respectively, in MM DD YYYY format.*** |
144 | 86 | >``` |
145 | | ->08 05 2015 |
| 87 | +>Hello321Bye360 |
146 | 88 | >``` |
147 | 89 |
|
148 | | -
|
149 | 90 | **Output** |
150 | | ->***Output the correct day in capital letters.*** |
151 | 91 | >``` |
152 | | ->WEDNESDAY |
| 92 | +>Digit - 6 |
| 93 | +>Letter - 8 |
153 | 94 | >``` |
154 | | -
|
155 | | -
|
156 | 95 | ---------------------- |
157 | 96 | ### Hints |
158 | | -> ***Use weekday function of calender module*** |
| 97 | +> ***Use isdigit() and isalpha() function*** |
159 | 98 |
|
160 | 99 | ---------------------- |
161 | 100 |
|
162 | 101 | **Solution:** |
163 | 102 | ```python |
164 | | -import calendar |
165 | | -
|
166 | | -month, day, year = map(int, input().split()) |
167 | | -
|
168 | | -dayId = calendar.weekday(year, month, day) |
169 | | -print(calendar.day_name[dayId].upper()) |
| 103 | +word = input() |
| 104 | +digit,letter = 0,0 |
| 105 | +for char in word: |
| 106 | + digit+=char.isdigit() |
| 107 | + letter+=char.isalpha() |
| 108 | +
|
| 109 | +print('Digit -',digit) |
| 110 | +print('Letter -',letter) |
170 | 111 | ``` |
171 | 112 | ---------------- |
172 | 113 |
|
173 | 114 |
|
174 | | -# Question 99 |
| 115 | +# Question 103 |
175 | 116 |
|
176 | 117 | ### **Question** |
177 | 118 |
|
178 | | ->***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.*** |
| 119 | +>***Given a number N.Find Sum of 1 to N Using Recursion*** |
179 | 120 |
|
180 | 121 | **Input** |
181 | | ->***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.*** |
182 | 122 | >``` |
183 | | ->4 |
184 | | ->2 4 5 9 |
185 | | ->4 |
186 | | ->2 4 11 12 |
| 123 | +>5 |
187 | 124 | >``` |
188 | 125 |
|
189 | 126 | **Output** |
190 | | ->***Output the symmetric difference integers in ascending order, one per line.*** |
191 | 127 | >``` |
192 | | ->5 |
193 | | ->9 |
194 | | ->11 |
195 | | ->12 |
| 128 | +>15 |
196 | 129 | >``` |
197 | 130 |
|
198 | | -
|
199 | 131 | ---------------------- |
200 | 132 | ### Hints |
201 | | -> ***Use \'^\' to make symmetric difference operation.*** |
| 133 | +> ***Make a recursive function to get the sum*** |
202 | 134 |
|
203 | 135 | ---------------------- |
204 | 136 |
|
205 | 137 | **Solution:** |
206 | 138 | ```python |
207 | | -if __name__ == '__main__': |
208 | | - n = int(input()) |
209 | | - set1 = set(map(int,input().split())) |
| 139 | +def rec(n): |
| 140 | + if n == 0: |
| 141 | + return n |
| 142 | + return rec(n-1) + n |
210 | 143 |
|
211 | | - m = int(input()) |
212 | | - set2 = set(map(int, input().split())) |
213 | 144 |
|
214 | | - ans = list(set1 ^ set2) |
215 | | - ans.sort() |
216 | | - for i in ans: |
217 | | - print(i) |
| 145 | +n = int(input()) |
| 146 | +sum = rec(n) |
| 147 | +print(sum) |
218 | 148 | ``` |
219 | 149 | ---------------- |
220 | 150 |
|
|
0 commit comments