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
do{ | |
cout<<"Input \"1\" or \"0\": "; | |
cin >> scenario; | |
if(cin.fail() || scenario < 0 || scenario > 1){ | |
cin.clear(); //clear failure state | |
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //ignore and discard failed input up to and including newline | |
cout << "\nInput failed. Trying again..." << endl; | |
continue; | |
} |
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 <tuple> | |
#include <iostream> | |
using std::tuple; | |
using std::tie; | |
using std::make_tuple; | |
using std::cout; | |
class rectangle | |
{ |
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
#ifndef HEADER_H | |
#define HEADER_H | |
#include <utility> | |
#include <algorithm> | |
#include <array> | |
template <typename T> | |
class MyClass | |
{ |
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
//this function converts any member of enum of any underlying type to that type. | |
//Effective Modern C++ by Scott Meyers. See item 10 | |
template <typename E> | |
constexpr auto to_underlying(E e) noexcept | |
{ | |
return static_cast<std::underlying_type_t<E>>(e); | |
} |
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
//point inversion | |
#include <vector> | |
#include <iostream> | |
struct Point { int x, y; }; | |
struct Line { Point petal, center; }; | |
//reflects Point 'mover' across another Point 'focus'. | |
//'focus' is the midpoint of 'mover' and the return value. | |
Point invert_point(const Point mover, const Point focus){ |
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 <vector> | |
#include <iostream> | |
#include <iterator> | |
#include <algorithm> | |
int main() | |
{ | |
std::vector<int> v {7,4,1,8,5,2,9,6,3}; | |
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout , " ")); | |
} |
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
<?php | |
require 'vendor/autoload.php'; | |
use Symfony\Component\Stopwatch\Stopwatch; | |
/** | |
* @param callable $callable is a function or functor callable with parentheses. | |
* @param array ...$args is the argument list which will be supplied to the $callable function. | |
* @return float|int which represents the time elapsed in the function called, in milliseconds. | |
*/ | |
function benchmarker(Callable $callable, ...$args){ |
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
//needed to make friendly for map function, which expects function taking dereferenced string index as argument | |
function charCodeOfChar(character){ | |
return character.charCodeAt(0); | |
} | |
//String's prototype function will give weird UTF-16 literal output otherwise | |
function charFromCharCode(charCode){ | |
return String.fromCharCode(charCode); | |
} |
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 <algorithm> | |
#include <array> | |
template <typename T> | |
constexpr std::array<char, sizeof(T)> into_bytes(const T& t){ | |
std::array<char, sizeof(T)> bytes; | |
std::copy( reinterpret_cast<const char*>(&t), reinterpret_cast<const char*>(&t) + sizeof(t), bytes.begin() ); | |
return bytes; | |
} |
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
def is_prime(n): | |
"""A very basic implementation of the Miller-Rabin primality test.""" | |
#from Wikipedia: "if n < 341,550,071,728,321, it is enough to test a = 2, 3, 5, 7, 11, 13, and 17." | |
small_primes = {2,3,5,7,11,13,17} | |
if n in small_primes: return True | |
if n < 2 or n & 1 == 0: return False | |
r, d = (0, n-1) | |
while (d & 1) == 0: |
OlderNewer