You are viewing our Forum Archives. To view or take place in current topics click here.
Some Useful Python Scripts
Posted:
Some Useful Python ScriptsPosted:
Status: Offline
Joined: Sep 06, 201014Year Member
Posts: 1,069
Reputation Power: 47
Status: Offline
Joined: Sep 06, 201014Year Member
Posts: 1,069
Reputation Power: 47
Here are some python scripts I've written for school/fun. Feel free to use/change them and post any that you've made and think are useful.
Solves the X intercepts of a parabola.
Factors any integer.
Solves the handshake problem with recursion.(There are n people in a room they all shake hands...)
from math import *
def quadratic(a,b,c):
# -b +- sqrt(b^2 - 4ac)
# x = ---------------------
# 2a
d=(b*b)-(4*a*c) #discriminant
if d>0: #2 solutions
x1=((-b)+sqrt(d))/(2*a)
x2=((-b)-sqrt(d))/(2*a)
print "X = %s and X = %s."%(x1,x2)
elif d==0: #1 solution(vertex)
x=(-b)/(2*a)
print "X = %s."%x
else:
print "No real solutions."
a=float(raw_input("Enter a:"))
b=float(raw_input("Enter b:"))
c=float(raw_input("Enter c:"))
quadratic(a,b,c)
Factors any integer.
def factor(n):
for i in range(n):
if i > 0:
if n%i == 0:
if i > n/i:
pass
else:
print "%s, %s"%(i,n/i)
n=int(raw_input("Enter a #:"))
factor(n)
Solves the handshake problem with recursion.(There are n people in a room they all shake hands...)
def handshake(n,t):
if n >= 2:
handshake(n-1, t + n-1)
else:
print(t)
n=int(raw_input("# Of People:"))
handshake(n,0)
#2. Posted:
Status: Offline
Joined: Aug 27, 201113Year Member
Posts: 227
Reputation Power: 8
Status: Offline
Joined: Aug 27, 201113Year Member
Posts: 227
Reputation Power: 8
Nice scripts, I was wondering what you meant when you said you don't have access to any other compiler, why not?
- 0useful
- 0not useful
#3. Posted:
Status: Offline
Joined: Sep 06, 201014Year Member
Posts: 1,069
Reputation Power: 47
Status: Offline
Joined: Sep 06, 201014Year Member
Posts: 1,069
Reputation Power: 47
There is a white list that determines which programs we can run on our school computers. And on top of that we have a web filter. In light of those two things I can't run any compiler that is an executable, and I can't go to most web sites, but repl.it happens to not be blocked by the web filter so I use it.
- 0useful
- 0not useful
#4. Posted:
Status: Offline
Joined: Aug 27, 201113Year Member
Posts: 227
Reputation Power: 8
Status: Offline
Joined: Aug 27, 201113Year Member
Posts: 227
Reputation Power: 8
Ahhh gotcha, do you not have access to a computer outside of school that you can install some IDE's, etc, on?
- 0useful
- 0not useful
#5. Posted:
Status: Offline
Joined: Sep 06, 201014Year Member
Posts: 1,069
Reputation Power: 47
Status: Offline
Joined: Sep 06, 201014Year Member
Posts: 1,069
Reputation Power: 47
Asynkro wrote Ahhh gotcha, do you not have access to a computer outside of school that you can install some IDE's, etc, on?
Yes I do; I know/use several other languages it just so happens that python is the language that is most easily accessible to me at school.
- 0useful
- 0not useful
You are viewing our Forum Archives. To view or take place in current topics click here.