Python 3 is the latest version of the Python programming language and was formally released on December 3rd, 2008.

learn more… | top users | synonyms

3
votes
1answer
23 views

Time optimization of counting shifts in insertion sort

Is there any way of counting how many shifts would it take to count shifts while doing insertion sort? This is a Hackerrank problem. Here's my solution, but it takes more than 16 seconds. counts = ...
5
votes
2answers
319 views

Sherlock and The Beast

I have recently written the program for the Sherlock and The Beast' HackerRank challenge. That's working fine, but the problem is that it takes too much time if a big number is given as a input. I ...
5
votes
2answers
229 views

Pretty-print Pascal's triangle

def printTrg(rows): r1 = [1] r2 = [1, 1] trg = [r1, r2] r = [] if rows == 1: r1[0] = str(r1[0]) print(' '.join(r1)) elif rows == 2: for o in trg: ...
0
votes
1answer
26 views

Calculating Leibniz of Pi [closed]

I have this Python program for calculating Leibniz of 'pi'. I am not able to shorten it more. Can anyone here optimise/shorten it? s,a,b=0,[],[] for i in range(int(input())):a.append(input()) for x ...
2
votes
1answer
41 views

Verlet integration movement - doubt for on_key_press() methods

Based on my understanding of Verlet integration I tryed to use it over my Euler method to move my character in a 2D space. I will put only the neccessery code, but if anything else is needed I will ...
4
votes
1answer
74 views

Modified BFS code optimizations

I need some help reviewing this code in terms of making as fast as possible. This is a modified version of BFS that does quite a bit of processing on the side. Use case: I have a large graph ...
8
votes
1answer
62 views

Produce bitcoin private key from 31 playing cards

I've written some Python code to generate a random hexadecimal string using 31 playing cards drawn without replacement (so no card appears more than once). This is sufficient to provide more than 160 ...
5
votes
4answers
349 views

Sieve of Eratosthenes - Python

So I've been doing a lot of Project Euler lately and just wanted to make sure my implementation was as good as it could be. Does anyone have any suggestions to speed this up? def sieve(upperlimit): ...
13
votes
2answers
145 views

Condorcet voting method in OOP Python

I'm currently learning Python and having a great time so far (coming from PHP, I have a feeling of suddenly seeing the light). At the same time, I'm studying voting algorithms. So, for training ...
5
votes
3answers
147 views

Code optimization for SQLite result set parsing

I am retrieving information from an SQLite database that gives me back around 20 million rows that I need to process. This information is then transformed into a dict of lists which I need to use. I ...
10
votes
3answers
322 views

Installing “mods” with Python

A little disclaimer: you're going to have a field day with this. It's horrible. Looking at it makes me want to vomit, and don't ask me to explain it, because I've long forgotten what I was thinking ...
4
votes
1answer
61 views

Cutting strings into smaller ones based on specific criteria

So, I've got this largish (for me) script, and I want to see if anybody could tell me if there are any ways to improve it, both in terms of speed, amount of code and the quality of the code. I still ...
5
votes
1answer
66 views

Hooking with Python3 Decorators

I wrote this prototype after reading the Wikipedia article on hooking. I didn't bother to read any of the code examples listed there because, well, I just didn't. I don't have a good excuse. The ...
7
votes
1answer
78 views

More efficient way of removing line indices?

After profiling my current script I've found that nearly 100% of the run time (and I'm not surprised) comes from the following function: def rem_asc_by_pos(xmlFile, fileOut,removeIndices): fileIn = ...
7
votes
3answers
151 views

Simple MPG calculator in Python

I am a self taught coder taking a Programming Fundamentals class to work towards a degree. It's based on Python, which I'm not as familiar with as other languages. I added error handling like I would ...
5
votes
1answer
126 views

Optimizing my code for Project Euler Problem #23 (Non-abundant sums)

Project Euler Problem 23 asks: A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be ...
3
votes
0answers
91 views

XLSX writer implementation

We have multiple scripts which write into XLSX with the same format, into a different format or slide variation. I am trying to write an API which saves time for other programs, but I need input on ...
6
votes
4answers
69 views

Record Cataloging Program

I made a simple program to catalog some old records I have. It seems a tad redundant in the searching function. Does anyone know what I can do about that? import easygui as eg import sys namedoc = ...
2
votes
1answer
57 views

Python Hangman feedback

Just looking for some feedback on a hangman script. It works perfectly fine; I'm just trying to master the Python language, and the best place way to get better is to ask the true masters! import ...
2
votes
1answer
51 views

Python data massager

Below is a pretty simple Python script to ingest some data, massage it as necessary, append a column, sort it, and then write it back to another file. Still learning all the Python best practices and ...
5
votes
1answer
179 views

Newton's method to solve cubic equations

I have deleted the previous question I had posted, and am reposting the program with the new version of the code without the error present in the previous one. I have used the Newton-Raphson method ...
4
votes
0answers
109 views

Tkinter file searching program

This is my first relatively big program. First it shows an askdirectory() screen to get the path. It gets all subfolders of that path and appends them to a list. It checks every folders' items if they ...
1
vote
1answer
69 views

Python converter: number-to-English

So I wrote this function to convert a given number to its interpretation in the English language as part of the Project Euler exercises. It works fine, but I sense that it's rather sloppy and ...
5
votes
2answers
155 views

Performance of modular square root

Here's Project Euler problem 451: Consider the number 15. There are eight positive numbers less than 15 which are coprime to 15: 1, 2, 4, 7, 8, 11, 13, 14. The modular inverses of these ...
2
votes
0answers
215 views

Reading columns and rows in a .csv file

