All Questions
Tagged with python-2.6 or python-2.x
164 questions
11
votes
1
answer
4k
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.
...
15
votes
4
answers
351
views
From Q to compiler in less than 30 seconds
(See the newer version here: CodeReview question markdown downloader)
As an adjunct to From new Q to compiler in 30 seconds, I've created a Python script to automatically download the markdown from ...
10
votes
1
answer
14k
views
K-Mean with Numpy
I have implemented the K-Mean clustering Algorithm in Numpy:
...
5
votes
1
answer
3k
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
3
answers
458
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 ...
21
votes
7
answers
4k
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, complexity ...
14
votes
1
answer
1k
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 ...
14
votes
1
answer
10k
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 ...
14
votes
2
answers
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
1
answer
2k
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 ...
10
votes
4
answers
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 ...
7
votes
4
answers
7k
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 ...
7
votes
1
answer
165
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
2
answers
806
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[...
5
votes
1
answer
377
views
Part 1: Create or update record via HTTP request
Update:
This is an older version of the question/script. The new version can be found here: Part 2: Create or update record via HTTP request
I have an external system that sends an HTTP request to a ...
5
votes
1
answer
2k
views
Python scapy - TCP port scan of subnet
Question
I love using NMAP and other related tools to scan networks really fast.
But using a tool and understanding how it works are 2 different things, thus I tried creating a program where I can ...
4
votes
1
answer
855
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
1
answer
155
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 ...
4
votes
1
answer
513
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 ...
3
votes
1
answer
88
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 ...
3
votes
1
answer
166
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 ...
2
votes
2
answers
907
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
2
answers
723
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 ...
2
votes
3
answers
140
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 ...
1
vote
1
answer
273
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
1
answer
118
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
2
answers
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 ...
62
votes
6
answers
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....
48
votes
1
answer
4k
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 ...
27
votes
6
answers
120k
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 ...
23
votes
6
answers
13k
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. ...
18
votes
7
answers
44k
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) :
...
18
votes
2
answers
1k
views
Given a page of content, determine shortest snippet containing all search phrases (no order required)
A recruiter gave me a homework problem as a part of the recruiting process and after receiving my submission he told me that he decided not to proceed with me. When I asked for the reason, he told me ...
16
votes
1
answer
25k
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 ...
15
votes
1
answer
23k
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 ...
15
votes
3
answers
926
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 ...
15
votes
5
answers
9k
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:
...
14
votes
4
answers
3k
views
Reading a DHT11 to a file
Disclaimer: not a Pythonista, first actual Python programme written right here.
I'm reading a DHT11 sensor from GPIO pin 4 on my Raspberry Pi (thanks Thomas Ward for sending the RPi to me), and I ...
14
votes
4
answers
2k
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 of test cases ...
14
votes
1
answer
12k
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 ...
13
votes
3
answers
4k
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
3
answers
16k
views
Project Euler Problem 12 in Python
I'm trying to do Project Euler Problem 12, which reads as:
The sequence of triangle numbers is generated by adding the natural
numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + ...
12
votes
1
answer
289
views
The dwindling Fibonacci
I've recently taken a renewed interest in non-standard languages like Brainfuck and TIS-100. I'm currently writing a Fibonacci generator in Brainfuck which differs a bit in approach from the usual ...
11
votes
1
answer
648
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|...
10
votes
3
answers
6k
views
A Simple Rock, Paper, Scissors program
I wrote a rock, paper, scissors program. It works fine, but I'm curious if there are any constructive critiques, tips, tricks, comments, or advice anyone may have on how the code operates, performs, ...
10
votes
1
answer
4k
views
Network protocol using TCP, sending images through sockets
I would like to ask about your opinion about my code. The idea is simple: I designed my own protocol, where client asks the server about the image, and the server sends the image, following the below ...
10
votes
3
answers
952
views
Displaying information about people from a JSON file
I'm working with Python 2.7.5 and I have written the following code:
...
9
votes
1
answer
846
views
Check “five in a row” for a 15x15 chess board (part 2)
This is a follow-up from here. I have a further optimization for space complexity by store only direction status data for one row for each directions (refer to variable ...
9
votes
2
answers
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 ...
9
votes
1
answer
9k
views
Find the nearest point of a given set of points
Suppose there are a set of given points (represented by x and y two dimensional coordinates), and for any given point A, I want ...