Skip to content

Instantly share code, notes, and snippets.

#include <stdexcept>
#include <iostream>
#include <memory>
#include <cstring>
#include <cmath>
using namespace std;
template<class T>
class RingBuffer;
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
// returns true if A is a fading palindome
bool ifp(char* A) {
int i, n = strlen(A);
for (i = 0; i < n/2; ++i) {
// we need to check the palindome invariant A[i] == A[n-i-1] and
@dgodfrey206
dgodfrey206 / stairbowtie.cpp
Last active November 30, 2024 02:41
Making stairs and a bow tie
#include <iostream>
using namespace std;
void makeBowTie(int h) {
int i, j, k, s;
for (i = 1; i <= h; ++i) {
s = 2*i-1;
s = h - abs(h - s);
for (j = 0; j < s; ++j) cout << '*';
for (j = 0; j < 2*h - 2*s; ++j) cout << ' ';
@dgodfrey206
dgodfrey206 / oddsumsub.cpp
Last active September 6, 2016 20:54
Write a recursive function that takes an integer n as a parameter and returns the following sum/subtraction of odd numbers 1 - 3 + 5 - 7 + 9....+/- n (if n is even, the last term is n-1)
#include <iostream>
#include <cmath>
using namespace std;
// 1 - 3 + 5 - 7 + 9 ... +/- n
// recursive
int f1(int n) {
if (n == 1) return 1;
if (n % 2 == 0) n--;
@dgodfrey206
dgodfrey206 / rotated_binary_search.cpp
Created August 19, 2016 21:17
Binary search on k-rotated array
#include <iostream>
int at(int* arr, int n, int k, int m) {
return arr[((m + (n - (k % n))) % n)];
}
int rotated_binary_search(int* arr, int n, int k, int key) {
int low = 0, high = n-1, mid, v;
while (low <= high) {
@dgodfrey206
dgodfrey206 / rotationidx.cpp
Last active August 19, 2016 02:31
Formula to generate index of element of array after k insertions
#include <iostream>
using namespace std;
// returns the element at index m in arr after k rotations
int at(int* arr, int n, int k, int m) {
return arr[((m + (n - (k % n))) % n)];
}
int main() {
int n, k, q, i, j, m;
@dgodfrey206
dgodfrey206 / liststuff.cpp
Created August 15, 2016 21:11
Random linked list stuff
#include<iostream>
struct node{
int data;
node* next;
};
node* NewNode(int data){
node* n=new node;
n->data=data;
@dgodfrey206
dgodfrey206 / downup.cpp
Last active March 26, 2022 16:12
Prints string removing tail then adds tail
#include <cstring>
using namespace std;
void downup(const char str[]) {
int n = strlen(str);
int i, j;
for (i = 0; i < n; ++i) {
for (j = 0; j < n-i; ++j)
cout << str[j];
cout << '\n';
@dgodfrey206
dgodfrey206 / GuessNumber.cpp
Created July 27, 2016 23:04
A console based number guessing game
// NumberGuessing.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <vector>
#include <numeric>
#include <limits>
#include <string>
struct ListNode {
int val;
struct ListNode* next;
ListNode(int val) : val(val), next(0) {}
};
struct ListNode* newNode(int data) {
return new struct ListNode(data);
}