Skip to content

Instantly share code, notes, and snippets.

View ryasmi's full-sized avatar
🐿️
Checks emails once per day Mon-Fri

Ryan Smith ryasmi

🐿️
Checks emails once per day Mon-Fri
View GitHub Profile
@ryasmi
ryasmi / primes.js
Last active December 10, 2015 18:28
Tests if a number is prime using JavaScript/Python. The function isPrime will return 0 if the value passed is prime and it's lowest prime divisor or 1 if it is not.
function testDivisors(value, divisor1, divisor2) {
if ((value != divisor1) && (value % divisor1 === 0)) {
return divisor1;
} else if (value != divisor2 && value % divisor2 === 0) {
return divisor2;
} else {
return 0;
}
}
@ryasmi
ryasmi / MutliKeyPress.vb
Last active December 10, 2015 18:29
Handling multiple key presses in Visual Basic. Useful for controls in games and shortcut keys. Based on my YouTube tutorial (http://www.youtube.com/watch?v=G6ZbQ1kpAtQ&list=PL63C2673281C99172&index=4).
' Tutorial @ http://www.youtube.com/watch?v=G6ZbQ1kpAtQ&list=PL63C2673281C99172&index=4
Private Sub _KeyDown(ByVal Sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyValue = Keys.W Then
Label1.Text = "W Pressed"
End If
If e.KeyValue = Keys.S Then
Label2.Text = "S Pressed"
End If
End Sub
@ryasmi
ryasmi / receiver.vb
Last active October 14, 2019 17:31
Lan Chat basics in Visual Basic using the TCP. The code below is based on my YouTube tutorial (http://www.youtube.com/watch?v=Pd8TGRrDDMc&list=PL63C2673281C99172&index=3).
' Tutorial @ http://www.youtube.com/watch?v=Pd8TGRrDDMc&list=PL63C2673281C99172&index=3
Option Explicit On
Imports System.IO
Imports System.Net.Sockets
Public Class Form1
Dim Listener As New TcpListener(8000)
Dim Client As TcpClient
Private Sub _FormClosing() Handles Me.FormClosing
@ryasmi
ryasmi / read-write.vb
Last active December 10, 2015 18:38
This Gist is for reading and writing files in Visual Basic and is based on my YouTube tutorial (http://www.youtube.com/watch?v=1TKdNy2lDH0&list=PL63C2673281C99172&index=2).
' Tutorial @ http://www.youtube.com/watch?v=1TKdNy2lDH0&list=PL63C2673281C99172&index=2
Imports System.IO
Public Class Form1
Private Sub readFile() Handles Button1.Click
Dim Location As String = ""
Dim Reader As New StreamReader(location, false)
TextBox1.Text = Reader.ReadToEnd
Reader.Close()
End Sub
@ryasmi
ryasmi / NoForeignKey.js
Last active December 11, 2015 01:38
Demo of how foreign keys are not required to link objects in Python and JavaScript because of Object references.
var x, y, Xs, Ys;
// Assign objects.
x = {
"id": 0,
"Ys": []
};
y = {
"id": 0,
@ryasmi
ryasmi / passwordSealer.js
Last active December 11, 2015 03:58
An example of using Douglas Crockford's JavaScript Sealer/Unsealer method for securely authenticating users. http://www.yuiblog.com/blog/2010/02/24/video-crockonjs-3/
var makeSealer = function () {
var users = [], passwords = [];
return {
sealer: function (username, password) {
var i = users.length,
user = {username: username};
users[i] = user;
passwords[i] = password;
return user;
@ryasmi
ryasmi / decToFloat.py
Created January 16, 2013 14:27
Converting decimal numbers into floating point binary (Excess 64 format).
import math
# This solution assumes valid input in terms of type and range.
# Main returns the sign, exponent and mantissa in "Excess 64" format.
def main():
value = float(input("Enter a number. "))
sign = "0"
if value < 0:
sign = "1"
@ryasmi
ryasmi / recursiveAssignment.js
Last active December 11, 2015 07:18
This runs a recursive function and assigns the result of the recursions to a variable without leaking the function in global scope.
var foo = (function me(x) {
if (x < 10) {
return me(x + 1);
}
else {
return x;
}
}(1));
// In this case foo will be equal to 10.
@ryasmi
ryasmi / sqrt.coffee
Last active December 11, 2015 09:28
Approximates the square root of any number. Note that the approximation will be an approximation for numbers that contain a large number of digits. Example use sqrt(16) will return 4.
sqrt = (() ->
sqrtIter = (guess, x) ->
if goodGuess(guess, x) then guess
else sqrtIter((guess + x / guess) / 2, x)
goodGuess = (guess, x) -> Math.abs(guess * guess - x) / x < 0.0000001
(x) -> sqrtIter(1.0, x)
)()
@ryasmi
ryasmi / triangular.js
Last active February 18, 2019 12:46
The function triangular returns the triangular number of the inputted value. For example triangular(3) would return 6 because 1 + 2 + 3 is equal to 6. This function accounts for both negative values and zero.
var triangular = function (value) {
var abs = Math.abs(value);
return ((abs / 2) * (abs + 1)) * (abs / value) || 0;
};
// Testing code.
var testTriangular = function () {
var testTriangularValue = function (arg, value, id) {
console.log(triangular(arg) === value ? "Test " + id + " passed." : "Test " + id + " failed.");
};