Skip to content

Commit e609049

Browse files
committed
Algorithms: Sorting Problems
1 parent 1568c36 commit e609049

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Bubble Sort Problem
2+
3+
# It's Lolympics 2016 right now, and we all know who's the best player there right now: Kalyani! Obviously, he has a
4+
# huge female fan following and he has to make sure they are all happy and rooting for him to win the gold medals.
5+
#
6+
# But with fan following comes arrogance and lack of time. Thus, he has sufficient time to interact with atmost T of
7+
# his fans. Each fan is defined by two parameters : Name and Fan Quotient. The name defines the name of the fan, while
8+
# the fan quotient is a measure of the fan's devotion towards Kalyani. Higher the fan quotient, greater is the devotion.
9+
# Kalyani now wants to meet T of his fans. While selecting the fans he wants to meet, he wants to make sure that a fan
10+
# with a higher fan quotient should be given a chance in favour of those with lesser fan quotient. In case of ties, he
11+
# sorts their name lexicographically and chooses the lexicographically lesser named fan.
12+
#
13+
# Given details of N fans, can you help out Kalyani by giving him a list of fans he would be interacting with?
14+
#
15+
# Input Format :
16+
#
17+
# The first line contains N and T, the number of fans and the maximum number of fans Kalyani can meet. Each of the
18+
# next N lines contains a string and an integer separated by a space. The string denotes the name of the fan while the
19+
# integer depicts the fan quotient.
20+
#
21+
# Output Format :
22+
#
23+
# Output T lines, each containing the name of the fans selected. Fans with higher fan quotient should be outputted
24+
# first and in case of a tie, the lexicographically minimum name should come first.
25+
#
26+
# Constraints :
27+
# 1≤T≤N≤1000
28+
# 1≤lengthofname≤20
29+
# 1≤fanquotient≤109
30+
#
31+
# Name would only consist of characters in set [a-z]. It is not guaranteed that the names are distinct.
32+
#
33+
# SAMPLE INPUT
34+
# 3 2
35+
# surbhi 3
36+
# surpanakha 3
37+
# shreya 5
38+
#
39+
# SAMPLE OUTPUT
40+
# shreya
41+
# surbhi
42+
43+
# less efficient solution
44+
n, t = map(int, input().split())
45+
fans = []
46+
for _ in range(n):
47+
fans.append(input().split())
48+
49+
for i in range(n - 1, 0, -1):
50+
for j in range(i):
51+
if int(fans[j][1]) > int(fans[j + 1][1]):
52+
fans[j], fans[j + 1] = fans[j + 1], fans[j]
53+
54+
fans = fans[::-1]
55+
56+
for i in range(t):
57+
if int(fans[i][1]) == int(fans[i + 1][1]):
58+
check = [fans[i][0], fans[i + 1][0]]
59+
check.sort()
60+
print(check[0])
61+
else:
62+
print(fans[i][0])
63+
64+
# more efficient solution
65+
n,m = map(int,input().split())
66+
s={}
67+
for i in range(n):
68+
k,v=input().split()
69+
v=int(v)
70+
if v not in s:
71+
s[v]=[k]
72+
else:
73+
s[v].append(k)
74+
s[v].sort()
75+
list1=[x for x in s.keys()]
76+
list1.sort(reverse=True)
77+
c=0
78+
for i in list1:
79+
if len(s[i])>0:
80+
for j in s[i]:
81+
print (j)
82+
c=c+1
83+
if c == m:
84+
break
85+
if c==m:
86+
break

0 commit comments

Comments
 (0)