Skip to content

Instantly share code, notes, and snippets.

/*
here case1 is always true before case2 (i.e. 1st call of help -> case1 is true, 2nd call of help -> case2 true).
So, in the 2nd call, case2 is true where we're using exp, ctinfoPrev, saddr_m what have been initialized before in case1.
Note: these structures are allocated in module init function which is called when the module is loaded
*/
union nf_inet_addr *saddr_m;
struct sk_buff* skbPrev;
enum ip_conntrack_info ctinfoPrev;
struct nf_conntrack_expect *exp;
// https://leetcode.com/problems/implement-trie-prefix-tree/description/
class TNode{
public:
bool isWord;
map< char, TNode* > child;
TNode() {
isWord = false;
}
};
@shiponcs
shiponcs / remove_dups.cpp
Created February 25, 2023 17:57
Remove All Adjacent Duplicates In String- solve using stack
string removeDuplicates(string toCleanUp) {
std:stack< char > str_stack;
for(char x : toCleanUp) {
if( !str_stack.empty() && x == str_stack.top() ) str_stack.pop();
else str_stack.push( x );
}
string res;
res.reserve(str_stack.size()); // reserve the space so that the concatenation speeds up.
@shiponcs
shiponcs / sol.c
Created February 20, 2023 11:29
Write a program that reads twelve strings from fruits.txt, reverses each string, and outputs them to output/outfile.txt
#include <stdio.h>
int main()
{
FILE *fp;
FILE* fout;
int numfruits=12;
char myFruits[numfruits][256];
char myFruitsReversed[numfruits][256];
@shiponcs
shiponcs / prime_gen.c
Created February 18, 2023 02:40
Implemtation of Sieve of Eratosthenes and get nth prime number
#include <stdio.h>
#include <math.h>
#define mx 10001
int status[mx];
int store[1001];
void preCalc(){
status[1] = 1;
for (int i = 4; i <= mx; i += 2) status[i] = 1;
@shiponcs
shiponcs / discussion.md
Last active February 17, 2023 09:11
Newton's method of finding root of a number (e.g. 612)

The equation to find the slope or first derivative of a function f(x):

$$f'(x_k) = {f(x) - f(x_k) \over x − x_k}$$ It only requires basic understanding of Function and Differential Calculus to comprehend how it works.

As we will find the root, f(x) needs to be 0 meaning the function graph will instersect the X-axis. putting f(x) = 0 and simplifying the equation we get $$x = x_k − {f(x_k) \over f′(x_k)}$$

We can rewrite the equation as recurrent relation: $$x_{k+1} = x_k − {f(x_k) \over f′(x_k)}$$

@shiponcs
shiponcs / .config
Created January 7, 2023 09:52
OpeWRT .config file
#
# Automatically generated file; DO NOT EDIT.
# OpenWrt Configuration
#
CONFIG_MODULES=y
CONFIG_HAVE_DOT_CONFIG=y
CONFIG_HOST_OS_LINUX=y
# CONFIG_HOST_OS_MACOS is not set
# CONFIG_TARGET_sunxi is not set
# CONFIG_TARGET_apm821xx is not set
@shiponcs
shiponcs / maxpool.cpp
Created November 9, 2022 19:19
It first draws a circle in 500x500 image, then reduces the dimension using Max Pool technique and saves as BMP file
// read about bmp file format: http://www.ece.ualberta.ca/~elliott/ee552/studentAppNotes/2003_w/misc/bmp_file_format/bmp_file_format.htm
#include <stdio.h>
#include <GL/gl.h>
#include <GL/glut.h>
#include <iostream>
#include <stdlib.h>
float x=0,y,x2,y2,m,i,j,p;
int dx=0,dy=0,r;
@shiponcs
shiponcs / fill_unfill_a_cirlce.c
Created November 8, 2022 06:03
this code first shows how fill a circle using flood fill (implemented using BFS) and then remove the filling
#include <stdio.h>
#include <GL/gl.h>
#include <GL/glut.h>
#include <queue>
#include <utility>
#include <cmath>
#include <map>
#include <iostream>
#include <string.h>
@shiponcs
shiponcs / draw-and-save-circle-as-bmp.c
Last active November 6, 2022 04:12
draw a circle in OpenGL and save the render as bmp file
// read about bmp file format: http://www.ece.ualberta.ca/~elliott/ee552/studentAppNotes/2003_w/misc/bmp_file_format/bmp_file_format.htm
#include <stdio.h>
#include <GL/gl.h>
#include <GL/glut.h>
#include <iostream>
#include <stdlib.h>
float x=0,y,x2,y2,m,i,j,p;
int dx=0,dy=0,r;