We’re rewarding the question askers & reputations are being recalculated! Read more.

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
4
votes
3answers
2k views

Python Barcode Generator

I was asked to provide some example code for a job interview and didn't have anything to offer, so wrote the following function. A barcode generator may seem a bit basic, but there's some logic ...
6
votes
2answers
630 views

Python Barcode Generator v2

This is a complete rewrite of the code posted for review in this question. The question is identical. ...
4
votes
0answers
48 views

Python Project Template

What's up smart people. A little backstory for context: I'm a completely self taught dev, and the only person ever looking at my code for the past 6 years is myself. I realize that's not the best idea ...
0
votes
1answer
51 views

Pyg Latin Translator [closed]

I have a code for a short Python 2 Pig Latin translator. Is it a good code, or should it be revised? ...
8
votes
2answers
1k views

Print a random anagram of a given string

I am given a task to create a function that prints a random anagram of a given string: ...
11
votes
1answer
7k views

Simple two-player Tic-Tac-Toe

How can I improve this simple code to look better, concise and elegant? I've been learning Python for 2 weeks. I knew about basic Python stuff and some algorithms and data structure as well. I made a ...
20
votes
6answers
18k 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 ...
2
votes
1answer
112 views

Simulate Tic-Tac-Toe game in Python

Simulate a tic-tac-toe game Requirements A player is picked at random as a first player. The two players plays tic tac toe game. The game ends when the board is full or either one of the player ...
6
votes
1answer
152 views

Universal string conversion

Below there is a function eating all strings and convert it to Unicode. It works fine. But is it the best way to do so? Is there maybe already an existing function for it? The reason was a German ...
2
votes
1answer
119 views

A bitwise common prefix algorithm in Python

Is my algorithm slow because it has problems? Or, is any bitwise common prefix solution not going to give me the performance I'm looking for? After profiling my algorithm, I found that over 60% of ...
6
votes
5answers
3k views

1D Peak Finder Algorithm

I'm reviewing MIT Introduction to Algorithm lectures/exercises and am trying to implement a one dimensional peak finder algorithm. I've got a working copy but it's a bit messy and I've had to put ...
13
votes
3answers
5k 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'...
4
votes
2answers
509 views

Python wrapper for the Help Scout API

I started porting an API wrapper from Java to Python for practice. I am looking for ways to improve the readability/maintainability this code. I have done some reading about "pythonic" style and I am ...
3
votes
2answers
99 views

How to make my neural network train faster

I'm trying to train my neural network and for the most part it's going well. However, I'd like it if it could train faster and was wondering if anyone could give some advice. I'm trying mostly to ...
12
votes
2answers
744 views

Project Euler #1 Sum of multiples of 3 and 5 python implementation

Given: 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 3 or 5 below 1000. ...
5
votes
2answers
95 views

Program to delete directories that do not contain a file

I'm an intern and have an ETL project I'm doing in python. Part of it is to make sure after I move files I delete empty directories. I wrote this code to clean up directories that are empty and in ...
1
vote
1answer
1k views

Scrape google apps page and store application details in database

Below is a python script which scrapes specific google apps url for example https://play.google.com/store/apps/details?id=com.mojang.minecraftpe and save the ...
2
votes
3answers
114 views

Reading, cleaning and parsing CSV

This project was completed as part of an interview challenge. The feedback I received was: The code is neither optimized nor does it follow generally accepted paradigms, naming conventions or best ...
6
votes
2answers
1k views

Remove outliers from a point cloud

This function accepts a cloud of points, and returns those points that are within delta distance of the average (mean) position. ...
4
votes
1answer
107 views

A word replacer that uses an API to check for different words to make up a new string

This is one of my first finished programs that I've written to date. I am not yet very familiar with Python, so please bear with me. I'd like to know from you: Can my code be called 'pythonic'? I'...
5
votes
1answer
75 views

Sorting software version list using Python 2.7 (no modules)

I am participating in a challenge and one of the challenges was to write a script to sort a list of software versions (ex: 1.0, 1.3.2, 12, 1.3.0). Here is the my solution: ...
3
votes
4answers
13k views

Prompt user for some numbers, then print the max and min

The function of this program is repeatedly prompting a user for integer numbers until the user enters 'done'. Once 'done' is ...
6
votes
1answer
128 views

Add an edge to a graph to minimize the average shortest path length

This program is used to find the nodes in a grid network, between which, if an edge is added, the average shortest path length of the entire grid reduces by the most. "Average shortest path length" ...
9
votes
2answers
3k views

4chan Tripcode Explorer in Python

Background information for those of you who don't know what 4chan tripcodes are: Via Wikipedia: A tripcode is the hashed result of a password that allows one's identity to be recognized without ...
2
votes
1answer
39 views

