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 60d4d65

Browse files
Updated code
1 parent fb996dd commit 60d4d65

File tree

8 files changed

+364
-361
lines changed

8 files changed

+364
-361
lines changed

‎groking/triple_sum_to_zero_grok.py‎

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,41 @@
1212
# Output: [[-5, 2, 3], [-2, -1, 3]]
1313
# Explanation: There are two unique triplets whose sum is equal to zero.
1414

15+
1516
def search_triplets(arr):
16-
arr.sort()
17-
triplets = []
18-
for i in range(len(arr)):
19-
if i > 0 and arr[i] == arr[i-1]: # skip same element to avoid duplicate triplets
20-
continue
21-
search_pair(arr, -arr[i], i+1, triplets) # i + 1 because we are looking at arr[i] as the target
17+
arr.sort()
18+
triplets = []
19+
for i in range(len(arr)):
20+
# skip same element to avoid duplicate triplets
21+
if i > 0 and arr[i] == arr[i - 1]:
22+
continue
23+
# i + 1 because we are looking at arr[i] as the target
24+
search_pair(arr, -arr[i], i + 1, triplets)
2225

23-
return triplets
26+
return triplets
2427

2528

2629
def search_pair(arr, target_sum, left, triplets):
27-
right = len(arr) - 1
28-
while(left < right):
29-
current_sum = arr[left] + arr[right]
30-
if current_sum == target_sum: # found the triplet
31-
triplets.append([-target_sum, arr[left], arr[right]])
32-
left += 1
33-
right -= 1
34-
while left < right and arr[left] == arr[left - 1]:
35-
left += 1 # skip same element to avoid duplicate triplets
36-
while left < right and arr[right] == arr[right + 1]:
37-
right -= 1 # skip same element to avoid duplicate triplets
38-
elif target_sum > current_sum:
39-
left += 1 # we need a pair with a bigger sum
40-
else:
41-
right -= 1 # we need a pair with a smaller sum
30+
right = len(arr) - 1
31+
while(left < right):
32+
current_sum = arr[left] + arr[right]
33+
if current_sum == target_sum: # found the triplet
34+
triplets.append([-target_sum, arr[left], arr[right]])
35+
left += 1
36+
right -= 1
37+
while left < right and arr[left] == arr[left - 1]:
38+
left += 1 # skip same element to avoid duplicate triplets
39+
while left < right and arr[right] == arr[right + 1]:
40+
right -= 1 # skip same element to avoid duplicate triplets
41+
elif target_sum > current_sum:
42+
left += 1 # we need a pair with a bigger sum
43+
else:
44+
right -= 1 # we need a pair with a smaller sum
4245

4346

4447
def main():
45-
print(search_triplets([-3, 0, 1, 2, -1, 1, -2]))
46-
print(search_triplets([-5, 2, -1, -2, 3]))
48+
print(search_triplets([-3, 0, 1, 2, -1, 1, -2]))
49+
print(search_triplets([-5, 2, -1, -2, 3]))
4750

4851

4952
main()

