Пример #1
0
def compress(s):
    '''Returns a new string that is a compression og the given string, s.'''
    if s[0] == '1':
        return '0' * COMPRESSED_BLOCK_SIZE + reduce(
            add, (map(zero_fill,
                      (list(map(numToBinary, num_breaker(consec_all(s))))))))
    return reduce(add,
                  (map(zero_fill,
                       (list(map(numToBinary, num_breaker(consec_all(s))))))))
Пример #2
0
def compress(S):
    """Takes a binary string and returns a new binary string that is the input's run-to-length encoding"""
    if S == '':
        return ''
    elif S[0] == '1':
        return '0' * COMPRESSED_BLOCK_SIZE + reduce(
            add,
            map(binaryPadder, map(numToBinary, compressHelp3(
                compressHelp2(S)))))
    else:
        return reduce(
            add,
            map(binaryPadder, map(numToBinary,
                                  compressHelp3(compressHelp2(S)))))
Пример #3
0
def compress(S):
    '''Takes as input a binary string, s, of length 64 and returns another binary string output. The output 
    string should be a run-length encoding of the input string'''
    if S == '':
        return ''
    elif S[0] == '1':
        return '0' * COMPRESSED_BLOCK_SIZE + reduce(
            add,
            map(binaryLength, map(numToBinary, compressBreak(
                compressList(S)))))
    else:
        return reduce(
            add,
            map(binaryLength, map(numToBinary,
                                  compressBreak(compressList(S)))))
Пример #4
0
def bestWord(Rack):
    '''Returns the list of a word alongside its wordScore of
    the highest wordScore available'''
    scorelist = scoreList(Rack)
    if scorelist == []:
        return ['', 0]
    return reduce(lambda x, y: x if x[1] > y[1] else y, scorelist)
Пример #5
0
def tower_reduce(n):
    def power(x, y):
        return y**x  #instead of x to the y do y to the x - draw it out

    if (n == 0):
        return 1
    return reduce(power, [2] * n)
Пример #6
0
def tower_reduce(n):
    def power(x, y):
        return y**x

    if n == 0:
        return 1
    return reduce(power, [2] * n)
Пример #7
0
def bestWord(Rack):
    ''' assuming Rack is a list of lowercase letters, return a single list with two elements, the word with the highest score, and that score'''
    if Rack == []:
        return []
    elif scoreList(Rack) == []:
        return ['', 0]
    return reduce(greaterThan, scoreList(Rack))
Пример #8
0
def compress(s):
    '''Takes a binary string s and returns a run length encoded binary string'''
    def compresshelper(s,c):
        '''Returns seperated segments of 0's and 1's in string s'''
        if len(s)==1:
            return [c]
        else:
            if s[0]==s[1]:
                return compresshelper(s[1:],c+1)
            else:
                return [c]+compresshelper(s[1:],1)   
    def compresshelper2(y):
        '''Returns the length of each divded segment in list y'''
        return map(lambda x: numToBinary(x),y)
    z=compresshelper2(compresshelper(s,1))
    def filler(z):
        '''Pads z with 0's or 1's so it has length of 5'''
        if len(z)>COMPRESSED_BLOCK_SIZE:
            w=binaryToNum(z)-31
            return '11111'+'00000'+filler(numToBinary(w))
        else:
            q=5-len(z)
            return q*'0'+z
    z=map(filler,z)
    t=reduce(lambda x,y: x+y, z)
    if s[0]=='1':
        return '00000'+t
    else:
        return t
Пример #9
0
def catenate(str_list):
    '''Assume str_list is a list of strings.
    Return a single string, their catenation.'''
    if str_list == []:
        return ''
    else:
        return reduce(lambda s, t: s + t, str_list)
Пример #10
0
def e(x):
    "taylor expansion with x values"
    y = list(range(1, x + 1))
    q = map(fact, y)
    z = map(inverse, q)
    result = 1 + reduce(add, z)
    return result
Пример #11
0
def e(n):
    '''Approximates the mathematical value e using a Taylor expansion.'''
    numberList = range(1, n + 1)
    factorialList = map(math.factorial, numberList)
    inverseList = map(inverse, factorialList)
    addedList = reduce(add, inverseList)

    return 1 + addedList
Пример #12
0
def mean(L):
    """Returns the mean of a given list"""
    if L==[]:
        return 0
    else:
        count=len(L)
        quant=reduce(add,L)
        return quant/count
Пример #13
0
def e(n):
    """approximates the mathematical value e using a Taylor polynomial
    expressed as the sum 1 + 1/1! + 1/2! + 1/3! + ... 1/n! where n is an integer given by the user
    """
    lst=range(0,n+1) #creating a list of all number 0 to n
    x= map(factorial,lst) #taking the list and making all the numbers factorials
    y = map(inverse, x) #inversing the list of the factorials
    return reduce(add,y) #adding the list of all the inverse factorials together and returning that value
def e(n):
    """This function returns the summation of 1+1/n!"""
    Step1 = list(range(1, n + 1))
    Step2 = list(map(factorial, Step1))
    Step3 = list(map(inverse, Step2))
    Step4 = reduce(add, Step3)
    Step5 = 1 + Step4
    return Step5
Пример #15
0
def bestWord(Rack):
    """takes as input a Rack as above and returns a list with two elements: 
    the highest possible scoring word from thatRack followed by its score"""
    scorelist = scoreList(Rack)
    if scorelist == []:
        return ['', 0]
    return reduce(
        lambda x, y: x if x[1] > y[1] else y, scorelist
    )  #the lambda function will compare the number values of x and y and return x if it is bigger
Пример #16
0
def e(n):
    '''approximates the mathematical value e using a Taylor expansion.'''
    list_of_numbers = range(1, n + 1)

    def add(x, y):
        return x + y

    return float(
        1 + reduce(add, map(inverse, map(math.factorial, list_of_numbers))))
Пример #17
0
def prblm2(n):
    myList= range(1,n)
    print(myList)
    #squareList = map(makeSquare, myList)
    #print(squareList)
    #listSum = sum(squareList)
    #return listSum

    return reduce(sum, map(makeSquare, range(1,n)))
Пример #18
0
def bestWord(Rack):
    '''uses scoreList and only outputs the word with the highest score.
    if theres a tie, do whatever to choose just one.'''
    if list_of_words_created(Dictionary, Rack) == []:
        return ['', 0]
    contenders = scoreList(Rack)
    return reduce(lambda x, y: x if x[1] > y[1] else y, contenders)


#print(bestWord(['g', 'y','p']))
Пример #19
0
def save_and_quit(database):
    handle=open("musicrecplus.txt", "w")
    for key, value in sorted(database.items()):
        if value == []:
            s = key + ':\n'
        else:            
            s = key + ':' + reduce(lambda s, t: s + ',' + t, value) +'\n'
            handle.write(s)
    handle.close() 
    sys.exit(0)           
Пример #20
0
def prime(n):
    '''takes argument n and checks whether the number is prime'''
    #first map applies the divides function which checks if n is divisible by any number excluding itself and 1
    #second map typecasts all the booleans to be string so I can apply len
    #third map applies the len() function to each string(boolean)
    #using reduce I add up all of the lengths
    #then I check whether they add up to 5*the length of the whole range itself, this means that n is not divisible by any number
    #since the above method doesn't work for 2 i just added an or statement
    if n < 2:
        return False
    return n == 2 or reduce(add,map(len,map(str,map(divides(n),range(2,n))))) == 5*len(range(2,n))
Пример #21
0
def bestWord(Rack):
    '''Returns the highest scoring string with the score'''
    scorelist = scoreList(Rack)
    if scorelist == []:
        return ['',0]
    
    def better_word(x,y):
        if x[1]>y[1]:
            return x 
        return y 
    return reduce(better_word, scorelist)
Пример #22
0
def bestWord(rack):
    contenders = scoreList(rack)
    if contenders == []:
        return ['', 0]

    def betterWord(x, y):
        if x[1] > y[1]:
            return x
        return y

    return reduce(betterWord, contenders)
