Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adjusted code.py for readability #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 36 additions & 13 deletions code.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
def function1(a):
b = dict()
for c in a:
if c not in b:
b[c] = 1
def calculate_character_frequency(input_string):
"""
Computes the frequency of each character in the input string.

Parameters:
input_string (str): The string for which character frequency is calculated.

Returns:
dict: A dictionary where keys are characters and values are their corresponding counts in the input string.
"""
frequency_dict = dict()
for char in input_string:
if char not in frequency_dict:
frequency_dict[char] = 1 # Initialize the count if the character is not in the dictionary.
else:
b[c] += 1
return b
frequency_dict[char] += 1 # Increment the count if the character is already in the dictionary.
return frequency_dict

def normalize_character_frequencies(frequency_dict):
"""
Prints the normalized frequency of each character based on their counts from the input dictionary.

Parameters:
frequency_dict (dict): A dictionary where keys are characters and values are their corresponding counts.

def function2(a):
print('freqs')
total = float(sum([a[b] for b in a.keys()]))
for b in a.keys():
print(b + ':' + str(a[b]/total))
Returns:
None: This function prints the frequencies directly to the console.
"""
print('Frequencies:')
total_count = sum(frequency_dict.values()) # Calculate the total count of all characters.

for char, count in frequency_dict.items():
frequency = count / total_count # Calculate the frequency of each character.
print(f'{char}: {frequency:.4f}') # Print each character and its frequency, formatted to 4 decimal places.

function2(function1('ATCTGACGCGCGCCGC'))
# Example usage: calculate fraction of nucleobases in a given string.
input_string = 'ATCTGACGCGCGCCGC'
frequency_dict = calculate_character_frequency(input_string)
normalize_character_frequencies(frequency_dict)