Skip to content

Commit e85ec9b

Browse files
RezwanRezwan
Rezwan
authored and
Rezwan
committed
Modified
1 parent 805ea33 commit e85ec9b

File tree

3 files changed

+244
-0
lines changed

3 files changed

+244
-0
lines changed

HackerRank/DefaultDict Tutorial.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'''
2+
Created on Jun 9, 2016
3+
4+
@author: Md. Rezwanul Haque
5+
'''
6+
from collections import defaultdict
7+
8+
d, n = defaultdict(list), list(map(int, input().split()))
9+
10+
for i in range(n[0]):
11+
d[input()].append(i + 1)
12+
13+
for i in range(n[1]):
14+
print(' '.join(map(str, d[input()])) or -1)

HackerRank/Tuples.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'''
2+
Created on May 5, 2016
3+
@author: Md. Rezwanul Haque
4+
'''
5+
input()
6+
ans = hash(tuple(map(int,input().split())))
7+
print(ans)

Tuples and Dictionaries.py

+223
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
####### Tuples #########
2+
3+
'''
4+
* A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists.
5+
'''
6+
7+
# Define a tuple
8+
my_tuple = ()
9+
10+
my_tuple = (1,2,3,5,4)
11+
print(my_tuple) # (1, 2, 3, 5, 4)
12+
13+
14+
my_tuple = ('Hello', 1, "World", 7)
15+
print(my_tuple) # ('Hello', 1, 'World', 7)
16+
print(my_tuple[0]) # Hello
17+
print(my_tuple[3]) # 7
18+
print(my_tuple[1:3]) # (1, 'World')
19+
20+
21+
# Tuples usually supports the sequence operations supported by string and lists:
22+
23+
# Concatenation
24+
a = (1, 3)
25+
b = (2, 4)
26+
c = a + b
27+
print(c) # (1, 3, 2, 4)
28+
29+
# Repetition
30+
a = (1,2,3,4,5)
31+
b = 2
32+
c = a * b
33+
print(c) # (1, 2, 3, 4, 5, 1, 2, 3, 4, 5)
34+
35+
# Indexing and slicing
36+
a = (1, 2, 3, 4, 5)
37+
print(a[0], a[1:5]) # 1 (2, 3, 4, 5)
38+
39+
# List to tuple
40+
my_list = [1, 2,3, 4, 5]
41+
my_tuple = tuple(my_list)
42+
print(my_tuple) # (1, 2, 3, 4, 5)
43+
44+
45+
# Sort a tuple
46+
my_tuple = (2, 5, 1, 4, 3)
47+
a = sorted(my_tuple) # It converts tuple to list and sort
48+
print(a) # [1, 2, 3, 4, 5]
49+
50+
51+
######
52+
B = (1, [2, 3], 4)
53+
B[1][0] = 'spam'
54+
print(B) # (1, ['spam', 3], 4)
55+
56+
# how to add value to a tuple?
57+
t = (1,2,3)
58+
t = t + (1,)
59+
print (t) # (1,2,3,1)
60+
61+
a = (1,2,3,5,6)
62+
a = a[:3] + (4,) + a[3:]
63+
print(a) # (1, 2, 3, 4, 5, 6)
64+
65+
# How to input an integer tuple from user?
66+
my_tuple = tuple(map(int,input().split()))
67+
print(my_tuple)
68+
69+
###
70+
my_tuple = ()
71+
for i in range(5):
72+
a = int(input())
73+
my_tuple += (a,)
74+
75+
print(my_tuple)
76+
'''
77+
Input:
78+
1
79+
2
80+
3
81+
4
82+
5
83+
84+
Output:
85+
(1, 2, 3, 4, 5)
86+
'''
87+
88+
89+
90+
91+
92+
############### Dictionaries ###################
93+
# empty dictionary
94+
my_dict = {}
95+
96+
# dictionary with integer keys
97+
my_dict = {1: 'apple', 2: 'ball'}
98+
print(my_dict) # {1: 'apple', 2: 'ball'}
99+
100+
# dictionary with mixed keys
101+
my_dict = {'name': 'John', 1: [2, 4, 3]}
102+
print(my_dict) # {'name': 'John', 1: [2, 4, 3]}
103+
104+
# using dict()
105+
my_dict = dict({1:'apple', 2:'ball'})
106+
print(my_dict) # {1: 'apple', 2: 'ball'}
107+
108+
# from sequence having each item as a pair
109+
my_dict = dict([(1,'apple'), (2,'ball')])
110+
print(my_dict) # {1: 'apple', 2: 'ball'}
111+
112+
113+
114+
### How to access elements from a dictionary?
115+
my_dict = {1: 'Rezwan', 'Haque' : 7}
116+
print(my_dict[1]) # Rezwan
117+
print(my_dict['Haque']) # 7
118+
print(my_dict.get('Haque')) # 7
119+
print(my_dict.keys()) # dict_keys([1, 'Haque'])
120+
121+
122+
123+
### Python Dictionary methods (https://www.programiz.com/python-programming/dictionary)
124+
'''
125+
clear() => Remove all items form the dictionary.
126+
127+
copy() => Return a shallow copy of the dictionary.
128+
129+
fromkeys(seq[, v]) => Return a new dictionary with keys from seq and value equal to v (defaults to None).
130+
131+
get(key[,d]) => Return the value of key. If key doesnot exit, return d (defaults to None).
132+
133+
items() => Return a new view of the dictionary's items (key, value).
134+
135+
keys() Return a new view of the dictionary's keys.
136+
137+
pop(key[,d]) => Remove the item with key and return its value or d if key is not found. If d is not provided and key is not found, raises KeyError.
138+
139+
popitem() => Remove and return an arbitary item (key, value). Raises KeyError if the dictionary is empty.
140+
141+
setdefault(key[,d]) => If key is in the dictionary, return its value. If not, insert key with a value of d and return d (defaults to None).
142+
143+
update([other]) => Update the dictionary with the key/value pairs from other, overwriting existing keys.
144+
145+
values() => Return a new view of the dictionary's values
146+
147+
'''
148+
149+
150+
# Map two lists into a dictionary in Python
151+
keys = ['a', 'b', 'c']
152+
values = [1, 2, 3]
153+
my_dict = dict(zip(keys, values))
154+
print(my_dict) # {'a': 1, 'b': 2, 'c': 3}
155+
156+
## OR
157+
my_dict = dict((k, v) for k, v in zip(keys, values))
158+
print(my_dict) # {'a': 1, 'b': 2, 'c': 3}
159+
160+
161+
d = {'a0': [1, 2, 3], 'a1': [4, 5, 6]}
162+
my_dict = zip(*d.values())
163+
print(*my_dict) # (1, 4) (2, 5) (3, 6)
164+
165+
166+
x = ['1', '2', '3', '4']
167+
y = [[1,0],[2,0],[3,0],[4,]]
168+
my_dict = {key:value for key, value in zip(x,y)}
169+
print(my_dict) # {'1': [1, 0], '2': [2, 0], '3': [3, 0], '4': [4]}
170+
171+
172+
### Make a dictionary in Python from input values
173+
strs="""A1023 CRT
174+
A1029 Regulator
175+
A1030 Therm"""
176+
177+
my_dict = dict(x.split() for x in strs.splitlines())
178+
print(my_dict) # {'A1023': 'CRT', 'A1029': 'Regulator', 'A1030': 'Therm'}
179+
180+
181+
182+
#####
183+
n = int(input()) #n is the number of items you want to enter
184+
d ={}
185+
for i in range(n):
186+
text = input().split() #split the input text based on space & store in the list 'text'
187+
d[text[0]] = text[1] #assign the 1st item to key and 2nd item to value of the dictionary
188+
print(d)
189+
'''
190+
Input:
191+
3
192+
A1023 CRT
193+
A1029 Regulator
194+
A1030 Therm
195+
196+
Output:
197+
{'A1023': 'CRT', 'A1029': 'Regulator', 'A1030': 'Therm'}
198+
'''
199+
200+
201+
202+
'''Important Link:
203+
(1) Add new keys to a dictionary? (https://stackoverflow.com/questions/1024847/add-new-keys-to-a-dictionary)
204+
(2) Map two lists into a dictionary in Python (https://stackoverflow.com/questions/209840/map-two-lists-into-a-dictionary-in-python)
205+
(3) zip two values from the dictionary in Python (https://stackoverflow.com/questions/32222563/zip-two-values-from-the-dictionary-in-python)
206+
(4) Python equivalent of zip for dictionaries (https://stackoverflow.com/questions/16458340/python-equivalent-of-zip-for-dictionaries)
207+
(5) Make a dictionary in Python from input values (https://stackoverflow.com/questions/14147369/make-a-dictionary-in-python-from-input-values)
208+
209+
'''
210+
211+
212+
213+
214+
215+
'''
216+
** You are now able to solve following online-judge problems.
217+
_____________________________________________________________
218+
(1) HackerRank | DefaultDict Tutorial
219+
(2) HackerRank | Tuples
220+
221+
'''
222+
223+

0 commit comments

Comments
 (0)