Skip to content

Instantly share code, notes, and snippets.

View iamshadmirza's full-sized avatar
👨‍💻
Tap Tap Tap

Shad Mirza iamshadmirza

👨‍💻
Tap Tap Tap
View GitHub Profile
@iamshadmirza
iamshadmirza / stonepaperscissor.py
Created February 19, 2018 12:03
Interacting with python
import random
def mainfunction():
while True:
rnum = random.randrange(1,4)
cpuschoice = numtochoice(rnum)
userscoice = input("Choose between stone, paper and scissor\n")
print('Cpu has choosen : ', cpuschoice)
print(decision(userscoice.lower(), cpuschoice))
def numtochoice(num):
@iamshadmirza
iamshadmirza / guessthenumber.py
Created February 19, 2018 14:24
beginning with python
import random
cpunum = random.randrange(1,9)
usernum = 0
count = 0
print('guess a number between 1 and 9')
while True:
usernum = input()
count += 1
if usernum== 'exit':
break
@iamshadmirza
iamshadmirza / listcomprehension.py
Created February 19, 2018 15:29
Intersection on two lists
import random
a= random.sample(range(1,30), 7)
b= random.sample(range(1,30), 10)
result1= []
result2 = []
result3 = []
for element in a:
if element in b:
result1.append(element)
print(result1)
@iamshadmirza
iamshadmirza / checkprime.py
Created February 21, 2018 08:33
prime, non prime and composite
userinput = int(input("Enter the number to check prime\n"))
a = []
if userinput == 1:
print("One is not a prime nor a composite")
else:
if userinput == 2 or userinput%2 != 0:
for x in range(1,userinput):
if userinput%x==0 :
a.append(x)
if len(a)==1:
@iamshadmirza
iamshadmirza / listclipper.py
Created February 21, 2018 09:39
output first and last element of list
def inserlist(givenlist):
first, *middle, last = givenlist
newlist = [first, last]
return newlist
list = [12,45,78,45,96,67,81,39]
print("List to be clipped is",list)
print("First and the last element of clip are",inserlist(list))
(function(){
let food = [
{type: 'beverage', name: 'tea'},
{type: 'veg', name: 'aloo paratha'},
{type: 'non-veg', name: 'biryani'},
{type: 'non-veg', name: 'butter chicken'}
];
const filterFunction = (string) => (item) => item.type === string;
@iamshadmirza
iamshadmirza / dayOffset.js
Created December 3, 2019 11:09
Solution of weekly problem from Cassidy Williams's Newsletter
// Interview question of the week by Cassidy Williams's Newsletter
// This week's question courtesy of Louis:
// Write a function that takes the name of a day (e.g "Monday", "Tuesday")
// and an integer offset, and returns the name of the day which would be the original + offset.
// Extra credit: do it in 1 line!
// Example:
// > dayOffset("Wednesday", 4)
// > "Sunday"
@iamshadmirza
iamshadmirza / memoizedStair.js
Created December 4, 2019 01:41
Cache recursive calls to optimize runtime
function stair(n){
if(n === 1){ return 1; }
if(n === 2){ return 2; }
return memoizedStair(n-1) + memoizedStair(n-2);
}
function memoize(fn){
const cache = {};
return function(...args){
if(cache[args]){
@iamshadmirza
iamshadmirza / memoizedCutRod.js
Created December 4, 2019 02:29
Optimized recursive solution for cut rod problem
function cutRod(n, prices){
if(n === 0){ return 0; }
let maxRevenue = -1;
for( let i = 0; i < n; i++){
maxRevenue = Math.max(maxRevenue, prices[n- i - 1] + memoizedCutRod(i, prices));
}
return maxRevenue;
}
function memoize(fn){
@iamshadmirza
iamshadmirza / recurseUnderscore.js
Created December 6, 2019 02:38
Iterate through object and flatten it.
//Iterate through user object and flatter in one object.
//append _ on every level
var user = {
name: "Shad",
address: {
personal: {
city: "Varanasi",
area: "Chauhatta",
},
office: {