Skip to content

Commit f090c96

Browse files
Sort Special...
1 parent aacd088 commit f090c96

8 files changed

+483
-0
lines changed

10X/Python/Array/Insert_In_Between.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
Insert in Between
3+
You have been given a sorted array arr and one integer x. You need to insert x into the given array,
4+
such that the array still remains sorted.
5+
6+
Input
7+
Two space seperated values, denoting n and x respectively. One line containing n space seperated values,
8+
denoting the elements of the array.
9+
10+
Output
11+
Resultant array.
12+
13+
Example
14+
Input1:
15+
16+
5 3
17+
1 2 3 4 5
18+
Output1:
19+
20+
1 2 3 3 4 5
21+
22+
"""
23+
x,n=map(int,input().split())
24+
ar=list(map(int,input().split()))
25+
ar.append(n)
26+
i=len(ar)-1
27+
while(i>0)and ar[i-1]>ar[i]:
28+
ar[i],ar[i-1]=ar[i-1],ar[i]
29+
i-=1
30+
[print(x,end=" ") for x in ar]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
'''
2+
Intersection Sorted Array
3+
Given 2 sorted arrays (Sorted in ascending order). You have to print out the numbers which are in intersection of these 2 arrays. Print -1 if there is intersection is empty.
4+
5+
Input
6+
First line is n which denotes the number of elements in first array. Following n lines are elements for the first array.
7+
Next line is m which denotes the number of elements in second array. Following m lines are elements for the second array.
8+
9+
Output
10+
k lines representing the numbers which are common to both the arrays.
11+
12+
Example
13+
Input:
14+
15+
4
16+
1
17+
1
18+
2
19+
2
20+
3
21+
2
22+
2
23+
5
24+
Output:
25+
26+
2
27+
2
28+
Explanation
29+
First line is 4, i.e. 4 elements in the first array. So the first array is [1,1,2,2].
30+
m is 3, so the second array is [2,2,5].
31+
Comparing the 2 arrays, common elements to both of them are [2,2] which is our output.
32+
33+
'''
34+
'''
35+
def intersect(nums1, nums2):
36+
# implement this function
37+
n1={}
38+
n2={}
39+
arr=[]
40+
for i in nums1:
41+
if i in n1:
42+
n1[i]+=1
43+
else:
44+
n1[i]=1
45+
for i in nums2:
46+
if i in n2:
47+
n2[i]+=1
48+
else:
49+
n2[i]=1
50+
for i in n1:
51+
if i in n1 and i in n2:
52+
for j in range(min(n1[i],n2[i])):
53+
arr.append(i)
54+
if arr:
55+
return arr
56+
else:
57+
return[-1]
58+
59+
60+
61+
'''
62+
from collections import defaultdict
63+
def intersect(nums1, nums2):
64+
# implement this function
65+
n1=defaultdict(int)
66+
n2=defaultdict(int)
67+
arr=[]
68+
n3=defaultdict(int)
69+
for i in nums1:
70+
if i in n1:
71+
n1[i]+=1
72+
else:
73+
n1[i]=1
74+
for i in nums2:
75+
if i in n2:
76+
n2[i]+=1
77+
else:
78+
n2[i]=1
79+
for i in n1:
80+
if i in n1 and i in n2:
81+
for j in range(min(n1[i],n2[i])):
82+
arr.append(i)
83+
if arr:
84+
return arr
85+
else:
86+
return[-1]
87+
88+
89+
90+
91+
# DO NOT change anything below this line
92+
if __name__ == "__main__":
93+
num1_len = int(input())
94+
nums1 = []
95+
for index in range(num1_len):
96+
nums1.append(int(input()))
97+
num2_len = int(input())
98+
nums2 = []
99+
for index in range(num2_len):
100+
nums2.append(int(input()))
101+
102+
for element in intersect(nums1, nums2):
103+
print(element)

10X/Python/Array/LuckyInteger.py

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
'''
2+
Lucky Number
3+
Given a sorted array of n integers, a lucky integer is an integer which has a frequency in the array equal to its value.
4+
5+
Return a lucky integer in the array. If there is no lucky integer return -1.
6+
7+
If you have multiple lucky integers, please return the first lucky integer in the array (lucky number with the least index).
8+
9+
Input
10+
First line contains a positive integer n, denoting the number of elements in the array. It is followed by n lines. Each line contains one integer denoting an element in the array.
11+
12+
Output
13+
One line specifying the lucky integer in the array
14+
15+
Example
16+
Input:
17+
18+
4
19+
2
20+
2
21+
3
22+
4
23+
Output:
24+
25+
2
26+
Explanation
27+
First line is 4, i.e. 4 elements in the array. The array is [2,2,3,4]. We can see that number 2 is repeating 2 times hence it is the lucky number which is our output
28+
29+
'''
30+
'''
31+
def findLuckyNumber(nums):
32+
for i in range(len(nums)):
33+
if i==0 and nums[i]==1:
34+
return nums[i]
35+
elif nums[i]==i:
36+
return nums[i]
37+
return -1
38+
'''
39+
'''
40+
def findLuckyNumber(nums):
41+
# implement this function
42+
for i in nums:
43+
if i==0 and nums[i]==1:
44+
return nums[i]
45+
elif count(nums[i])==i:
46+
return nums[i]
47+
return -1
48+
49+
# DO NOT change anything below this line
50+
if __name__ == "__main__":
51+
num_elems = int(input())
52+
input_arr = []
53+
for index in range(num_elems):
54+
input_arr.append(int(input()))
55+
56+
print(findLuckyNumber(input_arr))
57+
'''
58+
def findLuckyNumber(nums):
59+
# implement this function
60+
dic={}
61+
for i in nums:
62+
if i in dic:
63+
dic[i]+=1
64+
else:
65+
dic[i]=1
66+
for i in nums:
67+
if dic[i]==i:
68+
return i
69+
return -1
70+
71+
72+
73+
# DO NOT change anything below this line
74+
if __name__ == "__main__":
75+
num_elems = int(input())
76+
input_arr = []
77+
for index in range(num_elems):
78+
input_arr.append(int(input()))
79+
80+
print(findLuckyNumber(input_arr))

10X/Python/Array/Smaller_No.py

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
""""
2+
Smaller Numbers
3+
Given the array nums, for each nums[i] find out how many numbers in the array are smaller than it. That is, for each nums[i] you have to count the number of valid j's such that j != i and nums[j] < nums[i].
4+
5+
Return the answer in an array.
6+
7+
Input
8+
First line consists of a single integer, denoting N, length of array.
9+
10+
Next N lines containing one integer per line denoting the elements of array A.
11+
12+
Output
13+
Print the resultant array, one element in one line.
14+
15+
Constraints:
16+
2 <= nums.length <= 500
17+
0 <= nums[i] <= 100
18+
Example
19+
Input:
20+
21+
5
22+
23+
8
24+
25+
1
26+
27+
2
28+
29+
2
30+
31+
3
32+
33+
Output:
34+
35+
4
36+
37+
0
38+
39+
1
40+
41+
1
42+
43+
3
44+
45+
Explanation
46+
nums = [8,1,2,2,3]
47+
48+
Output: [4,0,1,1,3]
49+
50+
For nums[0]=8 there exist four smaller numbers than it (1, 2, 2 and 3).
51+
52+
For nums[1]=1 does not exist any smaller number than it.
53+
54+
For nums[2]=2 there exist one smaller number than it (1).
55+
56+
For nums[3]=2 there exist one smaller number than it (1).
57+
58+
For nums[4]=3 there exist three smaller numbers than it (1, 2 and 2).
59+
"""
60+
"""
61+
a=[int(input())for x in range(int(input())) ]
62+
inlis=[]
63+
outlis=[]
64+
no=a[0]
65+
inlis.append(no)
66+
for i in range(1,len(a)):
67+
if a[i]<a[0]:
68+
inlis.append(a[i])
69+
70+
for j in range(len(inlis)):
71+
count=0
72+
for i in range(len(a)):
73+
if inlis[j]>a[i]:
74+
count+=1
75+
outlis.append(count)
76+
77+
#[print(i) for i in outlis] print(outlis)
78+
#print(inlis)
79+
"""
80+
a=[int(input())for x in range(int(input())) ]
81+
b=sorted(a)
82+
for i in a:
83+
print(b.index(i))

10X/Python/Array/Sort Insertion.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
"""
3+
insertion
4+
The task is to complete the insert() function which is used to implement Insertion Sort. You don't need to worry about input/output.
5+
6+
Insertion Sort Visualization
7+
8+
Input
9+
First line denotes n, the size of the given array. The next line denotes the spaces seperated integers, which are the elements of the given array.
10+
11+
Output
12+
Sorted array, as space seperated values.
13+
14+
Example
15+
Input1:
16+
17+
5
18+
4 1 3 9 7
19+
Output1:
20+
21+
1 3 4 7 9
22+
23+
"""
24+
25+
def insert(arr,n):
26+
# Your code goes here
27+
j=0
28+
for i in range(1,len(arr)):
29+
j=i
30+
while(j>0) and arr[j]<arr[j-1]:
31+
arr[j],arr[j-1]=arr[j-1],arr[j]
32+
j-=1
33+
return arr
34+
### DO NOT EDIT ANYTHING BELOW THIS LINE
35+
36+
if __name__=='__main__':
37+
n = int(input())
38+
arr = [int(x) for x in input().split()]
39+
insert(arr, n)
40+
print(*arr)
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'''
2+
String Insertion
3+
The task is to complete the stringInsertionSort() function which is used to implement Insertion Sort, for string.
4+
5+
The resultant string should have characters in lexicographically sorted order. You don't need to worry about input/output.
6+
7+
Input
8+
One string
9+
10+
Output
11+
Sorted string
12+
13+
Example
14+
Input1:
15+
16+
adcb
17+
Output1:
18+
19+
abcd
20+
21+
22+
'''
23+
def stringInsertionSort(str):
24+
# Your code goes here
25+
26+
a=[]
27+
for i in range(len(str)):
28+
a.append(ord(str[i]))
29+
# a.sort()
30+
31+
for i in range(1,len(a)):
32+
j=i
33+
while(j>0)and a[j-1]>a[j]:
34+
a[j],a[j-1]=a[j-1],a[j]
35+
j-=1
36+
37+
w=""
38+
for i in a:
39+
w+=chr(i)
40+
#w+=str(chr(i))
41+
return w
42+
43+
44+
45+
46+
### DO NOT CHANGE ANYTHING BELOW THIS LINE
47+
48+
if __name__=='__main__':
49+
input_string = input()
50+
print(stringInsertionSort(input_string))
51+

0 commit comments

Comments
 (0)