Пример #23
0
def prime(n):
    '''detects whether a number is prime or not.'''
    ##is n divisible by any number other than 1 and n??
    '''if True in map(divides(n),range(2,n)) or n == 0 or n == 1: 
        ## divides is taking the variables as argument but it needs to be n and divided by the variables.##
        return False
    return True '''
    possible_divisors = range(2, math.ceil(math.sqrt(n) + 1))
    f = divides(n)
    composite_lst = map(f, possible_divisors)
    return not reduce(sum, composite_lst)
Пример #24
0
def compress(s):
    """param s: string to compress
    count the runs in s switching
    from counting runs of zeros to counting runs of ones
    return compressed string"""
    def compress_help(s, c):
        if s == "":
            return ""
        temp = lengthFix(numToBinary(countRun(s, c, MAX_RUN_LENGTH, 0)))
        return temp + compress_help(s[countRun(s, c, MAX_RUN_LENGTH, 0):],
                                    '0' if c == '1' else '1')

    return reduce(add, map(numToBinary(compress_help(s, '0'))))
Пример #25
0
 def helper(start, finish):
     if (finish - start == 1):  #only 1 long
         return start
     if (finish - start == 0):  #no range
         return 0
     if (finish - start <
             5):  #if length is less than 5, return the power (I don't
         #know why, but this just works
         return reduce(lambda a, b: a ^ b, range(start, finish))
     else:
         #gives the end of the worker list fed into the recursive function
         #this function makes the correct list to look at in the function
         return helper(start, start / 4 * 4 + 4) ^ helper(
             finish / 4 * 4, finish)
Пример #26
0
def save_and_quit(database):
    '''When the user chooses to quit, the current database
    is written to the musicrecplus.txt, replacing old contents (if
    any). If the file exists, its contents are overwritten. If the file doesn't
    exist, it is created with the proper contents.'''
    handle = open("musicrecplus.txt", "w")
    for key, value in sorted(database.items()):
        if value == []:
            s = key + ':\n'
        else:
            s = key + ':' + reduce(lambda s, t: s + ',' + t, value) + '\n'
            handle.write(s)
    handle.close()
    sys.exit(0)
Пример #27
0
def bestWord(Rack):
    """
    Returns a list with two elements: the highest possible scoring word
    and the score for that word.
    
    Inputs: Rack list of lowercase letters
    """
    # Check for bad input
    if not getScores(Rack):
        return ['', 0]
    maxScore = reduce(max, getScores(Rack))
    scoreLst = scoreList(Rack)
    # Helper function to find the best word from the max score
    def bestWordHelper(mx, sclst):
        if sclst[0][1] == mx:
            return  sclst[0][0]
        return bestWordHelper(mx, sclst[1:])
    return [bestWordHelper(maxScore, scoreLst), maxScore]
Пример #28
0
def answer3(start, length):
    #list of workers is always from length^2-l+start to that +l
    list_of_workers = [(length * (length - l) + start,
                        length * (length - l) + start + l)
                       for l in range(length, 0, -1)]

    def helper(start, finish):
        if (finish - start == 1):  #only 1 long
            return start
        if (finish - start == 0):  #no range
            return 0
        if (finish - start <
                5):  #if length is less than 5, return the power (I don't
            #know why, but this just works
            return reduce(lambda a, b: a ^ b, range(start, finish))
        else:
            #gives the end of the worker list fed into the recursive function
            #this function makes the correct list to look at in the function
            return helper(start, start / 4 * 4 + 4) ^ helper(
                finish / 4 * 4, finish)

    #the new list is inputted from the helper method, from start to finish
    new_xor = [helper(start, finish) for start, finish in list_of_workers]
    return reduce(lambda a, b: a ^ b, new_xor)
Пример #29
0
def span(lst):
    """returns the diff beteen the max and min numbers in a list"""
    return reduce(max, lst) - reduce(min, lst)
Пример #30
0
def sum_of_squares(n):
    """takes as input a positive integer a and returns the sum
    1^2 + 2^2 + 3^2...n^2"""
    return reduce(add, map(square, range(1, n + 1)))