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

-1
votes
0answers
57 views

pythonic way to create subarray of elements from an array satisfying a given property in numpy? [on hold]

I have an array of values like dats = np.array([[r1,x1,y1],[r2,x2,y2],...,[rn,xn,yn] and I want to make a subarray of dats which has elements [r,x,y] such that the points [x,y] lies within a radius ...
-3
votes
0answers
66 views

Balanced smileys check algorithm [on hold]

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 ...
0
votes
0answers
66 views

Find equal pairs [closed]

Problem Given an array A of unique integers, find the index of values that satisfy A + B =C + D, where A,B,C & D are integers values in the array. Find all combinations of quadruples. I ...
26
votes
3answers
339 views

Python workarounds for assignment in lambda

This is a tips question for golfing in Python. In Python golfing, it's common for a submission to be a function defined as a lambda. For example, f=lambda x:0**x or x*f(x-1) computes the factorial ...
9
votes
4answers
252 views

Abstract Syntax Tree Golfing: FizzBuzz, Python

Summary Implement FizzBuzz in Python, with the fewest possible tokens. Challenge Write a program that prints the decimal numbers from 1 to 100 inclusive. But for multiples of three print “Fizz” ...
12
votes
5answers
871 views

Add comments to a Python script and make it a bilingual Python/C++ “program”

Given the following Python 3 script: def greet(): print("Hello, world!") greet() Prepend some lines to this text file so that it can be both executed as a Python program as well as compiled and ...
30
votes
3answers
2k views

Python - Make (a+b)(c+d) == a*c + b*c + a*d + b*d

Your task is to write some code in Python or Python3 such that this expression: (a+b)(c+d) == a*c + b*c + a*d + b*d will evaluate to True without raising any exceptions. To clarify, I will copy ...
-2
votes
1answer
170 views

Import this into your own language [duplicate]

The Zen of Python must surely be one of the most beautiful Easter eggs in any language. (It is created by writing import this) So why not create a version for your own language? For those who don't ...
9
votes
1answer
397 views

How to find the item in a list whose f(item) is the smallest?

I have a list, l and a function f. f is not strictly increasing or decreasing. How can I find the item in the list whose f(item) is the smallest? For example, let's say the list is: l = [1, 2, 3, 4] ...
7
votes
3answers
343 views

How can I shorten this python code analyzing a 3d grid?

My Python 3 function golf(...) should take a list of lists of lists of strings representing a solid cube and return whether there are any places in which two equal strings are directly next to each ...
4
votes
1answer
431 views

Need help golfing Python 3 code to calculate volume & surface of spheroids in <150 bytes

I want to write a function golf(C, A) that takes the height (C = 2*c) and the width (A = 2*a) of an oblate (left image) or prolate (right image) spheroid or a sphere as parameters and returns the ...
12
votes
4answers
393 views

Strategies for representing a given large integer using arithmetic expression

I have a specific number in mind, but it's part of a challenge I'm doing, and I don't want people to do (all) the work for me. Here is a number which has the same digits, but shuffled: ...
3
votes
0answers
265 views

Most creative/diabolical/entertaining way to find Python version? [closed]

There are important differences between Python 2 and 3, so it's often useful to know which version of the interpreter is running your code. The straightforward method is something like this: def ...
1
vote
2answers
224 views

Shortening str.replace() [duplicate]

In Python it is often necessary to use str.replace(a,b), however .replace is rather long and feels unnecessary. So is there a shorter alternative to str.replace. Alternatively is there a similar ...
3
votes
4answers
331 views

Returning the range of a number as a palindrome

I like to do programming challenges in my free time to teach myself and keep fresh, this one I can't seem to optimize though and it's driving me insane. The goal is essentially, "Given a number, ...
-3
votes
1answer
505 views

Python Golfing Suggestions [closed]

I have read: Tips for golfing in Python I would like to golf this code further. Any more tips? x,y,q,w=[int(i) for i in raw_input().split()] while 1: a=int(input()) s=d="" if q>x: s="W" q-=...
4
votes
5answers
301 views

Python Code Reviewer

Task Your mission is to create a program that reviews Python code. The solution can be written in any language! It must take input that contains python code. The program must add # This is not ...
11
votes
1answer
425 views

Write a panic script to remote secure a Mac Pro from an untrustworthy local user [closed]

The Situation Suppose I allow "Shady Sam" admin access on my workstation to perform an important administrative task on my Mac Pro for me by giving them a temporary root login password. (Assume they ...
13
votes
1answer
690 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
2k views

Non-idempotent Python [closed]

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) method() ...
0
votes
1answer
84 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 (key,...
2
votes
1answer
163 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 [s[:j]]+...
13
votes
3answers
1k 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: a,b=...
7
votes
2answers
472 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 ...
3
votes
1answer
272 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 ...
4
votes
1answer
4k 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
582 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 y>b:...
9
votes
1answer
2k 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 b=list("1234"),...
-1
votes
1answer
202 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 ...
18
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 ...
12
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. ...
12
votes
3answers
526 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 quickSort([0,7,3,-1,8,10,57,...
7
votes
3answers
602 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 ...
28
votes
7answers
6k views

Golf Practice: Python [closed]

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
221 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
610 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
144 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 ...
11
votes
13answers
31k 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 ...
15
votes
2answers
750 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 using ...
8
votes
1answer
1k 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
votes
10answers
1k 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): ...
18
votes
7answers
1k views

How do I 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 ...
15
votes
10answers
4k views

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

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 ...
-1
votes
1answer
265 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
66 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
856 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
1k 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 30*(1000-#chars)...
2
votes
1answer
3k 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 9....
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 ...