For this problem you must write the functions in a recursive manner (i.e. the function must call itself) – it is not acceptable to submit an iterative solution to these problems.
A. Complete the recursive function gcd(m, n) that calculate the greatest common denominator of two numbers with the following rules:
# If m = n, it returns n
# If m < n, it returns gcd(m, n-m)
# If m > n, it returns gcd(m-n, n) #
def gcd(m,n):
return None # Replace this with your implementation
B. Complete the following function that uses recursion to find and return the max (largest) value in the list u.
# find_max([1, 7, 4, 5] returns 7 # find_ max ([1, 7, 4, 5, 9, 2] returns 9 # def find_max(u):
return None # Replace this with your implementation
C. Complete the following recursive function that returns the zip of two lists u and v of the same length. Zipping the lists should place the first element from each into a new array, followed by the second elements, and so on (see example output).
# zip([1, 2, 3], [4, 5, 6]) returns [1, 4, 2, 5, 3, 6] # def zip(u, v):
return None # Replace this with your implementation
D. Complete the following recursive function that removes all occurrences of the number x from the list nums.
# remove_number(5, [1, 2, 3, 4, 5, 6, 5, 2, 1]) returns [1, 2, 3, 4, 6, 2, 1] # def remove_number(x, nums):
return None # Replace this with your implementation
E. Write a recursive function removeLetter(string, letter) that takes a string and a letter as input, and recursively removes all occurrences of that letter from the string. The function is case sensitive.
Some example test cases are below:
>>> removeLetter("test string", "t")
es sring
>>> removeLetter("mississipi", "i")
mssssp
>>> removeLetter("To be or not to be is a question.", "t")
To be or no o be is a quesion.
In PyCharm
In: Computer Science
For the following, copy the questions into a text file and write your answers for each question.
I. Place the following algorithm time complexities in order from
fastest (least number of comparisons) to the slowest: nlogn, n, n2,
2n, logn, 2n
II. In your own words, explain the two characteristics that a
recursive solution must have.
III. Why are divide-and-conquer algorithms often very efficient in
terms of time complexity?
IV. Write in your own words 3 different examples of cases where
people around you are giving up their privacy to use some service
in exchange for a benefit. State for each example what data is
being collected and what benefits the user receives in
exchange.
In: Computer Science
C++ Program
Create Semester class (.h and .cpp file) with the
following three member variables:
▪ a semester name (Ex: Fall 2020)
▪ a Date instance for the start date of the semester
▪ a Date instance for the end date of the semester
The Semester class should have the following
member functions
1. Create a constructor that accepts three arguments: the semester
name, the start Date, and the end Date. Use default values for all
the parameters in the constructor
2. Overload the << operator for this
class
▪ Have it output the semester name and dates in a manner similar to
this:
Semester: Fall 2020 (08/31/2020 - 12/10/2020)
3. Overload the >> operator for this
class
▪ Have the >> operator accept input for the
Semester name, start date and end date.
4. Create the get and set functions for each variable - Do NOT
recreate the Date class - reuse code from Date Class.
Below is the Date .h and .cpp file
Date.h
========================================
#define DATE_H
#include <iostream>
using namespace std;
class Date
{
friend ostream& operator << (ostream&,
const Date&);
friend istream& operator >> (istream&,
Date&);
private:
int month;
int day;
int year;
void checkDate();
public:
Date(int = 1, int = 1, int = 1990);
~Date();
Date& setDate(int, int, int);
Date& setMonth(int);
Date& setDay(int);
Date& setYear(int);
int getMonth() const;
int getDay() const;
int getYear() const;
bool operator< (const Date&);
bool operator> (const Date&);
bool operator<= (const Date&);
bool operator>= (const Date&);
bool operator== (const Date&);
bool operator!= (const Date&);
};
Date.cpp
========================================
#include <iostream>
#include <iomanip>
#include "Date.h"
using namespace std;
Date::Date(int m, int d, int y)
{
setDate(m, d, y);
}
Date::~Date()
{
}
Date& Date::setDate(int m, int d, int y)
{
setMonth(m);
setDay(d);
setYear(y);
return *this;
}
void Date::checkDate()
{
static const int daysPerMonth[13] = { 0, 31, 28, 31,
30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (month <= 0 || month >= 12)
{
month = 1;
}
if (!(month == 2 && day == 29 &&
(year % 400 == 0 || (year % 4 == 0 && year % 100 !=
0)))
&& (day <= 0 || day >
daysPerMonth[month]))
{
day = 1;
}
if (year <= 1989)
{
year = 1990;
}
}
Date& Date::setMonth(int m)
{
month = m;
checkDate();
return *this;
}
Date& Date::setDay(int d)
{
day = d;
checkDate();
return *this;
}
Date& Date::setYear(int y)
{
year = y;
checkDate();
return *this;
}
int Date::getMonth() const
{
return month;
}
int Date::getDay() const
{
return day;
}
int Date::getYear() const
{
return year;
}
bool Date::operator<(const Date& right)
{
bool status;
if (year < right.year)
status = true;
else if ((year == right.year) && (month <
right.month))
status = true;
else if ((year == right.year) && (month <
right.month) && (day < right.day))
status = true;
else
status = false;
return status;
}
bool Date::operator>(const Date& right)
{
bool status;
if (year > right.year)
status = true;
else if ((year == right.year) && (month >
right.month))
status = true;
else if ((year == right.year) && (month >
right.month) && (day > right.day))
status = true;
else
status = false;
return status;
}
bool Date::operator<=(const Date& right)
{
bool status;
if (year <= right.year)
status = true;
else if ((year == right.year) && (month <=
right.month))
status = true;
else if ((year == right.year) && (month <=
right.month) && (day <= right.day))
status = true;
else
status = false;
return status;
}
bool Date::operator>=(const Date& right)
{
bool status;
if (year < right.year)
status = true;
else if ((year == right.year) && (month <
right.month))
status = true;
else if ((year == right.year) && (month <
right.month) && (day < right.day))
status = true;
else
status = false;
return status;
}
bool Date::operator==(const Date& right)
{
bool status;
if (year == right.year && month == right.month
&& day == right.day)
status = true;
else
status = false;
return status;
}
bool Date::operator!=(const Date& right)
{
bool status;
if (year != right.year && month != right.month
&& day != right.day)
status = true;
else
status = false;
return status;
}
ostream& operator<<(ostream& output, const
Date& obj)
{
output << setfill('0') << setw(2) <<
obj.month << '/' << setfill('0') << setw(2)
<< obj.day << '/' << obj.year;
return output;
}
istream& operator>>(istream& input, Date&
obj)
{
input >> obj.month;
input.ignore();
input >> obj.day;
input.ignore();
input >> obj.year;
obj.checkDate();
return input;
}
In: Computer Science
1. Given a string of at least 3 characters as input, if the length of the string is odd return the character in the middle as a string. If the string is even return the two characters at the midpoint.
public class Class1 {
public static String midString(String str) {
//Enter code here
}
}
-----------
2. Given an array of integers return the sum of the values stored in the first and last index of the array. The array will have at least 2 elements in it
public class Class1 {
public static int endSum(int[] values) {
//Enter code here
}
}
-----------
3. The method takes a string as a parameter. The method prints treat if the string is candy or chocolate (of any case, such as CaNdY) Otherwise print trick
import java.util.Scanner;
public class Class1 {
public static void trickOrTreat(String str) {
//Enter code here
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
trickOrTreat(s.nextLine());
}
}
In: Computer Science
(Area of a convex polygon)
A polygon is convex if it contains any line segment that connects
two points of the polygon. Write a program that prompts the user to
enter the number of points in a convex polygon, then enter the
points clockwise, and display the area of the polygon.
Sample Run
Enter the number of points: 7
Enter the coordinates of the points:
-12 0 -8.5 10 0 11.4 5.5 7.8 6 -5.5 0 -7 -3.5 -5.5
The total area is 244.575
In: Computer Science
CODE IN JAVA
Create a class Student includes name, age, mark and necessary methods. Using FileWriter, FileReader and BufferedReader to write a program that has functional menu:
|
Menu -------------------------------------------------
Your choice: _ |
+ Save to File: input information of several students and write that information into a text file, each student in a line (use tabs to separate the fields)
+ Read File: read and display information of students
In: Computer Science
JAVA code
Create a new class called BetterDiGraph that implements the EditableDiGraph interface See the interface below for details.
EditableDigraph below:
import java.util.NoSuchElementException;
/**
* Implements an editable graph with sparse vertex support.
*
*
*/
public interface EditableDiGraph {
/**
* Adds an edge between two vertices, v and w. If vertices do not
exist,
* adds them first.
*
* @param v source vertex
* @param w destination vertex
*/
void addEdge(int v, int w);
/**
* Adds a vertex to the graph. Does not allow duplicate
vertices.
*
* @param v vertex number
*/
void addVertex(int v);
/**
* Returns the direct successors of a vertex v.
*
* @param v vertex
* @return successors of v
*/
Iterable getAdj(int v);
/**
* Number of edges.
*
* @return edge count
*/
int getEdgeCount();
/**
* Returns the in-degree of a vertex.
* @param v vertex
* @return in-degree.
* @throws NoSuchElementException exception thrown if vertex does
not exist.
*/
int getIndegree(int v) throws NoSuchElementException;
/**
* Returns number of vertices.
* @return vertex count
*/
int getVertexCount();
/**
* Removes edge from graph. If vertices do not exist, does not
remove edge.
*
* @param v source vertex
* @param w destination vertex
*/
void removeEdge(int v, int w);
/**
* Removes vertex from graph. If vertex does not exist, does not try
to
* remove it.
*
* @param v vertex
*/
void removeVertex(int v);
/**
* Returns iterable object containing all vertices in graph.
*
* @return iterable object of vertices
*/
Iterable vertices();
/**
* Returns true if the graph contains at least one vertex.
*
* @return boolean
*/
boolean isEmpty();
/**
* Returns true if the graph contains a specific vertex.
*
* @param v vertex
* @return boolean
*/
boolean containsVertex(int v);
}
In: Computer Science
An ALP to print only lowercase alphabets[A-Z] using emu8086 and the code will be written like this form EX:
MOV AX, @DATA ; initialize
DS MOV DS, AX
and please write explanation when writing the code so we can understand it
In: Computer Science
****JAVA Program****
Make a LandTract class with the following fields:
• length - an int containing the tract's length
• width - an int containing the tract's width
The class should also have the following methods :
• area - returns an int representing the tract's area
• equals - takes another LandTract object as a parameter and
returns a boolean saying
whether or not the two tracts have the same dimensions (This
applies regardless of whether the dimensions match up. i.e., if the
length of the first is the same as the width of the other and vice
versa, that counts as having equal dimensions.)
• toString - returns a String with details about the LandTract
object in the format:
LandTract object with length 30 and width 40
(If, for example, the LandTract object had a length of 30 and a
width of 40.)
Write a separate program that asks the user to enter the dimensions
for the two tracts of
land (in the order length of the first, width of the first, length
of the second, width of the second). The program should print the
output of two tracts' toString methods followed by a sentence
stating whether or not the tracts have equal dimensions. (If the
tracts have the same dimensions, print, "The two tracts have the
same size." Otherwise, print, "The two tracts do not have the same
size.") Print all three statements on separate lines.
****Results have to look like this****
Enter·length·of·first·land·tract:10↵ Enter·width·of·first·land·tract:55↵ Enter·length·of·second·land·tract:36↵ Enter·width·of·second·land·tract:75↵ LandTract·with·length·10,·width·55,·and·area·550↵ LandTract·with·length·36,·width·75,·and·area·2700↵ The·two·tracts·do·not·have·the·same·size.↵
In: Computer Science
a) Write down the attributes of three computer hardware input
devices. Discuss
the form of interaction each supports in a computer system.
b) With your knowledge in variations in physical abilities and
physical workplaces,
what four practical guidelines would you adapt in your workplace?
Explain the
reasons for your choice.
c) Explain the main concerns of Human Computer Interaction
In: Computer Science
Some information about youTube.
Summarize to an Infographic. Focus on numbers, statistics, success stories, advantages, benefits, etc for youTube.
In: Computer Science
The charging min value is zero and max value is 100. When the
charger is not plugged, the robot is in discharging state and the
battery decreases. When charger is attached, the state is changed
to charging and the value increase. Once it reaches the max
limit, the state changes to battery full.
Q) Write boundary value analysis for battery level.
Q) Write test cases for battery level for both valid and invalid.
In: Computer Science
1. Servlet program to find the information about an account
holder at a bank.
( Bank name= your name )
(I'm using netbeans with glassfish, lecturer mentioned not to use
jsp, mostly use CSS, Javascript, html, xml)
• Case:
a) Have username and password authentication
b) Display his Account balance to be RM10000,
c) Show different functionalities of a bank account (Deposit,
Withdrawal, Print)
d) For every transaction make sure make use of his account
balance.
e) Perform the functionalities(Deposit, Withdraw and print) until
the user selects
to stop.
In: Computer Science
– Consider an XYZ Hypermarket in the Sultanate of Oman. The hypermarket has got a lot of branches spread across different Wilayats in the country. As a database administrator, which client server architecture will you be preferring for this hypermarket and why. Support you answer with valid in-text citations and references.
In: Computer Science
In order to implement a DNS amplification attack, the attacker must trigger the creation of a sufficiently large volume of DNS response packets from the intermediary to exceed the capacity of the link to the target organization. Consider an attack where the DNS response packets are 1000 bytes in size (ignoring framing overhead).
How many of these packets per second must the attacker trigger to flood a target organization using a 30-Mbps link? A 100-Mbps link? Or a 1-Gbps link?
If the DNS request packet to the intermediary is 60 bytes in size, how much bandwidth does the attacker consume to send the necessary rate of DNS request packets for each of these three cases?
In: Computer Science