In one or 2 sentences, explain the difference between upper bound of the time complexity of a problem and upper bound on the time complexity of an algorithm to solve that problem.
In: Computer Science
In: Computer Science
Write code for A counting sort is a technique to sort an array of n positive integers that lie between 0 and m, inclusive. You need m + 1 counters. Then, making only one pass through the array, you count the number of times each integer occurs in the array. For example, the figure below shows an array of integers that lie between 0 and 4 and the five counters after a counting sort has made its pass through the array. From the counters, you can see that the array contains one 0, three 1’s, two 2’s, one 3, and three 4’s. These counts enable you to determine that the sorted array should contain [0, 1, 1, 1, 2, 2, 3, 4, 4, 4].
import java.util.Arrays;
public class Question4
{
/** Sorts an array of postive integers that lie within the range 0 to max.
@param a The array.
@param max The largest value in the array. */
public static void question4(int[] a, int max)
{
}
/*
* Do not write any code inside the main method and expect it to get marked.
* Any code that you write inside the main method will be ignored. However,
* you are free to edit the main function in any way you like for
* your own testing purposes.
*/
public static void main(String[] args)
{
int[] array = {2, 8, 4, 10, 15, 0, 4, 8, 2, 2, 0, 15, 10};
System.out.println("The array before sorting:");
System.out.println(Arrays.toString(array)); //must print [2 8 4 10 15 0 4 8 2 2 0 15 10]
question4(array, 15);
System.out.println("\nThe array after sorting:");
System.out.println(Arrays.toString(array)); //must print [0 0 2 2 2 4 4 8 8 10 10 15 15]
}
}
In: Computer Science
Create a program called BubleSort.java that implements the Bubble Sort algorithm (The Art of Computer Programming - Donald Knuth). The algorithm is as follows: The program should be able to do the following: accepts one command line parameter. The parameter specifies the path to a text file containing the integers to be sorted. The structure of the file is as follows: There will be multiple lines in the file (number of lines unknown). Each line will contain multiple integers, separated by a single whitespace. reads the integers from the text file in part a into an array of integers. sort the integers in ascending order, and then prints out a sorted version of these integers, one per line. The implementation should follow the given the pseudo code/algorithm description. JAVA CODE
In: Computer Science
c++
I cannot get my operator+ to pass my test, it is failing on the first test, how would i convert my operator+ to use pointers and dynamic memory?
Requirements:
============================================================================
string.hpp:
#ifndef CS23001_STRING_INTERFACE_HPP
#define CS23001_STRING_INTERFACE_HPP
#include <iostream>
////////////////////////////////////////////////////
// CLASS INV: str[length()] == 0 &&
// length() == capacity() &&
// capacity() == stringSize - 1
//
class String {
public:
String (); //Empty string
String (char); //String('x')
String (const char[]); //String("abc")
String (const String&); //Copy Constructor
~String (); //Destructor
void swap (String&); //Constant time swap
String& operator= (String); //Assignment Copy
char& operator[] (int); //Accessor/Modifier
char operator[] (int) const; //Accessor
int capacity () const; //Max chars that can be stored (not including null)
int length () const; //Actual number of chars in string
String operator+ (const String&) const;
String& operator+= (const String&);
bool operator== (const String&) const;
bool operator< (const String&) const;
String substr (int, int) const; //The sub-string from staring position to ending position
int findch (int, char) const; //Find location of charater starting at a position
int findstr (int, const String&) const; //Find location of str starting at a position
void test_String ();
friend std::ostream& operator<<(std::ostream&, const String&);
friend std::istream& operator>>(std::istream&, String&);
private:
String (int n ); //String(10) - capacity 10, empty string
String (int , const char []); //String(10, "abc") - capacity 10 with "abc"
void resetCapacity (int); //Resets capacity to N, keeps string intact
char *str; //Pointer to char[]
int stringSize; //Size includes NULL terminator
};
String operator+ (const char[], const String&);
String operator+ (char, const String&);
bool operator== (const char[], const String&);
bool operator== (char, const String&);
bool operator< (const char[], const String&);
bool operator< (char, const String&);
bool operator<= (const String&, const String&);
bool operator!= (const String&, const String&);
bool operator>= (const String&, const String&);
bool operator> (const String&, const String&);
#endif
============================================================================
string.cpp:
#include <iostream>
#include "string.hpp"
#include <cassert>
String::String() { // default constructor - empty string
stringSize = 1;
str = new char [stringSize];
str[0] = 0;
}
String::String(char ch) {
stringSize = 2;
str = new char [stringSize];
str[0] = ch;
str[1] = '\0';
}
//REQUIRES: str.length() < capacity()
//String a("abc")
//Takes character array and turns into string array
String::String(const char X[]) {
int i = 0;
while (X[i] != '\0') ++i;
stringSize = i + 1;
str = new char [stringSize];
for(int j = 0; j < capacity(); ++j)
str[j] = X[j];
str[capacity()] = 0;
}
String::String(const String& rhs) { //copy constructor
stringSize = rhs.stringSize;
str = new char [stringSize];
for(int i = 0; i < capacity(); ++i)
str[i] = rhs.str[i];
}
String::~String() { //destructor
delete[] str;
}
void String::swap (String& rhs) { //Constant time swap
char * temporary = str;
str = rhs.str;
rhs.str = temporary;
int hold = stringSize;
stringSize = rhs.stringSize;
rhs.stringSize = hold;
}
String& String::operator= ( String rhs) { // Assignment copy
if (str == rhs.str) return *this; //check to see if they are already pointing to the same address
delete [] str;
stringSize = rhs.stringSize;
str = new char [stringSize];
for (int i = 0; i < capacity(); ++i)
str[i] = rhs.str[i];
return *this;
}
//REQUIRES: 0 <= i < length()
// operator[] const --- allows access to const objects
char String::operator[](int i) const {
assert( (i > 0) && (i < length()) );
return str[i];
}
//REQUIRES: 0 <= i < length()
// operator[] --- allows access / modification to non-const objects
char& String::operator[] (int i) {
assert( (i >= 0) && (i < length() ) );
return str[i];
}
int String::capacity() const { //capacity = stringSize -1;
return (stringSize - 1);
}
//ENSURES: Retval == i where str[i] = 0
int String::length() const {
int result = 0;
while (str[result] != '\0')
++result;
return result;
}
// retval == "xyzabc" where "xyx" + "abc"
String String::operator+(const String& rhs) const {
String result;
int offset = length();
int i = 0;
while(rhs.str[i] != 0) {
result.str[offset + i] = rhs.str[i];
++i;
if (offset + i == capacity()) break;
}
return result;
}
String operator+(char lhs, const String& rhs) {
return String(lhs) + rhs;
}
String operator+(const char lhs[], const String& rhs) {
return String(lhs) + rhs;
}
String& String::operator+=(const String& rhs) {
*this = operator+(rhs);
return *this;
}
bool String::operator==(const String& rhs) const {
int i = 0;
while ((str[i] != '\0') && (str[i] == rhs.str[i])) ++i;
return str[i] == rhs.str[i];
}
bool operator==(char lhs, const String& rhs) {
return String(lhs) == rhs;
}
bool operator==(char lhs[], const String& rhs) {
return String(lhs) == rhs;
}
bool String::operator<(const String& rhs) const {
int i = 0;
while ((str[i] != 0) && (rhs.str[i] != 0) && (str[i] == rhs.str[i])) ++i;
return str[i] < rhs.str[i];
}
bool operator<(char lhs, const String& rhs) {
return String(lhs) < rhs;
}
bool operator<(const char lhs[], const String& rhs) {
return String(lhs) < rhs;
}
bool operator!=(const String& lhs, const String& rhs) {
return !(lhs == rhs) || (lhs == rhs);
}
bool operator<=(const String& lhs, const String& rhs) {
return (lhs < rhs) || (lhs == rhs);
}
bool operator>(const String& lhs, const String& rhs) {
return (rhs < lhs);
}
bool operator>=(const String& lhs, const String& rhs) {
return !(lhs < rhs);
}
std::ostream& operator<<(std::ostream& out, const String& rhs) {
out << rhs.str;
return out;
}
std::istream& operator>>(std::istream& in, String& rhs) {
char placehold[540000];
in >> placehold;
rhs = String(placehold);
return in;
}
//REQUIRES: 0 <= start < length()
//ENSURES: retval == i where str[i] == ch && i >= start
// or retval == -1 if ch != s[start.length()-1]
int String::findch(int start, char ch) const {
if ( (start < 0) || (start >= length()) ) return -1;
int i = start;
while (str[i] != 0) {
if (str[i] == ch) return i;
++i;
}
return -1;
}
int String::findstr(int pos, const String& rhs) const {
int i = pos;
if ((pos < 0) || (pos >= length() - rhs.length()))
return -1;
if (length() < rhs.length())
return -1;
while ((str[pos] != 0) && (rhs.length() + pos - 1 <= length())) {
if (rhs == substr(i, i + rhs.length() - 1))
return pos;
++i;
}
return -1;
}
//REQUIRES: 0 <= start <= end < length()
//ENSURES: retval == s[start, ..., end]
String String::substr(int start, int end) const {
if (start < 0) return String();
if (start > end) return String();
if (end >= length()) return String();
String result;
int i = start;
while (i <= end) {
result += str[i];
++i;
}
return result;
}
String::String (int n) { //String(10) - capacity 10, empty string
stringSize = n;
str = new char [stringSize];
str[0] = 0;
}
String::String (int n, const char ch[]) { //String(10, "abc") - capacity 10 with "abc"
stringSize = n;
str = new char [n];
for (int i = 0; i < n; ++i)
str[i] = ch[i];
}
void String::resetCapacity (int n ) { //Resets capacity to N, keeps string intact
int smaller = stringSize;
if (smaller > n) smaller = n;
stringSize = n;
char * tmp = new char [stringSize];
for (int i = 0; i < smaller; ++i)
tmp[i] = str[i];
delete [] str;
str = tmp;
}
void String::test_String() {
String testing(5);
assert(testing.length() == 0);
assert(testing.capacity() == 5);
String test(15);
assert(test.length() == 0);
assert(test.capacity() == 15);
String CharArray(10, "abc");
assert(CharArray.length() == 3);
assert(CharArray.capacity() == 10);
}
============================================================================
concat test .cpp:
#include "string.hpp"
#include <cassert>
#include <iostream>
int main ()
{
{
// TEST
String str = "x";
String str2 = "y";
String result;
result = str+str2;
std::cout<< str << " " << str2 << " " <<result<<std::endl;
// VERIFY
assert(result == "xy");
}
{
// TEST
String str = "xyz";
String str2 = "abc";
String result;
result = str+str2;
// VERIFY
assert(result == "xyzabc");
}
{
// TEST
String str = "qlW3KSqbFk";
String str2 = "f6iSmJhRTl";
String result;
result = str+str2;
// VERIFY
assert(result == "qlW3KSqbFkf6iSmJhRTl");
}
{
//------------------------------------------------------
// SETUP FIXTURE
// TEST
String str = "lZ8kmGDuKeqzqPOmvthx94jQQg46C8";
String str2 = "SgiwD";
String result;
result = str+str2;
// VERIFY
assert(result == "lZ8kmGDuKeqzqPOmvthx94jQQg46C8SgiwD");
}
std::cout << "Done testing Concatination Constructor." << std::endl; }
In: Computer Science
**Keil uVision5 - ARM Cortex M0+ - Embedded C programming
Modify the program below so that the Red, Green, and Blue LEDs are switched ON and OFF in a sequence with one second delay for Red and Green LEDs, and 0.5 second delay for the Blue LED.
#include "MKL25Z4.h"
void delay(int n);
int main (void) {
SIM_SCGC5 |= SIM_SCGC5_PORTB(1); /* enable clock to Port B */
PORTB_PCR18 |=PORT_PCR_MUX(1); /* Configure PORTB ,pin 18 as GPIO ; set MUX*/
GPIOB_PDDR=(1UL << 18); /* make PORTB ,pin 18 as output pin */
while (1) {
GPIOB_PCOR = (1UL << 18); /* turn ON Red LED for one sec */
delay(10);
GPIOB_PSOR = (1UL << 18); /* turn OFF Red LED for one sec*/
delay(10);
}
}
/* Delay function for 100 ms */
void delay(int n) {
int i,j;
for(i = 0 ; i < n; i++)
for (j = 0; j < 349500; j++) {}
}
In: Computer Science
1. Create the binary tree that has postorder traversal of { b d a h g c f,} and has the inorder traversal of {b i a d f g h c}
2. Create the binary tree that has the preorder traversal of {10, 20, 40, 50, 80, 60, 70, 90} and the inorder traversal of { 40, 50, 20, 10, 80, 70, 90, 60}
In: Computer Science
Discuss how social media was used in the 2016 U.S. Presidential Election in terms of communicating with voters, fundraising, and campaign organizing. Discuss how databases of political information can be used to help voters make decisions on candidates and issues. Support your discussion with reliable sources.
In: Computer Science
"What is Social Media" and "How is social media influencing web applications and development?" Include in your discussion three of your favorite sites including what it is that you like or dislike about them. Your sites can be within the realm of social media or from sites that are somehow integrated into social media.
In: Computer Science
Using Java. The following is a constructor I need to implement but I am not sure how. It also must pass the Junit test. Please give reasoning while answering.
public class LZWDictionary {
// FIELDS
// map (for ease of checking existence of entries)
// list (ease of recall of an entry)
LinkedHashMap<String, Integer> map;
List<String> list;
/**
* Initializes the LZWDictionary to have an initial set of entries taken from
* the set of unique characters in a provided string
*
*
*
* Unique characters are added to a map as they are encountered in the provided
* input string, with an increasing index value (beginning at index 0). At the same
* time, the characters are added to a list. The indices associated with each
* dictionary entry in the map thus relate to their index (position) in the list
*
*
*
*
* @param characters a string of initial characters that may include duplicates
*
* @throws an IllegalArgumentException if the characters string is empty
*
*/
public LZWDictionary(String characters) {
// NOTE: Complete the accessors getMap() and getList() first before running
// your tester on this ctor
}
Must pass this Junit Test
In: Computer Science
This task is solved in Python 3.
Develop a program that can, among other things, manage your courses and course results.
It must also be able to calculate the average grade, for all grades or for a selection of grades
limited to a certain subject area and / or course level (100, 200 or 300 level).
NB! All courses are worth the same amount of credits.
The program should have two global collections
All courses belong to a subject area and have a course code that starts with at least three letters and always ends with three digits, of which the first digit is 1, 2 or 3 which indicates the level of the course.
Grades = {"INFO100": "C", "INFO104": "B", "ECON100": "B",…}
(All courses registered in Grades must be found in Courses, but not necessarily vice versa.)
The program should let the user select action from a menu and continue until the user chooses to exit.
The purpose of the program is for you to be able to calculate your grade points, in total and for granted subject areas and / or levels. In addition, you should be able to add new courses and set / change grades temporarily so you can experiment with how improved and upcoming results will give an effect on the grade point average.
Try to make the program robust for errors on the part of the user, such as entering a rating in form of a number, or a letter outside A-F.
On the next page you will find a guiding example of what the dialogue can look like:
>>> start ()
--------------------
1 Course list
2 Add course
3 Grade
4 Grade point average
5 Exit
--------------------
Choose action (0 for menu)> 1
Choose course and / or course level (<enter> for all)
- Course:
- Level:
INFO100 C
INFO104 B
INFO125 B
INFO132 A
INFO180
INFO216 A
INFO282 C
INFO284
ECON100 C
ECON110 C
ECON218
GE0100
GEO113 D
GEO124 D
Choose action (0 for menu)> 2
New course: ECON221
Choose action (0 for menu)> 1
Choose course and / or course level (<enter> for all)
- Course: Economics
- Level: 200
ECON218
ECON221
Choose action (0 for menu)> 3
Course: INFO284
Grade (<enter> to delete): B
Choose action (0 for menu)> 4
Choose course and / or course level (<enter> for all)
- Course: Information science
- Level: 100
Average: B
Choose action (0 for menu)> 4
Choose course and / or course level (<enter> for all)
- Course:
- Level:
Average: C
Choose action (0 for menu)> 0
--------------------
1 Course list
2 Add course
3 Grades
4 Grade point average
5 Exit
--------------------
Choose action (0 for menu)> 3
Course: GEO124
Grade (<enter> to delete): A
Choose action (0 for menu)> 4
Choose course and / or course level (<enter> for all)
- Course:
- Level:
Grade point average: B
Select action (0 for menu)> 5
Thanks for now
>>>
In: Computer Science
Write a paragraph about the VIM editor.
In: Computer Science
Your task is to determine the least common multiple of two integers.
You will need to use the Euclidean algorithm for GCD to prevent going above the time limit.
input specification
The first line will contain a single integer NN (1<N<100001
answer must be in c++ language
The following NN lines will each contain a 2 integers aa and bb (1<a,b<1000000001
In: Computer Science
In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win.
Write a program that emulates a Tic Tac Toe game.
When you are done, a typical session will look like this:
Welcome to tic-tac-toe. Enter coordinates for your move following the X and O prompts. 1 2 3 A | | ----- B | | ----- C | | X:A2 1 2 3 A |X| ----- B | | ----- C | | O:B3 1 2 3 A |X| ----- B | |O ----- C | |
And so on. Illegal moves will prompt the user again for a new move. A win or a stalemate will be announced, mentioning the winning side if any. The program will terminate whenever a single game is complete.
This file has the general framework of the TicTacToe class. An object of this class will represent a tic-tac-toe "board". The board will be represented internally by a two dimensional array of characters (3 x 3), and by a character indicating who's turn it is ('X' or 'O'). These are stored in the class instance variables as follows.
private char[][] board; private char player; // 'X' or 'O'
You will need to define the following methods:
1. A constructor:
public TicTacToe()
to create an empty board, with initial value of a space (' ')
2. play method
public play(String position)
if position represents a valid move (e.g., A1, B3), add the current player's symbol to the board and return true. Otherwise, return false.
3. switchTurn method
public void switchTurn()
switches the current player from X to O, or vice versa.
4. won method
public boolean won()
Returns true if the current player has filled three in a row, column or either diagonal. Otherwise, return false.
5. stalemate method
public boolean stalemate()
Returns true if there are no places left to move;
6. printBoard method
public void printBoard()
prints the current board
7. Use the following test code in your main method to create a TicTacToe object and print it using the printBoard method given, so as to test your code. Your printBoard method should produce the first board in the example above.
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
TicTacToe game = new TicTacToe();
System.out.println("Welcome to Tic-tac-toe");
System.out.println("Enter coordinates for your move following the X and O prompts");
while(!game.stalemate())
{
game.printBoard();
System.out.print(game.getPlayer() + ":");
//Loop while the method play does not return true when given their move.
//Body of loop should ask for a different move
while(!game.play(in.next()))
{
System.out.println("Illegal move. Enter your move.");
System.out.print(game.getPlayer() + ":");
}
//If the game is won, call break;
if(game.won())
break;
game.printBoard();
//Switch the turn
game.switchTurn();
}
game.printBoard();
if(game.won())
{
System.out.println("Player "+game.getPlayer()+" Wins!!!!");
}
else
{
System.out.println("Stalemate");
}
}In: Computer Science
In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win.
Write a program that emulates a Tic Tac Toe game.
When you are done, a typical session will look like this:
Welcome to tic-tac-toe. Enter coordinates for your move following the X and O prompts. 1 2 3 A | | ----- B | | ----- C | | X:A2 1 2 3 A |X| ----- B | | ----- C | | O:B3 1 2 3 A |X| ----- B | |O ----- C | |
And so on. Illegal moves will prompt the user again for a new move. A win or a stalemate will be announced, mentioning the winning side if any. The program will terminate whenever a single game is complete.
This file has the general framework of the TicTacToe class. An object of this class will represent a tic-tac-toe "board". The board will be represented internally by a two dimensional array of characters (3 x 3), and by a character indicating who's turn it is ('X' or 'O'). These are stored in the class instance variables as follows.
private char[][] board; private char player; // 'X' or 'O'
You will need to define the following methods:
1. A constructor:
public TicTacToe()
to create an empty board, with initial value of a space (' ')
2. play method
public play(String position)
if position represents a valid move (e.g., A1, B3), add the current player's symbol to the board and return true. Otherwise, return false.
3. switchTurn method
public void switchTurn()
switches the current player from X to O, or vice versa.
4. won method
public boolean won()
Returns true if the current player has filled three in a row, column or either diagonal. Otherwise, return false.
5. stalemate method
public boolean stalemate()
Returns true if there are no places left to move;
6. printBoard method
public void printBoard()
prints the current board
7. Use the following test code in your main method to create a TicTacToe object and print it using the printBoard method given, so as to test your code. Your printBoard method should produce the first board in the example above.
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
TicTacToe game = new TicTacToe();
System.out.println("Welcome to Tic-tac-toe");
System.out.println("Enter coordinates for your move following the X and O prompts");
while(!game.stalemate())
{
game.printBoard();
System.out.print(game.getPlayer() + ":");
//Loop while the method play does not return true when given their move.
//Body of loop should ask for a different move
while(!game.play(in.next()))
{
System.out.println("Illegal move. Enter your move.");
System.out.print(game.getPlayer() + ":");
}
//If the game is won, call break;
if(game.won())
break;
game.printBoard();
//Switch the turn
game.switchTurn();
}
game.printBoard();
if(game.won())
{
System.out.println("Player "+game.getPlayer()+" Wins!!!!");
}
else
{
System.out.println("Stalemate");
}
}In: Computer Science