Questions tagged [python]

Python is an interpreted, general-purpose high-level programming language whose design philosophy emphasizes code readability. Use the python tag for all Python related questions. If you believe your question may be even more specific, you can include a version specific tag such as python-3.x.

Filter by
Sorted by
Tagged with
18
votes
3answers
5k views

Password Strength Detector

I'm just a beginner in Python. I wrote a simple code that detects the user's password strength, but I feel that it isn't very efficient. How can I improve it? ...
18
votes
5answers
3k views

Flipping coins performance

This is the Flipping coins problem on CodeChef: There are \$N\$ coins kept on the table, numbered from \$0\$ to \$N - 1\$. Initially, each coin is kept tails up. You have to perform two types of ...
18
votes
4answers
77k views

Chess game in Python

I have programmed for 2 months, and I began writing a Chess game. I am a beginner programmer in Python, so please assess my code. ...
18
votes
4answers
7k views

User login database intended for beginners

I have been working on a small project intended for helping beginner Python programmers getting an idea of what a real project could be like (as opposed to programming exercises and such). This ...
18
votes
6answers
3k views

Comma Code - Ch. 4 Automate the Boring Stuff

I am new to programming and currently learning Python using "Automate the Boring" by Al Sweigart. I've just completed the "Comma Code" task in Chapter 4 and would like some ...
18
votes
6answers
5k views

Rock, Paper Scissors game in Python

I wrote a simple Rock, Paper Scissors game in Python for class. As a beginner, I would like some input as to how I could abstract or make my code smoother. I feel like the repeated use of ...
18
votes
3answers
3k views

Providing advice based on user's favourite YouTuber

I have written this code which provides advice based on user's favourite YouTuber. I wonder if there is a better way to do absolutely any of it. Also, if there is any way to optimise it or if there is ...
18
votes
4answers
3k views

Hero battle game

This is my first attempt at OOP/classes: a game where you will select a character and use attacks and abilities to defeat the enemy character. I have a few specific questions: I was told that having ...
18
votes
2answers
9k views

Intersection over Union for rotated rectangles

Problem Statement I am trying to find the intersection over union (IoU) metric for one to several rotated rectangles compared to many different rotated rectangles. Here are some images to help ...
18
votes
3answers
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
2answers
3k views

Making my own programming language

This question is really to help me decide on something. I have started development of my own programming language that I am calling DeliciousWaffle (or maybe samscript). So far it looks pretty cool. ...
18
votes
2answers
2k views

Simple game of Hangman in Python

I'm starting out with learning how to program in Python and created a Hangman game. I think there is a lot of unnecessary code but I am not sure how to improve it. I wanted to avoid using global ...
18
votes
2answers
2k views

Python backend interview: task management system for workers

I've recently applied to a backend position and I was asked to complete a take-home interview question. I thought the question was rather simple and I completed in about an hour although I was told it ...
18
votes
4answers
1k views

Discover random web servers and print the index page title

Github repo I wrote this little toy today that probes random web servers and prints out the title of their index page. I'm curious if this code is any good since I'm a bit rusty. get_titles.py <...
18
votes
2answers
4k views

Python bot to answer mathematical questions for a remote server

The MSP Tech Club puts on an annual CTF, available here. I've worked on a solution but was wondering if there's a better way to write it or if I have any particular bad habits showing themselves? The ...
18
votes
1answer
31k views

Adjacency List Graph representation on python

I began to have my Graph Theory classes on university, and when it comes to representation, the adjacency matrix and adjacency list are the ones that we need to use for our homework and such. At the ...
18
votes
4answers
37k views

RSA algorithm implementation in Python 3

First, a reminder of the RSA algorithm and what my program implements: Take two distinct, large primes p and q Ideally these ...
18
votes
2answers
1k views

Election vote-counting, result-posting chatbot

In preparation for the end of Code Review's 2015 election, I've built a simple bot to get the election votes, run OpenSTV on them, and post the result to a chatroom. I'm using this Python ...
18
votes
2answers
6k views

Replacing Python Classes with Modules [closed]

I try to avoid using classes in Python as much as possible; if I don't plan on building on it, I don't build it in the first place. It helps me avoid Java-like classes like ...
18
votes
1answer
16k views

Python Executer that kills processes after a timeout

Imagine you want to run a bunch of tasks in parallel, but you can't be sure that all of your tasks will eventually terminate or rather decide to eat up all your memory and CPU time. This should be ...
17
votes
5answers
5k views

Printing a list as “a, b, c.” using Python

Is there a better way to do this? Assuming very long lists, is this slow and wasteful? ...
17
votes
6answers
10k views

Counting the number of consecutive 1's in binary

hackerrank.com Task: Given a base-10 integer, n, convert it to binary (base-2). Then find and print the base-10 integer denoting the maximum number of consecutive 1's in n's binary representation. ...
17
votes
3answers
5k views

Python code that sorts books

I am currently a student just starting to learn python and I need help improving my code for a school assignment. I've already written the code for the program, just need suggestions on how to improve ...
17
votes
4answers
1k views

Sensor logger for Raspberry Pi in a stratospheric probe

I'm writing a Python Script for a Raspberry Pi to measure different sensors. We are planning to send the Pi with that Script running to the stratosphere, so the power usage for the Pi is limited. I ...
17
votes
4answers
4k views

Beginners Python Hangman Code

