Skip to content

Instantly share code, notes, and snippets.

View harryhanYuhao's full-sized avatar
🎯
Focusing

Harry Han harryhanYuhao

🎯
Focusing
View GitHub Profile
use std::fmt;
/// representing each term of the polynomial
struct Nomial {
coefficient: f64,
power: f64,
}
impl Nomial {
fn new(coefficient: f64, power: f64) -> Self {
@harryhanYuhao
harryhanYuhao / showip.c
Created March 31, 2024 15:02
Get Ip address with syscalls
/*
** showip.c -- show IP addresses for a host given on the command line
** showip google.com
** Credit: Beej's Guide to Network Programming
*/
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
// cargo add lettre
use lettre::address::Address;
use lettre::message::header::ContentType;
use lettre::message::Mailbox;
use lettre::transport::smtp::authentication::Credentials;
use lettre::{Message, SmtpTransport, Transport};
struct EmailInfo {
credential_username: String,
credential_password: String,
@harryhanYuhao
harryhanYuhao / execbash.go
Created February 25, 2024 13:33
go execBash
package execBash
import (
"bytes"
"os/exec"
)
func Execute(command string) (string, string, error) {
const ShellToUse = "bash"
var stdout bytes.Buffer
@harryhanYuhao
harryhanYuhao / binarySearch.go
Created February 5, 2024 22:31
binary search in go
// Expected input: a sorted array
// Return: index n, such that array[n] <= target < array[n+1]
func binarySearch(array []uint64, target uint64) (int, error) {
var lower, upper = 0, len(array) - 1
var preLower, preUpper = 0, len(array) - 1
for array[lower] > target || array[lower+1] <= target {
if array[lower+1] == target {
return lower + 1, nil
}
@harryhanYuhao
harryhanYuhao / csv.go
Last active February 5, 2024 20:10
go read/write csv
import (
"encoding/csv"
"log"
"os"
)
func readCsvFile(filePath string) [][]string {
f, err := os.Open(filePath)
if err != nil {
log.Fatal("Unable to read input file "+filePath, err)
@harryhanYuhao
harryhanYuhao / server.go
Last active February 25, 2024 13:36
Go minimal server
// to run: go mod init <modname>; go tidy; go run .
package main
import (
"fmt"
"log"
"net/http"
"github.com/davecgh/go-spew/spew"
@harryhanYuhao
harryhanYuhao / getline.c
Last active January 9, 2024 22:03
getline
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
ssize_t getline(char **restrict lineptr, size_t *restrict n,
FILE *restrict stream) {
register char c;
@harryhanYuhao
harryhanYuhao / gist:20bfa03e0272bc6ba433b5e1b9296574
Created October 30, 2023 13:41
i3status bar default config v2.14
# i3status configuration file.
# see "man i3status" for documentation.
# It is important that this file is edited as UTF-8.
# The following line should contain a sharp s:
# ß
# If the above line is not correctly displayed, fix your editor first!
general {
colors = true
@harryhanYuhao
harryhanYuhao / Makefile
Created August 2, 2023 12:25
clang makefile
CC=clang
CWARNINGS=-Wall -Wextra -Wpedantic -Wshadow -Wpointer-arith -Wcast-align \
COPTIMISING=-O0 -ggdb3 -fno-omit-frame-pointer -fno-common -fstrict-aliasing
CFLAGS=-std=c11 $(CWARNINGS) $(COPTIMISING)
INSTALL_DIR=$(HOME)/psu/bin