diff --git a/builtin_filters.py b/builtin_filters.py new file mode 100644 index 0000000..75f4ca4 --- /dev/null +++ b/builtin_filters.py @@ -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" + + diff --git a/builtin_filters.pyc b/builtin_filters.pyc new file mode 100644 index 0000000..cfa2c42 Binary files /dev/null and b/builtin_filters.pyc differ diff --git a/custom_filters.py b/custom_filters.py new file mode 100644 index 0000000..28e6041 --- /dev/null +++ b/custom_filters.py @@ -0,0 +1,4 @@ +#put your own filters right here... +"""Example filter: +def AddOne(num): + return num + 1""" \ No newline at end of file diff --git a/custom_filters.pyc b/custom_filters.pyc new file mode 100644 index 0000000..3b3c229 Binary files /dev/null and b/custom_filters.pyc differ diff --git a/main.py b/main.py new file mode 100644 index 0000000..44faddc --- /dev/null +++ b/main.py @@ -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)) \ No newline at end of file diff --git a/triangle.py b/triangle.py new file mode 100644 index 0000000..e59a42f --- /dev/null +++ b/triangle.py @@ -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 \ No newline at end of file diff --git a/triangle.pyc b/triangle.pyc new file mode 100644 index 0000000..3192c08 Binary files /dev/null and b/triangle.pyc differ