Answered step by step
Verified Expert Solution
Question
1 Approved Answer
# 1. # Explain what's wrong with the code below and rewrite it so it works correctly. # Rewrite the code so that the code
# 1. # Explain what's wrong with the code below and rewrite it so it works correctly. # Rewrite the code so that the code runs without error counter = 0 def increment(): counter += 1 increment() # 3.Given two strings gene1 and gene2, use string manipulation to create a new string called dna which combines the two genes, then repeat the pattern once, so the value will be "gggtttggttt" gene1 = 'ggg' gene2 = 'ttt' # 5.Complete missing function headers (replace only ??) for the functions below so that the following print statements produce the output specified in the comments. You can't change or add anything else in the code # def student_info(?? # print("Age: {} Country: {}".format(age, country)) # student_info(20, 'Belgium') # prints Age: 20 Country: Belgium # student_info(24) # prints Age: 24 Country: France # 6. Create a dictionary nucleotides from the DNA sequence "acgtaact", where the keys are the nucleotides (a,c,g,t) and the values are the frequency of each nucleotide. Print the dictionary as shown in the sample output below. Note that the output is sorted by frequency. Your code should work for any similar DNA sequence. Output: frequency of g is 1 frequency of c is 2 frequency of t is 2 frequency of a is 3 dna = list("acgtaact") # 8. Give the statement Given the following variable locations, give the statement that prints h on board. location = [34, [4.5, 12, [45, 6.7, 'ship']], 'wreck'] # 9. Input Validation Loop Write an input-validation loop to get the cost of one apple as a valid float, then print the cost of 10 apples. Your code should produce the following output (user input follows the "Cost of apples:" prompt): Cost of apples: 5 :) Invalid input cost: Apples with invalid input cost: -1 Cost must be between 0 and 3 Apples: 5 Cost must be between 0 and 3 Apples: 1.510 Cost of apples is $15.00 # 11.Refactor the code below (rewrite it to work the same way, but better), improve it based on the best practices you've been taught, including the Single Responsibility Principle (SRP) for function design and class inheritance. We provide refactored startup code. Movement and pickup methods to complete the course. Also, briefly explain how you refactored it and why. How is the code better in your new version? # Refactor the following code using inheritance class Car(): def __init__(self, speed=30, load=2): self.speed = speed self.load = load def print_speed(self, type): if type == "Sports": print("Sports Car Speed : {}".format(self.speed*10)) elif type == "Pick-up": print("Pick-up Car Speed : {}".format(self.speed - self.load)) Car().print_speed("Sports") Car().print_speed("Pick-up") # Explanation # 12. The following code doesn't work. You can read the programmer's intent in the comments below. Explain why it doesn't work. Then, fix it so that the intent is fulfilled. The "%" here is the modulo operator. Example 3 % 2 is 1, 8 % 5 is 3. # Start with a pattern of 6 0s: 0 0 0 0 0 0 pattern = 6*[0] #: 0 0 1 0 0 1 0 0 1 ): if index % 3 == 2: pattern. append(1) print(pattern) # Explanation: # Solution: # start with a pattern of six zeros : 0 0 0 0 0 0 pattern = 6 * [0] # add 1 after every two zeros : 0 0 1 0 0 1 0 0 1 # 13.Given the movie names below, write the expression to generate the output. In these 6 expressions, you must use the string at least once. You may not use actual string element values in expressions Output: a. Man b. Who knows too much c. Knows d. This is my name. The title of this movie has 6 words f. The title of this movie has 25 characters movie = "The man who knew too much" # 14. Write a function to count number of digits that are # even, odd or 0 before the decimal point # Replace the ?? so that the code runs without error def count_digits(number): return ?? number = input("Enter a number ") print(count_digits(number)) # 16. Explain the error and provide the code Which error is thrown when the file cannot be opened? Python statements are recommended to prevent errors from being raised. # 17.Given the variable primes defined below contains primes in random order, use a list comprehension to generate a list of numbers between 1 and 25 that are not prime. Next, use a list comprehension to generate a sorted list of the squares of the numbers in the variable primes. List of non-prime numbers: [1,4,6,8,9,10,12,14,15,16,18,20,21,22,24] prime numbers_sorted squares of prime numbers: [4,9, 25,49,121,169,289,361,529] primes = [11, 17, 3, 7, 19, 2, 5, 23, 13] # 18. Use list comprehension to complete the series.Given the sequence of variables defined below, use a list comprehension to complete the sequence (replacement only??) as shown below. Note that each new number is the sum of the previous two numbers in the sequence # series = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55] # replace only ?? series = [1, 1] # [series.append(??)]
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started