Skip to content

Commit

Permalink
Add HackerRank solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
Karsen Gauss committed Jan 9, 2017
1 parent c9c1b87 commit 8aa957e
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
A repository of solutions for various challenges (code tests, recruitment tests, code riddles etc) from several popular websites and communities.

For details see subdirectories.

**Note: If you are trying to learn and want to understand why your solution doesn't work, feel free to send me a message, so that I can give you a hint instead of a full solution.**
23 changes: 23 additions & 0 deletions hackerrank-algorithms-implementation/mini-max-sum.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Foundation


if let line = readLine() {
let numbers = line.components(separatedBy: " ")

var sum: Int64 = 0
var min: Int64 = 1_000_000_000
var max: Int64 = 0

for i in numbers {
let num = Int64(i)!
sum += num
if num <= min {
min = num
}
if num >= max {
max = num
}
}

print("\(sum - max) \(sum - min)")
}
8 changes: 8 additions & 0 deletions hackerrank-algorithms-warmup/a-very-big-sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/python3

import sys


n = int(input().strip())
arr = [int(arr_temp) for arr_temp in input().strip().split(' ')]
print(sum(arr))
11 changes: 11 additions & 0 deletions hackerrank-algorithms-warmup/circular-array-rotation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/python3

import sys


n,k,q = input().strip().split(' ')
n,k,q = [int(n),int(k),int(q)]
a = [int(a_temp) for a_temp in input().strip().split(' ')]
for a0 in range(q):
m = int(input().strip())
print(a[(m + n - k) % n])
21 changes: 21 additions & 0 deletions hackerrank-algorithms-warmup/compare-the-triplets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/python3

import sys


a0,a1,a2 = input().strip().split(' ')
a0,a1,a2 = [int(a0),int(a1),int(a2)]
b0,b1,b2 = input().strip().split(' ')
b0,b1,b2 = [int(b0),int(b1),int(b2)]

a = [a0, a1, a2]
b = [b0, b1, b2]

aa, bb = 0, 0
for i, x in enumerate(a):
if b[i] < x:
aa += 1
elif b[i] > x:
bb += 1

print('{0} {1}'.format(aa, bb))
17 changes: 17 additions & 0 deletions hackerrank-algorithms-warmup/diagonal-difference.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/python3

import sys


n = int(input().strip())
a = []
for a_i in range(n):
a_t = [int(a_temp) for a_temp in input().strip().split(' ')]
a.append(a_t)

acc = 0
i = 0
for row in a:
acc += row[i] - row[n - 1 - i]
i += 1
print(abs(acc))
20 changes: 20 additions & 0 deletions hackerrank-algorithms-warmup/plus-minus.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/python3

import sys


n = int(input().strip())
arr = [int(arr_temp) for arr_temp in input().strip().split(' ')]

pos = 0
neg = 0

for item in arr:
if item > 0:
pos +=1
elif item < 0:
neg += 1

print("{0:.6f}".format(pos / n))
print("{0:.6f}".format(neg / n))
print("{0:.6f}".format((n - pos - neg) / n))
11 changes: 11 additions & 0 deletions hackerrank-algorithms-warmup/simple-array-sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/python3

import sys


n = int(input().strip())
arr = [int(arr_temp) for arr_temp in input().strip().split(' ')]
acc = 0
for x in arr:
acc += x
print(acc)
11 changes: 11 additions & 0 deletions hackerrank-algorithms-warmup/staircase.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/python3

import sys


n = int(input().strip())

prin = "#"
for i in range(n):
print("{:>{align}}".format(prin, align=(n)))
prin += "#"
18 changes: 18 additions & 0 deletions hackerrank-algorithms-warmup/time-conversion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/python3

import sys


time = input().strip().split(":")
pm = time[2].endswith("PM")
time[0] = int(time[0])
if pm:
if time[0] < 12:
time[0] += 12
time[2] = time[2].strip("PM")
else:
if time[0] == 12:
time[0] = 0
time[2] = time[2].strip("AM")

print("{:02d}:{}:{}".format(time[0], time[1], time[2]))

0 comments on commit 8aa957e

Please sign in to comment.