An imitation of reading a C Struct from binary file in python 2.7

Preliminary I need to read a binary file constructed by writing a Struct to it with Python. So I decided to write a simple wrapper using ...
9
votes
1answer
570 views

Print a nice Latex table from an array of values

To present some results I have had to write a short method to turn an array (a list of lists) into a nice representable LaTex table. It needed to have some nice formatting features and the ...
5
votes
1answer
791 views

KNN pipeline w/ cross_validation_scores

Using the wine quality dataset, I'm attempting to perform a simple KNN classification (w/ a scaler, and the classifier in a pipeline). It works, but I've never used ...
6
votes
1answer
16k views

Updating a .csv file

I have a CSV file, call it csv_file. It has the following content: Username, Password name1, pass1 name2, pass2 ... I also have a dictionary, call it ...
6
votes
2answers
181 views

Guttag's Finger Exercises: Enter 10 integers, find largest odd integer and print result

I'm making my way through the Intro to Computation and Programming test by John Guttag and he gives these small exercises to practice gearing up into computational thinking. As of Ch.2, we've covered ...
9
votes
4answers
24k views

Simplify a fraction

I have been learning Python for a while (my first programming language) in my spare time. Since it's been 20 years since math class, and I didn't get very far with that, I just brushed up on pre-...
1
vote
1answer
73 views

Django view of reward points that heavily filters and sorts (many) database results

My page (even when it has no data) takes 10+ seconds to load. That's just too long, considering when you finally get to it there's no data. When it has data, it takes even longer. Here is my view, ...
27
votes
2answers
25k 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 ...
13
votes
4answers
31k 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'...
2
votes
2answers
493 views

Python deep get

I'm implementing deep_get functionality to look inside arbitrarily nested Python 2.7 objects. Primarily for further logging. This turned out to have surprising ...
1
vote
0answers
59 views

AWS plugin to create a CloudFormation stack if it doesn't exist

The following Python class is what I am trying to refactor in the case where I have to add validation of the existence of an AWS cloudformation stack before creating it in the method create_cfn_stack :...
1
vote
3answers
34 views

`set_includes` (string subsequence-containment) in Python 2

I've implemented C++'s std::includes algorithm in Python, so that I can use it to efficiently implement a Scrabble "can I make this word" function: ...
3
votes
2answers
825 views

Weaving rows of numbers

I want to write a cleaner version of this code. It's about two different rows consisting of numbers that have to be weaved. I have already asked this question at this link, but I have translated my ...
5
votes
2answers
562 views

Prime Number Generator (6n + 1 or 6n - 1)

This generator is like most where it brute forces an integer: it see whether the integer is divisible by any of the primes; if so, then it's not a prime and vice versa. This though only compares ...
4
votes
2answers
163 views

Print a pattern of text based on a line from an Excel file

I started to learn Python not long ago to automate certain tasks I work on. I want to optimize a script I wrote that takes a line in an Excel file and prints a pattern of text based on this line's ...
10
votes
1answer
5k views

Given a string, find the length of the longest substring without repeating characters

I wrote an algorithm that solves this problem but I would love to get some feedback on it (especially since I'm not so confident in my Big \$O\$ skills). Do you think this is a good quality algorithm?...
25
votes
4answers
11k 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 ...
1
vote
1answer
242 views
22
votes
5answers
45k 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 ...
3
votes
0answers
63 views

Animation of an incremental rotary encoder

While reworking my teaching materials for an exercise of an introductory course on mobile robotics, I recently created a animation/simulation of an incremental rotary encoder, e.g. often used for ...
1
vote
1answer
3k views

Tic Tac Toe (Player vs Computer)

I am looking on how I can improve my writing style and/or how to simplify how I write my code. ...
2
votes
0answers
71 views

Extract cell values from multiband rasters

I have the following function and code snippet to extract cell values for multiple years, format it, and save to a list. Each raster has 365 bands — one for each day. A separate operation is performed ...
7
votes
2answers
60 views

Count reoccurring elements in multiple lists in Python

I have a number of names divided into several lists. I am printing all the names which occur in more than one list, sorted by the number of occurrences. What is a better/easier/more pythonic way of ...
4
votes
0answers
34 views

Combining CSV files of simulation results

This script is a subset of a larger script where I have the output of many test simulations in the form of CSVs. Each file starts with the model name and includes the number of elements in my file. ...
2
votes
1answer
357 views

Python script for sorting a huge log file based on timestamps

I want to write a python script to sort a huge file, say 2 GB in size, which contains logs in the following format - ...
5
votes
1answer
112 views

Run a command for all files in a directory

I have a program that needs to run an command on all the files in a folder: ...