This challenge is related to the Python language. Note that challenges that require the answers to be in a specific language are generally discouraged.

learn more… | top users | synonyms

13
votes
1answer
350 views

Golfing with Python Import

When is it beneficial to use inline, single use importing in Python? For example: __import__("x").doSomething() Is the above ever shorter than the below? import x x.doSomething() Or from x ...
10
votes
7answers
1k views

Non-idempotent Python [on hold]

Write a few lines of Python code, X, that does not reference any global variables, such that def method(): X print(a) method() prints 1 but def method(): X X print(a) ...
0
votes
1answer
62 views

Merging a dict compression referencing the original list into 1 line [closed]

Background A question wanted to solve a problem in a way that made it unreasonably complex: http://stackoverflow.com/a/31551257/29347 The problem in the original question is: Take a list of ...
2
votes
1answer
134 views

all non-empty sublists partitionings of a list

I had to write a code for finding all non-empty sublists partitionings of a list: def f(s): if s: for j in range(1,len(s)+1): for p in f(s[j:]): yield ...
12
votes
3answers
764 views

Is there a shorter way to get user input in python?

This is not a challenge. I'm wondering if it's at all possible to get user input into two separate variables in python (2 or 3) with less than 19 bytes. These are all the shortest I can get: ...
6
votes
2answers
281 views

Generalized integer casting in Python

Background I have a string in Python which I want to convert to an integer. Normally, I would just use int: >>> int("123") 123 Unfortunately, this method is not very robust, as it only ...
2
votes
1answer
183 views

Minimum of a Polynomial in Python

What is the shortest amount of code that can find the minimum of an inputted polynomial? I realize that you can import packages like Numpy and others, but using only user defined functions, what is ...
1
vote
1answer
105 views

How can I use cmp(a,b) with Python3?

I want to compare a and b. How can I use cmp(a,b) with Python3 ?
5
votes
4answers
406 views

How could I reduce the length of this code?

I need to reduce the length of this code in Python 3 as much as possible (even if it will be less readable): a,b,x,y=[int(i) for i in input().split()] while 1: r='' if y<b:r='S';y+=1 if ...
9
votes
1answer
339 views

Concatenate lists of strings item-wise in Python

This is a tips question for golfing in python. Suppose you have two lists of strings, and you want to concatenate the corresponding entries from each list. E.g. with a=list("abcd") and ...
-1
votes
1answer
119 views

How can I rename .split or .join in python? [duplicate]

I can rename a built in module like this: r=range list(range(10)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] but whenever I rename .split, I get an error: s=split NameError: name 'split' is not defined How ...
16
votes
3answers
2k views

Is there a shorter way to assign one of two variables in Python?

This is a tips question for golfing in python. In multiple golfs I've done in Python, a fixed value is assigned to one of two variables chosen by a Boolean. The chosen variable is overwritten by the ...
11
votes
1answer
1k views

Am I code-golfing properly?

I am curious if I am Code Golfing properly. I set the challenge for myself to make a small hashing program into a single statement in Python. I first started off with: from itertools import ...
8
votes
1answer
1k views

Python 3 - Tried Golfing My Assignment

Note: This isn't as much a golfing challenge; it is more so asking for golfing suggestions. Recently I had a Python assignment for my web development class, in order to check whether we could code. ...
11
votes
3answers
409 views

Auto-meta-code-golf

You are sick of all of the codegolf challenges. Hence you decide to write a program that will automatically golf some Python code for you. There are 3 test cases: print ...
6
votes
3answers
408 views

Looking for shorter alternatives to `range(…)`

The best solution I've found so far for a golf code puzzle I'm working on includes two rather fat-looking invocations of range. I'm very new at code golf, especially in Python, so I could use a few ...
24
votes
7answers
4k views

Golf Practice: Python

This is a challenge of practicing golf optimization in Python -- reusable tricks and shortcuts to shave off a few characters. Many will be familiar to Python golfers and use common ideas from the ...
12
votes
1answer
195 views

Python advice: Portability of introspective function call

In Python, you can use the dir function on any object to get a list of the names of its instance functions: >>> dir('abc') ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', ...
5
votes
8answers
564 views

Golfing the Core

Note: although this is tagged as Python, other languages are permitted Challenge What you have to do is write the shortest functions to perform the same actions as the following Python 2 built in ...
1
vote
0answers
123 views

Write the shortest one-statement python quine [duplicate]

I wrote this one-line one-statement Python quine a little while back. print (lambda x: x + str((x,)))('print (lambda x: x + str((x,)))',) The point is, it's all one line, and one function (i.e. no ...
9
votes
13answers
13k views

Smallest python script to print even numbers 0 to 100

I'm work on a problem which I set myself for fun, which is to create a python script which prints the even numbers from 0 to 100. The challenge is to make the script as small as possible. This is what ...
14
votes
2answers
636 views

Maximum number of PEP8 violations in a single line

Your task is to write a file which contains a line with many pep8 violations. The rules: We use pep8 version 1.5.7 and the default settings. Calling pep8 with other command line options or ...
8
votes
1answer
815 views

Palindromic prime

I'm solving task, where: Input: A number as an integer. Output: The closest greater palindromic prime as an integer. I would appreciate hints how to make my solution shorter. Or directions if ...
1
vote
9answers
884 views

Make a for loop repeat forever

for loops are useful in python in many ways, but one of the most common usages associated with a for loop is it's ability to repeat a certain amount of times. Like this: for times in range(50): ...
14
votes
6answers
703 views

Alias Member Functions in Python

In Python, one can save bytes by aliasing functions that are used repeatedly. For example: r=range a=r(100) b=r(200) c=r(300) However, when the functions are member functions together, I don't know ...
14
votes
11answers
2k views

Crash (i.e. cause the interpreter to stop working and force close) Python

I would like to see who can make Python have a fatal crash with the most creative code. This means that when the program runs, Windows for example, will take over and pop up with something like “IDLE ...
3
votes
2answers
389 views

Violate all stylistic requirements of PEP8

In Python, the style guide PEP 8 defines the coding style for all Python code. Your task is, in as few characters as possible, to write a program that violates all those requirements and can still be ...
-1
votes
1answer
208 views

Finding nCr for code-golf [duplicate]

The problem is to compute nCr = n!/(r!) * (n-r)! in the fewest characters possible. The input will be in the form: 1st line will have number of test cases and second line will have n and r for each ...
0
votes
0answers
63 views

Fastest way to generate Smarandache–Wellin number [duplicate]

Given a number n find the Smarandache–Wellin number for n. Ex: for n=2 output=23 for n=5 output=235711 Answer it in ruby or python. given constraints are n<=70000
3
votes
5answers
745 views

(Python) Implementing incrementation

Challenge: Implement incrementation without addition, multiplication, exponentiation, and any higher binary operators (and their opposites) in Python. Winner is the person who can do it in the least ...
1
vote
4answers
890 views

secret language hackerrank.com

I tried this contest: https://www.hackerrank.com/challenges/secret-language Well, the score in this contest said I made 30 out of 30 points. As the score is calculated with the formula ...
3
votes
1answer
2k views

shortest possible python pascal triangle code [duplicate]

the contest is already finished, so I don't think it's cheating to ask: I am trying to optimize this small contest: pascal triangle I started with 4.72 and worked myself up to 7.48. The best made a ...
4
votes
3answers
1k views

Solve the CodeSprint4 Leibniz code golf challenge in Python in 66 characters

Check out this challenge: https://www.hackerrank.com/codesprint4/challenges/leibniz (The competition has already ended, so I'm just curious about the Python solution, which the organizers refused to ...
3
votes
2answers
654 views

Generate N number of “Go First” dice

Background As described here http://www.ericharshbarger.org/dice/#gofirst_4d12, "Go First" Dice is a set of four dice, each with unique numbering, so that: There will never be a tie. Each die has ...
1
vote
1answer
376 views

How can I make this program smaller? [closed]

I am trying my hand at writing super small programs. I wrote a simple "tree" display program for showing a graphical representation of a folder hierarchy. I have made it small in all the ways I can ...
-1
votes
2answers
215 views

Python Golfing - Narrow function calling on array down [closed]

Say i have a function named J and an array named f. now i call this code: result = [] for n in f: results.append(J(n)) is there any more efficient way of doing this? I was thinking something ...
12
votes
1answer
876 views

What is the minimum set of characters with which you can write any valid python program?

The proof of the set of characters is that there is a translator which takes any ascii python program as input and produces a limited character set version of the program. The limited character set ...
3
votes
3answers
2k views

List of paths to nested dictionary

Given a list of nested menu items, items = [ '3D/Axis', '3D/CameraTracker', 'Color/Invert', 'Color/Log2Lin', 'Color/Math/Add', 'Color/Math/Multiply', 'Other/Group', ...
4
votes
1answer
2k views

Shortest Way of creating a basic Space Invaders Game in Python

Hello all code golfers: Your challenge is to create a basic version of Space Invaders in Python Rules Program must be not written in Py3K All levels must be randomly generated There must be a HUD ...
0
votes
0answers
175 views

Python - Reverse Polish Notation [duplicate]

Possible Duplicate: Reverse Polish notation Create a function named rpn in python to evaluate a string written in Reverse Polish notation Numbers must be separated by one or more spaces; ...
3
votes
3answers
1k views

Fastest python implementation of multiplication table

I'd be interested to see who can come up with the fastest function to produce multiplication tables, in the following format: 1 2 3 4 5 6 7 8 9 10 11 12 2 4 6 8 10 12 ...
12
votes
2answers
1k views

Fastest python code to find a set of winning words in this game

This is a word game from a set of activity cards for children. Below the rules is code to find the best triplet using /usr/share/dict/words. I thought it was an interesting optimization problem, and ...
99
votes
85answers
19k views

Tips for golfing in Python

What general tips do you have for golfing in Python? I'm looking for ideas which can be applied to code-golf problems and which are also at least somewhat specific to Python (e.g. "remove comments" is ...