Skip to main content

PlaylistNode.cpp (lab 9)

0 likes • Nov 18, 2022 • 0 views
C++
Loading...

More C++ Posts

Two Letter Combinations

0 likes • Nov 18, 2022 • 0 views
C++
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;
//This program makes a new text file that contains all combinations of two letters.
// aa, ab, ..., zy, zz
int main(){
string filename = "two_letters.txt";
ofstream outFile;
outFile.open(filename.c_str());
if(!outFile.is_open()){
cout << "Something's wrong. Closing..." << endl;
return 0;
}
for(char first = 'a'; first <= 'z'; first++){
for(char second = 'a'; second <= 'z'; second++){
outFile << first << second << " ";
}
outFile << endl;
}
return 0;
}

Big O(n^2) Ascending Sort

0 likes • Nov 18, 2022 • 6 views
C++
#include <iostream>
using namespace std;
int main() {
int arr[5];
for(int i = 0; i < 5; i++) {
arr[i] = i;
}
for(int i = 0; i < 5; i++) {
cout << "Outputting array info at position " << i + 1 << ": " << arr[i] << endl;
}
for(int i=0;i<5;i++)
{
for(int j=i+1;j<5;j++)
{
if(arr[i]>arr[j])
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
cout << endl;
for(int i = 0; i < 5; i++) {
cout << "Outputting sorted array info at position " << i + 1 << ": " << arr[i] << endl;
}
return 0;
}

GCD using Stein's Algorithm

0 likes • Jun 30, 2023 • 2 views
C++
// Iterative C++ program to
// implement Stein's Algorithm
//#include <bits/stdc++.h>
#include <bitset>
using namespace std;
// Function to implement
// Stein's Algorithm
int gcd(int a, int b)
{
/* GCD(0, b) == b; GCD(a, 0) == a,
GCD(0, 0) == 0 */
if (a == 0)
return b;
if (b == 0)
return a;
/*Finding K, where K is the
greatest power of 2
that divides both a and b. */
int k;
for (k = 0; ((a | b) & 1) == 0; ++k)
{
a >>= 1;
b >>= 1;
}
/* Dividing a by 2 until a becomes odd */
while ((a & 1) == 0)
a >>= 1;
/* From here on, 'a' is always odd. */
do
{
/* If b is even, remove all factor of 2 in b */
while ((b & 1) == 0)
b >>= 1;
/* Now a and b are both odd.
Swap if necessary so a <= b,
then set b = b - a (which is even).*/
if (a > b)
swap(a, b); // Swap u and v.
b = (b - a);
} while (b != 0);
/* restore common factors of 2 */
return a << k;
}
// Driver code
int main()
{
int a = 12, b = 780;
printf("Gcd of given numbers is %d\n", gcd(a, b));
return 0;
}

Wing Project 1

0 likes • Oct 31, 2021 • 1 view
C++
//Get data file at https://codecatch.net/post.php?postID=91e87d73
//Iteration 1 of Wing Project. Solution breaks down around n=35
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
using namespace std;
int getSum(map<int, int> list);
void readData(map<int, float>* data);
void lowestPrice();
void findSums(int n, vector<map<int, int>>* sumsList, map<int, float>* data);
//void findSum(map<int, int> currList, int x, int n, vector<map<int, int>>* sumsList);
void findSum(map<int, int> currList, int x, int n, vector<map<int, int>>* sumsList, map<int, float>* data);
float getPrice(map<int, int> set, map<int, float>* data);
template <typename S>
ostream& operator<<(ostream& os, const vector<S>& vector)
{
// Printing all the elements using <<
for (auto element : vector) {
os << element << " ";
}
return os;
}
bool operator==(map<int, int> m1, map<int, int> m2)
{
if(m1.size() != m2.size())
return false;
bool ret = true;
for(auto it = m1.begin(); it !=m1.end() && ret; it++)
{
if(ret && m1.count(it->first) != m2.count(it->first))
ret = false;
if(ret && m1.count(it->first) == 1)
{
if(m1.at(it->first) != m2.at(it->first))
ret = false;
}
}
return ret;
}
int main()
{
map<int, float> data;
readData(&data);
vector<map<int, int>> *sumsList;
sumsList = new vector<map<int, int>>;
findSums(40, sumsList, &data);
for(auto el : *sumsList)
{
for(auto it = el.begin(); it != el.end(); it++)
{
cout << it->first << "->" << it->second << " ";
}
cout << getPrice(el, &data) << endl;
}
return 0;
}
/* Returns the price of wings given a set of numbers of wings to buy.
* Returns -1 if the set contains a number that is not possible to buy.
*/
float getPrice(map<int, int> set, map<int, float>* data)
{
float price = 0;
for(auto it = set.begin(); it != set.end(); it++)
{
//If data doesn't contain an element of set, return -1
if(data->count(it->first) == 0)
return -1;
price += data->at(it->first) * it->second; //pricePerPacket * qtyOfPackets
}
return price;
}
/* Adds the elements of list.
* Suppose mapping is <num, qty>.
* Returns sum(num*qty)
*/
int getSum(map<int, int> list)
{
int sum = 0;
for(auto it = list.begin(); it != list.end(); it++)
sum += it->first * it->second;
return sum;
}
void findSums(int n, vector<map<int, int>>* sumsList, map<int, float>* data)
{
map<int, int> currList;
//Recur when currSum < n
auto it = data->begin();
while(it->first <= n && it != data->end())
{
findSum(currList, it->first, n, sumsList, data);
it++;
}
}
void findSum(map<int, int> currList, int x, int n, vector<map<int, int>>* sumsList, map<int, float>* data)
{
//Append x to currList
if(currList.count(x) == 0)
currList.emplace(x, 1);
else
{
int val = 1+ currList.at(x);
currList.erase(x);
currList.emplace(x, val);
}
//Determine current sum, check for return cases
int currSum = getSum(currList);
if(currSum > n)
return;
else if(currSum == n)
{
//Check to make sure no duplicates
for(auto list : *sumsList)
{
if(list == currList)
return;
}
sumsList->push_back(currList);
return;
}
//Recur when currSum < n
auto it = data->begin();
while(it->first <= n-x && it != data->end())
{
findSum(currList, it->first, n, sumsList, data);
it++;
}
}
void readData(map<int, float>* data)
{
ifstream file ("./data", ifstream::in);
if(file.is_open())
{
int i = 0;
while(!file.eof())
{
float wings, price;
string skipnl;
file >> wings;
file >> price;
data->emplace(wings, price);
getline(file, skipnl);
i++;
}
}
}

Literal Bruh

0 likes • Jul 30, 2023 • 5 views
C++
//Constant prefix notation solver using bruh
//Could make it infix or postfix later
#include<string>
#include<vector>
#include<iostream>
std::vector<long double> bruhBuff;
long double operator ""bruh(long double a){
bruhBuff.push_back(a);
return a;
}
long double operator ""bruh(const char op){
if(bruhBuff.size() < 2) throw "Bruh weak";
long double b = bruhBuff.back();
bruhBuff.pop_back();
long double a = bruhBuff.back();
bruhBuff.pop_back();
switch(op){
case (int)('+'):
return a + b;
case (int)('-'):
return a - b;
case (int)('*'):
return a * b;
case (int)('/'):
return a / b;
}
return 69l;
}
int main(){
1.0bruh;
2.0bruh;
std::cout << '+'bruh << std::endl;
return 0;
}

Daily: Cutting a Wall

0 likes • Dec 20, 2021 • 0 views
C++
/*
Good morning! Here's your coding interview problem for today.
This problem was asked by LinkedIn.
A wall consists of several rows of bricks of various integer lengths and uniform height. Your goal is to find a vertical line going from the top to the bottom of the wall that cuts through the fewest number of bricks. If the line goes through the edge between two bricks, this does not count as a cut.
For example, suppose the input is as follows, where values in each row represent the lengths of bricks in that row:
[[3, 5, 1, 1],
[2, 3, 3, 2],
[5, 5],
[4, 4, 2],
[1, 3, 3, 3],
[1, 1, 6, 1, 1]]
The best we can we do here is to draw a line after the eighth brick, which will only require cutting through the bricks in the third and fifth row.
Given an input consisting of brick lengths for each row such as the one above, return the fewest number of bricks that must be cut to create a vertical line.
AUTHORS NOTE:
Makes following assumptions:
- Each row is same length
- Data is in file called "data.dat" and formatted in space-separated rows
- The cuts at the beginning and end of the wall are not solutions
This requires the following file named data.dat that is a space separated file, or similar formatted file:
----START FILE----
3 5 1 1
2 3 3 2
5 5
4 4 2
1 3 3 3
1 1 6 1 1
----END FILE----
*/
#include <algorithm>
#include <iostream>
#include <fstream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<vector<int>> wall;
ifstream in;
in.open("data.dat");
if(!in.good())
{
cout << "ERROR: File failed to open properly.\n";
}
/* Get input from space separated file */
string line;
while(!in.eof())
{
getline(in, line);
int i;
vector<int> currv;
stringstream strs(line);
while(strs >> i)
currv.push_back(i);
wall.push_back(currv);
}
/* Convert each value from "length of brick" to "position at end of brick" */
for(int y = 0; y < wall.size(); y++)
{
wall.at(y).pop_back(); //Delet last val
for(int x = 1; x < wall.at(y).size(); x++) //Skip the first bc data doesn't need change
wall.at(y).at(x) += wall.at(y).at(x-1);
}
/* Check output. COMMENT OUT */
// for(auto row : wall)
// {
// for(int pos : row)
// cout << pos << " ";
// cout << endl;
// }
/* Determine which ending position is most common, and cut there */
//Exclude final position, which will be the size of the wall
int mode = -1;
int amt = -1;
vector<int> tried;
for(auto row : wall)
{
for(int pos : row) //For each pos in the wall
{
//Guard. If pos is contained in the list, skip pos
if(find(tried.begin(), tried.end(), pos) != tried.end())
continue;
tried.push_back(pos);
/* Cycle through each row to see if it contains the pos */
int curramt = 0;
for(auto currrow : wall)
{
if( find( currrow.begin(), currrow.end(), pos ) != currrow.end() )
curramt++;
}
//cout << pos << " " << curramt << endl;
if(curramt > amt)
{
amt = curramt;
mode = pos;
}
}
}
cout << "Please cut at position " << mode << endl;
cout << "This will cut through " << (wall.size() - amt) << " bricks." << endl;
return 0;
}