Skip to content

Instantly share code, notes, and snippets.

a = int(input("Side a = "))
b = int(input("Side b = "))
c = int(input("Side c = "))
sides = []
sides.append(a)
sides.append(b)
sides.append(c)
sides.sort()
@solen003
solen003 / fibonacci_lru_cache.py
Created July 15, 2018 09:37
use of lru_cache
from functools import lru_cache
@lru_cache(maxsize = 1000)
# default is 128 values
def fibonacci3(n):
if n == 1 or n == 2:
return 1
elif n > 2:
return fibonacci3(n-1) + fibonacci3(n-2)
import random
def random_walk2(n):
"""n number of blocks to walk"""
x, y = 0, 0
for i in range(1, n):
(dx, dy) = random.choice([(0, 1), (0, -1), (-1, 0), (1, 0)])
x = x + dx
y = y + dy
movies2 = [("SW", 1992), ("GWR", 1999), ("PIY", 2010), ("AGE", 2015),
("GIT", 1995), ("BUT", 1970), ("GGG", 2001)]
pre2k = [i[0] for i in movies2 if i[1] < 2000]
print(pre2k)
pre2k2 = [title for (title, year) in movies2 if year < 2000]
print(pre2k2)
@solen003
solen003 / by7_notby5.py
Last active March 5, 2022 15:18
Write a program which will find all such numbers which are divisible by 7 but are not a multiple of 5, between 2000 and 3200 (both included). The numbers obtained should be printed in a comma-separated sequence on a single line.
#Write a program which will find all such numbers which are divisible
# by 7 but are not a multiple of 5,
#between 2000 and 3200 (both included).
#The numbers obtained should be printed in a comma-separated sequence
#on a single line.
by7_notby5 = [i for i in range(2000, 3201) if (i % 7 == 0) and (i % 5 != 0)]
print(by7_notby5)
@solen003
solen003 / factorial.py
Created July 15, 2018 13:29
Write a program which can compute the factorial of a given numbers. The results should be printed in a comma-separated sequence on a single line.
#Write a program which can compute the factorial of a given numbers.
#The results should be printed in a comma-separated sequence on a single line.
def factorial(n):
value = 1
for i in range(1, n+1):
value = value * i
return value
number = int(input("Type a number: "))
@solen003
solen003 / numberDict.py
Created July 15, 2018 13:36
With a given integral number n, write a program to generate a dictionary that contains (i, i*i) such that is an integral number between 1 and n (both included). and then the program should print the dictionary. Suppose the following input is supplied to the program: 8 Then, the output should be: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}
number = int(input("Type a number: "))
numberDict = {}
for i in range(1, number+1):
numberDict[i] = i*i
print(numberDict)
@solen003
solen003 / str_to_tuple_list.py
Created July 15, 2018 14:05
Write a program which accepts a sequence of comma-separated numbers from console and generate a list and a tuple which contains every number. Suppose the following input is supplied to the program: 34,67,55,33,12,98 Then, the output should be: ['34', '67', '55', '33', '12', '98'] ('34', '67', '55', '33', '12', '98')
numbers = input("Type in numbers seperated only by a comma :")
numbers_split = numbers.split(',')
number_tuple = tuple(numbers_split)
print(number_tuple)
print(numbers_split)
@solen003
solen003 / q_formula.py
Created July 15, 2018 14:31
Write a program that calculates and prints the value according to the given formula: Q = Square root of [(2 * C * D)/H] Following are the fixed values of C and H: C is 50. H is 30. D is the variable whose values should be input to your program in a comma-separated sequence. Example Let us assume the following comma separated input sequence is gi…
import math
numbers = input("Provide D: ")
numbers = numbers.split(',')
result_list = []
for D in numbers:
Q = round(math.sqrt(2 * 50 * int(D) / 30))
result_list.append(Q)
@solen003
solen003 / abc_order.py
Created July 15, 2018 20:35
Write a program that accepts a comma separated sequence of words as input and prints the words in a comma-separated sequence after sorting them alphabetically. Suppose the following input is supplied to the program: without,hello,bag,world Then, the output should be: bag,hello,without,world
phrase = input("Input words: ")
phrase_list = phrase.split(",")
phrase_list.sort()
print((', ').join(phrase_list))