Question

In: Computer Science

Homework 5: Lists Please show code in Pycharm The task is to create the following three...

Homework 5: Lists

Please show code in Pycharm

The task is to create the following three functions to demonstrate techniques for working with lists:

  1. greatestPair(x), which accepts a list of numbers x and returns the index position of the greatest adjacent pair. For example, if the list [4, 3, 8, 0] was passed to your function, it would return 1, as that the values 3 and 8 form the greatest adjacent pair in the list. Your function should return None if passed an empty list.
  2. isSorted(x) that takes a list of numbers and returns True if the list is sorted (either smallest-first or largest-first) and False otherwise. Your function should return None if passed an empty list.
  3. intersection(x,y) that takes two lists x and y as parameters and returns a new list containing the intersection of the two parameters (all elements that are common to both x and y). The returned list should not contain duplicate values.

Solutions

Expert Solution

1.def greatestPair(x):
    if len(x)==0:
        return None
    elif len(x)==1:
        return 0
    (a,b) = (x[0],x[1])
    sol = 0
    for i in range(len(x)-1):
        if max(a,b)>max(x[i],x[i+1]) and min(a,b)>min(x[i],x[i+1]):
            (a,b) = (x[i],x[i+1])
            sol = i
    return i
      
2. def isSorted(x):
    if(x==sorted(x) or x==sorted(x,reverse=True)):
        return True
    else:
        return False
  
3. def intersection(x,y):
    lis = []
    for i in x:
        if i in y and i not in lis:
            lis.append(i)
    return lis


Related Solutions

Please provide HTML code for the following: - Create a page that lists a set of...
Please provide HTML code for the following: - Create a page that lists a set of audio files and shows their duration - Create a page that lists a set of video files and plays a different video when you click on the play icon
Language: C and comment code please PThreads The goal of this homework is to show an...
Language: C and comment code please PThreads The goal of this homework is to show an understanding of pthreads and C. Assignment: Write a C program to use the bitwise operations with each bitwise operation contained within its own thread. Each thread should take 1 parameter and will only modify global values. First, create global integers x and y, then, in the main function you will read in values for x and y from the user and then create the...
java code: adds a new regular task, delete a task , show all tasks, and show...
java code: adds a new regular task, delete a task , show all tasks, and show regular tasks, mark a task as important (possibly through ID), complete task, show all completed tasks, show important tasks. I also need a UML diagram for the code update: The "task" is like something in a to do list. example) task 1. name: get carrots important: no completed: yes. you can run my code as an example if needed
Data Structures Homework – Singly Linked Lists Create a singly linked that represents a school. The...
Data Structures Homework – Singly Linked Lists Create a singly linked that represents a school. The school has multiple classes. Each class has a different number of students. class student { Long ID; string Name; string Address; float grades[3]; student *below; }; class Node // the node represents a class in school { int ID; int NoOfStudents; int NoOfQuizes; student *t;// a linked list of students is allocated dynamically Node *Next; }; class school { string Name; Node *Head; int...
Code should be Written in C Using initialization lists, create 5 one-dimensional arrays, one for each...
Code should be Written in C Using initialization lists, create 5 one-dimensional arrays, one for each of these: hold the names of the people running in the election hold the names of the subdivision hold the vote counts in the Brampton subdivision for each candidate hold the vote counts in the Pickering subdivision for each candidate hold the vote counts in the Markham subdivision for each candidate Use the data from the example below. Your C program should take the...
Code should be Written in C Using initialization lists, create 5 one-dimensional arrays, one for each...
Code should be Written in C Using initialization lists, create 5 one-dimensional arrays, one for each of these: hold the names of the people running in the election hold the names of the subdivision hold the vote counts in the Brampton subdivision for each candidate hold the vote counts in the Pickering subdivision for each candidate hold the vote counts in the Markham subdivision for each candidate Use the data from the example below. * Your C program should take...
Please use Java Eclipse and show code/output Please create a program that determines when a good...
Please use Java Eclipse and show code/output Please create a program that determines when a good day to go to the beach is. Please use the users input and its returning output. If the weather is 70 degree or greater, the program should say yes it is a good day to go If the weather is less than 70 degrees to say no the weather is not a good day to go
Using C# Windows App Form Create a simple calculator using +,-,*,/ Please show code GUI Code...
Using C# Windows App Form Create a simple calculator using +,-,*,/ Please show code GUI Code for calculator menus radio button input text boxes
1. You will need your ticker code (show your ticker code on your homework and include...
1. You will need your ticker code (show your ticker code on your homework and include a printout of the data!) for stock prices for this question. Use your ticker code to obtain the closing prices for the following three time periods to obtain three data sets: March 2, 2019 to March 16, 2019 Data set A March 17, 2019 to March 31, 2019 Data set B April 1, 2019 to April 17, 2019 Data set C To ensure equal...
Code the following in bash and run. Please show full code and don’t forget to comment...
Code the following in bash and run. Please show full code and don’t forget to comment your code. Make a code that takes any list of numbers and calculates and displays the mean, median and mode.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT