two_sum is a Python function that takes in a list of integers (elements) and an integer number (num) and returns True if there exist two values in elements that add up to num, otherwise, function returns False. def two_sum(elements: List[int], num: int): for i in range(len(elements)): for j in range(i + 1): if elements[i] + elements[j] == num: return True return False Study above function and answer below questions: A. What is time complexity of two_sum? B. Is it possible to improve on above algorithm performance (in terms of its asymptotic cost)? If yes, describe your algorithm, be detailed as much as possible.
Q: Write a function that takes list and a number (x) as parameters. Assume list contains only numbers,…
A: - We need to solve the number list problem in python.
Q: Write a function high_low_dict(numbers, sep) that takes a list of numbers and an int sep that will…
A: Note: The problem solved in JAVA. ALGORITHM: -> take a ArrayList to add elements and an int type…
Q: Algorithm Efficiency 1- Programming Problem Part 1 Write a function that will have a list as an…
A: the answer is given below:
Q: Write a function that accepts a variable number of string parameters. The function must use list…
A: Answer in step2
Q: Write a function flat_list() which takes a list that contains unknown number of lists. This function…
A: Since Python is weakly typed, you can encounter regular and irregular lists of lists. Every element…
Q: Write a function randomList() that gets three integers as parameters, start, bound and k. This…
A: Python program to generate the k random integer numbers. Find the square root of all the k numbers.…
Q: 6- Write a lambda function that finds the average of arguments a, b, and c and prints the result. 7-…
A: I give the code for both 6 and 7 in Python along with output and code screenshot
Q: Use Standard ML Code: Write a function that returns the factorial of a number. Write a function…
A: The above question is solved in step 2:-
Q: Write a function randomList() that gets three integers as parameters, start, bound and k. This…
A: In Python, to generate a list of random numbers with specified ranges and sizes, random.sample()…
Q: Write a function findMaxTuple() that gets a list of tuples as parameter, finds and returns the tuple…
A: Below is the python program which is implemented by using sum() function. Note: Please use the…
Q: Exercise 3: Single parameter Write a function print_even() which • takes a list of integer numbers…
A: A number is said to be an even number if the number is divisible by 2 and the remainder of the…
Q: Create a function: That takes a list and an integer v, -And returns True if the value v is in the…
A: Code will return the loop will run how many times. And this is the python code.
Q: python code Write a Python function listDictPrime() that gets as parameter a list of integers,…
A: Given Solution Contains: listDictPrime() function. Screenshot of code of listDictPrime() function.…
Q: Sort the list and show the steps in details based on the given tracking table. Show the value of…
A: Hello student, hope you are doing good.
Q: Write a function lastDigits() that takes a list of numbers as parameter, returns a string made of…
A: Required: Write a function lastDigits() that takes a list of numbers as parameter, returns a string…
Q: Part I Implementation In this part you are asked to write functions with python • Define a two…
A: import random def print_table(table): for i in range(n): for j in range(n):…
Q: Write the python function named keep_even_values. This function will have 1 parameter that will be a…
A: iterate from i=0 to len(lst) check if lst[i]%2==0 newList.append(lst[i]) i=i+1…
Q: Write a function primeFac in Python that computes the prime factorization of a number: it accepts a…
A: The Python function to get the list of prime factors of a number greater than 1:Also I have checked…
Q: Write a function toSentence : string list -> string = that takes a string list and concatenates its…
A: Since no programming language is mentioned, I am using python. Code: def toSentence(stringList):…
Q: In OCaml Programming Language, write a function: val prime_separator : int list -> int list * int…
A: val prime_separator : int list -> int list * int list = <fun> that takes an integer list as…
Q: In OCaml Programming Language, write a function:
A: ICAML CODE: let rec cond_dup (lst: 'a list) (f: 'a -> bool): 'a list = match lst with…
Q: Listdiff Use the built-in higher order functions2 and write a non-recursive function val…
A: OCAML programming language Listdiff Use the built-in higher order functions2 and write a…
Q: Write a function that takes a nested list of integers and add up the elements from all of the nested…
A: For nested list, you can iterate over a list and then use sum function or you can iterate over that…
Q: a function maxCylinderVolume that takes a list of floating-point tuples that represent dimensions of…
A: F# system function such as min or methods in the list module such as List.map are not allowed Write…
Q: Write a function evens (numbers) that returns a new list containing just the even elements from…
A: #Creating Functiondef evens(numbers): #Creating list to hold all even numbers list = []…
Q: Write a function lastDigits() that takes a list of numbers as parameter, returns a string made of…
A: The remainder of a number can be found using % operator and this is converted to string and appended…
Q: Please use python for this problem: Please use Python when answering the question: Question…
A: Python is a high level programming language. Guido van Rossum, is the father of Python. Just like…
Q: ) Write a function dropLowest() with one parameter n. 2) n is a list that contains any 5 random…
A: def dropLowest(n): index_Of_min = n.index(min(n)) n.remove(n[index_Of_min]) return…
Q: Write a function flat_list() which takes a list that contains unknown number of lists. This function…
A: new_li.append(ele): append ele in new_li in last position previous: new_li = [1, 2, 3] after…
Q: You and your neighbor have equal sized gardens. Your gardens are represented as 2D-lists, where…
A: Note : Indentation is important in Python programming. -> Code and screenshots of execution with…
Q: 3. Define a function in Python that takes a list as the parameter and returns True if the list…
A: Define a function in Python that takes a list as the parameter and returns True if the list contains…
Q: Questions P1 and P2 are related to the following code segment. Below is the skeleton of a function…
A: def f1(n):#takes a list of integers for i in range(0,len(n)-2):#runs for i=0 to length of n-3…
Q: ut name of new employee : Mike Menu : 1 . Print full list of employees 2 . Insert new employee 3 .…
A: code : #include <iostream>#include <string>using namespace std; class List{public:…
Q: Please answer it in Python Write a function convertir_liste that takes as an argument a list of…
A: Python Program: # Given list of stringsstringList = ['1', '12.3', '4.12'] # Display the given…
Q: Define colorListCircles with 2 parameters Use def to define colorListCircles with 2 parameters The…
A: Python's Turtle module, and he asked to see concentric circles. I thought it would be faster to use…
Q: in phython language Write a function named getOddList(). The function takes one parameter which is…
A: Create a function named getOddList(). It takes one parameter named list. It finds the odd numbers in…
Q: • Create a function which takes a tuple and returns a dictionary which is a histogram to count how…
A: I solve the question give the code along with screenshot of output along with histogram.
Q: def reverse_reversed(items): Create|and return a new list that contains the items in reverse, but so…
A: Actually, python is a easiest programming language. It is a dynamically typed programming language.…
Q: Write a recursive function named Multiply2 that multiples by 2 and prints each element in a given…
A: I have given answer in step2
Q: Write a function countRange() that takes three parameters, a list and two numbers, finds and returns…
A: I have given an answer in step 2.
Q: Code the following in OCaml. EDIT: Write a function that looks for a pair on elements from two…
A:
Q: Circular linked list is a form of the linked list data structure where all nodes are connected as in…
A: struct Node *insertTonull(struct Node *last, int data) { if (last != NULL) return last;…
Q: Write a function sumLength() that takes a list of strings as parameter, finds and returns the total…
A: A list is a collection that holds multiple data items using one variable. The values of the list can…
Q: Write a recursive function named Multiply2 that multiples by 2 and prints each element in a given…
A: def Multiply2(mylist): if len(mylist) == 0: return [] return [mylist[0]*2] +…
Q: Write a function flat_list() which takes a list that contains unknown number of lists. This function…
A: In the following python code , we need to perform the following operations : Define the python…
Q: Write a function randomList() that gets three integers as parameters, start, bound and k. This…
A: Given: Write a function randomList() that gets three integers as parameters, start, bound and k.…
Q: Python code Python Write a function named `freq(l)` that takes a list `l` of numbers and returns…
A: Here is the python3 code: See below step for code and output.
Q: PYTHON CODE Write a function removeRange() that takes a list of integer numbers and two integer…
A: According to the Question below the Solution: Program Output:
Q: should be in python: write a function that receives a list as its only parameter. Inside the…
A: 1. Create a list 2. Making a function remove_duplicates_list(duplist) to remove the duplicate…
Q: Use def to define colorListCircles with 2 parameters The colorListCircles function must draw…
A: Python's Turtle module, and he asked to see concentric circles. I thought it would be faster to use…
two_sum is a Python function that takes in a list of integers (elements) and an integer number (num) and returns True if there exist two values in elements that add up to num, otherwise, function returns False.
def two_sum(elements: List[int], num: int):
for i in range(len(elements)):
for j in range(i + 1):
if elements[i] + elements[j] == num:
return True
return False
Study above function and answer below questions:
A. What is time complexity of two_sum?
B. Is it possible to improve on above
Trending now
This is a popular solution!
Step by step
Solved in 2 steps
- Exercise, maxCylinderVolume F# system function such as min or methods in the list module such as List.map are not allowed Write a function maxCylinderVolume that takes a list of floating-point tuples that represent dimensions of a cylinder and returns the volume of the cylinder that has the largest volume. Each tuple has two floating point values that are both greater than zero. The first value is the radius r and the second value is the height h. The volume of the cylinder is computed using ??2h. The value π is represented in F# with System.Math.PI. If the list is empty, return 0.0. Examples: > maxCylinderVolume [(2.1, 3.4); (4.7, 2.8); (0.9, 6.1); (3.2, 5.4)];;val it : float = 194.3137888> maxCylinderVolume [(0.33, 0.66)];;val it : float = 0.2257988304***python only*** Write a function votes_by_race() which takes a list of dicts as a parameter and returns a dict. For example, given the following list: votes = [{'for': 'A, 'age': 35, 'race': 'w'}, {'for': 'B', 'age': 40, 'race': 'b'}, {'for': 'B', 'age': 20, 'race': 'b'}, {'for': 'A', 'age': 30, 'race': 'a'}, {'for': 'A', 'age': 41, 'race': 'w'}, ] The call votes_by_race(votes) should return the following dict:{"White": 2, 'Black': 2, 'Asian': 1, "Hispanic': 0}python: def traditions_dict(adict): """ Question 6 Given a dictionary that maps a person to a list of their favorite traditions, return a dictionary with the value being the list sorted by the last letter of each tradition. If two traditions have the same last letter, sort by the first letter. HINT: This will require the use of lambda functions Args: adict (dict) Returns: dict >>> traditions_dict({"Jacob": ["The Horse", "Stealing the t", "Midnight Bud"], "Athena": ["Mini Five-Hundred", "Freshman Cake Race", "Buzzweiser Song"], "Liv": ["Freshman Cake Race", "George P. Burdell", "Buzzweiser Song"]}) {'Jacob': ['Midnight Bud', 'The Horse', 'Stealing the t'], 'Athena': ['Mini Five-Hundred', 'Freshman Cake Race', 'Buzzweiser Song'], 'Liv': ['Freshman Cake Race', 'Buzzweiser Song', 'George P. Burdell']} >>> traditions_dict({"Madison": ["The Horse", "Midnight Bud", "Buzzweiser…
- Code in python Given a list, L, the function front_two creates a new list of length 2 out of the first two items in L as follows: In the new list, the second item becomes the first item, and the first item becomes the second. If L contains only one item, the first item in the new list should be the value None. If L is empty, both items in the new list should be value None. The function returns the new list. For example: Test Result L = ['a', 'b', 'c', 'd'] print(front_two(L)) ['b', 'a'] L = [] print(front_two(L)) [None, None] L = [5, 2] print(front_two(L)) [2, 5] L = [7] print(front_two(L)) [None, 7]### Q5: Reduce No Change Python def reduce_no_change(fn, lst, base): """Same as Q4. However, preserve the lst in this problem. Object can be any python type which the input Not Allowed To Import Libraries Args: fn (function): Combination function which takes in two arguments and return an value with the same type as the second argument lst (List): A list of any type base (Object): A value of custom type which fn can handle. Returns: Object: A value after applying fn on lst. >>> reducer = lambda x, y: x + y >>> lst = [1, 2, 3] >>> a = reduce_lst(reducer, lst, 0) >>> a # a = reducer(reducer(reducer(base, lst[0]), lst[1]), lst[2]) 6 >>> lst >>> [1, 2, 3] # we preserve the list """ ### Modify your code here ### Modify your code herea- Write a Python function Salary_levels, that takes a list as an argument and returns the number of the high salaries (salary>=5000), the average salaries(1000<=salary<5000, and the low salaries (salary<1000). You can use the following list : salaries= [500,1070,1450,6500,9000,940,450,400,600,2750,1100,8400,1500,900,1200] b- Use the above function to print the counts of the high, average, and low salaries. N.B: the codes of parts a and b should be included in your answer.
- PYTHON CS1 PROBLEM Define a function that takes a list of lists, glol, as a parameter. You may assume that the inner lists in glol are lists of positive integers. Your function should return True if at least one of the inner lists in glol contains two or more multiples of 10. Otherwise it should return False. For example: If glol = [ ] then your function returns False. If glol = [[ ], [4, 2]] then your function returns False. If glol = [[10, 2, 6, 8], [12, 6], [15, 20, 5]] then your function returns False. If glol = [[10, 2, 6], [12, 6], [10, 2, 70, 15], [15, 10, 5]] then your function returns True as the inner list [10, 2, 70, 15] contains 10 and 70. If glol = [[10, 2, 6], [12, 6], [10, 7, 15], [50, 10, 50, 4, 6]] then your function returns True as the inner list [50, 10, 50, 4, 6] contains 50 twice. 6. If glol = [[10, 20, 6, 90], [12, 6], [ 7, 3, 15], [50, 10, 50, 4, 7]] then your function returns True as the inner lists [50, 10, 50, 4, 7] and [10, 20, 6, 90] contain more…Python please! Generalized image blender function Create a function blend images() which takes multiple RGB images as an input, and outputs a blended image. The function should accept following parameters 1. image list- A Python list of 3D arrays where each 3D array corresponds to an RGB image 2. weight list-A Python list of float values between (0, 1) corresponding to the pixel weight to be given to each image-e.g. [0.2, 0.3, 0.1, 0.4] for 4 images. The sum of the weights should be equal to 1. Test your function against a provided list of 5 images with following weight lists (i.e. 2 blended images) [0.2, 0.2, 0.2, 0.2, 0.2]- blend all 5 images [0.2, 0.3, 0.5]- blend first 3 images NOTE: DO NOT USE pre-existing image blending functions.Please use Python when answering the question: Question 2: numPairs Write a function numPairs that accepts two arguments, a target number and a list of numbers. The function then returns the count of pairs of numbers from the list that sum to the target number. In the first example the answer is 2 because the pairs (0,3) and (1,2) both sum to 3. The pair can be two of the same number, e.g. (2,2) but only if the two 2’s are separate twos in the list. In the last example below, there are three 2’s, so there are three different pairs (2,2) so there are 5 pairs total that sum to 4. Output is shown in the picture:
- The function makeRandomList creates and returns a list of numbers of a given size(its argument). The numbers in the list are unique and range from 1 through thesize. They are placed in random order. Here is the code for the function:def makeRandomList(size):lyst = []for count in range(size):while True:number = random.randint(1, size)if not number in lyst:lyst.append(number)breakreturn lystYou can assume that range, randint, and append are constant time functions. Youcan also assume that random.randint more rarely returns duplicate numbers as therange between its arguments increases. State the computational complexity of thisfunction using big-O notation, and justify your answer. Only analytical work is requiredAlgorithm Efficiency 1- Programming Problem Part 1 Write a function that will have a list as an input, the task of the function is to check if all the elements in the list are unique,( i.e. no repetition of any value has occurred in the list), then the function returns true otherwise it returns false. Your program should include a main method that call the method to test it. (If you would use built-in functions in Python, use lists’ or dictionaries’ methods as we studied their time efficiency in class) 2- Algorithm AnalysisFor the function you implemented in part 1, please calculate T(n), which represents the running time of your algorithm in terms of n. Where n is the length of the list. Then find the order of magnitude of your algorithm (Big O). 3- Algorithms Comparison Please find another algorithm that solves part 1, write the code, calculate T(n) and find Big O. Then compare the efficiency with the algorithm from part1 to determine the more efficient one.Count dominators def count_dominators(items): An element of items is said to be a dominator if every element to its right (not just the one element that is immediately to its right) is strictly smaller than it. By this definition, the last item of the list is automatically a dominator. This function should count how many elements in items are dominators, and return that count. For example, dominators of [42, 7, 12, 9, 13, 5] would be the elements 42, 13 and 5. Before starting to write code for this function, you should consult the parable of "Shlemiel the painter" and think how this seemingly silly tale from a simpler time relates to today's computational problems performed on lists, strings and other sequences. This problem will be the first of many that you will encounter during and after this course to illustrate the important principle of using only one loop to achieve in a tiny fraction of time the same end result that Shlemiel achieves with two nested loops. Your workload…