Python Forum
Python 25 Line Challenge
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python 25 Line Challenge
#1
What is the most interesting, useful or fun program that you can develop in 25 lines of Python or less?

Working on developing projects for my Intro to Programming class, I reminded myself of an article I read back in the early days of the PC. The article was in one of the first PC only magazines, and it was publishing the results of a contest the magazine sponsored. Preliminaries complete, here is what they did:

They challenged their readers to come up with the most useful programs they could using only a single line of code in the BASIC interpreter. With the colon instruction separator you could easily get twenty commands on a single line. And the winning submissions were brilliant. One was a screen saver-esk art program, a second was a working word processor, and the third was a full sub chase game. I wish I could share the article and results with this group, but my net search for mention of the article turned up nothing.

So, in the spirit of the early days of PC programming, here is my proposal to all of you:

The 25 Line Python Challenge!

Give us your clever, entertaining, most creative best, but keep it under twenty five lines.
Reply
#2
I will get things started with one of my favorite chestnuts - Rock Paper Scissors in 24 lines (23 if you don't count the comment)

#Rock Paper Scissors; Example for the 25 line challange; Matthew L. Parets
from random import *
print("Welcome to the game of Rock-Paper-Scissors. Good Luck!")
print("  Enter 1 for Rock, 2 for Paper, and 3 for Scissors")
user = int(input("Enter your choice: "))
comp = randint(1,3)
if user == 1 and comp == 1:
  print("You choose Rock, Computer chooses Rock, Tie game.")
elif user == 1 and comp == 2:
  print("You choose Rock, Computer chooses Paper, Ha Ha! You lose.")
elif user == 1 and comp == 3:
  print("You choose Rock, Computer chooses Scissors, You Win!")
elif user == 2 and comp == 1:
  print("You choose Paper, Computer chooses Rock, You Win!")
elif user == 2 and comp == 2:
  print("You choose Paper, Computer chooses Paper, Tie game.")
elif user == 2 and comp == 3:
  print("You choose Paper, Computer chooses Scissors, Ha Ha! You lose.")
elif user == 3 and comp == 1:
  print("You choose Scissors, Computer chooses Rock, Ha Ha! You lose.")
elif user == 3 and comp == 2:
  print("You choose Scissors, Computer chooses Paper, You Win!")
else:
  print("You choose Scissors, Computer chooses Scissors, Tie game.")
Reply
#3
If we are going for one shot. This could be made even shorter. There is no validation of any kind.
from random import choice
wins = [('paper', 'rock', 'paper covers rock'),
('scissors', 'paper', 'scissors cut paper'),
('rock', 'scissors', 'rock smashes scissors')]
pc = choice(['rock', 'paper', 'scissors'])
print('Type in rock, paper, or scissors')
player = input('>> ').lower()
for win in wins:
    if player in win[0] and pc in win[1]:
        print(f'Player wins: {win[2]}')
        break
    elif pc in win[0] and player in win[1]:
        print(f'PC wins: {win[2]}')
        break
    elif player in win[0] and pc in win[0]:
        print('Tie Game')
        break
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#4
(May-02-2022, 08:02 PM)menator01 Wrote: If we are going for one shot. This could be made even shorter. There is no validation of any kind.

Brilliant. This is a great example of the strengths of Python. I love it. I wonder if anyone can get it down lower. :-)
Reply
#5
It depends a lot on the libraries that one is allowed to import. Can we use any library in Pypi or just the standard library?
Reply
#6
eight lines. As before there is not any kind of validation. typing anything other than the key words will result in errors.
from random import choice
items = ['rock', 'paper', 'scissors']
print(f'Options: {", ".join(items)}')
pc, player = choice(items), input('>> ').lower()
wins = ('paper', 'rock', 'paper covers rock'), ('rock', 'scissors', 'rock crushes scissors'), ('scissors', 'paper', 'scissors cuts paper')
winner = ['tie game' if player == pc else ('player wins' if player in wins[0] and pc in wins[1] else 'pc wins')]
print(f'player -> {player} | pc -> {pc}')
print(winner[0])
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#7
There used to be a python module to convert any script to a single line http://www.onelinerizer.com/

I'm afraid there is no python 3 version.
Reply
#8
(May-03-2022, 12:24 PM)Gribouillis Wrote: It depends a lot on the libraries that one is allowed to import. Can we use any library in Pypi or just the standard library?

I'm a teacher so I lean towards only using libraries are that part of most installations. Math, Random, Time, Turtle, Tkinter.

But I would say that any for an open challenge like this :-)
Reply
#9
(May-03-2022, 07:49 PM)menator01 Wrote: eight lines. As before there is not any kind of validation. typing anything other than the key words will result in errors.

Again, most impressive. :-D
Reply
#10
Here is another humble submission from yours truly, a drawing program staring the turtle, complete with color change in 25 lines.

from turtle import *
from random import *
leo = Turtle()           
leo.shapesize(3)    
leo.speed("fastest") 
colormode(255)   
def whenMouseClicks(x,y):
    leo.penup()
    leo.goto(x,y)
    leo.pendown()
def whenMouseDrags(x,y):
    leo.ondrag(None)  
    leo.goto(x,y)
    leo.ondrag(whenMouseDrags,btn=1,add=None) 
def whenCPressed():
    red = randint(0,255)
    grn = randint(0,255)
    blu = randint(0,255)
    leo.color( (red,grn,blu) )
Screen().onclick(whenMouseClicks)
leo.ondrag(whenMouseDrags,btn=1,add=None)
Screen().onkey(whenCPressed,"c")
Screen().listen()
Screen().mainloop()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python 100 line Challenge codingCat 9 3,265 Jun-20-2022, 07:18 AM
Last Post: Coricoco_fr
  Zen Python Challenge ichabod801 3 4,108 Aug-13-2018, 12:02 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020