Skip to content

Instantly share code, notes, and snippets.

View arpit-omprakash's full-sized avatar

Arpit Omprakash arpit-omprakash

View GitHub Profile
@arpit-omprakash
arpit-omprakash / happy numbers
Last active September 16, 2019 15:27
Happy numbers question from Project Euler
# Necessary imports
from math import pow
# List to store the values
happylist = []
# Function to check if the number 'n' ia a happy number
def is_happy(n):
  m = int(n)
  if m == 4:
@arpit-omprakash
arpit-omprakash / snake.py
Created January 28, 2020 19:53
The classic snake game made using python and turtle
# A very simple snake game
# By aceking007
# Imports
import turtle
import time
import random
# Score and delay
@arpit-omprakash
arpit-omprakash / game_screen.py
Created February 2, 2020 11:21
A simple screen using pygame
import pygame as pg
# Colors
red = pg.Color(255, 0, 0)
green = pg.Color(0, 255, 0)
black = pg.Color(0, 0, 0)
white = pg.Color(255, 255, 255)
brown = pg.Color(165, 42, 42)
# Variables
screen_width = 400
@arpit-omprakash
arpit-omprakash / Food.py
Last active February 2, 2020 12:13
A class for food objects
# The class for food object
class Food():
# Initialization
def __init__(self):
self.x = screen_width/2
self.y = screen_height/4
self.color = red
self.width = 10
self.height = 10
# Makes the food visible
@arpit-omprakash
arpit-omprakash / Player.py
Last active February 2, 2020 12:55
A player for the game of snake
# The snake object
class Player():
# Initialization
def __init__(self):
self.x = screen_width/2
self.y = screen_height/2
self.width = 10
self.height = 10
self.velocity = 10
self.direction = 'stop'
@arpit-omprakash
arpit-omprakash / gameloop.py
Created February 2, 2020 16:02
The final parts of snake
score, high_score = (0,0)
# Draw the score on the screen
def draw_score(surface):
global high_score
font_name = pg.font.match_font('arial')
if score > high_score:
high_score = score
# writing the score
font = pg.font.Font(font_name, 18)
text_surface = font.render('Score: {} High Score: {}'.format(score, high_score), True, white)
@arpit-omprakash
arpit-omprakash / julia_pycall.jl
Created February 12, 2020 06:20
Example of PyCall library in Julia
module MyModule
using PyCall
function __init__()
py """
import numpy as np
def one(x):
return np.sin(x) ** 2 + np.cos(x) ** 2
@arpit-omprakash
arpit-omprakash / binary_search.py
Last active February 12, 2020 07:23
Binary search recursive
def binarySearch (arr, l, r, x):
if r >= l:
mid = l + (r - l)/2
if arr[mid] == x:
return mid
elif arr[mid] > x:
return binarySearch(arr, l, mid-1, x)
else:
return binarySearch(arr, mid+1, r, x)
else:
@arpit-omprakash
arpit-omprakash / binary_search.jl
Created February 12, 2020 07:20
Binary search in Julia
function binarysearch(lst::Vector{T}, value::T, low=1, high=length(lst)) where T
if isempty(lst) return 0 end
if low ≥ high
if low > high || lst[low] != value
return 0
else
return low
end
end
mid = (low + high) ÷ 2
@arpit-omprakash
arpit-omprakash / drift.py
Created March 1, 2020 07:09
Simulating genetic drift using python
import random
import sys
generations = 5000
pop_size = 1000
gamete_no = 1000
pop = []
n = len(sys.argv)
if n == 2: