Question

1. use python List to Dictionary Write a function that has three parameters: a list of unsorted numbers with no duplicates, a

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1)

Python code:

#function lst_to_dict

def lst_to_dict(lst,start,end):

    #initializing dictionar

    dic={}

    #loop from start to end

    for i in range(start,end+1):

        #checking if value is in list

        if(i in lst):

            #adding that value and its index to dictionary

            dic[i]=lst.index(i)

        #if not present

        else:

            #adding that value and None to dictionary

            dic[i]=None

    #returning dictionary

    return dic

#calling lst_to_dict and printing output

print(lst_to_dict([2,1,10,0,4,3],3,10))

Screenshot:

> 1 #function lst_to_dict 2 v def lst_to_dict(lst, start, end): 3 #initializing dictionar 4 dic={} 5 #loop from start to end

Output:

Your Output (stdout) 1 {3: 5, 4: 4, 5: None, 6: None, 7: None, 8: None, 9: None, 10: 2}

2)

Python code:

#function target_sum

def target_sum(lst,target):

    #looping all elements

    for i in sorted(lst):

        #looping all elements

        for j in sorted(lst):

            #checking if sum of 2 elements are target

            if(i+j)==target:

                #returining their index

                return(lst.index(i),lst.index(j))

#calling target_sum and printing output

print(target_sum([1,2,3,4,5,9,15],7))

Screenshot:

w NP #function target_sum 2 v def target_sum(lst, target): #looping all elements 4 V for i in sorted (lst): 5 #looping all el

Output:

Your Output (stdout) 1 (1, 4)

Add a comment
Know the answer?
Add Answer to:
1. use python List to Dictionary Write a function that has three parameters: a list of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python 3 Write a function named starCounter that takes three parameters: 1. a dictionary named starDictionary....

    Python 3 Write a function named starCounter that takes three parameters: 1. a dictionary named starDictionary. Each key in starDictionary is the name of a star (a string). Its value is a dictionary with two keys: the string 'distance' and the string 'type'. The value associated with key 'distance' is the distance from our solar system in light years. The value associated with key 'type' is a classification such as 'supergiant' or 'dwarf'. 2. a number, maxDistance 3. a string,...

  • use python Write a function that will sort a given list using merge sort. You must...

    use python Write a function that will sort a given list using merge sort. You must use the merge sort algorithm (but may be recursive or iterative). The function will take a list as an input and return a sorted version of the list (you may assume it will be a list of integers). The method signature must be merge_sort(lst).

  • Python: Create a function count_target_in_list that takes a list of integers and the target value then...

    Python: Create a function count_target_in_list that takes a list of integers and the target value then counts and returns the number of times the target value appears in the list. Write program in that uses get_int_list_from_user method to get a list of 10 numbers, then calls the count_target_list method to get the count then prints the number of times the target was found in the list. def get_int_list_from_user(): lst=[] y = int(input("Enter number of numbers")) for x in range(y): lst.append(int(input("Enter...

  • Python recursive function: Given an ordered list L. A permutation of L is a rearrangement of...

    Python recursive function: Given an ordered list L. A permutation of L is a rearrangement of its elements in some order. For example (1,3, 2) and (3, 2, 1) are two different permutations of L=(1,2,3). Implement the following function: def permutations (lst, low, high) The function is given a list 1st of integers, and two indices: low and high (lows high), which indicate the range of indices that need to be considered The function should return a list containing all...

  • USE PYTHON / RECURSION METHOD Fibonacci Dictionary Function Name: fibtionary Parameters: num (int) Returns: dictionary (key:...

    USE PYTHON / RECURSION METHOD Fibonacci Dictionary Function Name: fibtionary Parameters: num (int) Returns: dictionary (key: int, value: int) Description: You're done with stats, but you still have other math homework to go! You're currently learning about the Fibonacci sequence in math class. The Fibonacci sequence is a series of numbers in the pattern 112 3 5 8 13 21 ..., where the next number is found by adding up the two numbers before it. Write a function that takes...

  • Write a Python function fun3(e) to update a list of numbers e such that the first...

    Write a Python function fun3(e) to update a list of numbers e such that the first and last elements have been exchanged. The function needs to also return multiplication of elements of e. You should assume that the list e has at least 2 elements. Example: >>>lst = [4, 1, 2, -1] >>>fun3(list) -8 >>>lst [-1, 1, 2, 4] *We are an intro to CS class so please do not use anything too advance or fancy. Stick to basics. Right...

  • python Question 7: Give a recursive implement to the following function: def split_by_sign (lst, low, high)...

    python Question 7: Give a recursive implement to the following function: def split_by_sign (lst, low, high) The function is given a list 1st of non-zero integers, and two indices: low and high (low Shigh), which indicate the range of indices that need to be considered. The function should reorder the elements in Ist, so that all the negative numbers would come before all the positive numbers. Note: The order in which the negative elements are at the end, and the...

  • Python 5. Write a function named grade_components that has one parameter, a dictionary. The keys for...

    Python 5. Write a function named grade_components that has one parameter, a dictionary. The keys for the dictionary are strings and the values are lists of numbers. The function should create a new dictionary where the keys are the same strings as the original dictionary and the values are tuples. The first entry in each tuple should be the weighted average of the non-negative values in the list (where the weight was the number in the 0 position of the...

  • PYTHON PROBLEM Define a function generate_kv_strings( dictionary ) that creates and return a list where the...

    PYTHON PROBLEM Define a function generate_kv_strings( dictionary ) that creates and return a list where the element is a string representing a key-value pair in the format "key: value". Your solution must use a list comprehension. Hint: Remember that you need to convert each key and value into a str before concatenating them, in case they are of a different type. Example Usage: states = {'Florida': 'FL', 'Oregon': 'OR', 'California': 'CA', 'New York': 'NY', 'North Carolina': 'NC'} numbers = {'four':...

  • using python Write a function that takes a dictionary and a list of keys as 2...

    using python Write a function that takes a dictionary and a list of keys as 2 parameters. It removes the entries associated with the keys from the given dictionary Your function should not print or return anything. The given dictionary should be modified after the function is called Note: it means that you can't create another dictionary in your function. Since dictionaries are mutable, you can modify them directly def remove_keys (dict, key_list) "" "Remove the key, value pair from...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT