Podcast #128: We chat with Kent C Dodds about why he loves React and discuss what life was like in the dark days before Git. Listen now.

Questions tagged [python-2.x]

Python 2 is the predecessor of Python 3 and its last version, Python 2.7 was formally released on July 3, 2010. Use this tag along with the main python tag to denote programs that are meant to be run on a Python 2 interpreter only. Do not mix this tag with the python-3.x tag.

Filter by
Sorted by
Tagged with
9
votes
1answer
3k views

Z-Algorithm for pattern matching in strings

I was trying to refactor the following Python code (keeping the same time-complexity) which is an implementation of Z-Algorithm for pattern matching in strings. ...
9
votes
1answer
13k views

K-Mean with Numpy

I have implemented the K-Mean clustering Algorithm in Numpy: ...
5
votes
1answer
2k views

Pretty print dice faces from multiple rolls of multi-sided dices

In this question related to rolling multiple dices, the OP pretty prints for a single case where number of dices is less than or equal to 6. This led to me finding this code golf on "Draw dice result ...
4
votes
3answers
407 views

Find the largest odd number (follow-up code)

Ask user to input 10 integers and then print the largest odd number that was entered. If no odd number was entered, print a message to that effect. This is a follow-up code on feedback from Find the ...
12
votes
1answer
1k views

Extended stable marriage challenge

I've written a solution for the stable marriage problem in the case that the number of men is not equal to the number of women. My problem is not that I can't prove my code is right. The problem is I ...
21
votes
7answers
3k views

Check if a number N is a power of K

I was asked this question in interview: Check if a number N is a power of K. Example: N = 32, K = 2 => True N = 40, K = 5 => False I wrote following code but got the feedback that, ...
7
votes
1answer
123 views

Classic merge sort (part 2)

(Initial discussion from Classic merge sort, since it is new code, I start a new thread) Post my code below, my major question is, I have to create another array ...
6
votes
2answers
669 views

4 sum challenge (part 2)

