Created
July 22, 2019 17:53
-
-
Save MayukhSobo/b5fc393a88634ab25cb27859dd3ba55d to your computer and use it in GitHub Desktop.
patterns
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 <vector> | |
#include <string> | |
#include <cmath> | |
#include <tuple> | |
using namespace std; | |
//std::string greeting(const std::string &name="Mayukh") { | |
// return "Hello " + name; | |
//} | |
int check_case(char ch) { | |
// Check if special char | |
if (ch < 65 or (ch > 90 and ch < 97) or ch > 122) | |
return -1; | |
else if (ch >= 65 and ch <= 90) | |
return 1; | |
return 0; | |
} | |
int sum_of_evens(int N) { | |
int cn = 1; | |
int sum = 0; | |
while (cn <= N) { | |
if (cn % 2 == 0) | |
sum += cn; | |
cn++; | |
} | |
return sum; | |
} | |
void f2c(int S, int E, int W) { | |
const float M = 5.0 * (1.0 / 9); // Because directly 5.0 / 9 is slower | |
while (S <= E) { | |
cout << S << '\t' << (int)((S - 32) * M) << endl; | |
S += W; | |
} | |
} | |
void pattern1(int N) { | |
/* | |
* 1 | |
* 23 | |
* 345 | |
* 4567 | |
* 56789 | |
*/ | |
int row = 1; | |
while (row <= N) { | |
int col = 1; | |
int print_elem = row; | |
while (col <= row) { | |
cout << print_elem++; | |
col++; | |
} | |
cout << endl; | |
row++; | |
} | |
} | |
void pattern2(int N) { | |
/* | |
* 1 | |
* 23 | |
* 345 | |
* 4567 | |
* 56789 | |
*/ | |
int row = 1; | |
while (row <= N) { | |
int col = 1; | |
int print_elem = row; | |
while (col <= N - row) { | |
cout << " "; | |
col++; | |
} | |
col = 1; | |
while (col <= row) { | |
cout << print_elem++; | |
col++; | |
} | |
cout << endl; | |
row++; | |
} | |
} | |
void pattern3(int N) { | |
/* | |
* * | |
* *** | |
* ***** | |
* ******* | |
* ********* | |
*/ | |
int row = 1; | |
while (row <= N) { | |
// Print N-row spaces | |
int col = 1; | |
while (col <= N-row) { | |
cout << " "; | |
col++; | |
} | |
col = 1; | |
while (col <= (2*row - 1)) { | |
cout << "*"; | |
col++; | |
} | |
cout << endl; | |
// Print stars | |
row++; | |
} | |
} | |
int salary(int basic, char grade) { | |
static constexpr const float m_hra = 20.0 * (1.0 / 100); | |
static constexpr const float m_da = 50.0 * (1.0 / 100); | |
static constexpr const float m_pf = -11.0 * (1.0 / 100); | |
int allowance = 1300; | |
if (grade == 'A') | |
allowance = 1700; | |
else if (grade == 'B') | |
allowance = 1500; | |
float salary = basic + (basic * m_hra) + (basic * m_da) + allowance + (basic * m_pf); | |
return (int)round(salary); | |
} | |
std::tuple<int, int> sum_even_odd_digits(int N) { | |
int even_sum = 0; | |
int odd_sum = 0; | |
int digit; | |
while (N > 0) { | |
digit = N % 10; | |
if (digit % 2 == 0) { | |
even_sum += digit; | |
} else { | |
odd_sum += digit; | |
} | |
N /= 10; | |
} | |
return std::make_tuple(even_sum, odd_sum); | |
} | |
uint fast_exponent(uint x, uint n) { | |
// Only handles positive numbers | |
if (n == 0) | |
return 1u; | |
else if (n == 1) | |
return x; | |
if (x == 0) | |
return 0u; | |
else if (x == 2) { | |
return 2u << (n-1); | |
} | |
uint result = 1; | |
while (n) { | |
if (n&1u) { | |
result *= x; | |
} | |
n >>= 1u; | |
x *= x; | |
} | |
return result; | |
} | |
void pattern4(int N) { | |
/* | |
* 1 | |
* 232 | |
* 34543 | |
* 4567654 | |
* 567898765 | |
*/ | |
int i = 1, j, offset, k; | |
while (i <= N) { | |
j = 1; | |
while (j <= N - i) { | |
cout << ' '; | |
j++; | |
} | |
j = 1; | |
k = i; | |
while (j <= 2 * i - 1) { | |
if (j <= i) offset = 1; | |
else offset = -1; | |
cout << k + offset - 1; | |
j++; | |
if (offset == 1) | |
k++; | |
else | |
k--; | |
} | |
cout << endl; | |
i++; | |
} | |
} | |
void pattern_diamond(int N) { | |
int i = 1, j; | |
int n; | |
n = N / 2 + 1; | |
while (i <= N / 2 + 1) { | |
j = 1; | |
while (j <= n - i) { | |
cout << ' '; | |
j++; | |
} | |
j = 1; | |
while (j <= 2 * i - 1){ | |
cout << '*'; | |
j++; | |
} | |
cout << endl; | |
i++; | |
} | |
i = N / 2; | |
n = N / 2 + 1; | |
while (i > 0) { | |
j = 1; | |
while (j <= n - i) { | |
cout << ' '; | |
j++; | |
} | |
j = 1; | |
while (j <= 2 * i - 1) { | |
cout << '*'; | |
j++; | |
} | |
cout << endl; | |
i--; | |
} | |
} | |
int main() { | |
pattern_diamond(10); | |
// pattern4(5); | |
// cout << sum_of_evens(6) << endl; | |
// f2c(0, 100, 20); | |
// pattern1(5); | |
// pattern2(5); | |
// pattern3(5); | |
// cout << salary(4567, 'B'); | |
// auto sums = sum_even_odd_digits(1234); | |
// cout << std::get<0>(sums) << " " << std::get<1>(sums) << endl; | |
// C++17 like unpacking | |
// auto [even_sum, odd_sum] = sum_even_odd_digits(1234); | |
// cout << even_sum << " " << odd_sum << endl; | |
// cout << fast_exponent(2, 5) << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment