Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 9 additions & 14 deletions py/p1.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
#program to print a tree pattern with asterisks
# eg :- height = 3
#output :- *
# ***
# *****
def asterisk_tree(height, level):
if level > height:
return
print(" " * (height - level), end="")
for j in range(2 * level - 1):
print("*", end="")
print()
asterisk_tree(height, level + 1)

def asterisk_tree(height,level):
if level > height:
return
for j in range(2*level-1):
print("*", end="")
print()
asterisk_tree(height, level + 1)

n=int(input("Enter height of tree : "))
n = int(input("Enter height of tree : "))
asterisk_tree(n, 1)
25 changes: 11 additions & 14 deletions py/p10.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
#Finding Average RGB values using the listmaker fuction to generate list of rgb values for a pixel
def listmaker(r, g, b):
return [r, g, b]

def listmaker(r,g,b,list1=[]):
list1.append(r)
list1.append(g)
list1.append(b)
return list1
pixel1 = listmaker(23, 78, 34)
pixel2 = listmaker(210, 56, 67)
pixel3 = listmaker(23, 78, 248)

pixel1=listmaker(23,78,34)
pixel2=listmaker(210,56,67)
pixel3=listmaker(23,78,248)
r=(pixel1[0]+pixel2[0]+pixel3[0])/3
g=(pixel1[1]+pixel2[1]+pixel3[1])/3
b=(pixel1[2]+pixel2[2]+pixel3[2])/3
rgb=[r,g,b]
print("Average RGB values of the pixels are",rgb)
r = (pixel1[0] + pixel2[0] + pixel3[0]) / 3
g = (pixel1[1] + pixel2[1] + pixel3[1]) / 3
b = (pixel1[2] + pixel2[2] + pixel3[2]) / 3

rgb = [r, g, b]
print("Average RGB values of the pixels are", rgb)
10 changes: 4 additions & 6 deletions py/p11.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# Function to convert decimal to binary number