‎hackerrank/LOLOLLOL_hacker.txt‎

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
BTW Enter your code here. Read input from STDIN. Print output to STDOUT
2-
3-
HAI 1.2
4-
I HAS A ENDP ITZ A NUMBR
5-
GIMMEH ENDP
6-
ENDP IS NOW A NUMBR
7-
8-
I HAS A STARTP ITZ 0
9-
IM IN YR LOOP UPPIN YR STARTP TIL BOTH SAEM STARTP AN ENDP
10-
I HAS A EMO ITZ A YARN
11-
GIMMEH EMO
12-
13-
EMO, WTF?
14-
OMG ":)"
15-
VISIBLE "HAPI"
16-
GTFO
17-
OMG ":("
18-
VISIBLE "SADZ"
19-
GTFO
20-
OMG ":\"
21-
VISIBLE "HUH"
22-
GTFO
23-
OMG ":D"
24-
VISIBLE "LOL"
25-
GTFO
26-
OMGWTF
27-
VISIBLE "IDK"
28-
GTFO
29-
OIC
30-
IM OUTTA YR LOOP
1+
BTW Enter your code here. Read input from STDIN. Print output to STDOUT
2+
3+
HAI 1.2
4+
I HAS A ENDP ITZ A NUMBR
5+
GIMMEH ENDP
6+
ENDP IS NOW A NUMBR
7+
8+
I HAS A STARTP ITZ 0
9+
IM IN YR LOOP UPPIN YR STARTP TIL BOTH SAEM STARTP AN ENDP
10+
I HAS A EMO ITZ A YARN
11+
GIMMEH EMO
12+
13+
EMO, WTF?
14+
OMG ":)"
15+
VISIBLE "HAPI"
16+
GTFO
17+
OMG ":("
18+
VISIBLE "SADZ"
19+
GTFO
20+
OMG ":\"
21+
VISIBLE "HUH"
22+
GTFO
23+
OMG ":D"
24+
VISIBLE "LOL"
25+
GTFO
26+
OMGWTF
27+
VISIBLE "IDK"
28+
GTFO
29+
OIC
30+
IM OUTTA YR LOOP
3131
KTHXBYE
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
# Arrays: Left Rotation
2-
# A left rotation operation on an array shifts each of the array's elements unit to the left.
3-
# For example, if left rotations are performed on array [1, 2, 3, 4, 5], then the array would become [3, 4, 5, 1, 2].
4-
# Note that the lowest index item moves to the highest index in a rotation. This is called a circular array.
5-
# Given an array a of n integers and a number, d, perform d left rotations on the array.
6-
# Return the updated array to be printed as a single line of space-separated integers.
7-
class Solution:
8-
def rotLeft(self, a, d):
9-
result = [0] * len(a)
10-
11-
for i in range(len(a)):
12-
result[i-d] = a[i]
13-
14-
return result
15-
16-
print(Solution().rotLeft([1, 2, 3, 4, 5], 4))
1+
# Arrays: Left Rotation
2+
# A left rotation operation on an array shifts each of the array's elements unit to the left.
3+
# For example, if left rotations are performed on array [1, 2, 3, 4, 5], then the array would become [3, 4, 5, 1, 2].
4+
# Note that the lowest index item moves to the highest index in a rotation. This is called a circular array.
5+
# Given an array a of n integers and a number, d, perform d left rotations on the array.
6+
# Return the updated array to be printed as a single line of space-separated integers.
7+
class Solution:
8+
def rotLeft(self, a, d):
9+
result = [0] * len(a)
10+
11+
for i in range(len(a)):
12+
result[i-d] = a[i]
13+
14+
return result
15+
16+
print(Solution().rotLeft([1, 2, 3, 4, 5], 4))
1717
# [5, 1, 2, 3, 4]

‎hackerrank/bar_chart_hacker.py‎

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,55 @@
1-
import fileinput
2-
import numpy as np
3-
4-
input_arr = [[3, 1, 2], [5, 2, 3]]
5-
6-
def readInput():
7-
for line in fileinput.input():
8-
input_arr.append([int(num) for num in line.rstrip() if num.isnumeric()])
9-
10-
def barChart(points):
11-
cols = len(points[0])
12-
result = [0] * cols
13-
points_result = []
14-
15-
for i in range(len(points)):
16-
for j in range(len(points[0])):
17-
result[j] = max(result[j], points[i][j])
18-
19-
print(result)
20-
21-
# for i in range(len(result)):
22-
# for j in range(result[i]):
23-
# print('+ ', end='')
24-
# print('')
25-
26-
result_array = []
27-
for i in range(len(result)):
28-
currentstring = ''
29-
for j in range(result[i]):
30-
currentstring += '+'
31-
result_array.append(currentstring)
32-
33-
print(currentstring)
34-
print(result_array)
35-
36-
# arr1 = np.array(result_array)
37-
38-
# print(f'Original Array:\n{arr1}')
39-
40-
# arr1_transpose = arr1.transpose()
41-
42-
# print(f'Transposed Array:\n{arr1_transpose}')
43-
44-
45-
46-
47-
48-
49-
50-
51-
# readInput()
52-
# print(input_arr)
53-
barChart(input_arr)
54-
1+
import fileinput
2+
import numpy as np
3+
4+
input_arr = [[3, 1, 2], [5, 2, 3]]
5+
6+
def readInput():
7+
for line in fileinput.input():
8+
input_arr.append([int(num) for num in line.rstrip() if num.isnumeric()])
9+
10+
def barChart(points):
11+
cols = len(points[0])
12+
result = [0] * cols
13+
points_result = []
14+
15+
for i in range(len(points)):
16+
for j in range(len(points[0])):
17+
result[j] = max(result[j], points[i][j])
18+
19+
print(result)
20+
21+
# for i in range(len(result)):
22+
# for j in range(result[i]):
23+
# print('+ ', end='')
24+
# print('')
25+
26+
result_array = []
27+
for i in range(len(result)):
28+
currentstring = ''
29+
for j in range(result[i]):
30+
currentstring += '+'
31+
result_array.append(currentstring)
32+
33+
print(currentstring)
34+
print(result_array)
35+
36+
# arr1 = np.array(result_array)
37+
38+
# print(f'Original Array:\n{arr1}')
39+
40+
# arr1_transpose = arr1.transpose()
41+
42+
# print(f'Transposed Array:\n{arr1_transpose}')
43+
44+
45+
46+
47+
48+
49+
50+
51+
# readInput()
52+
# print(input_arr)
53+
barChart(input_arr)
54+
5555

0 commit comments

Comments
(0)

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