Question

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,...

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.

Solutions

Expert Solution

/* 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 */


Related Solutions

Jan 523.9 Feb 510.23 March 545.65 April 514.23 May 550.71 June 505.34 July 525.34 August 600...
Jan 523.9 Feb 510.23 March 545.65 April 514.23 May 550.71 June 505.34 July 525.34 August 600 Sept. 569.42 October 500.2 Novem. 533.12 December 507.11 What percentage of your bills are within two standard deviations of the mean?
Finish the following Cash Budget CASH BUDGET JAN FEB MAR APRIL MAY JUNE JULY AUG SEPT...
Finish the following Cash Budget CASH BUDGET JAN FEB MAR APRIL MAY JUNE JULY AUG SEPT OCT NOV DEC SALES 200,000 230,000 150000 210,000.00 220,000.00 170,000.00 160,000.00 150,000.00 180,000.00 210,000.00 220,000.00 210,000.00 COLLECTIONS: No cash Sales 30 days 50% 60 days 50% TOTAL CASH RECEIPTS CASH DISBURSEMENTS SUPPLIES 70% PAYABLES/SUPPLIES paid next month(in 30 days) SALARIES/OVER monthly 40000 INTEREST monthly 1000 DIVIDENDS monthly 1000 TAXES quarterly 1200 TOTAL DISBURSEMENT 42000 0 1200 0 0 0 0 0 0 0 0...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT