Skip to content

Instantly share code, notes, and snippets.

@TheEyesightDim
TheEyesightDim / inputcheck.cpp
Last active March 1, 2017 08:05
Detect invalid input and clear input stream, C++ STL
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;
}
@TheEyesightDim
TheEyesightDim / tuples.cpp
Last active July 1, 2018 08:23
Example of working with tuples to return multiple values; Example of using tuples in place of structs
#include <tuple>
#include <iostream>
using std::tuple;
using std::tie;
using std::make_tuple;
using std::cout;
class rectangle
{
@TheEyesightDim
TheEyesightDim / header.h
Last active July 1, 2018 08:26
Demonstrates the use of a variadic template function within a class template.
#ifndef HEADER_H
#define HEADER_H
#include <utility>
#include <algorithm>
#include <array>
template <typename T>
class MyClass
{
//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);
}
@TheEyesightDim
TheEyesightDim / point_inversion.cc
Last active July 1, 2018 03:34
Inversion of a point around another point
//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){
@TheEyesightDim
TheEyesightDim / ostream_itr_example.cpp
Last active July 1, 2018 00:34
Example of using ostream iterators with standard algorithms to produce interesting results.
#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 , " "));
}
<?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){
@TheEyesightDim
TheEyesightDim / rot_n_string.js
Last active December 17, 2018 19:01
Rotate alphabet characters in a string by an arbitrary amount
//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);
}
@TheEyesightDim
TheEyesightDim / into_bytes.cpp
Last active September 5, 2019 12:02
Converts some type into its representation as a byte array.
#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;
}
@TheEyesightDim
TheEyesightDim / miller_rabin.py
Created November 8, 2019 01:28
Python implementation of Miller-Rabin primality test
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: