Skip to content

Commit 5130ffd

Browse files
Virtusa --> Pairs,Sorting_Comparator
1 parent ba649bd commit 5130ffd

File tree

4 files changed

+290
-0
lines changed

4 files changed

+290
-0
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"n="
10+
]
11+
},
12+
{
13+
"cell_type": "markdown",
14+
"metadata": {},
15+
"source": [
16+
"# Pairs\n",
17+
"![Pairs](Pairs.png)"
18+
]
19+
},
20+
{
21+
"cell_type": "code",
22+
"execution_count": 23,
23+
"metadata": {},
24+
"outputs": [
25+
{
26+
"name": "stdin",
27+
"output_type": "stream",
28+
"text": [
29+
" 5 2\n",
30+
" 1 5 4 3 2\n"
31+
]
32+
},
33+
{
34+
"data": {
35+
"text/plain": [
36+
"3"
37+
]
38+
},
39+
"execution_count": 23,
40+
"metadata": {},
41+
"output_type": "execute_result"
42+
}
43+
],
44+
"source": [
45+
"n,k=map(int,input().split())\n",
46+
"a=list(map(int,input().split()))\n",
47+
"\n",
48+
"len(set(a) & set(x-k for x in a))"
49+
]
50+
},
51+
{
52+
"cell_type": "markdown",
53+
"metadata": {},
54+
"source": [
55+
"# Sorting: Comparator\n",
56+
"![Sorting: Comparator](Sorting_Comparator.png)"
57+
]
58+
},
59+
{
60+
"cell_type": "code",
61+
"execution_count": 60,
62+
"metadata": {},
63+
"outputs": [
64+
{
65+
"name": "stdin",
66+
"output_type": "stream",
67+
"text": [
68+
" 5\n",
69+
" amy 100\n",
70+
" david 100\n",
71+
" heraldo 50\n",
72+
" aakansha 75\n",
73+
" aleksa 150\n"
74+
]
75+
},
76+
{
77+
"name": "stdout",
78+
"output_type": "stream",
79+
"text": [
80+
"aleksa 150\n",
81+
"amy 100\n",
82+
"david 100\n",
83+
"aakansha 75\n",
84+
"heraldo 50\n"
85+
]
86+
}
87+
],
88+
"source": [
89+
"from functools import cmp_to_key\n",
90+
"class Player:\n",
91+
" def __init__(self, name, score):\n",
92+
" self.name=name\n",
93+
" self.score=score\n",
94+
" def __repr__(self):\n",
95+
" return '{0} {1}'.format(self.name,self.score)\n",
96+
" def comparator(a, b):\n",
97+
" if a.score > b.score: return -1 \n",
98+
" if a.score < b.score: return 1 \n",
99+
" if a.name > b.name: return 1 \n",
100+
" if a.name < b.name: return -1 \n",
101+
" return 0\n",
102+
"\n",
103+
"n = int(input())\n",
104+
"data = []\n",
105+
"for i in range(n):\n",
106+
" name, score = input().split()\n",
107+
" score = int(score)\n",
108+
" player = Player(name, score)\n",
109+
" data.append(player)\n",
110+
"\n",
111+
"data = sorted(data, key=cmp_to_key(Player.comparator))\n",
112+
"for i in data:\n",
113+
" print(i.name, i.score)"
114+
]
115+
},
116+
{
117+
"cell_type": "code",
118+
"execution_count": null,
119+
"metadata": {},
120+
"outputs": [],
121+
"source": []
122+
}
123+
],
124+
"metadata": {
125+
"kernelspec": {
126+
"display_name": "Python 3",
127+
"language": "python",
128+
"name": "python3"
129+
},
130+
"language_info": {
131+
"codemirror_mode": {
132+
"name": "ipython",
133+
"version": 3
134+
},
135+
"file_extension": ".py",
136+
"mimetype": "text/x-python",
137+
"name": "python",
138+
"nbconvert_exporter": "python",
139+
"pygments_lexer": "ipython3",
140+
"version": "3.8.2"
141+
}
142+
},
143+
"nbformat": 4,
144+
"nbformat_minor": 4
145+
}

HackerRank/Virtusa/Pairs.png

109 KB
Loading
198 KB
Loading

HackerRank/Virtusa/Untitled.ipynb

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"n="
10+
]
11+
},
12+
{
13+
"cell_type": "markdown",
14+
"metadata": {},
15+
"source": [
16+
"# Pairs\n",
17+
"![Pairs](Pairs.png)"
18+
]
19+
},
20+
{
21+
"cell_type": "code",
22+
"execution_count": 23,
23+
"metadata": {},
24+
"outputs": [
25+
{
26+
"name": "stdin",
27+
"output_type": "stream",
28+
"text": [
29+
" 5 2\n",
30+
" 1 5 4 3 2\n"
31+
]
32+
},
33+
{
34+
"data": {
35+
"text/plain": [
36+
"3"
37+
]
38+
},
39+
"execution_count": 23,
40+
"metadata": {},
41+
"output_type": "execute_result"
42+
}
43+
],
44+
"source": [
45+
"n,k=map(int,input().split())\n",
46+
"a=list(map(int,input().split()))\n",
47+
"\n",
48+
"len(set(a) & set(x-k for x in a))"
49+
]
50+
},
51+
{
52+
"cell_type": "markdown",
53+
"metadata": {},
54+
"source": [
55+
"# Sorting: Comparator\n",
56+
"![Sorting: Comparator](Sorting_Comparator.png)"
57+
]
58+
},
59+
{
60+
"cell_type": "code",
61+
"execution_count": 60,
62+
"metadata": {},
63+
"outputs": [
64+
{
65+
"name": "stdin",
66+
"output_type": "stream",
67+
"text": [
68+
" 5\n",
69+
" amy 100\n",
70+
" david 100\n",
71+
" heraldo 50\n",
72+
" aakansha 75\n",
73+
" aleksa 150\n"
74+
]
75+
},
76+
{
77+
"name": "stdout",
78+
"output_type": "stream",
79+
"text": [
80+
"aleksa 150\n",
81+
"amy 100\n",
82+
"david 100\n",
83+
"aakansha 75\n",
84+
"heraldo 50\n"
85+
]
86+
}
87+
],
88+
"source": [
89+
"from functools import cmp_to_key\n",
90+
"class Player:\n",
91+
" def __init__(self, name, score):\n",
92+
" self.name=name\n",
93+
" self.score=score\n",
94+
" def __repr__(self):\n",
95+
" return '{0} {1}'.format(self.name,self.score)\n",
96+
" def comparator(a, b):\n",
97+
" if a.score > b.score: return -1 \n",
98+
" if a.score < b.score: return 1 \n",
99+
" if a.name > b.name: return 1 \n",
100+
" if a.name < b.name: return -1 \n",
101+
" return 0\n",
102+
"\n",
103+
"n = int(input())\n",
104+
"data = []\n",
105+
"for i in range(n):\n",
106+
" name, score = input().split()\n",
107+
" score = int(score)\n",
108+
" player = Player(name, score)\n",
109+
" data.append(player)\n",
110+
"\n",
111+
"data = sorted(data, key=cmp_to_key(Player.comparator))\n",
112+
"for i in data:\n",
113+
" print(i.name, i.score)"
114+
]
115+
},
116+
{
117+
"cell_type": "code",
118+
"execution_count": null,
119+
"metadata": {},
120+
"outputs": [],
121+
"source": []
122+
}
123+
],
124+
"metadata": {
125+
"kernelspec": {
126+
"display_name": "Python 3",
127+
"language": "python",
128+
"name": "python3"
129+
},
130+
"language_info": {
131+
"codemirror_mode": {
132+
"name": "ipython",
133+
"version": 3
134+
},
135+
"file_extension": ".py",
136+
"mimetype": "text/x-python",
137+
"name": "python",
138+
"nbconvert_exporter": "python",
139+
"pygments_lexer": "ipython3",
140+
"version": "3.8.2"
141+
}
142+
},
143+
"nbformat": 4,
144+
"nbformat_minor": 4
145+
}

0 commit comments

Comments
 (0)