Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
2.4 kB
4
Indexable
#1
def to_hex_string(data):
    ans=''
    for i in data:
        if  int(i) < 10:
            ans+=str(i)
        elif int(i) ==10:
            ans+='a'
        elif int(i) ==11:
            ans+='b'
        elif int(i) ==12:
            ans+='c'
        elif int(i) ==13:
            ans+='d'
        elif int(i) ==14:
            ans+='e'
        elif int(i) ==15:
            ans+='f'
        else:
            ans+=''
    return ans

print(to_hex_string([3, 15, 6, 4]))

#2
def count_runs(flat_data):
    runs={}
    
    for i in flat_data:
        if i in runs:
            runs[i] = runs[i]+ 1
        else:
            runs[i] = 1
    return len(runs)
    

print(count_runs([15, 15, 15, 4, 4, 4, 4, 4, 4]))

#3
def encode_rle(flat_data):
    l=len(flat_data)
    i=0
    count=0
    res=[]
    
    while i<l:
        curr=flat_data[i]
        res.extend( [flat_data.count(curr),curr] )
        i+= flat_data.count(curr)

    print(res)
   
#3.1 
encode_rle([15, 15, 15, 4, 4, 4, 4, 4, 4])

def encode_rle2(flat_data):
    l=len(flat_data)
    result=[]
    i=0 
    while i<l:
        curr=flat_data[i]
        count=0
        j=i
        while j<l and flat_data[i] == flat_data[j] :
            count+=1
            j+=1
            
        result.extend([count, curr ])
        i+=count
            
    return result
    
print(encode_rle2([15, 15, 15, 4, 4, 4, 4, 4, 4]))
    
    
#4
def get_decoded_length(rle_data):
    length=0
    for i in range(0, len(rle_data), 2):
        length+= rle_data[i]
    return length
    
print(get_decoded_length([3, 15, 6, 4]))


#5
def decode_rle(rle_data):
    result=[]
    
    for i in range(0, len(rle_data), 2):
        result.extend( [rle_data[i+1]] * rle_data[i] )    
    
    return result
    
print(decode_rle([3, 15, 6, 4]))

#6
def string_to_data(data_string):
    result=[]
    
    for i in data_string:
        if 48 <= ord(i) <= 57:
            result.append(int(i))
        elif i=='a' or i=='A':
            result.append(10)
        elif i=='b' or i=='B':
            result.append(11)
        elif i=='c' or i=='C':
            result.append(12)
        elif i=='d' or i=='D':
            result.append(13)
        elif i=='e' or i=='E':
            result.append(14)
        elif i=='f' or i=='F':
            result.append(15)

    return result
        
    
print(string_to_data ("3f64"))