Skip to content
🤔 AI effective tips collection 🧠
question:Write a python function to analyze a given set of DNA sequences from multiple ancient species and determine the percentage similarity between each pair of sequences. The function should take a list of DNA sequences as input, where each sequence is a string consisting of the characters 'A', 'T', 'C', and 'G'. The function should return a dictionary with keys as tuple pairs of indices representing the sequences being compared, and values as the percentage similarity between these sequences. For simplicity, assume all sequences have the same length. The percentage similarity is calculated as the number of matching characters at the same position in both sequences, divided by the total length of the sequence, and then multiplied by 100.Input:- A list of DNA sequences (strings).Output:- A dictionary with tuple keys and float values representing the percentage similarity.Example:```pythonsequences = ["ATCG", "ATGG", "CTAG"]# The function should return:# {# (0, 1): 75.0,# (0, 2): 50.0,# (1, 2): 50.0# }```In the example, sequence 0 and sequence 1 have 3 matching characters ('A', 'T', 'G'), so their similarity is 75.0%. Sequence 0 and sequence 2 have 2 matching characters ('A', 'T'), so their similarity is 50.0%. Similarly, sequence 1 and sequence 2 have 2 matching characters ('T', 'G'), resulting in 50.0% similarity.

answer:Here's the Python function to analyze the similarity between DNA sequences:```pythondef analyze_dna_sequences(sequences): similarity_dict = {} sequence_length = len(sequences[0]) for i in range(len(sequences)): for j in range(i+1, len(sequences)): matches = sum(1 for a, b in zip(sequences[i], sequences[j]) if a == b) similarity = (matches / sequence_length) * 100 similarity_dict[(i, j)] = similarity return similarity_dict```This function takes a list of DNA sequences as input and returns a dictionary with tuple keys representing the indices of the sequences being compared, and float values representing the percentage similarity between those sequences.Here's how the function works:1. The function initializes an empty dictionary `similarity_dict` to store the similarities.2. It gets the length of the first sequence, assuming all sequences have the same length.3. It uses two nested loops to iterate over all pairs of sequences.4. For each pair of sequences, it calculates the number of matching characters using the `zip` function and a generator expression.5. It calculates the percentage similarity by dividing the number of matches by the sequence length and multiplying by 100.6. It stores the similarity value in the `similarity_dict` dictionary, using a tuple of the indices of the sequences being compared as the key.7. Finally, it returns the `similarity_dict` dictionary.Note that this implementation assumes that all sequences have the same length. If this is not the case, you would need to modify the code to handle sequences of different lengths.

question:Write a python function to analyze the cleanliness ratings of various restaurants in York and return the names of those that have a rating below a certain threshold. As a restaurant inspector with a love for York's rich history, I want to ensure our local establishments uphold the city's proud standards. The function should take a list of dictionaries as input, where each dictionary represents a restaurant. Each dictionary contains the following keys: `"name"` (string), `"rating"` (integer), and `"address"` (string). The function should also take an integer `threshold` as a second parameter.Your function should return a list of strings, where each string is the name of a restaurant with a cleanliness rating below the given threshold.Example:```pythonrestaurants = [ {"name": "The Old York Inn", "rating": 85, "address": "123 York St"}, {"name": "Medieval Munch", "rating": 70, "address": "456 Castle Rd"}, {"name": "Viking Veg", "rating": 92, "address": "789 Viking Way"}, {"name": "Roman Roasts", "rating": 65, "address": "321 Roman Rd"}]threshold = 80# Expected output: ["Medieval Munch", "Roman Roasts"]```This function will help maintain the historical charm of York by ensuring our eateries meet high standards of cleanliness!

answer:def analyze_restaurant_cleanliness(restaurants, threshold): below_threshold = [] for restaurant in restaurants: if restaurant["rating"] < threshold: below_threshold.append(restaurant["name"]) return below_threshold

question:Write a python function to parse a list of strings containing merger and acquisition (M&A) news articles and return a list of company names mentioned in each article. Each string in the list represents a single news article. The company names are always capitalized and have no special characters or numbers. Assume that each word in the article is separated by a space.Input: A list of strings, where each string is a news article.Output: A list of lists, where each inner list contains company names mentioned in the corresponding article.Example:```pythonarticles = [ "TechCorp has acquired SoftWareCo in a deal worth billions", "MegaCorp and SuperInc are in talks for a possible merger", "StartUp is looking for investments from BigBank and SmallFinance"]# Expected output:# [# ["TechCorp", "SoftWareCo"],# ["MegaCorp", "SuperInc"],# ["StartUp", "BigBank", "SmallFinance"]# ]```Note: You can assume that any capitalized word in an article is a company name, and there are no other capitalized words (like at the start of sentences) except for names.

answer:def parse_articles(articles): result = [] for article in articles: companies = [] words = article.split() for word in words: if word.isalpha() and word.isupper(): companies.append(word) result.append(companies) return result

question:Write a python function to determine whether a given list of integers contains any supernatural sequences that sum to a specific target number. A supernatural sequence is defined as a contiguous subsequence of the list. The input will be a list of integers and an integer target. The output should be a boolean value: `True` if such a supernatural sequence exists, otherwise `False`.Example:Input: `nums = [1, 2, 3, 4, 5]`, `target = 9`Output: `True`Explanation: The subsequence `[2, 3, 4]` sums up to 9.Input: `nums = [1, 2, 3, 4, 5]`, `target = 15`Output: `False`Explanation: There is no contiguous subsequence that sums up to 15.

answer:Here's the Python function to determine if a given list of integers contains a contiguous subsequence (supernatural sequence) that sums up to a specific target number:```pythondef has_supernatural_sequence(nums, target): current_sum = 0 start = 0 for end in range(len(nums)): current_sum += nums[end] while current_sum > target: current_sum -= nums[start] start += 1 if current_sum == target: return True return False```Explanation:1. The function takes two arguments: `nums` (a list of integers) and `target` (the target sum).2. We initialize two variables: `current_sum` to keep track of the running sum, and `start` to keep track of the starting index of the current subsequence.3. We iterate through the list using a variable `end` as the index.4. In each iteration, we add the current element `nums[end]` to `current_sum`.5. If `current_sum` becomes greater than `target`, we subtract elements from the beginning of the current subsequence until `current_sum` becomes smaller than or equal to `target`. This is done using a `while` loop that subtracts `nums[start]` from `current_sum` and increments `start`.6. If `current_sum` becomes equal to `target`, it means we have found a supernatural sequence, so we return `True`.7. If the loop completes without finding a supernatural sequence, we return `False`.The time complexity of this solution is O(n), where n is the length of the input list `nums`. This is because we iterate through the list once, and the operations inside the loop (adding/subtracting elements) take constant time. The space complexity is O(1) since we only use a few extra variables and don't create any additional data structures that scale with the input size.

question:Write a python function to calculate the total cost of electricity consumption for a household over a given period. The function should take a list of daily electricity consumption values (in kilowatt-hours, kWh) and a fixed rate per kWh (in cents) as inputs. It should return the total cost in dollars, rounded to two decimal places. Inputs:- A list of integers or floats representing daily electricity consumption (e.g., `[30, 28.5, 32, 29, 31.5]`).- A float representing the cost per kWh in cents (e.g., `12.5`).Expected Output:- A float representing the total electricity cost in dollars, rounded to two decimal places.Example:```pythondaily_consumption = [30, 28.5, 32, 29, 31.5]rate_per_kwh = 12.5# The function should return 22.7```

answer:def calculate_electricity_cost(daily_consumption, rate_per_kwh): total_consumption = sum(daily_consumption) rate_per_kwh_in_dollars = rate_per_kwh / 100 total_cost = total_consumption * rate_per_kwh_in_dollars return round(total_cost, 2)

question:Write a python function to determine the order of creditors based on their priority in an insolvency case. The function should take a list of tuples as input, where each tuple contains the name of the creditor and their priority level (an integer, with a lower number indicating a higher priority). The function should return a list of creditor names ordered from highest priority to lowest priority.Input:- A list of tuples, where each tuple consists of: - A string representing the creditor's name. - An integer representing the creditor's priority level.Output:- A list of strings, representing creditor names, sorted by their priority levels from highest to lowest.Example:```pythoncreditors = [("Creditor A", 2), ("Creditor B", 1), ("Creditor C", 3)]# Expected output: ["Creditor B", "Creditor A", "Creditor C"]```Note: Assume all priority levels are unique.

answer:def order_creditors(creditors): # Sort the list of tuples based on the priority level sorted_creditors = sorted(creditors, key=lambda x: x[1]) # Extract the creditor names in the sorted order ordered_names = [creditor[0] for creditor in sorted_creditors] return ordered_names

Released under the MIT License.

has loaded