-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumber_of_sender_2284.py
102 lines (80 loc) · 3.23 KB
/
number_of_sender_2284.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import operator
import random
def largestWordCount(messages, senders):
length = len(messages)
m = messages
s = senders
i = 0
#print (length)
n = length + 1
hash_dict = {}
#arr = []
updated_table = {}
for i in range(len(m)):
#print (i)
if s[i] in hash_dict:
hash_dict[s[i]] += (m[i].count(' ')) + 1
else:
hash_dict[s[i]] = (m[i].count(' ')) + 1
#arr.append(s[i])
#hash_dict.sort()
#sorted(hash_dict.items())
keymax = max(hash_dict.items(), key = operator.itemgetter(1))[0]
#Keymax = max(hash_dict, value= lambda x: hash_dict[x])
#hash_dict.values().sorted()
#for each in sorted (hash_dict.values()):
#print (each)
#dict['key3'] = 'Geeks'
#hash_count = {}
#hash_count[hash_dict[0]] = 6
#for each in
#for each in
#print(hash_dict.values())
#position = hash_dict.index(key)
#print(key_list[position])
#for each in hash_dict:
#print (each)
#if hash_dict[each]
#print (hash_dict[Keymax])
array = []
for each in hash_dict:
if (hash_dict[keymax]) == hash_dict[each]:
array.append(each)
#using quick select to get the last element
def partition(arr, l, r, ):
random_i = random.randint(l, r)
arr[l], arr[random_i] = arr[random_i], arr[l]
pivot, j = arr[l], l
#print (arr[l], j)
for i in range(l + 1, r + 1):
if arr[i] < pivot:
j += 1
arr[i], arr[j] = arr[j], arr[i]
arr[l], arr[j] = arr[j], arr[l]
#print( arr[j], j)
return j
def maxElement(arr, l, r, max):
if (max > 0 and max <= r-l+1):
pivot_index = partition(arr, l , r)
if (pivot_index-l == max-1):
return arr[pivot_index]
if (pivot_index-l > max-1):
return maxElement(arr, l, pivot_index-1, max)
#else when pivot_index < max-1, return
return maxElement(arr, pivot_index+1, r, max-pivot_index+l-1)
else:
return ("kya kar rha bhai tu?")
print (array)
n = len(array)
#print (n)
maximum = n
largest = maxElement(array, 0, n-1, maximum)
print (largest)
# array.sort()
# array = array[::-1]
# print (array)
# print (array[0]) #if we use sort function to get the last element it will take O(n log(n)) time instead we can use QUICKSelect function to get the last element in O(n) time
#print (arr)
messages = ["Hello userTwooo","Hi userThree","Wonderful day Alice and and","Nice day userThree", "blah blah blah blah blah"]
senders = ["random","userTwo","userzhree","random", "userone"]
largestWordCount(messages, senders)