I just started learning about Python at school and decided to start this little project with the basic knowledge I have. Most of the code is pretty lengthy and I know there are probably more efficient ...
17
votes
4answers
21k views

Bitcoin wallet address and private key generator

I wanted to learn how to create a Bitcoin wallet in code. I used as reference this guide which has code examples in JavaScript. I wrote my implementation in Python. Here is the resulting code: ...
17
votes
2answers
3k views

Discount Pokemon Battles

I have decided to create a discount Pokemon battle simulator using a module called guizero. It's basically a wrapper for Tkinter that is easier to learn. Recently, ...
17
votes
2answers
91k views

Fastest way to iterate over Numpy array

I wrote a function to calculate the gamma coefficient of a clustering. The bottleneck is the comparison of values from dist_withing to ...
17
votes
3answers
8k views

Converting numbers to words - Python

I'm new to programming. I tried to find a way to convert numbers into letters with Python. I would like to receive some advice to improve myself. This program works for numbers between 1 and 106 and ...
17
votes
2answers
2k views

Yandex programming contest: Alarms

I've tried to solve this challenge about 4 hours during the contest, but all my attempts exceeded the time limit. I tried to solve it with min-heap, but can't pass all the tests. How it can be solved ...
17
votes
2answers
3k views

Pancake sort interview with Python

I got asked this question during my interview. You can find information about this question here. Pancake Sort Given an array of integers arr: Write a function flip(arr, k) that reverses ...
17
votes
2answers
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 ...
17
votes
2answers
20k views

Pokémon style battle game

I haven't been learning Python for too long and I was just wondering how this Pokémon style battle looks? It's based off of this: Turn Based Pokémon Style Game. It's my first proper time using ...
17
votes
2answers
21k views

Python Back-Up Script

I wrote this script to backup some important files. It backs up the files to a local folder as well as to an external hard drive. It creates a new subdirectory which has its name constructed with ...
17
votes
2answers
288 views

Gimme some random passwords

pwgen is a nice password generator utility. When you run it, it fills the terminal with a bunch of random passwords, giving you many options to choose from and ...
17
votes
2answers
879 views

FizzBuzz example in Python for game manual

I'm developing an implementation of the FizzBuzz game as part of an edutainment game for a retro video game platform. In its manual, I want to mention the history of writing a bot for the FizzBuzz ...
17
votes
1answer
2k views

Rock Paper Scissors Lizard Spock

I got some good feedback from the last code I posted which I have incorporated in to this project (enum, try, f string formatting), so again just looking for some feedback on how the code could be ...
17
votes
2answers
2k views

Split mp3 of album into individual tracks

Recently, I've been transferring some of my old vinyl records to MP3 files. One of the things I did to make some progress was to automate things, so I initially set things up so that it would record ...
17
votes
1answer
4k views

Plot timings for a range of inputs

When writing Code Review answers, it becomes often necessary to measure how long the modified code takes vs how long the OP's code takes. I needed a nice way to visualize this as a function of the ...
17
votes
1answer
14k views

Scraping after login using Scrapy

I just finished a scraper in python using scrapy. The scraper logs in to a certain page and then scrapes a list of other pages using the authenticated session. It retrieves the title of these pages ...
17
votes
1answer
17k views

Min/Max Heap implementation in Python

I'm refreshing some of my datastructures. I saw this as the perfect opportunity to get some feedback on my code. I'm interested in: Algorithm wise: Is my implementation correct? (The tests say so) ...
17
votes
2answers
2k views

A KenKen puzzle/solver in Python

I've written a simple KenKen puzzle/solver in Python. I'd love some feedback on the design of the puzzle, as well as the architecture for the solver. To model the puzzle, I have the following classes:...
17
votes
1answer
22k views

Myth-busting SQLite3 performance w. pysqlite

I've read most of the posts I could find on optimizing SQLite3 performance, such as: How do I improve the performance of SQLite?, Is it possible to insert multiple rows at a time in an SQLite ...
17
votes
2answers
12k views

Django Rest Framework - ManyToMany relationship through intermediate model

I'm new to Django and have just started to learn the basics. I have some questions about how to structure the code, how relationships should be written, and where the business logic should live. I ...
17
votes
1answer
410 views

Efficiently selecting spatially distributed weighted points

Background: Motivation behind writing the following code is originated in the area of computer vision. More specifically – image rectification. In order to obtain rectified images, one has to find a ...
17
votes
1answer
561 views

Python Dictionary Black Magic

I am defining a subclass of a the python dictionary object mpCmd where every item is converted to a lambda. The intended usage is that every item in the dictionary ...
16
votes
6answers
11k views

Verifying IPv6 addresses

While trying to learn Python I stumbled upon a question which asks you to write a code that is able to take an input and verify it to see whether it meets the IPv6 Criteria. I wrote the following ...
16
votes
11answers
4k views

Check if array has the same number of even and odd values in Python

Task: Complete the following function that determines if the number of even and odd values in an list of Integers is the same. ...
16
votes
7answers
3k views

Stack data structure in Python 3

I have implemented a stack in Python 3, and it works well. If anything needs to be improved, I would appreciate the criticism. ...
16
votes
8answers
7k views

Program for finding longest run of zeros from a list of 100 random integers which are either 0 or 1

I have written an answer to this question on Stack Overflow. To make it easier for you, I have copy-pasted the main question below. Write a program that generates 100 random integers that are ...