The count
function returns the number of times a given sequence of characters appears in a string.
DNA encodes biological information as a chain of nucleotides, just as computers encode digital information with 0's and 1's. The four nucleotides in DNA are adenine, guanine, cytosine, and thymine, abbreviated as A
, G
, C
, and T
.
We can represent a strand of DNA as a string.
We can then use the count
function to count its nucleotides.
Given a dna
sequence, print the percentage of the nucleotides that are either guanine (G
) or cytosine (C
). This metric is called the GC content of a DNA sequence.
Use the len
function to get the length of the DNA sequence, and use the count
function to count the number of G
and C
characters in the dna
string. Don't forget to cast your output as a string!
Given a dna
sequence, print the ratio between A
and T
nucleotides (adenine and thymine) and C
and G
nucleotides (cytosine and guanine). This metric is called the AT/GC ratio of a DNA sequence.
in
operatorThe in
operator returns True
if a certain string is a substring of another string, and False
otherwise.
Assume if the DNA sequence TAGCGGT
is in both parents' DNA, their child will have black hair. If the sequence is only in one of the parents' DNA, the child will have brown hair, and if the sequence is in neither of the parents' DNA, the child will have blonde hair.
Given the DNA sequence of a mother
and father
, print either black
, brown
, or blonde
based on the expected hair color of their child.
not in
operatorThe not in
operator can be used to determine if a string does not contain another string.
Given two strings str1
and str2
, print the characters that are in str1
that are not in str2
.
The replace
function replaces all occurrences of the given substring with the replacement string.
You have a necklace of white and black beads represented as a string of w
and b
characters. Think about replacing each black bead with a white bead, and each white bead with a black bead followed by a new white bead.
Replacing black with white beads, then white with a black-white bead sequence, does not solve the problem correctly. After the first replacement, all of the new white beads will be converted back into a black bead followed by a white bead.
Instead, you need to use an intermediate letter to store where black beads were.
You have a necklace of white and black beads represented as a string. For the given number of replacements
, replace each black bead with a white bead, and each white bead with a black and white bead, like in the previous problem. Print out the final necklace.
Hint: Use a loop to repeat the replacement process. Repeatedly perform the string replacement explained above.
The format
function inserts values into a string.
Various templates to the format
function can quickly perform string manipulation tasks like adding commas into a number.
You can use a for
loop to iterate on strings. In the case of a string, the iteration variable is a copy of each character in the string.
Given two strings a
and b
, use a for
loop to count the number of letters in a
that also appear in b
. For every letter in a
, check if the letter is in b
with the in
operator.
The program below iterates to each letter of forward
and adds it to the beginning of reverse
. The resulting string has the characters of the original string in reverse order.
Given a name
, reverse it and then print it out.
A palindrome is a word that is spelled the same forward as backward, like "level" and "racecar". Given a lowercase word
, print palindrome
if it is a palindrome, and print not palindrome
if it is not a palindrome.
Given a string quote
, add a space between every character, then print out the new quote.