Created
June 14, 2017 05:56
-
-
Save tlkahn/31f7e8d51f1b54d62b47997f1a87c858 to your computer and use it in GitHub Desktop.
hackerrank playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# hackerrank playground | |
import pdb | |
import sys | |
# def solve(*args): | |
# result = [0, 0] | |
# a_ = [args[i] for i in range(int(len(args)/2))] | |
# b_ = [args[i] for i in range(int(len(args)/2), len(args))] | |
# pairs = zip(a_, b_) | |
# for pair in pairs: | |
# if pair[0] > pair[1]: | |
# result[0] += 1 | |
# elif pair[0] < pair[1]: | |
# result[1] += 1 | |
# return result | |
# print(solve(5,6,7,3,6,10)) | |
# import math | |
# a = range(25) | |
# n = 25 | |
# diag_elems = [] | |
# for i in range(int(math.sqrt(n))): | |
# for j in range(int(math.sqrt(n))): | |
# if i==j: diag_elems.append(a[i+j*int(math.sqrt(n))]) | |
# anti_diag_elems = [] | |
# for i in range(int(math.sqrt(n))): | |
# for j in range(int(math.sqrt(n))): | |
# if i + j == math.sqrt(n) - 1: anti_diag_elems.append(a[i+j*int(math.sqrt(n))]) | |
# print(diag_elems) | |
# print(anti_diag_elems) | |
# diag_elems_total = 0 | |
# anti_diag_elems_total = 0 | |
# for i in range(int(math.sqrt(n))): | |
# diag_elems_total += diag_elems[i] | |
# anti_diag_elems_total += anti_diag_elems[i] | |
# print(diag_elems_total - anti_diag_elems_total) | |
# n = 6 | |
# arr = [-4, 3, -9, 0, 4, 1] | |
# def positivity_ratios(arr): | |
# pos = 0 | |
# neg = 0 | |
# zs = 0 | |
# for i in arr: | |
# if i > 0: pos += 1 | |
# if i < 0: neg += 1 | |
# else: zs += 1 | |
# result = ["{:.6f}".format(el/len(arr)) for el in [pos, neg, zs]] | |
# return result | |
# print(positivity_ratios(arr)) | |
# n = 6 | |
# def draw_triangle(n): | |
# for i in range(n): | |
# j = n - 1 - i | |
# row = " " * j + "*" * (i + 1) | |
# print(row) | |
# draw_triangle(n) | |
# def solve(arr): | |
# lst = [] | |
# for i in range(arr): | |
# lst.push(arr[0:i] + arr[i+1:]) | |
# sum_lst = [sum(l) for l in lst] | |
# return "%d %d" % (min(sum_lst), max(sum_lst)) | |
# def birthdayCakeCandles(n, ar): | |
# running_max = 0 | |
# result = 0 | |
# for a in ar: | |
# print("running_max", running_max) | |
# print("result", result) | |
# if a > running_max: | |
# result = 1 | |
# running_max = a | |
# elif a == running_max: | |
# result += 1 | |
# else: | |
# pass | |
# return result | |
# print(birthdayCakeCandles(4, [3, 2, 1, 3])) | |
# import re | |
# def timeConversion(s): | |
# r = re.compile(r"^(\d{2})(.*)(AM|PM|am|pm)$") | |
# hour = r.match(s).groups()[0] | |
# middle = r.match(s).groups()[1] | |
# ampm = r.match(s).groups()[2] | |
# new_hour = hour | |
# if ampm == "AM" or ampm == "am": | |
# if hour == "12": new_hour = "00" | |
# else: | |
# if hour == "12": pass | |
# else: new_hour = int(hour) + 12 | |
# return str(new_hour) + middle | |
# print(timeConversion("12:45:54PM")) | |
# def get_distances(arr): | |
# result = [] | |
# for i in range(1, len(arr)): | |
# result.append(arr[i] - arr[i-1]) | |
# return result | |
# def solution(k, arr, origin=0): | |
# arr.sort() | |
# if len(arr) < 1: | |
# return 0 | |
# if len(arr) == 1: | |
# return 1 | |
# else: | |
# if arr[1] - arr[0] > k: | |
# return 1 + solution(k, arr[1:], origin+1) | |
# else: | |
# flag = 1 | |
# for i in range(1, len(arr)): | |
# if arr[0] + k >= arr[i]: | |
# flag = i | |
# continue | |
# else: | |
# break | |
# # print(origin + flag) | |
# new_start = flag | |
# for i in range(flag + 1, len(arr)): | |
# if arr[i] - arr[flag] <= k: | |
# new_start = i | |
# continue | |
# else: | |
# break | |
# return 1 + solution(k, arr[(new_start + 1):], origin + new_start) | |
# n = 5 | |
# k = 1 | |
# x = [1,2,3,4,5] | |
# k = 2 | |
# x = [9, 5, 4, 2, 6, 15, 11, 12] | |
def solution(k, arr, origin=0): | |
arr = sorted(list(set(arr))) | |
result = 1 | |
cur = 0 | |
origin = 0 | |
for i in range(1, len(arr)): | |
f = False | |
print(origin) | |
if arr[i] - arr[origin] <= k: | |
pass | |
else: | |
origin = i - 1 | |
print(origin) | |
j = 1 | |
while origin + j < len(arr): | |
if arr[origin + j] - arr[origin] <= k: | |
pass | |
else: | |
origin = origin + j | |
i = origin + j + 1 | |
result += 1 | |
f = True | |
break | |
j += 1 | |
if f: continue | |
return result | |
k = 1 | |
x = [1,2,3,4,5] | |
solution(k,x) | |
# print(solution(k,x)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment