In: Computer Science
Using basic Python
2. Given the following dictionary:
speed ={'jan':47, 'feb':52, 'march':47, 'April':44, 'May':52, 'June':53, 'july':54, 'Aug':44, 'Sept':54}
Get all values from the dictionary and add it to a new list called speedList, but don’t add duplicates values to the new list.
Your code should print out:
speedList [47, 52, 44, 53, 54]
3. Given the following list of numbers:
numberList = [10, 20, 33, 46, 55, 61, 70, 88, 95]
First iterate the list and print only those numbers, which are divisible of 5. Then iterate the list and print only those numbers that are odd.
Your code should print out:
Numbers from numberList that are only divisible of 5 are:
10
20
55
70
95
Numbers from numList that are odd are:
33
55
61
95
4. For the given list:
item_list= [ 2, 6, 13, 17, 22, 25, 29, 31, 38, 42, 47, 55, 59, 63, 68, 71, 77, 84, 86, 90, 99]
Write python code to do a binary search over the given list, where the search value is input by a user (positive integers from 1 to 100 only). Print out the results if the search found the number or not.
HINT: Our author provided the pseudocode in Figure 3.18 from Chapter 3 of our text for solving this problem.
/* PLEASE ALIGN CODE USING SCREENSHOT */
2. Given the following dictionary:
speed ={'jan':47, 'feb':52, 'march':47, 'April':44, 'May':52, 'June':53, 'july':54, 'Aug':44, 'Sept':54}
Get all values from the dictionary and add it to a new list called speedList, but don’t add duplicates values to the new list.
Your code should print out:
speedList [47, 52, 44, 53, 54]
Ans.)
speed ={'jan':47, 'feb':52, 'march':47, 'April':44,
'May':52, 'June':53, 'july':54, 'Aug':44, 'Sept':54}
speedList = []
for key,value in speed.items():
if value not in speedList:
speedList.append(value)
print("speedList
{}".format(speedList))
3. Given the following list of numbers:
numberList = [10, 20, 33, 46, 55, 61, 70, 88, 95]
First iterate the list and print only those numbers, which are divisible of 5. Then iterate the list and print only those numbers that are odd.
Ans.)
numberList = [10, 20, 33, 46, 55, 61, 70, 88, 95]
print("Numbers from numberList that are only divisible of 5
are:")
for num in numberList:
if(num%5 == 0):
print(num)
print("Numbers from numList that are odd are:")
for num in numberList:
if(num%2):
print(num)
4. For the given list:
item_list= [ 2, 6, 13, 17, 22, 25, 29, 31, 38, 42, 47, 55, 59, 63, 68, 71, 77, 84, 86, 90, 99]
Write python code to do a binary search over the given list, where the search value is input by a user (positive integers from 1 to 100 only). Print out the results if the search found the number or not.
Ans)
item_list= [ 2, 6, 13, 17, 22, 25, 29, 31, 38, 42, 47,
55, 59, 63, 68, 71, 77, 84, 86, 90, 99]
key = int(input("Enter Value to be search: "))
l = 0
r = len(item_list) - 1
flag = False
while l <= r:
mid = int(l + (r - l)/2);
# Check if x is present at mid
if item_list[mid] == key:
flag = True
break;
# If x is greater, ignore left half
elif item_list[mid] < key:
l = mid + 1
# If x is smaller, ignore right half
else:
r = mid - 1
if(flag):
print("{} found at index {}".format(key,mid+1))
else:
print("Not Found")
/* PLEASE UPVOTE */