Professional Documents
Culture Documents
solutionsExerciseMaster1 10
solutionsExerciseMaster1 10
1.py
Code
#!/usr/bin/env python3
2.py
Code
#!/usr/bin/env python3
1
3.py
Code
#!/usr/bin/env python3
if userValue == 1:
print(1)
elif userValue == 2:
print(1, '\n', 1, sep='')
else:
print(1, '\n', 1, sep='')
values = [1, 1]
for i in range(userValue - 2):
newValue = sum(values)
print(newValue)
values[0] = values[1]
values[1] = newValue
2
4.py
Code
#!/usr/bin/env python3
import sys
number = int(input('Type a number: '))
if number == 0:
print(1) # 0! = 1
sys.exit() # To avoid an else statement
elif number < 0:
print('The number can not be negative', file=sys.stderr)
sys.exit() # To avoid an else statement
total = 1
print(total)
3
5.py
Code
#!/usr/bin/env python3
# https://en.wikipedia.org/wiki/List_of_prime_numbers#The_first_1000_prime_numbers
primeList = ['2'] # Ta be able to join in the last step the list must contain strings
currentValue = 3
while True:
if prime is True:
# To be able to join in the last step the list must contain strings
primeList.append(str(currentValue))
if len(primeList) == 1000:
print('\n'.join(primeList))
break
4
6.py
Code
#!/usr/bin/env python3
import re
import sys
import random
while True: # There is a 25% risk that we pick a nucleotide that the position already has
if oligoLowercase[randomPosition] != randomNucleotide:
# The oligo string is immutable so we print out two parts
# that have not changed plus the mutated nucleotide
print(oligoLowercase[:randomPosition] + randomNucleotide + \
oligoLowercase[randomPosition + 1:])
break # When we found a different nucleotide we break the while loop
5
7.py
Code
#!/usr/bin/env python3
ingredients = ['bread']
ingredient1 = input('Input first ingredient: ')
ingredients.append(ingredient1)
ingredient2 = input('Input second ingredient: ')
ingredients.append(ingredient2)
ingredient3 = input('Input third ingredient: ')
ingredients.append(ingredient3)
ingredients.append('bread')
print(ingredients)
6
8.py
Code
#!/usr/bin/env python3
import re
import sys
par = [4, 3, 5, 2, 5, 4, 7, 6]
7
9.py
Code
#!/usr/bin/env python3
import sys
import os.path
if len(sys.argv) != 3:
print('The program should be run as 9.py fasta_file sequence_id', file=sys.stderr)
sys.exit()
if not os.path.isfile(sys.argv[1]):
print("The input file doesn't exist", file=sys.stderr)
sys.exit()
i = 0 # row counter
8
10.py
Code
#!/usr/bin/env python3
import sys
import os.path
if len(sys.argv) != 3:
print('The program should be run as 10.py fastq_input_file fasta_output_file', \
file=sys.stderr)
sys.exit()
if not os.path.isfile(sys.argv[1]):
print("The input file doesn't exist", file=sys.stderr)
sys.exit()
i = 0