9
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. Since I already feel comfortable in Python, I decided to try and golf it, and I was wondering if people could point out things that I missed.
I already know that there are extra spaces in some places, but I'm more interested in conceptual things, like using while r:
when r is a variable, and then waiting for it to "run out"!
import random
from collections import Counter
s=l=''
c=['yellow','blue','white','green','Black', 'purple', 'silver', 'cyan', 'magenta', 'red']
n=[10,15,1,10,6,15,10,25,1,12,5,10,4,6,5,12,0,10,1,1]
o=i=0
for y in c:l+=y[0]*(random.randint(n[o],n[o+1]));o+=2
l=list(l)
print("Welcome to the CIMS Gumball Machine Simulator\nYou are starting with the following gumballs:")
for b in c:print(str(l.count(b[0])) + " "+b);random.shuffle(l)
print("Here are your random purchases:")
while 'r' in l:
random.shuffle(l); r=l.pop(); s+=r
for j in c:
if j[0] == r:print(j.capitalize())
print("You purchased %i gumballs, for a total of $%.2f \nMost common gumball(s):" % (len(s),len(s)*25/100))
a=Counter(s).most_common()
m=[x[1] for x in a]
while m[0] == m[i]:
for j in c:
if j[0] == a[i][0]:print(j.capitalize(), end=" ")
if(i<(len(m)-1)):i+=1
else:break
Also: I'm sorry if this isn't an appropriate question for the code golf page, since it is not a challenge and will remove it on request.
Putting the issue of on-topic-ness aside (since I'm not sure), perhaps take a look at the Python golf tips page? Also, which Python version? (I'm assuming 3 due to the parens around
– Sp3000 – 2015-02-24T04:15:37.417print
, but just to check)5Have you attempted to golf it yet? – feersum – 2015-02-24T04:18:12.070
2This code has a lot of simple golf improvements remaining. I think you'd learn better if you reviewed the golf tips and looked at other Python golfs, and did more to shorten it your code on your own. Then, if you post what you get, people can give more insightful advice. – xnor – 2015-02-24T06:40:02.233