I have some data in a .csv file, which looks roughly like this: [fragment1, peptide1, gene1, replicate1, replicate2, replicate3] [fragment1, peptide2, gene1, replicate1, replicate2, replicate3] ...
3
votes
2answers
204 views

Python Port Scanner

This is only my third Python script. Be brutal with me. Any tips, tricks, best practices, or better usages would be great! import socket from concurrent.futures import ThreadPoolExecutor THREADS = ...
0
votes
1answer
150 views

Fastest way for working with itertools.combinations

I need to speed up the function below: import numpy as np import itertools def combcol(myarr): ndims = myarr.shape[0] solutions = [] for idx1, idx2, idx3, idx4, idx5, idx6 in ...
2
votes
1answer
91 views

Partial data reading implementation

I'm trying to learn Python and am working a simple Tiled Map Format (.tmx) reader as practice. So as to not post too much code at once, I'm only publishing the <data> element which stores the ...
1
vote
1answer
189 views

struct.unpack on a bytearray that works under Python 2.6.6, 2.7 and 3.3?

Given the test code: import struct buf = bytearray(b'\xef\xf8\t\xf2') number = struct.unpack("<I", buf)[0] How do I cleanly make it work both under Python 2.6 and Python 3.3? The problem is that ...
1
vote
1answer
60 views

Reading Multidimensional arrays

I am building a cypher program which uses a matrix of numbers and letters. A message is encoded according to the row an collum each letter of the message is found in the matrix. I am trying to find a ...
3
votes
1answer
137 views

chess moves - pythonic

I started a chess in python for fun. i learned python on my free time but i've been doing it for a while. The code works but i just want to know if there's anything non-pythonic in my code. The ...
4
votes
2answers
293 views

Python Script to Search PirateBay

I've written a very basic Python 3 script to search ThePirateBay. Since the tracker doesn't have an API, I had to parse the HTML using BeautifulSoup. I'd like to get some reviews, I'm pretty sure the ...
0
votes
0answers
71 views

Drawing stars around the center of the screen

I have an homework as follows: Write a program where you enter from the keyboard a number n. Then n stars are drawn onto the screen, circling around the center of your screen. You can assume ...
0
votes
2answers
108 views

Counting the even digits in a number [closed]

I made a function to count the even digits in a number and when I enter a number that doesn't include any even digits. I need some one to tell me why and how to fix this. And please be simple in the ...
0
votes
2answers
62 views

Function to count the digits in a number is not working [closed]

I tried to test this function and the first test is OK, but the seconds test failes and I don't know why.... Note: I'm a beginner, so be easy and Please, don't give me complicated answers! import ...
5
votes
2answers
93 views

Copying data to a .txt, but making it look nice?

This is probably horribly sloppy, but here it is! This is a section from a large (imo) GUI program I am making. The below code is what triggers when a specific button is pressed. def ...
4
votes
4answers
253 views

Rock paper scissors

I've started learning Python recently and I wanted to test myself. So, can you tell me how many I would get out of 10 and how I could improve my future coding? import random import time ...
4
votes
1answer
55 views

Organization of complex empirical equations

I am working on coding up a set of models that are based on empirical models. These models tend to be quite complicated. In some cases, the logic used by the equations makes it impossible to compute ...
1
vote
2answers
126 views

Taking the sum and average of the keyboard inputs

I have an assignment as follows: Write a program where you can enter from the keyboard up to 10 integers. If the number entered is equal to -99, stop reading numbers from the keyboard and ...
2
votes
4answers
122 views

Printing a rectangle

I have an assignment as follows: Write a program which reads from the keyboard two integers n and m, and a character c from the keyboard. This program should define and call a function: ...
2
votes
1answer
87 views

Drawing a table in Python3

I have created a module to draw text tables. The relevant parts are below and here's the full code for txtable). class Row(Txtable): [...] def format(self, col_widths): """Return the ...
1
vote
0answers
81 views

A program that deliberately makes “mistakes” [closed]

I am confused about an assignment question. We are simply asked to code a program which deliberately makes mistakes. The assignment question is as follows: Write a program that deliberately makes ...
0
votes
1answer
38 views

Best way to print the value of a variable?

Which of the following is better coding style and why? print("We have x = %i" %x) print("We have x = "+str(x))
1
vote
0answers
67 views

Script for concatenating delimited text files

I need to concatenate a bunch of delimited text files. In the process, I need to add a new column to the data based on part of the name of one of the directories containing each file. The script ...
0
votes
1answer
269 views

Find scrambled words within word list

I'm just getting started with Python -- completing a simple exercise from a practice website. My code works -- just looking for feedback on better ways to achieve the goal. Goal: Find the ...
1
vote
2answers
76 views

How to make this random number game better?

I'm a newbie trying to teach myself Python. Could somebody tell me if there is a better way to write this? I am also curious as to whether or not there is anything I should not do. This code does ...
0
votes
2answers
54 views

How to enumerate the internal nodes and leaves of a tree more elegantly?

Might there be a simpler way to write this code? This is the code I have, and I'd like to learn to write code more efficiently. Thank you. def leaves_and_internals(self): """(BTNode) -> ...
2
votes
1answer
84 views

How to turn this working A/B optimization program into more pythonic code?

I have a working solution for the given problem of A/B Optimization, but you as an experienced Python (3) developer will agree with me, that this solution is not very pythonic at all. My Question ...
3
votes
2answers
358 views

Returning a list of the values from the binary tree

I want to return a list of the values from the binary tree. Is there a shorter and more efficient way to write the method for numbers? class BTNode(object): """A node in a binary tree.""" ...
3
votes
1answer
75 views

Subscriptable/Indexable generator

I'm not a Python developper, but I enjoy programming with it, and for a project I wanted to have generators that I can easily index. Using python's slice model is obviously the way to go, and here's ...