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
73 votes
3 answers
19k views

Monopoly simulator

I was advised by a Reddit user to get my code reviewed on this site. The complete code is on GitHub. ...
  • 833
61 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....
46 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 ...
39 votes
4 answers
4k views

Someone thinks poorly of my server log parser

I have just been informed that the following code written by me is extremely poor. I have absolutely no idea why. It is memory efficient, and looks clean to me. But still the feedback is very poor. I ...
35 votes
10 answers
9k views

Search for a multiword string in a list of strings

Unlike Perl, you can't to my knowledge match a regular expression inside an if statement in Python and assign the result to a variable at the same moment. This ...
  • 465
30 votes
2 answers
36k views

Password checker in Python

Using Python 2.7.12 I wrote a simple little script, psk_validate.py, that prompts the user for a potential password and checks if it has upper and lower-case ...
  • 827
27 votes
6 answers
108k 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 ...
  • 535
26 votes
5 answers
104k views

Replacing letters with numbers with its position in alphabet

If anything in the text isn't a letter, ignore it and don't return it. a being 1, b ...
  • 415
26 votes
6 answers
55k views

Python code to retry function

I am trying to write a python function which retries a given function until given time or function returns True with given delay. I have written the following function, but was thinking if there is ...
26 votes
3 answers
2k views

"The python that ate a calculator"

In a YouTube video I watched, the speaker said it is important to know how to calculate between numbers of different bases; i.e. two, ten, and sixteen. I was then inspired to write a small C program ...
  • 2,521
25 votes
4 answers
17k views

Validating Credit Card Numbers

This is one of the puzzles I was trying to solve, where the question is to validate the credit card number with following characteristics: It must contain exactly 16 digits. It must start ...
25 votes
5 answers
25k views

Simple random number generator

I have been wanting to learn programming for some time now and decided to really give it a shot today. I have edited and read lots of things in Java to fix small issues but never written from scratch. ...
  • 351
24 votes
4 answers
476 views

When it rains, it pours - August 2016 Community Challenge

1. Introduction This code is my attempt at solving the August 2016 Community Challenge. Coming from a city where it rains cats and dogs on a daily basis this challenge was right up my alley =) 2. ...
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. ...
23 votes
4 answers
2k views

Monty Hall Simulation

I wrote this code as my first self-directed effort and would appreciate any input on things that I've done seriously wrong. I worry that perhaps I'm over-relying on ...
  • 333
23 votes
1 answer
6k views

Python - Minesweeper

I'm new to Python (and generally to programming), and I have recently written this Minesweeper game: ...
  • 517
22 votes
4 answers
10k views

Regex to parse semicolon-delimited fields is too slow

I have a file with just 3500 lines like these: filecontent= "13P397;Fotostuff;t;IBM;IBM lalala 123|IBM lalala 1234;28.000 things;;IBMlalala123|IBMlalala1234" ...
  • 223
22 votes
1 answer
568 views

Raspberry PI controllable Christmas Tree

After having the scales, thermometer and a coffeemaker connected to the internet, it is finally time for Christmas Tree to become smarter. Hardware 6ft Christmas Tree An addressable WS2811 LED strip (...
  • 17.2k
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 ...
  • 1,261
20 votes
1 answer
1k views

Messed up Elevator Management System

In light of our current community-challenge I decided to build an Elevator Management System. Initially I intended to program the EMS like a real-time operating system and the elevators as finite-...
  • 13k
20 votes
1 answer
296 views

Rainfall - August 2016 Community Challenge (Hooray for graphs!)

A solution for the August 2016 Community Challenge (Rainfall). I've intentionally left out error checking on the file formatting. This seems like a very obvious graph problem to me - we want to ...
  • 7,734
19 votes
5 answers
60k views

Get value from dictionary given a list of nested keys

I would like to get a deeply-nested value, for example {"a":{"b":{"c":"myValue"}} by providing the keys to traverse. I tried chaining together .get() but that didn'...
18 votes
7 answers
41k 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) : ...
  • 347
18 votes
3 answers
4k views

Python preprocesser that allows "until" and "unless" statements

I've implemented the beginnings of a Python preprocesser I plan to implement sometime in the future, and the below code is my simple prototype. At the moment, it only converts ...
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
24k 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 ...
  • 1,695
15 votes
4 answers
8k 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: ...
15 votes
3 answers
76k views

Splitting a list by indexes

I am trying to build an efficient function for splitting a list of any size by any given number of indices. This method works and it took me a few hours to get it right (I hate how easy it is to get ...
15 votes
3 answers
894 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.5k
15 votes
4 answers
329 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 ...
  • 64.9k
15 votes
3 answers
805 views

Virtual machine management with decorated selfies

I have a class which provides various methods for retrieving information from a VMware vSphere environment. Over the time my class got extended and became large, so I thought I should spend some ...
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
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 ...
  • 21.8k
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
3 answers
2k views

Find my colleagues

I recently finished the Using Databases with Python course. To figure out whether I really understood what was being taught, I build an application which does roughly the same but uses a different ...
  • 13k
14 votes
2 answers
16k views

Implementing heap in Python

This is basically a straightforward heap implementation. I am just moving from C to Python and I wanted to make sure that I follow Python's best practices in general. This heap is supposed to support ...
  • 805
14 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 ...
14 votes
3 answers
5k views

Lexer for C- in Python

I am currently taking a compilers course where we are designing a compiler for C- (which is a subset of C). Our first step was the lexer and I have written that but I believe that it is not very "...
  • 141
14 votes
3 answers
24k views

Character creator for a role-playing game

Goal: Write a character creator program for a role-playing game. The player should be given a pool of 30 points to spend on four attributes: strength, health, wisdom, and dexterity. The ...
  • 143
14 votes
1 answer
11k 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 ...
  • 275
14 votes
1 answer
845 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 ...
  • 13k
14 votes
1 answer
206 views

Download images from an image board based on tags and blacklists

This code is meant to download images from an image board to create a local backup. Images are grouped by tags selected by the user and then downloaded using multiprocessing. Normal use of this ...
  • 241
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. ...
13 votes
3 answers
6k views

Optimizing an Anagram Solver

I've built an anagram solver in Python 2.7. It goes through a text file with over 100,000 words and checks if any permutations of the input matches the line. It works great, except it is very slow. I'...
13 votes
1 answer
173 views

#TODO Remove duplication in XML parsing

I have to modify the number of points in this XML in order to test the performance of another program of mine. Here is an example of the XML I have to modify. performance.xml: ...
  • 21.8k
13 votes
1 answer
9k 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 ...
13 votes
1 answer
7k views

Optimizing "Poker hands" challenge solution

I'm trying to solve the project Euler problem 54. In the card game poker, a hand consists of five cards and are ranked, from lowest to highest, in the following way: High Card: Highest ...
13 votes
2 answers
748 views

A classy DNA nucleobase counter

This question is part of a series solving the Rosalind challenges. For the previous question in this series, see Counting nucleobases in a nucleotide c. The repository with all my up-to-date solutions ...
  • 13k
13 votes
1 answer
424 views

Voting plugin for an IRC bot

MetaBrainz has an IRC channel called #metabrainz on which there's a bot called BrainzBot running. It provides utilities such as linking to JIRA issues or Github PRs ...
  • 101
12 votes
6 answers
4k views

Program to find the largest odd number among three variables

...
  • 811

1
2 3 4 5
25