Skip to main content

All Questions

6 votes
1 answer
495 views

Python knapsack program

I have this data for the knapsack problem: List of product prices = MENU Most popular product count = MOST_POPULAR_COCKTAIL ...
3 votes
2 answers
184 views

Find the join of two set partitions

Two partitions of a set have a greatest lower bound (meet) and a least upper bound (join). See here: Meets and joins in the lattice of partitions (Math SE) The meet is easy to calculate. I am trying ...
3 votes
1 answer
83 views

Number of partitions of (a,b) into k distinct parts which sum up to (a,b)

Problem set This is somewhat a generalization of the famous partition of integer n into k parts. Given two integers ...
8 votes
3 answers
294 views

Refactor the code which performs "cross-product", "reduce", "product of list" and "sum of list"

I have come up with a sequence of steps to find the maximum product of y positive numbers which add up to x. But the program is ...
7 votes
2 answers
1k views

Find all combinations of length 3 whose sum is divisible by a given number

I came up with a suitable solution to a HackerRank problem that failed because the execution took longer than 10 seconds on lists that were of very large size. The problem: Given a list ...
1 vote
1 answer
90 views

An algorithm that minimizes the number of ingredients necessary

I am working on a code that will minimize the number of ingredients necessary to make some dishes. Each dish can be prepared in an arbitrary large number of ways, with combinations of two ingredients. ...
22 votes
4 answers
21k views

Finding all k-subset partitions

The following code generates all \$k\$-subsets of a given array. A \$k\$-subset of set \$X\$ is a partition of all the elements in \$X\$ into \$k\$ non-empty subsets. Thus, for ...
1 vote
1 answer
130 views

Subset Product Algorithm

Subset Product is an NP-complete problem and is my favorite problem. So, I've written a slightly smarter way of solving the problem. Will performance impact matter if I use multi-cpus and my GPU? Do ...
5 votes
2 answers
2k views

Find non-overlapping pairs of elements in a list whose difference is less than some given threshold

I have the following task: Let us have a list (denoted by L, and for simplicity, the elements come from the interval [0,1]). We are given a parameter (denoted by C), and we want to find as many pairs ...
7 votes
2 answers
6k views

Permutations with replacement in Python

Many a times, I've had the need to use a permutations with replacement function. So, I've written a function to do just that: ...
1 vote
2 answers
134 views

Get the next bigger number by rearranging the digits of the input integer

...
2 votes
1 answer
91 views

Count the different positions reached in a cycle of permutations

I have come across a problem on cyclic permutations. It is about swapping numbers around. It starts out with input N < 100,000 which makes the first N natural numbers as orders: 1 2 3 ... N-1 N ...
4 votes
1 answer
93 views

Filter out unwanted substrings while generating all possible relevant substrings

Write a Program that determines where to add periods to a decimal string so that the resulting string is a valid IP address. There may be more than one valid IP address corresponding to a string, ...
9 votes
2 answers
2k views

Calculating 24 from 4 numbers (math Poker game)

This is a game of math in Poker. Two people each have a pile of cards. They then each place 2 cards on the table at the same time. Every card is treated as a number. The players need to find a way to ...
7 votes
2 answers
3k views

Sums of all sublists

The parts_sums function in the code takes a list as parameter and then returns sum of all sub-lists present in the list. e.g. if, ...

15 30 50 per page