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
8
votes
2answers
83 views

log setup using RotatingFileHandler and basicConfig

I am new to python, and developing a moderate application for which I need to setup a logging mechanism. The below program sets up log handling, which gives the user an option to specify whether log ...
4
votes
2answers
799 views

Telegram Bot that retrieves book information from GoodReads

The project below is about a Telegram bot that implements both inline and offline modes. The idea is to get a brief info about a book, searched by title(it's author, rating, short description). I ...
7
votes
2answers
1k views

Generate Letter Combinations of a Phone Number

The task: Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. and my solution: ...
1
vote
1answer
85 views

Bank database program

I have been trying to improve my Python coding and was just wondering if there was a more pythonic way to perform what I have done so far, namely around the date in the ...
2
votes
0answers
22 views

Interactive, real-time bikeshare web application with Bokeh

As a side project, I'm working on a Bokeh web application to display public bikeshare data on a map. The data is updated every 2 minutes using a periodic callback. Below is the full implementation. I'...
3
votes
0answers
41 views

Python script that getting list of all files with specific ext starting from the current folder. Using timer for reporting about the process

I am using a function that returns a list of all files (full path for each of them) with the given extension in the given folder and all subfolders. As the process is quite long and user can get ...
11
votes
1answer
3k views

A CNN in Python WITHOUT frameworks

Here's some code that I've written for implementing a Convolutional Neural Network for recognising handwritten digits from the MNIST dataset over the last two days (after a lot of research into ...
3
votes
3answers
584 views

Bifurcating recursive calculation with redundant calculations

def T(n): if n <= 0: return 1 else: return T(n-1) + (n-1) * T(n-2) print T(4) I need an effective way to print out the output of the function <...
2
votes
1answer
454 views

Balanced smileys check algorithm

Problem Your friend John uses a lot of emoticons when you talk to him on Messenger. In addition to being a person who likes to express himself through emoticons, he hates unbalanced ...
8
votes
1answer
725 views

Animation of linear transformations

Coordinates Given a 2d space, we can fix a coordinate system to talk about transformations numerically. The red vector, that ends at (1, 0) and the green vector that ends at (0, 1) are called the ...
0
votes
0answers
16 views

Reproduce Hashing and pbkdf in newer versions? (python2)

I found this old code in one of the old files of mine which I have converted into Python3 but not very much satisfied and cannot reproduce the whole thing especially decoding it in check_password ...
7
votes
1answer
813 views

Simulate object.__getattribute__ in Python

I want a Python example that illustrates how object.__getattribute__ resolves instance attribute lookups. I'm looking for feedback about the following code. Is it ...
5
votes
0answers
72 views

Simple chat server in Python

I'm new to both Python and socket programming and figured setting up a simple chat server would be good practice. In addition to general critiques of the code, I have specific questions: What are the ...
6
votes
4answers
288 views

Decrease Computation Time for Python Tic-Tac-Toe Minimax Algorithm

Background I am currently participating in a tic-tac-toe coding challenge. During their move, each participant is given the board state and their team, X or O. The blank board is: ...
3
votes
1answer
36 views

Parse backslash-delimited hierarchy path into separate columns

I have rows in a database table with a hierarchypath column. The values in the column are hierarchy paths with up to four levels: ...
26
votes
6answers
40k 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 ...
1
vote
2answers
224 views

Python Regex to validate an email

I have written this regex to validate an email. It seems to work fine. Can someone advise on the regex used? Does it work properly or can be done in a better way? ...
6
votes
3answers
3k views

“Cut the sticks” Python implementation

Problem Statement You are given N sticks, where the length of each stick is a positive integer. A cut operation is performed on the sticks such that all of them are reduced by the length of the ...
1
vote
1answer
91 views

LeetCode 839: Similar String Groups III

I'm posting my code for a LeetCode problem. If you'd like to review, please do so. Thank you! Problem Two strings X and Y are similar if we can swap two letters (in different positions) of X, so that ...
4
votes
1answer
460 views

PyCrypto AES-CFB with SCrypt and HMAC

I am brushing up on my Python, and since I would like to learn a bit about encryption, I figured I would give PyCrypto a try. I obviously wouldn't implement encryption in any production environment, ...
2
votes
3answers
3k views

Script to activate an alarm at a time in the future

I am making a flashlight plugin for macOS. Here is the main code, plugin.py, the method results gets called first and is passed ...
7
votes
1answer
111 views

Scrape points from CTF sites

I am relatively new to classes. This one uses one. I am pretty sure this is not the correct way to do this. But I also do not know the correct way. You can use the ...
2
votes
1answer
126 views

Determine if points are within a rotated rectangle

Using only Python 2.7 and the standard library (no imports): Determine if a point is inside the rotated rectangle. ...
3
votes
3answers
1k views

Find 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. The above exercise is from chapter 2 of Computation ...
6
votes
4answers
2k views

Kadane's Algorithm for 2D array with known boundaries

I asked this question first on StackOverflow but I didn't get an answer and was advised to try here too. So here we go. I have implemented Kadane's algorithm for a 2D array in Python 2 with known ...
3
votes
2answers
104 views

Trellis Generator

I've been tasked with writing a simple trellis generator. The user is prompted with entering the height and width of the desired trellis, and gets a printed result of a trellis with the entered ...
5
votes
1answer
9k views

Python script for monitoring systemd services (cpu/memory usage)

This is a python (2.7.13 according to sys.version) script that reads systemd service names from a file and gets their CPU and Memory usage. It does that by first ...
11
votes
3answers
924 views

Finding the prime factors of a number in Python 2

I’ve created a function that takes a number and, if it’s prime, tells you so, or if it’s composite, gives you the prime factors of the number (and if it’s 1, tells you that it’s neither). ...
8
votes
2answers
30k views

K-means clustering in Python

The following code uses scikit-learn to carry out K-means clustering where \$K = 4\$, on an example related to wine marketing from the book DataSmart. That book uses excel but I wanted to learn Python ...
6
votes
3answers
13k views

Cadbury problem solution in Python

Problem Statement In a School, Chocolate bars have to be distributed to children waiting in a queue. Each Chocolate bar is rectangular in shape. Consider its side lengths are integer values. The ...
5
votes
0answers
214 views

Speeding up the implementation of Stochastic Gradient Ascent in Python

I am implementing a time-dependent Recommender System which applies BPR (Bayesian Personalized Ranking), where Stochastic Gradient Ascent is used to learn the parameters of the model. Such that, one ...
7
votes
0answers
423 views

Hit the target number

After I played the game "The Mesh" for a while I figured that I wanted to make a solver in Python. See the video on the website for a quick intro, I also explain the rules below. The Mesh ...
2
votes
0answers
66 views

Find the available squares for rooks in empty board using boolean arrays

I will describe two techniques, then the question will be how I can make an algorithm that is more efficient, if possible. I want to find an efficient way to determine the squares where the pieces on ...
2
votes
1answer
65 views

Part 2: Create or update record via HTTP request

This script was previously reviewed here: Part 1: Create or update record via HTTP request I've made the changes that were suggested by @Reinderien. Now that the code has been significantly refactored,...
15
votes
4answers
6k 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: ...
5
votes
1answer
169 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 ...
2
votes
1answer
63 views

Updating a dictionary of trajectories after a step has been made

I am using a dictionary to save possible trajectories in a game. A trajectory is defined as a list of numbers separated by _. For example ...
2
votes
1answer
115 views

Python - Do I need a class or are these functions okay?

I'm fairly new to Python but had to start using it and for reasons I have to use 2.7, which is why I am using that syntax. I have this function which gets data from a file, if it exists, and then gets ...
19
votes
7answers
36k 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) : ...
0
votes
1answer
68 views

Image color-moment extractor [closed]

I was just wondering if there is a way to speed up the performances of this for loops in Python. I'm trying to process an image to get the color-moments without using libraries. It takes about 12sec ...
25
votes
5answers
87k 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 ...
6
votes
3answers
820 views

Prime sieve generator

I wrote a simple prime sieve in Python. But it ended up looking... ugly. I included a few of the most basic optimizations. When posting this question SE suggested a bunch of questions on the same ...
2
votes
3answers
2k views

Given a sorted array nums, remove the duplicates in-place

I wrote an algorithm to solve this challenge but would love some feedback on its efficiency and, if you were an interviewer, what your opinion would be on my solution. I believe my time complexity is \...
3
votes
4answers
23k 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 ...
1
vote
1answer
32 views

Selecting minimal sum of two distinct entries of two dicts with common keys

Can this function simplified? The goal is return one key from each dictionary (both contain the identical keys), so that the sum of the values that belong to the selected keys are minimal (and also ...
3
votes
3answers
6k views

Finding the longest palindromic substring

I have the following code that works correctly. I am wondering if there is a simpler way to go about implementing my solution and/or if there is anything that looks non-standard in my code. If anyone ...
14
votes
2answers
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 ...
6
votes
4answers
19k views

Plus Minus in Python

You're given an array containing integer values. You need to print the fraction of count of positive numbers, negative numbers and zeroes to the total numbers. Print the value of the fractions ...
2
votes
1answer
94 views

Linux bonding status check

I have created the below code which works for me, but I feel there might be a better way to accomplish this in a new Pythonic way or better Python syntactic way. Code snippet: ...
1
vote
2answers
81 views

Better pythonic way to pass condition to multiple functions [closed]

I have the following scenario where most of my functions require to return different things based on a condition. ...

1
2 3 4 5
25