def binaryconversion(n):
print(n % 2,end = '')
if n > 1:
binaryconversion(n/2)
if n > 1:
binaryconversion(n // 2)
print(n % 2, end='')

number=int(input("Enter Number: "))
number = int(input("Enter Number: "))
binaryconversion(number)
print()
8 changes: 3 additions & 5 deletions py/p12.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Program to multiply two matrices using nested loops

A = [[1,2,3],[4,5,6],[7,8,9]]
B = [[10,11,12],[13,14,15],[16,17,18]]

Expand All @@ -9,8 +7,8 @@
t = len(B)
r = len(B[0])

if(q!=t):
print("Error! Matrix sizes are not compatible")
if q != t:
print("Error! Matrix sizes are not compatible for multiplication")
quit()

C = []
Expand All @@ -24,7 +22,7 @@
for j in range(r):
curr_val = 0
for k in range(q):
curr_val += A[i][k]*B[k][j]
curr_val += A[i][k] * B[k][j]
C[i][j] = curr_val

print(C)
11 changes: 5 additions & 6 deletions py/p2.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#program to print an input as the only member of an array everytime the function is called
# input- 1 -> output- [1], input- 2 -> output- [2]

def add_item(item, items=[]):
items.append(item)
return items
def add_item(item, items=None):
if items is None:
items = []
items.append(item)
return items

print(add_item(1))
print(add_item(2))
18 changes: 6 additions & 12 deletions py/p3.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
#program to remove all even numbers from the beginning of a list
#eg: [2, 4, 6, 17, 10] -> [17, 10]
def delete_starting_evens(lst):
while lst and lst[0] % 2 == 0:
lst = lst[1:]
return lst

def delete_starting_evens(list):
for item in list:
if list[0] % 2 == 0:
list.pop(0)
else:
break
return list

list = [2, 8, 10, 11]
print(delete_starting_evens(list))
lst = [2, 8, 10, 11]
print(delete_starting_evens(lst))
19 changes: 12 additions & 7 deletions py/p4.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
# Python program for counting sort

def countSort(arr):
k = max(arr)
count = [0 for i in range(k+1)]
ans = [0 for i in range(len(arr))]
count = [0 for _ in range(k+1)]
ans = [0 for _ in range(len(arr))]

# Count occurrences of each element
for i in arr:
count[i] += 1
for i in range(k+1):

# Modify count array to contain actual positions in the sorted array
for i in range(1, k+1):
count[i] += count[i-1]

# Place elements in the correct positions in the ans array
for i in range(len(arr)-1, -1, -1):
ans[count[arr[i]]] = arr[i]
ans[count[arr[i]] - 1] = arr[i]
count[arr[i]] -= 1

return ans

arr = [3, 4, 12, 2, 4, 5, 5, 6, 12, 33, 10, 1, 2, 8, 6]
ans = countSort(arr)
print (f"Sorted character array is {ans}")
print(f"Sorted array is {ans}")
7 changes: 2 additions & 5 deletions py/p5.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#recursive program to accept a number between 1 and 10 and print it

def get_number():
val1 = input('Enter a number: ')
try:
Expand All @@ -8,10 +6,9 @@ def get_number():
val1 = input('Enter a number: ')
val1 = int(val1)

str_to_print = '{:.1f}'.format(val1)
return str_to_print
return val1

except ValueError:
get_number()
return get_number()

print(get_number())
20 changes: 9 additions & 11 deletions py/p6.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
#program to perform binary addition

def addBinary(a: str, b: str) -> str:
s = []
carry = 0
i = len(a) - 1
j = len(b) - 1

while i >= 0 or j >= 0:
if i >= 0:
carry += int(a[i])
i -= 1
if j >= 0:
carry += int(b[j])
j -= 1
s.append(str(carry % 2))
carry //= 2
while i >= 0 or j >= 0 or carry:
if i >= 0:
carry += int(a[i])
i -= 1
if j >= 0:
carry += int(b[j])
j -= 1
s.append(str(carry % 2))
carry //= 2

return ''.join(reversed(s))

Expand Down
14 changes: 1 addition & 13 deletions py/p7.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
#program to add two numbers represented by linked lists in reverse order
#e.g. 2->4->3 + 5->6->4 = 7->0->8 (342 + 465 = 807)

# Definition for singly-linked list.
class ListNode(object):
def __init__(self, val=0, next=None):
self.val = val
self.next = next


class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
dummyHead = ListNode(0)
Expand All @@ -29,6 +19,4 @@ def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
l1 = l1.next if l1 is not None else None
l2 = l2.next if l2 is not None else None

result = dummyHead.next
dummyHead.next = None
return result
return dummyHead.next
22 changes: 4 additions & 18 deletions py/p8.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
#Python program to create a doubly linked list and print nodes from beginning

class Node(object):
# Doubly linked node
def __init__(self, data=None, next=None, prev=None):
self.data = data
self.next = next
self.prev = prev

class doubly_linked_list(object):
def __init__(self):
def _init_(self):
self.head = None
self.tail = None
self.count = 0
Expand All @@ -26,16 +17,11 @@ def append_item(self, data):

self.count += 1

def print_foward(self):
print(self.iter())

def iter(self):
# Iterate the list
def print_forward(self):
current = self.head
while current:
item_val = current.data
print(current.data)
current = current.next
yield item_val

items = doubly_linked_list()
items.append_item('C#')
Expand All @@ -45,4 +31,4 @@ def iter(self):
items.append_item('Python')

print("Print Items in the Doubly linked:")
items.print_foward()
items.print_forward()
19 changes: 6 additions & 13 deletions py/p9.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
#Write a Python function to check whether a string is a pangram or not.
#Note : Pangrams are words or sentences containing every letter of the alphabet at least once.

import string
import sys

# Define a function named 'ispangram' that checks if a string is a pangram
def ispangram(str1, alphabet=string.ascii_lowercase):
alphaset = list(alphabet)

str = list(str1.lower())

# Check if all lowercase characters in the input string covers all characters in 'alphaset'
return alphaset <= str
str_set = set(str1.lower())
for char in alphabet:
if char not in str_set:
return False
return True

# Print the result of checking if the string is a pangram by calling the 'ispangram' function
print(ispangram('The quick brown fox jumps over the lazy dog'))
print(ispangram('The quick brown fox jumps over the lazy dog'))