Skip to main content

Sherlock Holmes Curious Lockbox Solver

0 likes • Mar 12, 2021 • 0 views
Python
Loading...

More Python Posts

two-digit integer

0 likes • Feb 26, 2023 • 0 views
Python
#You are given a two-digit integer n. Return the sum of its digits.
#Example
#For n = 29 the output should be solution (n) = 11
def solution(n):
return (n//10 + n%10)

Copy file to destination

0 likes • Nov 18, 2022 • 1 view
Python
# importing the modules
import os
import shutil
# getting the current working directory
src_dir = os.getcwd()
# printing current directory
print(src_dir)
# copying the files
shutil.copyfile('test.txt', 'test.txt.copy2') #copy src to dst
# printing the list of new files
print(os.listdir())

Create a Floyd’s Triangle

0 likes • May 31, 2023 • 0 views
Python
def generate_floyds_triangle(num_rows):
triangle = []
number = 1
for row in range(num_rows):
current_row = []
for _ in range(row + 1):
current_row.append(number)
number += 1
triangle.append(current_row)
return triangle
def display_floyds_triangle(triangle):
for row in triangle:
for number in row:
print(number, end=" ")
print()
# Prompt the user for the number of rows
num_rows = int(input("Enter the number of rows for Floyd's Triangle: "))
# Generate Floyd's Triangle
floyds_triangle = generate_floyds_triangle(num_rows)
# Display Floyd's Triangle
display_floyds_triangle(floyds_triangle)

collect dictionary

0 likes • Nov 19, 2022 • 0 views
Python
from collections import defaultdict
def collect_dictionary(obj):
inv_obj = defaultdict(list)
for key, value in obj.items():
inv_obj[value].append(key)
return dict(inv_obj)
ages = {
'Peter': 10,
'Isabel': 10,
'Anna': 9,
}
collect_dictionary(ages) # { 10: ['Peter', 'Isabel'], 9: ['Anna'] }

Differentiate Between type() and instance()

0 likes • May 31, 2023 • 0 views
Python
class Rectangle:
pass
class Square(Rectangle):
pass
rectangle = Rectangle()
square = Square()
print(isinstance(rectangle, Rectangle)) # True
print(isinstance(square, Rectangle)) # True
print(isinstance(square, Square)) # True
print(isinstance(rectangle, Square)) # False

Palindrome checker

0 likes • Nov 19, 2022 • 3 views
Python
# function which return reverse of a string
def isPalindrome(s):
return s == s[::-1]
# Driver code
s = "malayalam"
ans = isPalindrome(s)
if ans:
print("Yes")
else:
print("No")