Add files via upload
now it's here
This commit is contained in:
parent
d9906524c8
commit
35b162fc9d
7 changed files with 185 additions and 0 deletions
85
builtin_filters.py
Normal file
85
builtin_filters.py
Normal file
|
@ -0,0 +1,85 @@
|
||||||
|
import math
|
||||||
|
|
||||||
|
def Modulo2(num):
|
||||||
|
return num % 2
|
||||||
|
|
||||||
|
def Modulo3(num):
|
||||||
|
return num % 3
|
||||||
|
|
||||||
|
def Digits(num):
|
||||||
|
return int(math.log10(num)) + 1
|
||||||
|
|
||||||
|
def IsPrime(num):
|
||||||
|
if num == 1:
|
||||||
|
return 0
|
||||||
|
elif num == 2:
|
||||||
|
return 1
|
||||||
|
primeVar = 1
|
||||||
|
digits = int(math.log10(num)) + 1
|
||||||
|
if digits > 12:
|
||||||
|
for divider in range(2, int(num/10000000000)):
|
||||||
|
if num % divider == 0:
|
||||||
|
primeVar = 0
|
||||||
|
for i in range(2,10000000001):
|
||||||
|
for divider in range(int((i-1)*num/10000000000), int(i*num/10000000000)):
|
||||||
|
if num % divider == 0:
|
||||||
|
primeVar = 0
|
||||||
|
return primeVar
|
||||||
|
|
||||||
|
elif digits > 9:
|
||||||
|
for divider in range(2, int(num/10000000)):
|
||||||
|
if num % divider == 0:
|
||||||
|
primeVar = 0
|
||||||
|
for i in range(2,10000001):
|
||||||
|
for divider in range(int((i-1)*num/10000000), int(i*num/10000000)):
|
||||||
|
if num % divider == 0:
|
||||||
|
primeVar = 0
|
||||||
|
return primeVar
|
||||||
|
|
||||||
|
elif digits > 6:
|
||||||
|
for divider in range(2, int(num/10000)):
|
||||||
|
if num % divider == 0:
|
||||||
|
primeVar = 0
|
||||||
|
for i in range(2,10001):
|
||||||
|
for divider in range(int((i-1)*num/10000), int(i*num/10000)):
|
||||||
|
if num % divider == 0:
|
||||||
|
primeVar = 0
|
||||||
|
return primeVar
|
||||||
|
|
||||||
|
elif digits > 3:
|
||||||
|
for divider in range(2, int(num/10)):
|
||||||
|
if num % divider == 0:
|
||||||
|
primeVar = 0
|
||||||
|
for i in range(2,11):
|
||||||
|
for divider in range(int((i-1)*num/10), int(i*num/10)):
|
||||||
|
if num % divider == 0:
|
||||||
|
primeVar = 0
|
||||||
|
return primeVar
|
||||||
|
|
||||||
|
else:
|
||||||
|
for divider in range(2,num):
|
||||||
|
if num % divider == 0:
|
||||||
|
primeVar = 0
|
||||||
|
return primeVar
|
||||||
|
|
||||||
|
def Mod2Vis(num):
|
||||||
|
if Modulo2(num) == 0:
|
||||||
|
return " "
|
||||||
|
else:
|
||||||
|
return "X"
|
||||||
|
|
||||||
|
def Mod3Vis(num):
|
||||||
|
if Modulo3(num) == 0:
|
||||||
|
return " "
|
||||||
|
elif Modulo3(num) == 1:
|
||||||
|
return "X"
|
||||||
|
else:
|
||||||
|
return "O"
|
||||||
|
|
||||||
|
def PrimeVis(num):
|
||||||
|
if IsPrime(num) == 0:
|
||||||
|
return "_"
|
||||||
|
else:
|
||||||
|
return "X"
|
||||||
|
|
||||||
|
|
BIN
builtin_filters.pyc
Normal file
BIN
builtin_filters.pyc
Normal file
Binary file not shown.
4
custom_filters.py
Normal file
4
custom_filters.py
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
#put your own filters right here...
|
||||||
|
"""Example filter:
|
||||||
|
def AddOne(num):
|
||||||
|
return num + 1"""
|
BIN
custom_filters.pyc
Normal file
BIN
custom_filters.pyc
Normal file
Binary file not shown.
32
main.py
Normal file
32
main.py
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
from triangle import Triangle, BeautifyTri, FilterTri
|
||||||
|
|
||||||
|
print """Hi! I'm glad You chose me as your go to Pascal's Triangle software! You are currently running my main.py,
|
||||||
|
so I assume You're interested in using my friendly I/O access. Feel free to also implement my functions
|
||||||
|
in any of your own code. For now, I will just ask you for the numbers of lines you want and which filter
|
||||||
|
to apply. A filter usually applies some function to each number in the triangle. You can create your own
|
||||||
|
filters in custom_filters.py. If you don't know what to do use the filter "help" ;). Have Fun!"""
|
||||||
|
|
||||||
|
while True:
|
||||||
|
error = False
|
||||||
|
try:
|
||||||
|
size = int(raw_input("Number of rows: "))
|
||||||
|
except ValueError:
|
||||||
|
print "Damn, I couldn't get that number. Please only use numerical characters here..."
|
||||||
|
error = True
|
||||||
|
if not error:
|
||||||
|
filter = raw_input("Filter: ")
|
||||||
|
if filter == "none":
|
||||||
|
print BeautifyTri(Triangle(size))
|
||||||
|
elif filter == "help" or filter == "Help" or filter == "HELP":
|
||||||
|
print """Here are all the builtin filters, try them out as you like:
|
||||||
|
- Modulo2
|
||||||
|
- Modulo3
|
||||||
|
- Digits
|
||||||
|
- IsPrime
|
||||||
|
- Mod2Vis
|
||||||
|
- Mod3Vis
|
||||||
|
- PrimeVis
|
||||||
|
|
||||||
|
!The Prime family of filters might take a while for triangles over 20 rows!"""
|
||||||
|
else:
|
||||||
|
print BeautifyTri(FilterTri(Triangle(size), filter))
|
64
triangle.py
Normal file
64
triangle.py
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
from builtin_filters import *
|
||||||
|
from custom_filters import *
|
||||||
|
|
||||||
|
def Triangle(size):
|
||||||
|
"""takes int size. Generates a pascals triangle with that number of rows."""
|
||||||
|
if size == 1:
|
||||||
|
return [[1]]
|
||||||
|
else:
|
||||||
|
rows = Triangle(size-1)
|
||||||
|
newRow = [1]
|
||||||
|
for index in range(len(rows[-1])-1):
|
||||||
|
newRow.append(rows[-1][index] + rows[-1][index+1])
|
||||||
|
newRow.append(1)
|
||||||
|
rows.append(newRow)
|
||||||
|
return rows
|
||||||
|
|
||||||
|
def BeautifyTri(triangle = Triangle(10)):
|
||||||
|
"""takes triangle (default is normal Tri with length 10), returns middle-aligned tri."""
|
||||||
|
rowStrList = []
|
||||||
|
outStr = ""
|
||||||
|
for row in triangle:
|
||||||
|
rowStr = ""
|
||||||
|
for num in row:
|
||||||
|
rowStr += str(num) + " "
|
||||||
|
rowStrList.append(rowStr[:len(rowStr)])
|
||||||
|
for rowStr in rowStrList:
|
||||||
|
while len(rowStr) < len(rowStrList[-1]):
|
||||||
|
rowStr = " " + rowStr
|
||||||
|
if len(rowStr) < len(rowStrList[-1]):
|
||||||
|
rowStr += " "
|
||||||
|
rowStr += "\n"
|
||||||
|
outStr += rowStr
|
||||||
|
return outStr
|
||||||
|
|
||||||
|
def FilterTri(inTri = Triangle(10), filterFunc = Modulo2):
|
||||||
|
"""takes triangle inTri and/or function filterFunc (defaults are Triangle(10) and Modulo2).
|
||||||
|
Returns filtered Triangle."""
|
||||||
|
failed = False
|
||||||
|
if type(filterFunc) == str:
|
||||||
|
try:
|
||||||
|
filterFunc = eval(filterFunc)
|
||||||
|
except NameError:
|
||||||
|
failed = True
|
||||||
|
if failed:
|
||||||
|
print """This filter doesn't seem to exist :/ I imagine it would be really
|
||||||
|
cool if it did... Maybe create it? ^^ But since it would be really
|
||||||
|
boring if nothing happened, have a normal pascal's triangle."""
|
||||||
|
return Triangle(10)
|
||||||
|
outTri = []
|
||||||
|
for row in inTri:
|
||||||
|
outRow = []
|
||||||
|
for num in row:
|
||||||
|
try:
|
||||||
|
outNum = filterFunc(num)
|
||||||
|
outRow.append(outNum)
|
||||||
|
except NameError:
|
||||||
|
failed = True
|
||||||
|
outTri.append(outRow)
|
||||||
|
if failed:
|
||||||
|
print """This filter doesn't seem to exist :/ I imagine it would be really
|
||||||
|
cool if it did... Maybe create it? ^^ But since it would be really
|
||||||
|
boring if nothing happened, have a normal pascal's triangle."""
|
||||||
|
return Triangle(10)
|
||||||
|
return outTri
|
BIN
triangle.pyc
Normal file
BIN
triangle.pyc
Normal file
Binary file not shown.
Loading…
Reference in a new issue