Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
lbvf50mobile / reject.js
Created Aug 1, 2020
Codewars.com: The reject() function.
View reject.js
// https://www.codewars.com/kata/52988f3f7edba9839c00037d The reject() function.
let reject = (a,x) => a.filter( z => ! x(z));
@lbvf50mobile
lbvf50mobile / modified_sum.py
Created Aug 1, 2020
Codewars.com: Nth power rules them all!
View modified_sum.py
# https://www.codewars.com/kata/58aed2cafab8faca1d000e20 Nth power rules them all!
def modified_sum(a, n):
return sum(x**n for x in a) - sum(a)
@lbvf50mobile
lbvf50mobile / is_boomerang.rb
Created Aug 1, 2020
Leetcode: 1037. Valid Boomerang.
View is_boomerang.rb
# Leetcode: 1037. Valid Boomerang.
# https://leetcode.com/problems/valid-boomerang/
# @param {Integer[][]} points
# @return {Boolean}
def is_boomerang(points)
p0,p1,p2 = points
x0,y0 = p0
x1,y1 = p1
x2,y2 = p2
a = (y1-y0)/(x1-x0)
@lbvf50mobile
lbvf50mobile / all_cells_dist_order.rb
Created Aug 1, 2020
Leetcode: 1030. Matrix Cells in Distance Order.
View all_cells_dist_order.rb
# Leetcode: 1030. Matrix Cells in Distance Order.
# https://leetcode.com/problems/matrix-cells-in-distance-order/
# Runtime: 208 ms, faster than 50.00% of Ruby online submissions for Matrix Cells in Distance Order.
# Memory Usage: 14.3 MB, less than 50.00% of Ruby online submissions for Matrix Cells in Distance Order.
# @param {Integer} r
# @param {Integer} c
# @param {Integer} r0
# @param {Integer} c0
# @return {Integer[][]}
def all_cells_dist_order(r, c, r0, c0)
@lbvf50mobile
lbvf50mobile / remove_duplicates.rb
Created Aug 1, 2020
Leetcode: 1047. Remove All Adjacent Duplicates In String.
View remove_duplicates.rb
# Leetcode: 1047. Remove All Adjacent Duplicates In String.
# https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/submissions/
# Runtime: 72 ms, faster than 93.10% of Ruby online submissions for Remove All Adjacent Duplicates In String.
# Memory Usage: 12.3 MB, less than 33.33% of Ruby online submissions for Remove All Adjacent Duplicates In String.
# @param {String} s
# @return {String}
def remove_duplicates(s)
a = s.gsub(/(.)\1/,'')
while(a != s)
s = a
View get_strings.php
<?php
# https://www.codewars.com/kata/5b358a1e228d316283001892 Interview Question (easy).
function get_strings($city) {
$ans = [];
$x = count_chars(strtolower($city),1);
$city = preg_replace('/[^a-z]/i','',$city);
$chrs = array_values(array_unique(str_split(strtolower($city))));
foreach( $chrs as $v)
array_push($ans,chr(ord($v)).":".str_repeat('*',$x[ord($v)]));
return implode(',',$ans);
@lbvf50mobile
lbvf50mobile / fibs_fizz_buzz.py
Created Jul 31, 2020
Codewars.com: Fibonacci's FizzBuzz.
View fibs_fizz_buzz.py
# https://www.codewars.com/kata/57bf599f102a39bb1e000ae5 Fibonacci's FizzBuzz.
def fibs_fizz_buzz(n):
if 1 == n: return [1]
if 2 == n: return [1,1]
a,b,i = 1,1,2
ans = [1,1]
while i < n:
new_b = a+b
a,b = b,new_b
if 0 == new_b%15: new_b = "FizzBuzz"
@lbvf50mobile
lbvf50mobile / toNumberArray.js
Created Jul 31, 2020
Codewars.com: Convert an array of strings to array of numbers.
View toNumberArray.js
// https://www.codewars.com/kata/5783d8f3202c0e486c001d23 Convert an array of strings to array of numbers.
let toNumberArray = x => x.map(x => +x)
@lbvf50mobile
lbvf50mobile / two_city_sched_cost.rb
Last active Jul 31, 2020
Leetcode: 1029. Two City Scheduling.
View two_city_sched_cost.rb
# Leetcode: 1029. Two City Scheduling.
# https://leetcode.com/problems/two-city-scheduling/
# Runtime: 224 ms, faster than 9.30% of Ruby online submissions for Two City Scheduling.
# Memory Usage: 10.2 MB, less than 100.00% of Ruby online submissions for Two City Scheduling.
# @param {Integer[][]} costs
# @return {Integer}
def two_city_sched_cost(costs)
@dp = {}
@costs = costs
top_down(0,costs.size/2, costs.size/2)
@lbvf50mobile
lbvf50mobile / divisor_game.rb
Last active Jul 31, 2020
Leetcode: 1025. Divisor Game.
View divisor_game.rb
# Leetcode: 1025. Divisor Game.
# https://leetcode.com/problems/divisor-game/
# Runtime: 48 ms, faster than 51.35% of Ruby online submissions for Divisor Game.
# Memory Usage: 9.6 MB, less than 25.00% of Ruby online submissions for Divisor Game.
# @param {Integer} n
# @return {Boolean}
def divisor_game(n)
n%2 == 0
end
You can’t perform that action at this time.