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
0answers
55 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
133 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
35 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: ...
25
votes
6answers
32k 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
95 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
78 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
433 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 ...
6
votes
1answer
763 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 ...
7
votes
1answer
105 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
73 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
92 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
8k 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
493 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
212 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
394 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
63 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
60 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,...
14
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
130 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
62 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
108 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
32k 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
66 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
71k 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
736 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
20k 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
15k 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
16k 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
74 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
80 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. ...
9
votes
4answers
11k views

Consolidate list of ranges that overlap

I wanted to implemented an algorithm in Python 2.4 (so the odd construction for the conditional assignment) where given a list of ranges, the function returns another list with all ranges that overlap ...
-1
votes
1answer
1k views

Foo Bar - Power Hungry challenge test case failing on unknown edge cases [closed]

The challenge is to find the maximum product of a subset of a given array. Here's the problem: Write a function solution(xs) that takes a list of integers representing the power output levels of ...
3
votes
1answer
68 views

Estimation of min_samples for DBSCAN

I'm attempting to speed up some python code that is supposed to automatically pick the minimum samples argument in DBSCAN. Currently the execution time grows exponentially as the number of training ...
1
vote
1answer
29 views

Executing Scipy Tensor Product [closed]

Can anyone advise on how to use the SymPy TensorProduct function without having to explicitly indicate the full sequence of objects to take the product of. Below (for the simple case N=3) you can see ...
2
votes
1answer
5k 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. ...
3
votes
0answers
69 views

Store special deals, like 3 for 2

I have to apply two deals on the cart. Buy 3 (equal) items and pay for 2 Buy 3 (in a set of items) and the cheapest is free Items in cart: ...
14
votes
3answers
69k 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 ...
2
votes
2answers
113 views

Python - Hexadecimal to Decimal conversions

I have written a python 2 program for a school project to convert from hexadecimal to decimal without using built-in functions and vice versa in python. I would like to know how I can make my code ...
1
vote
1answer
191 views

Reading employee data from a PostgreSQL database

I have developed this script to read employee_language table records of a PostgreSQL database using Python 2 (due to some OS limitation). I want to find duplicate ...
1
vote
0answers
49 views

Reading attachment data from a PostgreSQL table with python

I have developed this script to find the counts of attachment records stored in a table in PostgreSQL for different other tables. I check if the res_id field of <...
2
votes
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 ...

1
2 3 4 5
25