#datacalculation


Conversions - Decimal, Hexadecimal, and Binary Numbers

This code converts decimal, hexadecimal, and binary numbers into another type of number.

import math ##OTHER METHODS def turnToHexaNum(remainder): if(remainder == '10'): remainder = 'A' elif(remainder == '11'): remainder = 'B' elif(remainder == '12'): remainder = 'C' elif(remainder == '13'): remainder = 'D' elif(remainder == '14'): remainder = 'E' elif(remainder == '15'): remainder = 'F' return remainder ##CONVERSION METHODS def BinaryToDecimal(Binary): Binary = Binary [::-1] arrayBinary = [] index = 0 decimal = 0 for ch in Binary: array = (ch, index) arrayBinary.append(array) index += 1 for num in arrayBinary: decimal += int(num[0]) * int(2 ** num[1]) return decimal def DecimalToBinary(decimal): remainders = '' while decimal > 0: remainder = decimal % 2 decimal = decimal // 2 remainders += str(remainder) return remainders [::-1] def DecimalToHexadecimal(decimal): remainders = '' while(decimal > 16): remainder = str(decimal % 16) decimal = decimal // 16 remainder = turnToHexaNum(remainder) remainders += remainder remainder = str(decimal) remainder = turnToHexaNum(remainder) remainders += remainder return remainders [::-1] def BinaryToHexadecimal(Binary): decimal = BinaryToDecimal(Binary) return DecimalToHexadecimal(decimal) def HexadecimalToDecimal(Hexadecimal): decimal = 0 Hexadecimal = Hexadecimal [::-1] index = 0 for ch in Hexadecimal: if ch == 'A': ch = '10' elif ch == 'B': ch = '11' elif ch == 'C': ch = '12' elif ch == 'D': ch == '13' elif ch == 'E': ch == '14' elif ch == 'F': ch == '15' decimal += int(ch) * (16 ** index) index += 1 return decimal def HexadecimalToBinary(Hexadecimal): decimal = HexadecimalToDecimal(Hexadecimal) return DecimalToBinary(decimal) print('Binary to Decimal: ' + str(BinaryToDecimal('110101010'))) print('Decimal to Binary: ' + DecimalToBinary(426)) print('Decimal to Hexadecimal: ' + DecimalToHexadecimal(426)) print('Binary to Hexadecimal: ' + BinaryToHexadecimal('110101010')) print('Hexadecimal to Decimal: ' + str(HexadecimalToDecimal('1AA'))) print('Hexadecimal to Binary: ' + HexadecimalToBinary('1AA'))

DNA to RNA to Amino Acid Chain

This code makes a random DNA chain of a length given by the user. Then it converts the DNA chain into a RNA chain. Finally, it converts the RNA chain into an amino acid chain and prints out the chain. **The length has to be divisible by 3 because each amino acid is derived from a codon and each codon is 3 RNA bases.


import random length = int(input("Enter the length of the DNA sequence(length had to divisible by 3): ")) while length % 3 != 0: length = int(input("Length has to be divisible by 3. Enter new length: ")) DNA = [] RNA = [] codons = [] aminoAcids = [] def makeDNA(): for x in range(length): num = random.randint(1, 5) if num == 1: DNA.append('A') elif num == 2: DNA.append('C') elif num == 3: DNA.append('T') else: DNA.append('G') def RNAconvertor(): for char in DNA: if char == 'A': RNA.append('U') elif char == 'C': RNA.append('G') elif char == 'T': RNA.append('A') else: RNA.append('C') def codonConvertor(): numOfCodons = 0 for x in range(int(len(RNA) / 3)): codon = RNA[numOfCodons * 3] codon += RNA[(numOfCodons * 3) + 1] codon += RNA[(numOfCodons * 3) + 2] codons.append(codon) numOfCodons += 1 def aminoAcidMaker(): for codon in codons: if codon == 'UUU' or codon == 'UUC': aminoAcids.append('Phe') elif codon == 'UUA' or codon == 'UUG' or codon == 'CUU' or codon == 'CUC' or codon == 'CUA' or codon == 'CUG': aminoAcids.append('Leu') elif codon == 'AUU' or codon == 'AUC' or codon == 'AUA': aminoAcids.append('Ile') elif codon == 'AUG': aminoAcids.append('Met') elif codon == 'GUU' or codon == 'GUC' or codon == 'GUA' or codon == 'GUG': aminoAcids.append('Val') elif codon == 'UCU' or codon == 'UCC' or codon == 'UCA' or codon == 'UCG' or codon == 'AGU' or codon == 'AGC': aminoAcids.append('Ser') elif codon == 'CCU' or codon == 'CCC' or codon == 'CCA' or codon == 'CCG': aminoAcids.append('Pro') elif codon == 'ACU' or codon == 'ACC' or codon == 'ACA' or codon == 'ACG': aminoAcids.append('Thr') elif codon == 'GCU' or codon == 'GCC' or codon == 'GCA' or codon == 'GCG': aminoAcids.append('Ala') elif codon == 'UAU' or codon == 'UAC': aminoAcids.append('Tyr') elif codon == 'UAA' or codon == 'UAG' or codon == 'UGA': aminoAcids.append('Stop') elif codon == 'CAU' or codon == 'CAC': aminoAcids.append('His') elif codon == 'CAA' or codon == 'CAG': aminoAcids.append('Gln') elif codon == 'AAU' or codon == 'AAC': aminoAcids.append('Asn') elif codon == 'AAA' or codon == 'AAG': aminoAcids.append('Lys') elif codon == 'GAU' or codon == 'GAC': aminoAcids.append('Asp') elif codon == 'GAA' or codon == 'GAC': aminoAcids.append('Glu') elif codon == 'UGU' or codon == 'UGC': aminoAcids.append('Cys') elif codon == 'UGG': aminoAcids.append('Trp') elif codon == 'CGU' or codon == 'CGC' or codon == 'CGA' or codon == 'CGG' or codon == 'AGA' or codon == 'AGG': aminoAcids.append('Arg') elif codon == 'AGU' or codon == 'AGC': aminoAcids.append('Ser') elif codon == 'GGU' or codon == 'GGC' or codon == 'GGA' or codon == 'GGG': aminoAcids.append('Gly') makeDNA() RNAconvertor() codonConvertor() aminoAcidMaker() print("The amino acid chain is: " + str(aminoAcids))

Correlation Coefficient of two Stocks

This is a code which will calculate the correlation coefficient of two stocks when given the data. The data is just arrays. Each array has the value of the two stocks at the same time.


import math data = [(170.66, 56.54), (170.95, 56.40), (170.70, 56.10), (169.73, 55.49), (169.18, 55.30), (169.80, 54.83), (169.31, 54.52), (169.11, 54.09), (169.61, 54.29), (168.74, 54.15), (166.38, 53.29), (165.83, 51.83)] security1Squared = [] security2Squared = [] security1security2List = [] average = [] security1Sum = 0 security2Sum = 0 def averageList(randList): numSum = 0 for period in range(0, len(randList)): numSum += randList[period] return numSum/len(randList) for period in range(0, len(data)): security1Sum += data[period][0] security2Sum += data[period][1] security1Squared.append((data[period][0]) ** 2) security2Squared.append((data[period][1]) ** 2) security1security2List.append(data[period][0] * data[period][1]) average.append(security1Sum/len(data)) average.append(security2Sum/len(data)) average.append(averageList(security1Squared)) average.append(averageList(security2Squared)) average.append(averageList(security1security2List)) security1Variance = average[2] - (average[0] ** 2) security2Variance = average[3] - (average[1] ** 2) covariance = average[4] - (average[0] * average[1]) corrCoeff = covariance / math.sqrt(security1Variance * security2Variance) print("The Correlation Coefficient is: " + str(corrCoeff))
1