IMPORTANT!

Snipt is going open source. We've toyed with this idea for quite a while, and have finally decided it's the right way to move forward.

A few things:
  • The entire Snipt source code will be released on GitHub under the 3-clause BSD License on Friday, September 10th.
  • While we'd like to think we're perfect, we realize we're only human. By open sourcing the software that runs this website, certain bugs or security flaws may be discovered that could compromise the privacy of your snipts.
  • Only the Lion Burger team will be able to push commits to the Snipt.net site. Contributors should send a pull request to add new features or submit patches.
  • By using this site, you agree not to be too angry or take any legal action against Lion Burger should this whole thing go up in flames some day.
  • Follow us on Twitter for updates.
I agree, close this message
Sign up to create your own snipts, or login.

Latest 100 public snipts » dgg32's snipts The latest snipts from dgg32.

showing 1-20 of 42 snipts
  • watchdog
    #!/usr/bin/env python
    
    import time
    import sys
    import os
    import shutil
    from sets import Set
    
    dir = sys.argv[1]
    dirList = os.listdir(dir)
    fileList = set()
    
    
    while len(fileList) <= 105:
        time.sleep(60)
        for d in dirList:
            if "Flavobacteria" in d or "Gammaproteobacteria" in d or "Alphaproteobacteria" in d:
                fileList.add(d)
    
    """
    for d in dirList:
        if "Flavobacteria" in d or "Gammaproteobacteria" in d or "Alphaproteobacteria" in d:
            fileList.append(d)
    """     
    for f in fileList:
        fullpath = dir + f
        shutil.copy2(fullpath,"/Users/user/Dropbox/mimas_function")
    

    copy | embed

    0 comments - tagged in  posted by dgg32 on Sep 02, 2010 at 8:41 a.m. EDT
  • leverage, core 2 duo combining linear and poisson
    #!/usr/bin/env python
    import sys
    import re
    
    from collections import defaultdict
    import math
    
    #usage: cat file1 file2 file3... | python poisson.py no_of_project r_cutoff filter_value p_cutoff norm_factor (eliminate records with too many zero. For 3 "0" out of 7, filter_value = floor(3/7) = floor(0.42) = 0.4. That means only records with no more than 2 "0" are gonna included in the result)
    #written to answer the question: why are there more genes, gene copy or simply more genomes? This program is to check whether the second one is true
    #be careful with r: http://en.wikipedia.org/wiki/Anscombe%27s_quartet
    
    def stand_unit(values):
        average = sum(values) / float(len(values))
        quar = 0
        for i in range(len(values)):
            quar += (values[i] - average)**2
        SD = math.sqrt(quar /float(len(values)))
        #print values,SD, quar, average
        
        if SD == 0:
            return []
        else:
            stand_u = []
            for i in range(len(values)):
                stand_u.append((values[i]-average)/float(SD))
            
            return stand_u
    
    def r(x_su, y_su):
        if len(x_su) != len(y_su):
            return -2
        else:
            product = 0
            for i in range(len(x_su)):
                product += x_su[i] * y_su[i]
            #print "product",product
            return product/len(x_su)
    
    def poisson_probability(actual, mean):
        # naive:   math.exp(-mean) * mean**actual / factorial(actual)
    
        # iterative, to keep the components from getting too large or small:
        p = math.exp(-mean)
        for i in xrange(actual):
            p *= mean
            p /= i+1
        return p
    
    N = list()
    
    Column = -1
    Table = defaultdict(list)
    rx_contig = re.compile(r'Base\s+(\d+)')
    rx_item = re.compile(r'^(.+?)\t\t(\d+)\t\t.+')
    rx_total = re.compile(r'total.+')
    
    
    no_of_project = int(sys.argv[1])
    r_cutoff = float(sys.argv[2])
    filter_value = float(sys.argv[3])
    
    p_cutoff = float(sys.argv[4])
    norm_factor = float(sys.argv[5])
    
    for line in sys.stdin:
        contig = rx_contig.match(line)
        total = rx_total.match(line)    
        item = rx_item.match(line)
        
        if contig:
            Column = Column + 1
            N.append(int(contig.group(1)))
            #print int(contig.group(1))
            
        elif total:
            pass
        elif line == "":
            pass
        elif item:
            #print line
            
            name = item.group(1)
            amount = int(item.group(2))
            
            if name in Table.keys():
    
                    Table[name][Column] = amount
                    
            else:
                for i in range(no_of_project):
                    if i == Column:
                        Table[name].append(amount)
                    else:
                        Table[name].append(0)
    
    
    
    Y_stand_unit = stand_unit(N)
    
    print N
    for enzyme in Table.keys():
        
        if Table[enzyme].count(0) / float(len(Table[enzyme])) < filter_value: 
            X_stand_unit = stand_unit(Table[enzyme])
            #print "X_stand_unit",X_stand_unit
            if len(X_stand_unit) != 0:
                r_value = r(X_stand_unit, Y_stand_unit)
                #print r_value
                if r_value == -2:
                    print "something is wrong. len(x) != len(y)"
                else:
                    if abs(r_value) >= r_cutoff:
                        print "linear\t",enzyme,"r:",r_value,Table[enzyme]
                    else:
                       
                        
                        
                        total = sum(Table[enzyme])
                        base = sum(N)
                        
                        norm = 0
                        if norm_factor == 0:
                            norm = N[i] 
                        else:
                            norm = norm_factor
                        
                        #norm_occur has no calculatoric meaning for the analysis, i.e. its values are not used in further calculation. It only approximately shows how fit the sample for poisson is.
                        #Based on the approximation: (1/4 + 2/5)/2 about = (1+2)/(4+5)
                        norm_occur = []
                        for j in range(len(N)):
        
                            norm_occur.append(Table[enzyme][j]/(N[j]/float(norm)))
        
                        for i in range(len(Table[enzyme])):
                            rest_total = total - Table[enzyme][i]
                            rest_base = base - N[i]
    
            
                
            
                            expected = rest_total / (rest_base/float(norm))
            
                            p = poisson_probability(int(math.ceil(Table[enzyme][i]/(N[i]/float(norm)))), expected)
                            
                            if p < p_cutoff:
                            #if int(math.ceil(Table[enzyme][i])) > expected and enzyme == "DAO":
                          #  print "sample",i+1,":",N,Table[enzyme], enzyme,"\t\thigh\t\tcutoff",cutoff,"\t\tp", p, "\t\texpected",expected,"\t\tabundance",Table[enzyme][i],"\t\tactual",int(math.ceil(Table[enzyme][i]/(N[i]/float(norm))))
                                if int(math.ceil(Table[enzyme][i])) > expected:
                                    print "poisson\tsample",i+1,":",enzyme,"\t\thigh\tp", p, "\texpected",expected,"\tabundance",Table[enzyme][i],"\tactual",int(math.ceil(Table[enzyme][i]/(N[i]/float(norm)))),"\t",norm_occur
                
                                else:
                                    print "poisson\tsample",i+1,":",enzyme,"\t\tlow\tp", p, "\texpected",expected,"\tabundance",Table[enzyme][i],"\tactual",int(math.ceil(Table[enzyme][i]/(N[i]/float(norm)))),"\t",norm_occur
    
        
    

    copy | embed

    0 comments - tagged in  posted by dgg32 on Aug 28, 2010 at 4:12 p.m. EDT
  • r plot linear line
    y <- c(1034869, 7258090, 11788562, 32434044, 21325944, 10605149, 1716304);x <- c(3, 10, 9, 37, 26, 23, 3)
    
    plot(x,y, xlab="x axis", ylab="y axis", main="my plot", pch=15, col="blue")
    myline.fit <- lm(y ~ x)
    abline(myline.fit)
    

    copy | embed

    0 comments - tagged in  posted by dgg32 on Aug 28, 2010 at 11:02 a.m. EDT
  • linear_analysis
    #!/usr/bin/env python
    import sys
    import re
    
    from collections import defaultdict
    import math
    
    #usage: cat file1 file2 file3... | python poisson.py no_of_project r_cutoff filter_value (eliminate records with too many zero. For 3 "0" out of 7, filter_value = floor(3/7) = floor(0.42) = 0.4. That means only records with no more than 2 "0" are gonna included in the result)
    #written to answer the question: why are there more genes, gene copy or simply more genomes? This program is to check whether the second one is true
    #be careful with r: http://en.wikipedia.org/wiki/Anscombe%27s_quartet
    
    def stand_unit(values):
        average = sum(values) / float(len(values))
        quar = 0
        for i in range(len(values)):
            quar += (values[i] - average)**2
        SD = math.sqrt(quar /float(len(values)))
        #print values,SD, quar, average
        
        if SD == 0:
            return []
        else:
            stand_u = []
            for i in range(len(values)):
                stand_u.append((values[i]-average)/float(SD))
            
            return stand_u
    
    def r(x_su, y_su):
        if len(x_su) != len(y_su):
            return -2
        else:
            product = 0
            for i in range(len(x_su)):
                product += x_su[i] * y_su[i]
            #print "product",product
            return product/len(x_su)
    
    
    N = list()
    
    Column = -1
    Table = defaultdict(list)
    rx_contig = re.compile(r'Base\s+(\d+)')
    rx_item = re.compile(r'^(.+?)\t\t(\d+)\t\t.+')
    rx_total = re.compile(r'total.+')
    
    
    no_of_project = int(sys.argv[1])
    cutoff = float(sys.argv[2])
    filter_value = float(sys.argv[3])
    
    for line in sys.stdin:
        contig = rx_contig.match(line)
        total = rx_total.match(line)    
        item = rx_item.match(line)
        
        if contig:
            Column = Column + 1
            N.append(int(contig.group(1)))
            #print int(contig.group(1))
            
        elif total:
            pass
        elif line == "":
            pass
        elif item:
            #print line
            
            name = item.group(1)
            amount = int(item.group(2))
            
            if name in Table.keys():
    
                    Table[name][Column] = amount
                    
            else:
                for i in range(no_of_project):
                    if i == Column:
                        Table[name].append(amount)
                    else:
                        Table[name].append(0)
    
    
    
    Y_stand_unit = stand_unit(N)
    
    print N
    for enzyme in Table.keys():
        
        if Table[enzyme].count(0) / float(len(Table[enzyme])) < filter_value: 
            X_stand_unit = stand_unit(Table[enzyme])
            #print "X_stand_unit",X_stand_unit
            if len(X_stand_unit) != 0:
                r_value = r(X_stand_unit, Y_stand_unit)
                if r_value == -2:
                    print "something is wrong. len(x) != len(y)"
                else:
                    if abs(r_value) >= cutoff:
                        print enzyme,"r:",r_value,Table[enzyme], Table[enzyme].count(0)
                
        
    

    copy | embed

    0 comments - tagged in  posted by dgg32 on Aug 28, 2010 at 10:54 a.m. EDT
  • poisson for paola output
    #!/usr/bin/env python
    
    import sys
    import re
    
    from collections import defaultdict
    import math
    
    def poisson_probability(actual, mean):
        # naive:   math.exp(-mean) * mean**actual / factorial(actual)
    
        # iterative, to keep the components from getting too large or small:
        p = math.exp(-mean)
        for i in xrange(actual):
            p *= mean
            p /= i+1
        return p
    
    #usage: cat file1 file2 file3... | python poisson.py no_of_project p_cutoff norm_factor (0 for individual bin size)
    #attention Rashomon effect!
    #result largely affected by norm_factor. In my opionion, choosing a norm_factor = average genome size of the target makes most of the sense. In
    # that case, the normalization can be understood as computing the amount of gene copies. For Flavo, 3M is reasonal for me.
    #written to answer the question: why are there more genes, gene copy or simply more genomes? This program is to check whether the first one is true
    
    N = list()
    Column = -1
    Table = defaultdict(list)
    rx_contig = re.compile(r'Base\s+(\d+)')
    rx_item = re.compile(r'^(.+?)\t\t(\d+)\t\t.+')
    rx_total = re.compile(r'total.+')
    
    
    no_of_project = int(sys.argv[1])
    cutoff = float(sys.argv[2])
    norm_factor = float(sys.argv[3])
    
    for line in sys.stdin:
        contig = rx_contig.match(line)
        total = rx_total.match(line)    
        item = rx_item.match(line)
        
        if contig:
            Column = Column + 1
            N.append(int(contig.group(1)))
            #print int(contig.group(1))
            
        elif total:
            pass
        elif line == "":
            pass
        elif item:
            #print line
            
            name = item.group(1)
            amount = int(item.group(2))
            
            if name in Table.keys():
    
                    Table[name][Column] = amount
                    
            else:
                for i in range(no_of_project):
                    if i == Column:
                        Table[name].append(amount)
                    else:
                        Table[name].append(0)
    
    for enzyme in Table.keys():
        total = sum(Table[enzyme])
        base = sum(N)
        
        for i in range(len(Table[enzyme])):
            rest_total = total - Table[enzyme][i]
            rest_base = base - N[i]
            norm = 0
            if norm_factor == 0:
                norm = N[i] 
            else:
                norm = norm_factor
            
            
                
            
            expected = rest_total / (rest_base/float(norm))
            
            p = poisson_probability(int(math.ceil(Table[enzyme][i]/(N[i]/float(norm)))), expected)
            
            if p < cutoff:
                #if int(math.ceil(Table[enzyme][i])) > expected and enzyme == "DAO":
                  #  print "sample",i+1,":",N,Table[enzyme], enzyme,"\t\thigh\t\tcutoff",cutoff,"\t\tp", p, "\t\texpected",expected,"\t\tabundance",Table[enzyme][i],"\t\tactual",int(math.ceil(Table[enzyme][i]/(N[i]/float(norm))))
                if int(math.ceil(Table[enzyme][i])) > expected:
                    print "sample",i+1,":",enzyme,"\t\thigh\t\tcutoff",cutoff,"\t\tp", p, "\t\texpected",expected,"\t\tabundance",Table[enzyme][i],"\t\tactual",int(math.ceil(Table[enzyme][i]/(N[i]/float(norm))))
                
                else:
                    print "sample",i+1,":",enzyme,"\t\tlow\t\tcutoff",cutoff,"\t\tp", p, "\t\texpected",expected,"\t\tabundance",Table[enzyme][i],"\t\tactual",int(math.ceil(Table[enzyme][i]/(N[i]/float(norm))))
    

    copy | embed

    0 comments - tagged in  posted by dgg32 on Aug 23, 2010 at 8:47 p.m. EDT
  • batch rename, ls | python rename.py
    #!/usr/bin/env python
    
    import shutil
    import sys
    import re
    
    rx_ori = re.compile(r'\d+_(fs_img2.+)')
    for line in sys.stdin:
        ori = rx_ori.match(line.rstrip())
        
        if ori:
            #print ori.group(1)
            
            shutil.copy2(line.rstrip(), ori.group(1))
    
        
    

    copy | embed

    0 comments - tagged in  posted by dgg32 on Aug 23, 2010 at 7:49 p.m. EDT
  • euler_problem_297
    #!/usr/bin/env python
    found = 0
    F = list()
    F.append(1)
    F.append(2)
    i = 0
    #print F
    
    Z = list()
    Z.append(1)
    Z.append(2)
        
        
    while found == 0:
    
        newValue = F[i] + F[i+1]
        
        
        if newValue >= 10**17:
            F.append(newValue)
            found = 1
        else:
            F.append(newValue)
            newSum = Z[i] + Z[i+1] + F[i] - 1
            Z.append(newSum)
            i += 1
            
    
    def sumZ(n):
        
        if n in F:
            return Z[F.index(n)]
        else:
            lastF = 0
            i = 1
            found = 0
            while found == 0:
                print i,n,len(F),F[len(F)-1]
                if F[i] > n:
                    lastF = F[i-1]
                    
                    #print lastF
                    found = 1
                i += 1
                            
            return sumZ(lastF) + sumZ(n-lastF) + n - lastF
            
    Value = 10**17-1
    print sumZ(Value)
    

    copy | embed

    0 comments - tagged in  posted by dgg32 on Aug 17, 2010 at 7:34 a.m. EDT
  • euler_project_267
    FindMinimum[(9*Log[10] - 1000*Log[1 - f])/Log[(1 + 2 f)/(1 - f)], {f, 
      0.0001, 1}]
    N[1 - CDF[BinomialDistribution[1000, 1/2], 431], 12]
    

    copy | embed

    0 comments - tagged in  posted by dgg32 on Aug 16, 2010 at 7:07 p.m. EDT
  • euler_project_29
    #!/usr/bin/env python
    from sets import Set
    
    collect = Set()
    for a in range(2,101):
        for b in range(2,101):
            collect.add(a**b)
            
    print len(collect)
        
    

    copy | embed

    0 comments - tagged in  posted by dgg32 on Aug 15, 2010 at 5:20 p.m. EDT
  • euler_project_286
    #!/usr/bin/env python
    
    def what_pro(q):
        MasterHash = {0:1}
        for throw in range(1,51):
            newHash = {}
        
            for key in MasterHash.keys():
            
            #score!
                score = key + 1
                probability = MasterHash[key] * (1-throw/float(q))
                if score in newHash:
                    newHash[score] += probability
                else:
                    newHash[score] = probability
                    
                score = key
                probability = MasterHash[key] * (throw/float(q))
                if score in newHash:
                    newHash[score] += probability
                else:
                    newHash[score] = probability
        
            MasterHash = newHash
        
        return MasterHash[20]
    
    
    l = 52
    u = 53
    step = 0
    m = 0
    while step <= 100:
        m = (l+u)/(float(2))
        
        pro = what_pro(m)
        
        if 0.02 > pro:
            u= m
        elif 0.02 < pro:
            l = m
        step += 1
        print m, pro,l,u
    print m
    
    #print what_pro(52.5)
    

    copy | embed

    0 comments - tagged in  posted by dgg32 on Aug 14, 2010 at 5:08 p.m. EDT
  • euler_project_280
    #!/usr/bin/env python
    
    
    
        
    
    # direction: 0 : up, 1 : down, 2 : left, 3 : right
    
    #print Begin.position
    #0: position
    #1: upper
    #2: lower
    #3: loaded
    MasterHash = {((2,2),(0,0,0,0,0),(1,1,1,1,1),0):1}
    
    sum = 0
    for steps in range (1,3000):
        newHash = {}
        
        
        for key in MasterHash.keys():
            possibleDirs = [1,1,1,1]
           
            if key[0][0] == 0:
                possibleDirs[0] = 0
            if key[0][0] == 4:
                possibleDirs[1] = 0
            if key[0][1] == 0:
                possibleDirs[2] = 0
            if key[0][1] == 4:
                possibleDirs[3] = 0
                
            
            
            if possibleDirs[0] == 1:
                
                newkey = list(key)
                
                probability = MasterHash[key] / float(possibleDirs.count(1))
                newpro = (newkey[0][0] - 1, newkey[0][1])
                newkey[0] = newpro
                if newkey[3] == 1 and newkey[0][0] == 0 and newkey[1][newkey[0][1]] == 0:
                    newkey[3] = 0
                    l = list(newkey[1])
                    l[newkey[0][1]] = 1
                    newkey[1] = tuple(l)
                    
                if newkey[1] == (1,1,1,1,1):
                    sum += probability * steps
                elif tuple(newkey) in newHash:
                    
                    newHash[tuple(newkey)] += probability
                else:
                    
                    newHash[tuple(newkey)] = probability
               
    # direction: 0 : up, 1 : down, 2 : left, 3 : right
    
    #print Begin.position
    #0: position
    #1: upper
    #2: lower
    #3: probability
    #4: loaded
    
            if possibleDirs[1] == 1:
                
                newkey = list(key)
                
                probability = MasterHash[key] / float(possibleDirs.count(1))
                newpro = (newkey[0][0] + 1, newkey[0][1])
                newkey[0] = newpro
                if newkey[3] == 0 and newkey[0][0] == 4 and newkey[2][newkey[0][1]] == 1:
                    newkey[3] = 1
                    l = list(newkey[2])
                    l[newkey[0][1]] = 0
                    newkey[2] = tuple(l)
                    
    
                if tuple(newkey) in newHash:
                    
                    newHash[tuple(newkey)] += probability
                else:
                    
                    newHash[tuple(newkey)] = probability
                    
    
    # direction: 0 : up, 1 : down, 2 : left, 3 : right
    
    #print Begin.position
    #0: position
    #1: upper
    #2: lower
    #3: probability
    #4: loaded
                
            if possibleDirs[2] == 1:
                
                newkey = list(key)
                
                probability = MasterHash[key] / float(possibleDirs.count(1))
                newpro = (newkey[0][0], newkey[0][1] - 1)
                newkey[0] = newpro
                if newkey[3] == 0 and newkey[0][0] == 4 and newkey[2][newkey[0][1]] == 1:
                    newkey[3] = 1
                    l = list(newkey[2])
                    l[newkey[0][1]] = 0
                    newkey[2] = tuple(l)
                elif newkey[3] == 1 and newkey[0][0] == 0 and newkey[1][newkey[0][1]] == 0:
                    newkey[3] = 0
                    l = list(newkey[1])
                    l[newkey[0][1]] = 1
                    newkey[1] = tuple(l)
                    
                if newkey[1] == (1,1,1,1,1):
                    sum += probability * steps
                elif tuple(newkey) in newHash:
                    
                    newHash[tuple(newkey)] += probability
                else:
                    
                    newHash[tuple(newkey)] = probability
                
                
                
            if possibleDirs[3] == 1:
                
                newkey = list(key)
                
                probability = MasterHash[key] / float(possibleDirs.count(1))
                newpro = (newkey[0][0], newkey[0][1] + 1)
                newkey[0] = newpro
                if newkey[3] == 0 and newkey[0][0] == 4 and newkey[2][newkey[0][1]] == 1:
                    newkey[3] = 1
                    l = list(newkey[2])
                    l[newkey[0][1]] = 0
                    newkey[2] = tuple(l)
                elif newkey[3] == 1 and newkey[0][0] == 0 and newkey[1][newkey[0][1]] == 0:
                    newkey[3] = 0
                    l = list(newkey[1])
                    l[newkey[0][1]] = 1
                    newkey[1] = tuple(l)
                    
                if newkey[1] == (1,1,1,1,1):
                    sum += probability * steps
                elif tuple(newkey) in newHash:
                    
                    newHash[tuple(newkey)] += probability
                else:
                    
                    newHash[tuple(newkey)] = probability
    
        
        MasterHash = newHash
        #print steps, len(MasterHash)
        #print len(MasterHash)
    """
        for newkey in newHash.keys():
            print newkey[0],newkey[1],newkey[2],newkey[3], newHash[newkey]
    """
    print sum 
    

    copy | embed

    0 comments - tagged in  posted by dgg32 on Aug 14, 2010 at 8:41 a.m. EDT
  • euler_project_19
    #!/usr/bin/env python
    
    def cYearCode (n):
        result_d4 = n / 4
        result_m7 = n % 7
        return (result_d4 + result_m7) % 7
    
    def cDayCode (n):
        return (n % 7)
    
    monthcode = [0,3,3,6,1,4,6,2,5,0,3,5]
    day = ["sun", "mon", "tue", "wed","thu","fri","sat"]
    
    year = 2000
    month = 3
    date = 1
    
    code = (cYearCode (year - 1900) +monthcode[month] + cDayCode(1)) % 7
            
    if (year - 1900) % 4 == 0 and month <= 1:
        code -= 1
    print day[code]
    
    
    """
    sum = 0
    for year in range(1901,2001):
        for month in range(12):
            code = (cYearCode (year - 1900) +monthcode[month] + cDayCode(1)) % 7
            
            if (year - 1900) % 4 == 0 and month <= 1:
                code -= 1
            if code == 0:
                sum += 1
                
    print sum
    """
    

    copy | embed

    0 comments - tagged in  posted by dgg32 on Aug 11, 2010 at 3:26 p.m. EDT
  • euler_project_18_67
    #!/usr/bin/env python
    
    import sys
    
    numbers = []
    
    for line in sys.stdin:
        numbers.append([int(x) for x in line.rstrip().split(" ")])
        
    MasterHash = {(0, 0):numbers[0][0]}
    
    for line in range(len(numbers)):
        if line != 0:
            prev = numbers[line-1]
            now = numbers[line]
            newHash = {}
            for i in range(len(now)):
                if i == 0:
                    tempsum = now[i] + MasterHash[(line-1,i)]
                    newHash[(line,i)] = tempsum
                elif i == len(now) - 1:
                    tempsum = now[i] + MasterHash[(line-1,i-1)]
                    newHash[(line,i)] = tempsum
                else:
                    tempsum1 = now[i] + MasterHash[(line-1,i)]
                    tempsum2 = now[i] + MasterHash[(line-1,i-1)]
                    newHash[(line,i)] = max(tempsum1,tempsum2)
            MasterHash = newHash
                
                
    print max([MasterHash[key] for key in MasterHash.keys()])
    

    copy | embed

    0 comments - tagged in  posted by dgg32 on Aug 11, 2010 at 2:54 p.m. EDT
  • euler_project_298
    #!/usr/bin/env python
    
    def RobinResponse(call, Mem):
        newMem = Mem
        if call not in newMem:
            if len(newMem) == 5:
                newMem.pop(0)
            
            newMem.append(call)
        
        return newMem
    
    
    def LarryResponse(call, Mem):
        newMem = Mem
        if call not in newMem:
            if len(newMem) == 5:
                newMem.pop(0)
            
        else:
            newMem.remove(call)    
        
        newMem.append(call)
            
        return newMem
        
        
    def NormalizeLarry (LarryMem, RobinMem):
        outlaw = 5
        newLarryMem = []
        
        for i in range(len(LarryMem)):
            found = 0
            
            for j in range(len(RobinMem)):
                if LarryMem[i] == RobinMem[j]:
                    found = 1
                    newLarryMem.append(j)
                    
            if found == 0:
                newLarryMem.append(outlaw)
                outlaw += 1
        
        return newLarryMem
    
    
    MasterHash = {((),0):1}
    
    for round in range(50):
        
        
        newHash = {}
        for key in MasterHash.keys():
            for call in range(10):
                RobinPoint = 0
                LarryPoint = 0
                LarryMem = list(key[0])
                RobinMem = range(len(LarryMem))
                
                
                if call in RobinMem:
                    RobinPoint = 1
                if call in LarryMem:
                    LarryPoint = 1
                    
                LarryMem= LarryResponse(call, LarryMem)
                
                RobinMem = RobinResponse(call, RobinMem)
                
                LarryMem = NormalizeLarry(LarryMem, RobinMem)
                
                newDiff = key[1] + LarryPoint - RobinPoint
                if (tuple(LarryMem), newDiff) in newHash:
                    newHash[(tuple(LarryMem), newDiff)] += MasterHash[key]
                else:
                    newHash[(tuple(LarryMem), newDiff)] = MasterHash[key]
        MasterHash = newHash
        print "round",round,"MasterHash.size",len(MasterHash)
        
        
    sum = 0
        
    for key in MasterHash.keys():
        
        sum += MasterHash[key]*abs(key[1])
    print sum/float(10**50)
        
    
        
    

    copy | embed

    0 comments - tagged in  posted by dgg32 on Aug 10, 2010 at 5:24 p.m. EDT
  • correct txt format subtitle
    #!/usr/bin/env python
    
    
    
    
    import sys
    import re
    
    
    """
    {8078}{8161} 5:25 325 24,85
    
    {45272}{45351} 30:12 1812 24,98
    
    {54078}{54153} 36:04 2164 24,98
    """
    
    offset = sys.argv[1]
    
    rx_time = re.compile(r'{(\d+)}{(\d+)} (.+)')
    
    for line in sys.stdin:
        time = rx_time.match(line)
        
        if time:
            output =  "{"+ str(int(time.group(1)) + int(offset) ) + "}{" + str(int(time.group(2)) + int(offset) ) + "} " + time.group(3).rstrip()
            
    
            print output
    

    copy | embed

    0 comments - tagged in  posted by dgg32 on Aug 02, 2010 at 5:11 p.m. EDT
  • euler_project_290
    #include <iostream>
    #include <QMap>
    
    qulonglong digitsum(qulonglong n)
    {
        qulonglong sum = 0;
        while (n > 0)
        {
            sum += n % 10;
            n /= 10;
        }
        return sum;
    }
    
    qulonglong carry(qulonglong product)
    {
    
        return product / 10;
    }
    
    qulonglong diff (qulonglong original, qulonglong m_137_plus_carry)
    {
        qulonglong d_original = digitsum(original);
        qulonglong d_m_137_plus_carry = digitsum(m_137_plus_carry);
        return d_original - d_m_137_plus_carry;
    }
    
    qulonglong ge_wei_cha(qulonglong n, qulonglong m_137_plus_carry)
    {
        qulonglong ge_of_m_137_plus_carry = m_137_plus_carry % 10;
        return n - ge_of_m_137_plus_carry;
    }
    
    int main()
    {
        //diff, carry, amount
        QMap <qulonglong, QMap <qulonglong, qulonglong> > diff_carry_amount;
        QMap <qulonglong, qulonglong> map;
        map.insert(0,1);
        diff_carry_amount.insert(0,map);
        QMap <qulonglong, QMap <qulonglong, qulonglong> >::iterator diff_carry_amount_it;
        QMap <qulonglong, qulonglong>::iterator carry_amount_it;
        qulonglong sum = 0;
    
        for (int position = 1; position <=18; position++)
        {
    
            //std::cout << "position " << position << std::endl;
            QMap <qulonglong, QMap <qulonglong, qulonglong> > temp;
            for (int digit = 0; digit <= 9; ++digit)
            {
                //qulonglong smallsum = 0;
                for (diff_carry_amount_it = diff_carry_amount.begin(); diff_carry_amount_it != diff_carry_amount.end(); ++diff_carry_amount_it)
                {
                    for (carry_amount_it = diff_carry_amount_it.value().begin(); carry_amount_it != diff_carry_amount_it.value().end(); ++carry_amount_it)
                    {
                        //std::cout << "carry_amount_it.key " << carry_amount_it.key()  << " carry_amount_it.value " << carry_amount_it.value() << std::endl;
    
                        if (diff(digit,137*digit+carry_amount_it.key()) == -diff_carry_amount_it.key() && digit != 0)
                        {
                            //smallsum += carry_amount_it.value();
                            sum += carry_amount_it.value();
                        }
    
    
    
                        int c = carry(137*digit + carry_amount_it.key());
                        int d =  ge_wei_cha(digit, 137*digit + carry_amount_it.key())+ diff_carry_amount_it.key();
                       // std::cout <<digit*137+ carry_amount_it.key() << " " << digit << " " << c << " " << d << std::endl;
                        QMap <qulonglong, QMap <qulonglong, qulonglong> >::iterator found = temp.find(d);
                        if (found == temp.end())
                        {
                            QMap <qulonglong, qulonglong> smap;
                            smap.insert(c,carry_amount_it.value());
                            temp.insert(d,smap);
                        }
                        else
                        {
                            QMap <qulonglong, qulonglong>::iterator found2 = found.value().find(c);
                            if (found2 == found.value().end())
                            {
                                found.value().insert(c,carry_amount_it.value());
                            }
                            else
                            {
                                found2.value() += carry_amount_it.value();
                            }
                        }
                    }
                }
                //std::cout << "position " << position << " digit: " << digit << " smallsum " << smallsum << std::endl;
            }
            diff_carry_amount = temp;
        }
    /**
        QMap <int, int> result= diff_carry_amount.value(0);
    
        sum = result.size();
    */
    
        std::cout << "Total: " << sum + 1<< std::endl;
        return 0;
    }
    

    copy | embed

    0 comments - tagged in  posted by dgg32 on Aug 02, 2010 at 6:12 a.m. EDT
  • binary search
    #!/usr/bin/env python
    
    import sys
    
    phonebook = [2,3,5,8,11,17]
    #phonebook = [2,5,8]
    wanted = int(sys.argv[1])
    spacesize = len(phonebook)
    found = 0
    start = 0
    
    while found == 0:
        if len(phonebook) == 1:
            if phonebook[0] != wanted:
                print "wanted not found in phonebook"
                
            else:
                print "wanted found","where:",start
            
            found = 1
        else:
            midindex = int(len(phonebook) / 2)
            print "minindex",midindex
    
            if wanted > phonebook[midindex]:
                phonebook = phonebook[midindex:]
                start += midindex
            elif wanted < phonebook[midindex]:
                phonebook = phonebook[:midindex]
                 
            else:
                found = 1
                print "wanted found","where:",start+midindex
            
    

    copy | embed

    0 comments - tagged in  posted by dgg32 on Jul 20, 2010 at 6:09 a.m. EDT
  • significa
    #!/usr/bin/env python
    
    import sys
    import re
    
    from collections import defaultdict
    import math
    
    
    #usage: cat file1 file2 | python significa.py >> output.txt
    # p > e: x < y
    # p < e: x > y
    
    def factorial(n):
        f = 1
        for i in range(1,n+1):
            f = f * i
        return f
    
    
    def proba(x, N1, y, N2, epsi, name):
        #print "x:",x,"y:",y
        p = 0
    
        for i in range(0, y + 1):
                
            expo = i*math.log10(N2/float(N1)) + math.log10(factorial(x+i)) - (math.log10(factorial(x)) + math.log10(factorial(i)) + (x + i+ 1)* math.log10(1+N2/float(N1)))
                #print expo
            p = p + 10**expo
    
        if p < epsi or p > (1 - epsi):
            print name,": x and y are significantly different. p = ",p
        else:
            #pass
            print name,": not a big deal, p = ",p
    
    N = list()
    Column = -1
    Table = defaultdict(list)
    rx_contig = re.compile(r'Base\s+(\d+)')
    rx_item = re.compile(r'^(.+?)\t\t(\d+)\t\t.+')
    rx_total = re.compile(r'total.+')
    
    epsi = 0.05
    epsi = float(sys.argv[1])
    
    for line in sys.stdin:
        contig = rx_contig.match(line)
        total = rx_total.match(line)    
        item = rx_item.match(line)
        
        if contig:
            Column = Column + 1
            N.append(int(contig.group(1)))
        elif total:
            pass
        elif line == "":
            pass
        elif item:
            #print line
            
            name = item.group(1)
            amount = int(item.group(2))
            
            if name in Table.keys():
    
                    Table[name][Column] = amount
                    
            else:
                if Column == 0:
                    Table[name].append(amount)
                    Table[name].append(0)
                else:
                    Table[name].append(0)
                    Table[name].append(amount)
                    
    
    #print Table
    for key in Table.keys():
        x = Table[key][0]
        y = Table[key][1]
        
        proba(x, N[0], y, N[1], epsi, key)
    

    copy | embed

    0 comments - tagged in  posted by dgg32 on May 26, 2010 at 11:41 a.m. EDT
  • winflat
    #!/usr/bin/env python
    
    import sys
    import math
    
    def proba(x, N1, y, N2):
        p = float (0)
        for i in range(0, y + 1):
            p = p + math.factorial(x+i) * (N2/float(N1))**i/float(math.factorial(x)*math.factorial(i)*(1+N2/float(N1))**(x+i+1))
        print "P( y <= ",y," | x = ",x," ) =",p
        print "P( y >= ",y," | x = ",x," ) =",1-p
    
    def sign(x, N1, N2, sig):
        previous = float(0)
        p = float(0)
        i = 0
        while p <= sig and i <= N2:
            previous = p
            p = p + math.factorial(x+i) * (N2/float(N1))**i/float(math.factorial(x)*math.factorial(i)*(1+N2/float(N1))**(x+i+1))
            
            i = i + 1
        
        if p < sig:
            print "error, no value is found"
        else:
            print "sig value",sig,"is between x=",i-2," (p=",previous,") and x=",i-1," (p=",p,")."
    
    x= 0
    y =0
    N1 = 0
    N2 = 0
    sig = 0
    if sys.argv[1] == "-sig":
        x = int(sys.argv[2])
        N1 = int(sys.argv[3])
        N2 = int(sys.argv[4])
        sig = float(sys.argv[5])
    elif sys.argv [1] == "-proba":
        x = int(sys.argv[2])
        N1 = int(sys.argv[3])
        y = int(sys.argv[4])
        N2 = int(sys.argv[5])
    
    
    if x > N1:
        print "x is bigger than N1"
    elif y > N2:
        print "y is bigger than N2"
    elif sig > 1:
        print "sig is bigger than 1"
    elif sig == 0:
        proba(x,N1,y,N2)
    elif sig != 0:
        sign(x, N1, N2, sig)
    

    copy | embed

    0 comments - tagged in  posted by dgg32 on May 26, 2010 at 4:54 a.m. EDT
  • export and import tabular data for sector chart
    Export ["/Users/dgg32/Downloads/test2.dat", {{a, 1, 1}, {b, 2, 3}, {c,
        3, 4}}]
    
    data = Import["/Users/dgg32/Downloads/test2.dat", "Table"];
    names = data[[All, 1]]
    numbers = data[[All, {2, 3}]]
    SectorChart [numbers, SectorOrigin -> {Automatic, 1}, 
     ChartLabels -> Placed[names, "RadialCallout"]]
    

    copy | embed

    0 comments - tagged in  posted by dgg32 on May 08, 2010 at 5:47 p.m. EDT
Sign up to create your own snipts, or login.