Skip to content

Instantly share code, notes, and snippets.

Avatar
🤔
<Comprehending>

Kushagra Bansal Kush1101

🤔
<Comprehending>
View GitHub Profile
View Project Euler Problem 37 solution
"""
Problem 37 on project Euler
https://projecteuler.net/problem=37
"""
"""
Note: This code takes about 2 seconds to execute. You ca reduce this time by adapting to
faster methods of checking if a number is prime or not like the Seive method.
But this code is intended to be beginner friendly so I have used the simple way to check for primes.
"""
View Project Euler Problem 52 solution
import time
import sys
a=time.time()
def same(n):
return sorted(str(n))==sorted(str(2*n)) ==\
sorted(str(3*n))==sorted(str(4*n))==sorted(str(5*n))
"""
Here I have tried to optimize the performance by chekcing only the numbers from n to 2n
View MiiStringFuck_example.py
MiniStringFuck_interpreter(+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.)
#>> Prints all the uppercase English Alphabets.
View MiniStringFuck_interpreter.py
def MiniStringFuck_interpreter(code):
memory = 0 #initialize the memory cell value at zero
output="" #output string
for char in code:
if char=="+":
memory+=1
if memory>255:
memory=memory%256
if char==".":
output+=chr(memory)
@Kush1101
Kush1101 / timeit_example1.py
Created September 1, 2020 06:40
Medium_timeit
View timeit_example1.py
import timeit
#This code will be executed only once, before stmt.
setup_code = "from math import sqrt"
#This code will be executed as specified by the parameter 'number'
stmt_code = "sum(sqrt(x) for x in range(1,10000))"
iterations = 10000
time = timeit.timeit(stmt = stmt_code, setup = setup_code, number = iterations)
View timeit_example2.py
def bubble_sort(arr):
for i in range(len(arr)):
for j in range(0, len(arr) - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
return arr
def make_random_array():
from random import randint
View measure_time1.py
import time
start_time = time.time()
"""
Some Code
"""
end_time = time.time()
print(f"The execution time is: {end_time-start_time}")
View measure_time2.py
import time
def function_1(*parameters):
#do something
# return value
def function_2(*parameters):
#do something
#return value
#measuring time taken by function_1
start_of_f1 = time.time()
View count1.py
from itertools import count
for i in count(0,2):
print(i, end = " ")
if i==20:
break
# OUTPUT
'''
0 2 4 6 8 10 12 14 16 18 20
'''
View count2.py
from itertools import count
count_floating_negative = count(start=0.5, step = -0.5)
print(list(next(count_floating_negative) for _ in range(10)))
# OUTPUT
'''
[0.5, 0.0, -0.5, -1.0, -1.5, -2.0, -2.5, -3.0, -3.5, -4.0]
'''