Welcome, Guest

Author Topic: Coding  (Read 262312 times)

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: Python
« Reply #60 on: October 26, 2012, 01:43:34 PM »
oh i figured it out
allobjects[2][2]

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: Python
« Reply #61 on: October 26, 2012, 02:32:18 PM »
someone tell me:

def countline():
    count=0
    for line in open('save.txt', 'r+'):
        count = count + 1
    return count

yeah so what name does python use to reference the file so i can close it  like
Code: [Select]
f.close()

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: Python
« Reply #62 on: October 26, 2012, 04:21:36 PM »
if i take lines from a text file and append them to an array, they come out as strings.

for example if i have array
allobjects=[]

and then the line in my save.txt is [0,320,40,51]
allobjects.append(save.readline().rstrip())

print allobjects
['[0,320,40,51]']

i want it to be
[[0,320,40,51]]

blotz

  • Formerly 'bong'
  • *****
  • Posts: 813
  • op pls
Re: Python
« Reply #63 on: October 26, 2012, 05:37:04 PM »
why post question if no one but you can solve them?

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: Python
« Reply #64 on: October 26, 2012, 05:53:27 PM »
why post question asking "why post question if no one but you can solve them" if other people can solve them.

your logic is like saying that if you don't know if people have the answer to your question, don't ask it.

blotz

  • Formerly 'bong'
  • *****
  • Posts: 813
  • op pls
Re: Python
« Reply #65 on: October 26, 2012, 06:19:29 PM »
because you might not have the answer and other ppl might.

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: Python
« Reply #66 on: October 26, 2012, 06:34:42 PM »
why post question if no one but you can solve them?
because you might not have the answer and other ppl might.


you have just answered your own question.

blotz

  • Formerly 'bong'
  • *****
  • Posts: 813
  • op pls
Re: Python
« Reply #67 on: October 26, 2012, 06:36:42 PM »
i know, but no one else here knows python good enought to solve your python problems

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: Python
« Reply #68 on: October 26, 2012, 06:37:14 PM »
proof?

Darvince

  • *****
  • Posts: 1842
  • 差不多
Re: Python
« Reply #69 on: October 26, 2012, 06:39:25 PM »
godstab, greenland

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: Python
« Reply #70 on: October 27, 2012, 05:08:35 AM »
updated code

and i figurd out the for statement :P
Code: [Select]
import time
import os
import random
import math

#defines all the variables
emitter_xpos = 0
emitter_ypos = 0
emitter_zpos = 0
emitter_turnx = 0
emitter_turny = 0
emitter_moved = 0

#moves emitter forwards
def emitter_move_distance():
    global emitter_moved
    emitter_moved_1000 = random.randint(50,100)
    emitter_moved = float(emitter_moved_1000)/1000
#turns the emitter up down right or left
def emitter_turn_x():
    global emitter_turnx
    emitter_turnx_1000 = random.randint(-500,500)
    emitter_turnx = float(emitter_turnx_1000)/1000 + emitter_turnx
def emitter_turn_y():
    global emitter_turny
    emitter_turny_1000 = random.randint(-500,500)
    emitter_turny = float(emitter_turny_1000)/1000 + emitter_turny
#calculates new position of emitter
def emitter_pos():
    global emitter_xpos, emitter_ypos, emitter_zpos
    emitter_xpos = emitter_moved*math.sin(-(emitter_turny)+math.pi/2)*math.cos(emitter_turnx)+emitter_xpos
    emitter_ypos = emitter_moved*math.sin(-(emitter_turny)+math.pi/2)*math.sin(emitter_turnx)+emitter_ypos
    emitter_zpos = emitter_moved*math.cos(-(emitter_turny)+math.pi/2)+emitter_zpos

#runs entire thing
def move_emitter():
    emitter_move_distance()
    emitter_turn_x()
    emitter_turn_y()
    emitter_pos()

allobjects=[]

#opening objects
def load_obj():
    global allobjects
    for line in open('save.txt', 'r+'):
        derp = [int(x) for x in line.rstrip().split(",")]
        allobjects.append(derp)

load_obj()

#collecting energy
def energy_collection(emitter_xpos, emitter_ypos, emitter_zpos):
    global allobjects
    for list in allobjects:
        #list[4] is the distance
        #list[5] is the energy collection unit
        #list[6] is the enery stored
        #list[7] is the maximum value of the energy storage unit
        list[4] = ((list[0]-emitter_xpos)**2+(list[1]-emitter_ypos)**2+(list[2]-emitter_zpos)**2)**0.5
        list[6] = list[6]+list[5]/list[4]**0.5
        if list[6] > list[7]:
            list[6] = list[7]

energy_collection(emitter_xpos, emitter_ypos, emitter_zpos)
print allobjects
    

    


os.system('pause')

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: Python
« Reply #71 on: October 28, 2012, 07:22:10 PM »
testplz

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: Python
« Reply #72 on: November 02, 2012, 06:39:59 PM »
i've been busy

code for primality testing using the miller-rabin probabilistic test:
Code: [Select]
import random

def primality(number, iterations): #insert a number and the amount of iterations for a return of True or False for primality
    n = number
    k = iterations
    n_=n-1 #supposed to write n-1 as 2^s*d later; so substitute this instead
    s=0
    d=0
    if n <= 1:
        return False
    if n == 3 or n == 2: #filters out numbers <= 3 because the primality test can't
        return True
    if n % 2 == 0:
        return False
    while n_ % 2 == 0: #making n_ into 2^s*d
        s = s + 1
        n_= n_/ 2
    d = n_
    while k != 0: #corresponds to line : LOOP: repeat k times:
        k = k - 1
        a = random.randint(2, n-2) # corresponds to : pick a random integer a in the range from 2 to n minus 2
        x = pow(a,d,n)           # corresponds to : x = a^d mod n
        if x == 1 or x == n - 1: #corresponds to : if x = 1 or x = n - 1 then do next loop
            continue            # if this is true then go back to the while k!=0 loop
        r = 1
        for r in xrange (1,s): #do this from r = 1 to r = s - 1
            x = pow(x, 2, n)       #corresponds to x = x^2 mod n
            if x == 1:            #corresponds to if x = 1 then return composite
                return False
            elif x == n - 1:          #if x = n - 1, then it will ignore the if and break and go back to while k!=0 loop
                break
        else:
            return False #composite
    return True                   #return prime

code that factors smallest integer out of a number using a pollard-brent method, partly taken from comeoncodeon blog/wordpress thingy:
Code: [Select]
import random
import fractions

#come on code on wordpress blog

def pollard_brent(number): #returns a factor of an integer where number > 1 is True
    if number %  2 == 0:
        return 2
    y = random.randint(1,number-1)
    c = random.randint(1,number-1)
    m = random.randint(1,number-1)
    g = 1
    r = 1
    q = 1
    while g == 1:
        x = y
        for i in range(r):
            y = (pow(y,2,number)+c) % number
        k = 0
        while k < r and g == 1:
            ys = y
            for i in range(min(m, r-k)):
                y = (pow(y,2,number)+c) % number
                q = q*(abs(x-y)) % number
            g = fractions.gcd(q, number)
            k = k + m
        r = r * 2
    if g == number:
        while True:
            ys = (pow(ys, 2, number)+c) % number
            g = fractions.gcd(abs(x-ys), number)
            if g > 1:
                break
    return g
i'm not sure how the above code works so don't ask me :P

and this is a prime factorization code; returns an array with (hopefully) all the prime factors of a number:
Code: [Select]
import random
import fractions
import os

def primality(number, iterations):
    n = number
    k = iterations
    n_=n-1
    s=0
    d=0
    if n <= 1:
        return False
    if n == 3 or n == 2:
        return True
    if n % 2 == 0:
        return False
    while n_ % 2 == 0:
        s = s + 1
        n_= n_/ 2
    d = n_
    while k != 0:
        k = k - 1
        a = random.randint(2, n-2)
        x = pow(a,d,n)
        if x == 1 or x == n - 1:
            continue
        r = 1
        for r in xrange (1,s):
            x = pow(x, 2, n)
            if x == 1:
                return False
            elif x == n - 1:
                break
        else:
            return False
    return True

def pollard_brent(number): #returns a factor of an integer where number > 1 is True
    if number % 2 == 0:
        return 2
    y = random.randint(1,number-1)
    c = random.randint(1,number-1)
    m = random.randint(1,number-1)
    g = 1
    r = 1
    q = 1
    while g == 1:
        x = y
        for i in range(r):
            y = (pow(y,2,number)+c) % number
        k = 0
        while k < r and g == 1:
            ys = y
            for i in range(min(m, r-k)):
                y = (pow(y,2,number)+c) % number
                q = q*(abs(x-y)) % number
            g = fractions.gcd(q, number)
            k = k + m
        r = r * 2
    if g == number:
        while True:
            ys = (pow(ys, 2, number)+c) % number
            g = fractions.gcd(abs(x-ys), number)
            if g > 1:
                break
    return g

def prime_factorization(number, iterations):
    n = number
    primefactors = []
    while primality(number, iterations) == False:
        if number == 1:
            break
        factor = pollard_brent(number)
        primefactors.append(int(factor))
        number = number / factor
    if number != 1 and n != number:
        primefactors.append(int(number))
    return primefactors

os.system('pause')

attached is a prime.py and a primefactorization.py

prime.py will tell you if a number is prime or even given an input. you may input through the terminal or by entering a number in a number.txt in the same directory as the prime.py file. i strongly suggest using notepad++ to input the number; notepad incorrectly warps large numbers.

the only input for primefactorization.py is with the terminal. a sorted array with all the prime factors should be returned. entering a number beyond 50 digits may result in several minutes of computation. it took my computer 5 minutes to calculate the prime factorizations for all the numbers from 2 to 1000000 excluding.

*A note on accuracy:
since the miller-rabin test is probabilistic -- that means, it relies on probability; there is a chance that when you run one of the scripts, you will get an incorrect result.

the probability that prime.py will result in a false positive; meaning that a composite number is designated prime is:
 4^(-k) where k is the amount of iterations running. running 10 iterations, which means a wrong result chance of 1 in 10,000 should be enough for most applications, but if necessary, you can run a thousand or a million trials which will give a false positive rate of one in a trillion or one in a septillion respectively. a prime number will never be mistakenly labeled as composite.

**the actual probability is significantly less than 4^(-k), read here for further maths:
http://www.math.dartmouth.edu/~carlp/PDF/paper88.pdf

because primefactorization.py relies on the miller-rabin test, it will also have a chance of miscalculation; although the probability is a bit more complex to calculate it should be on the same order of magnitude as the false positive rate of the primality.py algorithm.

and post if you notice any bugs.

blotz

  • Formerly 'bong'
  • *****
  • Posts: 813
  • op pls
Re: Python
« Reply #73 on: November 02, 2012, 07:05:27 PM »
i just read the program names and not the post. :)

Hellpotatoe

  • *****
  • Posts: 230
  • JooJ
Re: Python
« Reply #74 on: November 03, 2012, 05:08:24 AM »
i ignore python things because idk how to work with 'em

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: Python
« Reply #75 on: November 03, 2012, 05:16:15 AM »
ok you can download and install from here http://www.python.org/getit/releases/2.7.3/ (pick any one)
and then i recommend you install this: http://sourceforge.net/projects/pywin32/files/pywin32/Build%20218/pywin32-218.win-amd64-py2.7.exe/download

then open python interpreter and you can try some basic commands like
print 'hello world'
or
print 5*3
etc

Hellpotatoe

  • *****
  • Posts: 230
  • JooJ
Re: Python
« Reply #76 on: November 03, 2012, 05:23:04 AM »
no thanks, this pc kant run python

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: Python
« Reply #77 on: November 03, 2012, 05:24:02 AM »
really? have you tried? i'm pretty sure python can run on any computer that can run a browser :P

Hellpotatoe

  • *****
  • Posts: 230
  • JooJ
Re: Python
« Reply #78 on: November 03, 2012, 05:25:49 AM »
this pc kan't run a browser

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: Python
« Reply #79 on: November 03, 2012, 05:26:45 AM »
then how are you on universe sandbox! what program do you use to search the internet

Hellpotatoe

  • *****
  • Posts: 230
  • JooJ
Re: Python
« Reply #80 on: November 03, 2012, 05:35:58 AM »
i'm using ms-dos -kolno
this pc kant run internet

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: Python
« Reply #81 on: November 03, 2012, 05:39:43 AM »
yes it can thats why you're posting herez1!

Hellpotatoe

  • *****
  • Posts: 230
  • JooJ
Re: Python
« Reply #82 on: November 03, 2012, 05:41:32 AM »
NO, THATS A DREAM!
i wonder if bla will delete our random posts or if he will make one topic with them...

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: Python
« Reply #83 on: November 03, 2012, 05:43:19 AM »
^

yes bu go install pyfon

Hellpotatoe

  • *****
  • Posts: 230
  • JooJ
Re: Python
« Reply #84 on: November 03, 2012, 05:53:31 AM »
no, because i kant use internet

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: Python
« Reply #85 on: November 03, 2012, 05:54:53 AM »
yes you can and that post above just prove it!

Hellpotatoe

  • *****
  • Posts: 230
  • JooJ
Re: Python
« Reply #86 on: November 03, 2012, 05:57:09 AM »
IM USING A PROXYPAPER!!!

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: Python
« Reply #87 on: November 03, 2012, 05:59:37 AM »
YOU CANNOT USE THAT TO CONNECT TO BLACRAFT

vh

  • formerly mudkipz
  • *****
  • Posts: 1140
  • "giving heat meaning"
Re: Python
« Reply #88 on: November 03, 2012, 08:00:49 PM »
semi working code

Code: [Select]
import random
import fractions

def primality(number, iterations): #converted from http://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test#Algorithm_and_running_time
    n = number       #number to test for primality
    k = iterations   #corresponds to line 'k, a parameter that determines the accuracy of the test
    n_=n-1           #n-1 is substituted by n_, for later use when i need to write n - 1 as 2**s * d,
    s=0
    d=0
    if n == 3 or n == 2:
        return True
    if n % 2 == 0:
        return False
    while n_ % 2 == 0: #writes n - 1 aka n_ as 2**s * d by dividing by two and checking if n_ is still even
        s = s + 1
        n_= n_/ 2
    d = n_
    while k != 0:                   #corresponds to line in pseudocode | Loop: repeat k times:
        k = k - 1                   
        a = random.randint(2, n-2)  #picks random integer in range 2, n-2
        x = pow(a,d,n)              # sets x to a^d mod n
        if x == 1 or x == n - 1:    # corresponds to line in pseudocode| if x = 1 or x = n - 1 then do next Loop
            continue
        for r in xrange (1,s):      #corresponds to line in pseudocode | for r = 1...s-1 
            x = pow(x, 2, n)       #sets x to x^2 mod n
            if x == 1:              #corresponds to line | if x = 1 then return composite
                return False
            elif x == n - 1:     #corresponds to line | if x = n - 1 then do next Loop
                break
        else:                   #corrresponds to line | return composite
            return False
    return True                 #corresponds to line | return probably prime

def pollard_brent(number): #returns a factor of an integer: code from http://comeoncodeon.wordpress.com/2010/09/18/pollard-rho-brent-integer-factorization/#viewSource
    if number % 2 == 0:
        return 2
    y = random.randint(1,number-1)
    c = random.randint(1,number-1)
    m = random.randint(1,number-1)
    g = 1
    r = 1
    q = 1
    while g == 1:
        x = y
        for i in range(r):
            y = (pow(y,2,number)+c) % number
        k = 0
        while k < r and g == 1:
            ys = y
            for i in range(min(m, r-k)):
                y = (pow(y,2,number)+c) % number
                q = q*(abs(x-y)) % number
            g = fractions.gcd(q, number)
            k = k + m
        r = r * 2
    if g == number:
        while True:
            ys = (pow(ys, 2, number)+c) % number
            g = fractions.gcd(abs(x-ys), number)
            if g > 1:
                break
    return g

def prime_factorization(number, iterations):            #returns list of prime factors of a number
    n = number
    primefactors = []
    while primality(number, iterations) == False: # if number is not a prime number
        if number == 1:                         # and if number is not 1
            break
        factor = pollard_brent(number)          #factor the number
        if factor != number and primality(factor, 10) == True: #if the factor is composite or equal to number then try again.
            primefactors.append(int(factor))                # if factor is composite and not equal to number then add it to the list of prime factors
            number = number / factor                            #set number to number/factor
    if number != 1 and n != number:                         #if number is not 1 or n, then
        primefactors.append(int(number))                    #append it
    return primefactors                                     #return array with all the prime factors

def divisorlist(num):                                       #returns list of proper divisors when given prime factors
#all the divisors of a number can be generated by multiplying the prime factors of the number in all possible ways
    divisors=[]
    f = lambda l: reduce(lambda z, x: z + [y + [x] for y in z], l, [[]]) #returns the set of all subsets of its argument http://wiki.python.org/moin/Powerful%20Python%20One-Liners
    for list in f(prime_factorization(num,10)):                         #for all lists in the set of subsets that can be created from the list of the prime factors of num
        n=1                                                             #set n to one
        for int in list:                                                #then multiply all the numbers in each list
            n = n * int
        divisors.append(n)                                              #and that is one possible divisor of num
    if num in divisors:                                                 #remove num if it exists because only proper divisors needed
        divisors.remove(num)
    divisors_=[]
    for int in divisors:                                                #remove duplicates
        if int not in divisors_:
            divisors_.append(int)
    divisors = divisors_
    return divisors                         #return all the proper divisors
       

def subset(num):                                                            #figure out if a number is semiperfect or not
    semiperfect = False                                         
    f = lambda l: reduce(lambda z, x: z + [y + [x] for y in z], l, [[]])    #creates all possible subsets,
    for list in f(divisorlist(num)):                                        #of the list of proper divisors
        if sum(list) == num:                                                #if the sum any list is equal to the number
            semiperfect = True                                              #then the number is semiperfect
    return semiperfect

#weird number is both NOT semiperfect and abundant
for num in xrange(2,1000000):
    if sum(divisorlist(num)) > num:     #if the sum of proper divisors of num is larger than num then the number is abundant
        if subset(num) is False:        # if the num is NOT semiperfect subset(num) should be False
            print num                   #if both of the above are true then the number is weird and print it.

Darvince

  • *****
  • Posts: 1842
  • 差不多
Re: Python
« Reply #89 on: November 03, 2012, 09:54:49 PM »
at the beginning of every python code:
import alphabet
import dictionary
import english
import python
import x, y, z