Homework Solution

Conditionals

Problems to be Submitted (18 points)

  1. (10 points)
    original = raw_input('Enter numeric string: ') 
    base = int(raw_input('What base is the number? ')) 
    place = 1 
    total = 0 
    original = original[ : :-1] # reverse for convenience 
    for symbol in original: 
        if symbol.isdigit( ): 
            value = int(symbol) 
        else: 
            value = 10 + ord(symbol) - ord('A') 
        total += value * place 
        place *= base 
    print total 
    
    (click to see it run live)
  2. (4 points)

    There is a bit of care needed with the precise stopping condition, and floating-point arithmeitc is imprecise. But the most accurate attempt, which works on both positive and negative step sizes is the following:

    result = [start + k * step for k in range(int(math.ceil((stop-start)/step)))]

  3. (4 points)

    rnaList = [rnaCode[dnaCode.index(base)]] for base in dna]


Extra Credit

  1. (2 points)

    uniq = [ orig[i] for i in range(len(orig)) if orig.index(orig[i]) == i ]


Last modified: Sunday, 26 February 2017