AChampion
  • Member for 6 years, 4 months
  • Last seen more than a month ago
  • US (via U.K)
Permutation of list in Python so that list[x] != list[x+1]
5 votes

A simple algorithm that uses heapq. Construct heap with (-count, value) [Negative count provides the appropriate priority as heapq returns lowest first] Pop from heap if no previous element or ...

View answer
Python list dictionary items round robin mixing
5 votes

Using the roundrobin() recipe from itertools is already pythonic. The solve() method could be replaced with more use of itertools. In particular itertools.groupby() would do the same as your ...

View answer
Pig latin translating program
5 votes

Some comments on your code. Overall you are using a lot of unnecessary variables. another_word = " " sentence1 = " " new_list = [] another = " " You really don't need these variables declared ...

View answer
MST Kruskal's algorithm in Python
Accepted answer
4 votes

Why do you assume this code is limited to 10 vertices? This code comes from: http://www.geeksforgeeks.org/greedy-algorithms-set-2-kruskals-minimum-spanning-tree-mst/ But you have an error in use: g ...

View answer
Finding number of integer partitions
Accepted answer
3 votes

A slightly more efficient dynamic programming approach means you only need O(n) space: def partitions(n): parts = [1]+[0]*n for t in range(1, n+1): for i, x in enumerate(range(t, n+1))...

View answer
Project Euler #8 - Largest product in series
3 votes

Given even the naive solution finishes in ms, is this one test you care about optimising? It can be done in one line which would be in the order of the naive solution: max(reduce(mul, (int(a) for a ...

View answer
Very basic Hangman game in Python
2 votes

A couple of high-level comments: You have many places where you can pull things out of the conditionals to avoid repeating yourself (DRY principle) I would look to pass values into functions vs. ...

View answer
__str__ method for a tic-tac-toe board
2 votes

You can hugely reduce your __str__ using loops, e.g. using join() and a couple of generator expressions: def __str__(self): return '\n-----------\n'.join(' | |\n '+' | '.join(str(self.spaces[...

View answer
Printing all the factors as well as the prime factors of a given number using Python 2.7.x
Accepted answer
1 votes

A couple of stylistic comments and python gotchas: If you are just starting out in Python I would look to use Py3 and if you can't use Py3, use some of the __future__ imports to ensure you are ...

View answer
Maximum consecutive subarray sum for a variable sliding window size
1 votes

Maximum subarray is pretty well known problem with a simple algorithm to solve in O(n). Directly from the link above: def max_subarray(A): max_ending_here = max_so_far = A[0] for x in A[1:]: ...

View answer
Longest common substring using dynamic programming
1 votes

Sorry I've not examined too closely your code to make a comment, but just considering the problem as stated and the fact you are using Py3, I would probably solve it with itertools.accumulate, e.g.: &...

View answer
Reordering a list in python so that no neighboring elements are equal
0 votes

A simple algorithm that uses heapq. Construct heap with (-count+1, value) [Negative count provides the appropriate priority as heapq returns lowest first] Pop from heap if not the previous element ...

View answer