Last active
February 7, 2019 16:42
-
-
Save nikoladimitroff/3252006247704a3bcd4f9bc47923fb9a to your computer and use it in GitHub Desktop.
Exam prep
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <iostream> | |
double MyAbs(double a) | |
{ | |
return a < 0 ? -a : a; | |
} | |
double Calc(double a, double b, | |
double epsilon, long end) | |
{ | |
double xk1 = a; | |
double xk2 = b; | |
double xk = 0; | |
for (int k = 3; MyAbs(xk1 - xk2) >= epsilon; k++) | |
{ | |
xk = 0.2 * (xk1 + xk1 / xk2); | |
xk2 = xk1; | |
xk1 = xk; | |
if (k > end) | |
{ | |
return -1; | |
} | |
} | |
return xk; | |
} | |
// ------------- | |
void PrintArray(int* arr, int length) | |
{ | |
for (int i = 0; i < length; i++) | |
{ | |
std::cout << arr[i] << " "; | |
} | |
std::cout << std::endl; | |
} | |
void PrintAllAdditives(int* arr, int length, int targetSum) | |
{ | |
int* additives = new int[length]; | |
for (int i = 0; i < length; i++) | |
{ | |
for (int k = 0; k < length - i; k++) | |
{ | |
int currentIndex = 0; | |
int currentSum = targetSum; | |
for (int j = i; j < length; j++) | |
{ | |
if (currentSum - arr[j] >= 0) | |
{ | |
additives[currentIndex] = arr[j]; | |
currentIndex++; | |
currentSum -= arr[j]; | |
} | |
// This is the first iteration of the j loop | |
// and arr[j] is part of a potential combo; | |
// then skip the others | |
if (j == i && currentIndex == 1) | |
{ | |
j += k; | |
} | |
// there's no point in going further if arr[j] is not part | |
// of a combo | |
else if (j == i) | |
{ | |
k = length; | |
break; | |
} | |
} | |
if (currentSum == 0) | |
{ | |
PrintArray(additives, currentIndex); | |
} | |
} | |
} | |
delete[] additives; | |
} | |
// ------------- | |
struct QuadraticEquationRoots | |
{ | |
bool IsEquationSolvable; | |
double X1; | |
double X2; | |
}; | |
double ParseNumber(const char* text, int& symbolsRead) | |
{ | |
double sign = 1; | |
int currentIndex = 0; | |
// TODO: Skip all whitespace | |
if (text[currentIndex] == '+') | |
{ | |
currentIndex++; | |
} | |
if (text[currentIndex] == '-') | |
{ | |
sign = -1; | |
currentIndex++; | |
} | |
// TODO: Skip all whitespace | |
double number = 0; | |
for (; | |
text[currentIndex] >= '0' && text[currentIndex] <= '9'; | |
currentIndex++) | |
{ | |
number *= 10; | |
number += (text[currentIndex] - '0'); | |
} | |
number *= sign; | |
symbolsRead = currentIndex; | |
return number; | |
} | |
const char* SkipUntilNextNumber(const char* text) | |
{ | |
while (*text != '+' && *text != '-') | |
{ | |
text++; | |
} | |
return text; | |
} | |
QuadraticEquationRoots SolveEquation(double a, double b, double c) | |
{ | |
double d = b * b - 4 * a * c; | |
QuadraticEquationRoots roots; | |
roots.IsEquationSolvable = d >= 0; | |
roots.X1 = (-b + sqrt(d)) / (2 * a); | |
roots.X2 = (-b - sqrt(d)) / (2 * a); | |
return roots; | |
} | |
QuadraticEquationRoots SolveQuadraticEquation(const char* equation) | |
{ | |
int symbolsRead; | |
double a = ParseNumber(equation, symbolsRead); | |
const char* nextText = SkipUntilNextNumber(equation + symbolsRead); | |
double b = ParseNumber(nextText, symbolsRead); | |
nextText = SkipUntilNextNumber(nextText + symbolsRead); | |
double c = ParseNumber(nextText, symbolsRead); | |
QuadraticEquationRoots roots = SolveEquation(a, b, c); | |
return roots; | |
} | |
// ------------- | |
int* CheckNums(long num1, long num2, int& commonDigitsLength) | |
{ | |
int* commonDigits = new int[10]; | |
int currentIndex = 0; | |
for (long currentNum1 = num1; currentNum1 != 0; currentNum1 /= 10) | |
{ | |
long digit1 = currentNum1 % 10; | |
bool isDigitAlreadyAdded = false; | |
for (int i = 0; i < currentIndex; i++) | |
{ | |
if (commonDigits[i] == digit1) | |
{ | |
isDigitAlreadyAdded = true; | |
break; | |
} | |
} | |
if (isDigitAlreadyAdded) | |
{ | |
continue; | |
} | |
for (long currentNum2 = num2; currentNum2 != 0; currentNum2 /= 10) | |
{ | |
long digit2 = currentNum2 % 10; | |
if (digit1 == digit2) | |
{ | |
commonDigits[currentIndex] = digit1; | |
currentIndex++; | |
break; | |
} | |
} | |
} | |
commonDigitsLength = currentIndex; | |
return commonDigits; | |
} | |
// --------- | |
int StrLen(const char* text) | |
{ | |
int length; | |
for (length = 0; text[length] != '\0'; length++) | |
{} | |
return length; | |
} | |
bool doExists(const char* symbols, const char* word) | |
{ | |
int wordLength = StrLen(word); | |
int symbolsLength = StrLen(symbols); | |
for (int i = symbolsLength; i >= wordLength; --i) | |
{ | |
bool isSubstring = true; | |
for (int j = 0; j < wordLength; ++j) | |
{ | |
if (word[j] != symbols[i - j]) | |
{ | |
isSubstring = false; | |
break; | |
} | |
} | |
if (isSubstring) | |
{ | |
return true; | |
} | |
} | |
return false; | |
} | |
// ------------ | |
void EncodeNextChar(char symbol, char* encodedText, int& encodedIndex, bool shouldAddDelimiter) | |
{ | |
int digitsCount = 0; | |
int symbolValue = symbol; | |
for (; symbolValue != 0; symbolValue /= 10) | |
{ | |
digitsCount++; | |
} | |
for (int nextPowerOf10 = digitsCount - 1; nextPowerOf10 >= 0; --nextPowerOf10, encodedIndex++) | |
{ | |
int digit = (symbol / (int)pow(10, nextPowerOf10)) % 10; | |
encodedText[encodedIndex] = digit + '0'; | |
} | |
if (shouldAddDelimiter) | |
{ | |
encodedText[encodedIndex] = '_'; | |
encodedIndex++; | |
} | |
} | |
const char* Encode(const char* text) | |
{ | |
int textLength = StrLen(text); | |
char* encodedText = new char[textLength * 4]; | |
int encodedIndex = 0; | |
for (int i = 0; i < textLength; ++i) | |
{ | |
EncodeNextChar(text[i], encodedText, encodedIndex, i != (textLength - 1)); | |
} | |
encodedText[encodedIndex] = '\0'; | |
return encodedText; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include "Solutions.h" | |
int main() | |
{ | |
//int numbers[] = { 1, 2, 0, 0, 3, 8, 5, 10, 0, 0 }; | |
//int size = sizeof(numbers) / sizeof(int); | |
//PrintAllAdditives(numbers, size, 10); | |
//const char text[] = "5*x^2 -66*x -7688=0"; | |
//QuadraticEquationRoots roots = SolveQuadraticEquation(text); | |
//std::cout << roots.IsEquationSolvable << " " << roots.X1 << " " << | |
// roots.X2 << std::endl; | |
//int digitsLength; | |
//int* digits = CheckNums(14539, 95026, digitsLength); | |
//for (int i = 0; i < digitsLength; i++) | |
//{ | |
// std::cout << digits[i] << " " << std::endl; | |
//} | |
//delete[] digits; | |
//std::cout << doExists("Hello, world", "row") << " " | |
// << doExists("Hello, world", "Edje") << std::endl; | |
const char* text = "abc 2"; | |
std::cout << Encode(text) << std::endl; | |
int _; | |
std::cin >> _; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment