Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3576660

Browse files
issue fix and merge sort program added
1 parent bdf45d1 commit 3576660

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/python3
2+
3+
import math
4+
import os
5+
import random
6+
import re
7+
import sys
8+
9+
# Complete the jumpingOnClouds function below.
10+
def jumpingOnClouds(c, k):
11+
e=100
12+
energy=0
13+
i=0
14+
while(i!=len(c)):
15+
if(c[i]==1):
16+
energy=e-3
17+
e=energy
18+
else:
19+
energy=e-1
20+
e=energy
21+
i+=k
22+
return energy
23+
24+
if __name__ == '__main__':
25+
fptr = open(os.environ['OUTPUT_PATH'], 'w')
26+
27+
nk = input().split()
28+
29+
n = int(nk[0])
30+
31+
k = int(nk[1])
32+
33+
c = list(map(int, input().rstrip().split()))
34+
35+
result = jumpingOnClouds(c, k)
36+
37+
fptr.write(str(result) + '\n')
38+
39+
fptr.close()

‎Mearge Sort/merge_sort.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Python 3 program to count inversions in an array
2+
3+
# Function to Use Inversion Count
4+
def mergeSort(arr, n):
5+
# A temp_arr is created to store
6+
# sorted array in merge function
7+
temp_arr = [0] * n
8+
return _mergeSort(arr, temp_arr, 0, n - 1)
9+
10+
11+
# This Function will use MergeSort to count inversions
12+
13+
def _mergeSort(arr, temp_arr, left, right):
14+
# A variable inv_count is used to store
15+
# inversion counts in each recursive call
16+
17+
inv_count = 0
18+
19+
# We will make a recursive call if and only if
20+
# we have more than one elements
21+
22+
if left < right:
23+
# mid is calculated to divide the array into two subarrays
24+
# Floor division is must in case of python
25+
26+
mid = (left + right) // 2
27+
28+
# It will calculate inversion counts in the left subarray
29+
30+
inv_count += _mergeSort(arr, temp_arr, left, mid)
31+
32+
# It will calculate inversion counts in right subarray
33+
34+
inv_count += _mergeSort(arr, temp_arr, mid + 1, right)
35+
36+
# It will merge two subarrays in a sorted subarray
37+
38+
inv_count += merge(arr, temp_arr, left, mid, right)
39+
return inv_count
40+
41+
42+
# This function will merge two subarrays in a single sorted subarray
43+
def merge(arr, temp_arr, left, mid, right):
44+
i = left # Starting index of left subarray
45+
j = mid + 1 # Starting index of right subarray
46+
k = left # Starting index of to be sorted subarray
47+
inv_count = 0
48+
49+
# Conditions are checked to make sure that i and j don't exceed their
50+
# subarray limits.
51+
52+
while i <= mid and j <= right:
53+
54+
# There will be no inversion if arr[i] <= arr[j]
55+
56+
if arr[i] <= arr[j]:
57+
temp_arr[k] = arr[i]
58+
k += 1
59+
i += 1
60+
else:
61+
# Inversion will occur.
62+
temp_arr[k] = arr[j]
63+
inv_count += (mid - i + 1)
64+
k += 1
65+
j += 1
66+
67+
# Copy the remaining elements of left subarray into temporary array
68+
while i <= mid:
69+
temp_arr[k] = arr[i]
70+
k += 1
71+
i += 1
72+
73+
# Copy the remaining elements of right subarray into temporary array
74+
while j <= right:
75+
temp_arr[k] = arr[j]
76+
k += 1
77+
j += 1
78+
79+
# Copy the sorted subarray into Original array
80+
for loop_var in range(left, right + 1):
81+
arr[loop_var] = temp_arr[loop_var]
82+
83+
return inv_count
84+
85+
86+
# Driver Code
87+
# Given array is
88+
arr = [1, 20, 6, 4, 5]
89+
n = len(arr)
90+
result = mergeSort(arr, n)
91+
print("Number of inversions are", result)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /