Dynamically create hash from array of arrays

I want to dynamically create a Hash without overwriting keys from an array of arrays. Each array has a string that contains the nested key that should be created. However, I am running into the issue where I am overwriting keys and thus only the last...
more »

2017-08-15 21:08 (4) Answers

Removing an input from a recursive method

Good morning! I received a problem statement to write a method that returns all possible combinations of a String input passed, e.g. if ABC is passed then it returns [A, AB, BC, ABC, AC, B, C] if ABCD is passed then it returns [A, AB, BC, CD, ABC,...
more »

2017-08-14 14:08 (4) Answers

Efficient algorithm to get a new element in a set

Hello fellow programmers, I will start with a contrived example. Lets say you are in charge of conducting an athletic meet for a school. School consists of number of classes. Each class will send some athletes for the meet. Each athlete will have ...
more »

2017-08-13 13:08 (2) Answers

Resolving symbolic links algorithm

What should the algorithm for resolving symlinks on Linux look like? Something like: Split path to steps /usr/bin/hello -> ['usr', 'bin', 'hello'] First resolve /usr -> /something1 Add next step and resolve /something1/bin -> /something2 Add next ...
more »

2017-08-12 21:08 (2) Answers

Remove zero entries from an array in C

I have an array of values x = {0,0,1,2,3,0,0,7,8} and I want to remove the zero entries using C. Attempt: I am attempting to loop through each value in the array and check if the entry is not equal to zero. If this condition is true, then I am att...
more »

2017-08-09 12:08 (11) Answers

Least Recently Used (LRU) Cache

I know that I can use various container classes in STL but it's an overkill and expensive for this purpose. We have over 1M+ users online and per user we need to maintain 8 unrelated 32-bit data items. The goal is to find if an item exists in the...
more »

2017-08-09 09:08 (6) Answers

A* star algorithm open and closed lists

I have looked at the implementation of the problem on the internet and i have a question: whenever you add a node you need to search it in the open list(why it is not enough to search it in the closed list?)? And why do you search it to see if you ca...
more »

2017-08-08 21:08 (1) Answers

2D Greedy Ascent Search Algorithm Clarification

I am doing some remedial work on algorithms as I am taking a graduate course on them in the Fall and was a physics undergrad. Watching this video and at the 38:00 mark he goes over the greedy ascent algorithm for a 2D array. I am confused as he defin...
more »

2017-08-06 06:08 (1) Answers

Algorithm - longest wiggle subsequence

Algorithm: A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequenc...
more »

2017-08-05 05:08 (4) Answers

fastest starts with search algorithm

I need to implement a search algorithm which only searches from the start of the string rather than anywhere within the string. I am new to algorithms but from what I can see it seems as though they go through the string and find any occurrence. I ...
more »

2017-08-04 16:08 (3) Answers

Understanding Bitwise OR '|' in Ruby example

I was working on implementing a recursive powerset algorithm in Ruby and I came across this Stack Overflow post. def powerset(set) return [set] if set.empty? p = set.pop subset = powerset(set) subset | subset.map { |x| x | [p] } end pow...
more »

2017-07-31 20:07 (1) Answers

how to reverse random.seed in Python

I want a way to give me the seed given a random number it could have generated. for example, if I have the following code import random seed = 3 lower_bound = 1 upper_bound = 10 random.seed(seed) random.randint(lower_bound, upper_bound) given t...
more »

2017-07-29 00:07 (0) Answers

Radius Range based search using a Ball Tree?

I've currently created a ball tree based on the design seen here: http://www.astroml.org/book_figures/chapter2/fig_balltree_example.html My implementation looks like this: class BallTree: def __init__(self, data, level): self.data = No...
more »

2017-07-28 17:07 (0) Answers

Ruby algorithm to determine valid HTML structure

I have to as input data an array with hashes, each hash is description of an html tag (open and end position in the text and type of the tag). I need to generate another array where tags are put in order. For example: input = [ {start_p: 0...
more »

2017-07-25 19:07 (2) Answers

Count number of tails since the last head

Consider a sequence of coin tosses: 1, 0, 0, 1, 0, 1 where tail = 0 and head = 1. The desired output is the sequence: 0, 1, 2, 0, 1, 0 Each element of the output sequence counts the number of tails since the last head. I have tried a naive method:...
more »

2017-07-22 19:07 (6) Answers

How does array access affect the performance?

int steps = 256 * 1024 * 1024; int[] a = new int[2]; // Loop 1 for (int i=0; i<steps; i++) { a[0]++; a[0]++; } // Loop 2 for (int i=0; i<steps; i++) { a[0]++; a[1]++; } Can someone explain why the second loop is 20x times slower than the fi...
more »

2017-07-21 21:07 (1) Answers