Skip to main content

All Questions

Tagged with or
Filter by
Sorted by
Tagged with
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. ...
SohamC's user avatar
  • 221
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 ...
Edward's user avatar
  • 66.6k
10 votes
1 answer
14k views

K-Mean with Numpy

I have implemented the K-Mean clustering Algorithm in Numpy: ...
Frames Catherine White's user avatar
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 ...
holroy's user avatar
  • 11.5k
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 ...
srig's user avatar
  • 183
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 ...
Harsha's user avatar
  • 1,291
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 ...
Mast's user avatar
  • 13.5k
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 ...
adrienlucca.net's user avatar
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 ...
syb0rg's user avatar
  • 21.8k
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 ...
Arman Malekzadeh's user avatar
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 ...
srig's user avatar
  • 183
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 ...
Lin Ma's user avatar
  • 3,453
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 ...
Lin Ma's user avatar
  • 3,453
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[...
Lin Ma's user avatar
  • 3,453
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 ...
User1974's user avatar
  • 198
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 ...
Ludisposed's user avatar
  • 11.6k
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 ...
Lin Ma's user avatar
  • 3,453
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 ...
Lin Ma's user avatar
  • 3,453
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 ...
Lin Ma's user avatar
  • 3,453
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 ...
Lin Ma's user avatar
  • 3,453
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 ...
Lin Ma's user avatar
  • 3,453
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 ...
mkrieger1's user avatar
  • 1,704
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 ...
Deepend's user avatar
  • 253
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 ...
Lin Ma's user avatar
  • 3,453
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 ...
Lin Ma's user avatar
  • 3,453
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. ...
Lin Ma's user avatar
  • 3,453
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 ...
Lin Ma's user avatar
  • 3,453
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....
SuperBiasedMan's user avatar
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 ...
N3buchadnezzar's user avatar
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 ...
MTHL's user avatar
  • 535
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. ...
Stewie Griffin's user avatar
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) : ...
Natha's user avatar
  • 347
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 ...
ATT's user avatar
  • 183
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 ...
Ricky Wilson's user avatar
  • 1,695
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 ...
SuperBiasedMan's user avatar
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 ...
Ludisposed's user avatar
  • 11.6k
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: ...
Edwin Rice's user avatar
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 ...
Der Kommissar's user avatar
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 ...
Subham Goyal's user avatar
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 ...
Sigur's user avatar
  • 295
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. ...
Sanatan Chaudhary's user avatar
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 + ...
Ali's user avatar
  • 223
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 ...
Mast's user avatar
  • 13.5k
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|...
user avatar
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, ...
Plue's user avatar
  • 379
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 ...
yak's user avatar
  • 223
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: ...
user2421117's user avatar
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 ...
Lin Ma's user avatar
  • 3,453
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 ...
Renae Lider's user avatar
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 ...
Lin Ma's user avatar
  • 3,453