This is a continued discussion from (4 sum challenge) by return count only. Problem Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[...
14
votes
2answers
3k views

O PC, why are you so slow?

In order to test some software I've written, I need to write values to a test harness with OLE for Process Control (OPC). The problem is, for a larger range of values I'm generating points for (say ...
13
votes
1answer
6k views

Faster computation of barycentric coordinates for many points

I'm just starting to understand the Python syntax and I created a module that does what I wanted, but really slow. Here are the stats of cProfile, top 10 ordered by ...
4
votes
1answer
395 views

Using range minimal query for lowest common ancestor (LCA)

Here is my code to use range minimal query to resolve the LCA problem. Any advice on code bugs, performance improvement in terms of algorithm time complexity, code style are appreciated. More ...
4
votes
1answer
627 views

Find valid triples for a sorted list of integers

I'm working on a problem in which I have an input array, sorted positive unique integers, and have to try to find all possible triples \$(x,y,z)\$ which satisfy \$x+y>z\$ and \$x<y<z\$. For ...
4
votes
1answer
117 views

Group duplicate files

Here is my source code which group duplicate files together, written in Python 2.7. Any advice on smarter ideas to group duplicate files together more efficient, it will be great. Any general advice ...
3
votes
1answer
75 views

Group duplicate files (part 3)

This is a continued discussion (from here => Group duplicate files (part 2)) with new code and new thoughts/questions (see special question part for details for new questions), and I decide to make a ...
2
votes
3answers
121 views

Different path for grid move (part 2)

This is a continued discussion from (Different path for grid move) to optimize for space complexity, and since it is new code and I make a new post. Given a m * n grids, and one is allowed to move ...
14
votes
1answer
532 views

Racetrack plotter

My Racetrack is just that. A Racetrack. You can't race it (yet) because I had trouble with collision detection, but I wanted to share it anyway. It creates a base polygon by using ...
13
votes
3answers
109 views

From Q to compiler in less than 30 seconds

As an adjunct to From new Q to compiler in 30 seconds, I've created a Python script to automatically download the markdown from any question on Code Review and save it to a local file using Unix-style ...
5
votes
4answers
2k views

Find the shortest whole repetitive substring

I'm working on a problem to find wholly repeated shortest substring of a given string, and if no match, return length of the string. My major idea is using a Trie ...
3
votes
1answer
129 views

Data structure and movement for tilt maze (part 2)

This is a design data structure and movement for a tilt maze. For example, this one. My major idea is learned from abrarisme's smart idea of using two boolean matrix to represent wall. Please refer ...
1
vote
1answer
202 views

Find path from source to destination in tilt maze

For a tilt maze general reference, you can refer to this example. The previous related code review discussion could be referred here. The problem I want to resolve is to find all possible path (so ...
1
vote
1answer
101 views

Group duplicate files (part 2)

This is a continued discussion (from here => Group duplicate files) with new code and new thoughts/questions (see special question part for details for new questions), and I decide to make a new post. ...
0
votes
2answers
2k views

JSON string parsing

I need to parse a simple JSON string (flat JSON, no hierarchy) for keys and values, and there is a system constraint that I cannot use any built-in JSON library and can only read a string once due to ...
10
votes
4answers
2k views

Print the largest odd number

Ask user to input 10 integers and then print the largest odd number that was entered. If no odd number was entered, print a message to that effect. I've written this code as my solution to the above ...
2
votes
2answers
707 views

Project Euler problem 23 - non-abundant sums

I present my solution for Project Euler problem 23 written in Python (2.7). The problem statement is: A perfect number is a number for which the sum of its proper divisors is exactly equal to ...
2
votes
2answers
524 views

Django Person Model created from forms.ModelForm

This is the current iteration of my first model of a Person. The model is populated by a survey application using forms.ModelForm and a ...
55
votes
6answers
5k views

Accurate email syntax validation (no seriously)

So a friend happened to show me how odd and specific the general email syntax rules are. For instance, emails can have "comments". Basically you can put characters in parentheses that are just ignored....
21
votes
6answers
43k views

Project Euler #1: Multiples of 3 and 5

Challenge Description: If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of ...
18
votes
6answers
25k views

Pythonic way to add each previous element in list

It's a very simple question, but I wonder if there is a more pythonic way to add each previous elements in a list like this (for example with a list comprehension maybe) : ...
23
votes
6answers
8k views

Check if a Sudoku board is filled out correctly

The code below takes a filled out Sudoku board of size NxN, with sub-blocks of nxn, and checks if the solution is correct. main_function takes a board as input. ...
14
votes
4answers
1k views

Codechef: Prime Number Generator

Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers! Input The input begins with the number t ...
13
votes
1answer
15k views

Decorator to measure execution time of a function

This decorator adds the elapsed time to a function's attributes when applied. My concerns: Is the code pythonic? Could this code be useful? Should I just use the ...
13
votes
3answers
3k views

Stone, Paper, Scissors in Python

Recently I started programming with python. Today I was trying to make a stone, paper, scissor game. After so scratching my head long time, finally I got a working code. ...
12
votes
1answer
18k views

Sudoku Puzzle Generator

I've written a Sudoku puzzle generator. It currently runs through each line of the 9x9 grid and places numbers randomly if they're valid. It loops over all the numbers from 1-9 and then if it finds ...
11
votes
1answer
4k views

Farthest point algorithm in Python

I am learning Python and I tried to do a program to find K points from a set of N points, farthest as much as possible (I'm not ...
8
votes
2answers
3k views

Barnes-Hut n-body quadtree

Here's an attempt I've made of implementing the Barnes-Hut n-body algorithm, or its initial stage - The quadtree. That there're lots of lengthy doc strings might excuse the lack of detailed ...
41
votes
1answer
3k views

The right way to hang a man

Problem: I have seen a few questions around hangman. Usually this is done in a very hackish way, which usually can not be generalized any further. My thought or question is about the creation of the ...
15
votes
3answers
775 views

My images have secrets A.K.A. the making of aesthetic passwords

This is my implementation of a prng steganography tool written in Python. You can also find the code on GitHub. Steganography is the art of hiding messages in (images, videos or even audio) I've ...
11
votes
1answer
189 views

Python with alternative keywords

A while ago, a user on Programming Puzzles and Code Golf had an idea: How about a language where all of the core commands are PPCG usernames? -- Helka Homba A list was made of username/(keyword|...
7
votes
2answers
4k views

Google FooBar XOR Checksum Challenge

Google FooBar came up a few days ago and I took it as a challenge to learn python quickly while having fun. However, I ran into one challenge today that has left me stumped, I've come up with a ...
7
votes
3answers
1k views

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

I was solving this question: Given there is a 6 sided dice. Find the total number of ways W, in which a sum S can be reached in N throws. Example: S = 1, N = 6 => W = 0 S = 6, N = 6 => W ...
5
votes
2answers
119 views

Classic merge sort

Here is my code for classic merge sort. My question is, each time I create a new list result to merge sub-parts, is there a way to save the space -- not creating a ...
5
votes
0answers
425 views

4 sum challenge [duplicate]

Problem Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero. To make problem a bit easier, all A, B, C,...
5
votes
1answer
389 views

Improving the speed of creation for three Perlin Noise Maps in Python?

I am interested in learning how I can improve the speed of the code in this pygame file. I iterate over 6400 * 1800 * 3 or 34,560,000 elements of various numpy arrays here to apply noise values to ...
4
votes
1answer
2k views

Trie Tree match string containing ? and *

Using Python 2.7 and here is my code which implement trie match for a whole word, and also support if the whole word contains ? (match any one character) or ...
3
votes
1answer
166 views

My images have secrets A.K.A. the making of aesthetic passwords V.2

This is my implementation of a prng steganography tool written in Python. You can also find the code on GitHub. Steganography is the art of hiding messages in (images, videos or even audio) I've ...
13
votes
3answers
4k views

Google Foobar Challenge: Lucky Triples

Note: Since my time has passed for this challenge, I do not remember exactly the stipulations but will attempt to recapitulate them to the best of my knowledge. Essentially, the challenge was this: ...
10
votes
5answers
19k views

Python Hash Table Implementation

I'm attempting to implement a basic hash table in Python using only lists. Any tips would be appreciated (including better hash functions). I intend this to handle collisions with separate chaining. ...
8
votes
1answer
340 views

Grid displayer: Game of Life and Langton's Ant

Re-use Given that both the Langton's Ant and the Game of Life use a grid, I decided to code a generic grid_diplayer code and use it both for GoL (Game of Life) and ...
8
votes
3answers
2k views

2-player in a Python hangman game

This is a follow-up: Python Hangman Program I'm working on adding a few features to my hangman game, and I just implemented two-player gameplay, in the form of one player chooses the word to be ...
7
votes
1answer
386 views

Raspberry Pi GPIO safe clean up

We are always told to call GPIO.cleanup() before we exit our Pi programs. I've seen people using try ... catch ... finally to ...