Skip to main content
40 votes
Accepted

Counting permutations without repetitions for a number or a string

One of the best ways to make a program faster is not to compute things that you don't have to. In this question you are asked to compute the number of permutations. But your implementation goes and ...
Gareth Rees's user avatar
  • 49.6k
16 votes
Accepted

List all possible permutations from a python dictionary of lists

The tool you need here is itertools.product: ...
Gareth Rees's user avatar
  • 49.6k
16 votes
Accepted

Get all combinations of selecting k elements from an n-sized array

Binomial coefficient \$\binom{n}{k}\$ is growing too fast (with \$n\$) to keep a list of all combinations in memory. I would suggest to use yield return instead. ...
pgs's user avatar
  • 912
15 votes
Accepted

Generate a dictionary of passwords

Please take a look at itertools.product. It will simplify your code greatly. ...
Sjoerd Job Postmus's user avatar
15 votes

Determine if one string is a permutation of the another

I would say yes, use an array. Arrays afford very good memory locality, which is good for cache performance. They essentially are the most basic and efficient hash map if the key is the index. You ...
Josiah's user avatar
  • 5,996
14 votes

Counting permutations without repetitions for a number or a string

You're looking for set(). The reason for your program being slow is: you're calculating the permutation once: ...
Grajdeanu Alex's user avatar
14 votes
Accepted

Find the total number of ways W, in which a sum S can be reached in N throws of a dice

1. Review The sum \$s=0\$ can be reached in \$n=0\$ throws in exactly one way, but: ...
Gareth Rees's user avatar
  • 49.6k
14 votes
Accepted

Determine if one string is a permutation of the another

Comparing two maps is linear, so that's not a big problem. I don't think you've made particularly good use of maps though. As I see it, you have two choices. You could use an ...
Jerry Coffin's user avatar
  • 33.5k
13 votes
Accepted

A beautiful and easy way of permutation with recursion

The simpler code would look something like this: ...
Jerry Coffin's user avatar
  • 33.5k
12 votes

Generating all combinations of an array

In this case, a non-recursive solution is easier to understand, IMHO: ...
Mathias Bynens's user avatar
12 votes
Accepted

Generating Permutations in Python

Python has so many neat stuff to help you with this one, I feel alot can be rewritten: I did a lot of C++ and miss std::swap(..) Luckily you can swap really easy in python For instance the swap ...
Ludisposed's user avatar
  • 11.5k
12 votes
Accepted

Earliest time frog can jump to the other side of a river in C#

Readability The code needs more spacing. Considering the comments, the indentation and the ifs, it's hard to read without some good old empty lines. Use brackets when using conditions, especially if ...
IEatBagels's user avatar
  • 12.4k
11 votes
Accepted

On Knuth's "Algorithm L" to generate permutations in lexicographic order

Optimising the functional approach Iterative vs functional approach: Is it possible to use the (existing) functional methods without losing performance? I don't think it's possible to use the ...
Hamish's user avatar
  • 423
11 votes
Accepted

Print all "balanced" sequences of 'A' and 'B'

Refactor to use functions Try to avoid boolean variables. Pseudocode like: ...
ggorlen's user avatar
  • 3,412
10 votes

Project Euler #15: counting paths through a 20 × 20 grid

#include <cstdio> If your goal is to write C++, you're really off to a bad start using the C standard I/O library. ...
indi's user avatar
  • 11.1k
10 votes

Generate Letter Combinations of a Phone Number

You can set the keypad lookup as a constant instead of computing it dynamically. Setting it up as constant makes it clearer that what your intent is. ...
hjpotter92's user avatar
  • 8,716
10 votes

HackerRank: Electronics Shop

early pruning This is very nice: // delete any that are over our budget Doing it before sorting can slightly speed the sorting operation. I say slightly ...
J_H's user avatar
  • 25.6k
10 votes

Print sums of all subsets

Preface This is great code. Your solution is more than \$10^{42}\$ times nicer than the given solutions on the linked page that promote crap like ...
L. F.'s user avatar
  • 9,550
10 votes
Accepted

LINQ for generating all possible permutations

English language These are relatively minor issues, but fixing them might help other people to use / maintain your code. The verb corresponding to permutation is permute. I'm pretty sure that ...
Peter Taylor's user avatar
  • 24.1k
9 votes

Pizza Hut math problem #1 in Haskell

Basically, you're testing a lot of paths you don't have to. It would probably be more efficient to find a 1-digit number that is divisible by 1, then use that digit to find a 2-digit number that is ...
Karl Bielefeldt's user avatar
9 votes
Accepted

Sequence-based enumeration of permutations in lexicographic order

Performance I'm not particularly surprised that the version of permutations(of:) which uses AnySequence and ...
Hamish's user avatar
  • 423
9 votes
Accepted

Maximum sub-array of non-negative numbers

1. Bugs maxset returns the wrong answer for some inputs. For example: >>> maxset([2, 1, -1, 1, 3]) [2, 1] The ...
Gareth Rees's user avatar
  • 49.6k
9 votes

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

One immediate improvement is realizing that for e.g the input 14 7, all combinations like x x x x x x 9 and above can never sum ...
Graipher's user avatar
  • 41k
9 votes

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

Time complexity of iterating through all the combinations is O(n³). We can solve it in O(n²) by using the fact that if you know 2 numbers out of 3, you know exactly ...
QuasiStellar's user avatar
  • 2,239
8 votes

Generate a dictionary of passwords

Instead of building characters using integers and chr, you can rely on the pre-built ones in the string module: ...
301_Moved_Permanently's user avatar
8 votes

Generating Permutations in Python

Are you allowed to use standard library functions? If so, why not just use itertools.permutations(): ...
Daniel's user avatar
  • 4,490
8 votes
Accepted

Project Euler #15: counting paths through a 20 × 20 grid

Why are you using unsigned short? I presume the answer is somewhere along the lines of worrying about memory usage. There isn't a problem with that, but given that ...
Josiah's user avatar
  • 5,996
8 votes

Generating the powerset in C

I'll just work through this from the top: #include <stdio.h> #include <stdlib.h> #include <string.h> Looks good; we need these. ...
Toby Speight's user avatar
  • 75.1k
8 votes

HackerRank: Electronics Shop

General Guidelines You have coded everything in a single class Program. Take advantage of the fact C# is an object oriented language. Create at least one custom ...
dfhwze's user avatar
  • 13.9k

Only top scored, non community-wiki answers of a minimum length are eligible