Skip to main content

Remove numbers from files

0 likes • Mar 26, 2023 • 0 views
Python
Loading...

More Python Posts

Untitled

0 likes • Apr 21, 2023 • 0 views
Python
print("hellur")

check prop lambda

0 likes • Nov 19, 2022 • 0 views
Python
def check_prop(fn, prop):
return lambda obj: fn(obj[prop])
check_age = check_prop(lambda x: x >= 18, 'age')
user = {'name': 'Mark', 'age': 18}
check_age(user) # True

Return Letter Combinations

0 likes • Nov 18, 2022 • 0 views
Python
# @return a list of strings, [s1, s2]
def letterCombinations(self, digits):
if '' == digits: return []
kvmaps = {
'2': 'abc',
'3': 'def',
'4': 'ghi',
'5': 'jkl',
'6': 'mno',
'7': 'pqrs',
'8': 'tuv',
'9': 'wxyz'
}
return reduce(lambda acc, digit: [x + y for x in acc for y in kvmaps[digit]], digits, [''])

sum of powers

0 likes • Nov 19, 2022 • 0 views
Python
def sum_of_powers(end, power = 2, start = 1):
return sum([(i) ** power for i in range(start, end + 1)])
sum_of_powers(10) # 385
sum_of_powers(10, 3) # 3025
sum_of_powers(10, 3, 5) # 2925

read file contents into a list

0 likes • Jun 1, 2023 • 0 views
Python
filename = "data.txt"
with open(filename, "r") as file:
file_contents = file.readlines()
file_contents = [line.strip() for line in file_contents]
print("File contents:")
for line in file_contents:
print(line)

Factorial of N

0 likes • Nov 19, 2022 • 0 views
Python
import math
def factorial(n):
print(math.factorial(n))
return (math.factorial(n))
factorial(5)
factorial(10)
factorial(15)