diff --git a/py/p1.py b/py/p1.py index b648ec0..83c7e5c 100644 --- a/py/p1.py +++ b/py/p1.py @@ -4,13 +4,15 @@ # *** # ***** -def asterisk_tree(height,level): - if level > height: - return - 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 _ in range(height - level): + print(" ", end="") + 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) diff --git a/py/p10.py b/py/p10.py index cfc822f..bd445df 100644 --- a/py/p10.py +++ b/py/p10.py @@ -1,16 +1,18 @@ -#Finding Average RGB values using the listmaker fuction to generate list of rgb values for a pixel - -def listmaker(r,g,b,list1=[]): +def listmaker(r, g, b, list1=None): + if list1 is None: + 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) -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) +pixel1 = listmaker(23, 78, 34) +pixel2 = listmaker(210, 56, 67) +pixel3 = listmaker(23, 78, 248) + +r = (int(pixel1[0]) + int(pixel2[0]) + int(pixel3[0])) / 3 +g = (int(pixel1[1]) + int(pixel2[1]) + int(pixel3[1])) / 3 +b = (int(pixel1[2]) + int(pixel2[2]) + int(pixel3[2])) / 3 + +rgb = [r, g, b] +print("Average RGB values of the pixels are", rgb) diff --git a/py/p11.py b/py/p11.py index 3d35661..8ae9993 100644 --- a/py/p11.py +++ b/py/p11.py @@ -1,9 +1,9 @@ # Function to convert decimal to binary number def binaryconversion(n): - print(n % 2,end = '') - if n > 1: - binaryconversion(n/2) + if n > 0: + binaryconversion(n // 2) + print(n % 2, end = '') number=int(input("Enter Number: ")) binaryconversion(number) diff --git a/py/p12.py b/py/p12.py index c5b0f4d..820823d 100644 --- a/py/p12.py +++ b/py/p12.py @@ -1,5 +1,4 @@ # 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]] diff --git a/py/p2.py b/py/p2.py index 237c9b9..10af50f 100644 --- a/py/p2.py +++ b/py/p2.py @@ -2,8 +2,11 @@ # input- 1 -> output- [1], input- 2 -> output- [2] def add_item(item, items=[]): - items.append(item) - return items + if items!=[]: + items.remove(items[0]) + items.append(item) + return items + print(add_item(1)) print(add_item(2)) diff --git a/py/p3.py b/py/p3.py index c83d5ad..c6d89e6 100644 --- a/py/p3.py +++ b/py/p3.py @@ -1,13 +1,12 @@ #program to remove all even numbers from the beginning of a list #eg: [2, 4, 6, 17, 10] -> [17, 10] -def delete_starting_evens(list): - for item in list: - if list[0] % 2 == 0: - list.pop(0) - else: - break - return list +def delete_starting_evens(lst): + i = 0 + while i < len(lst) and lst[i] % 2 == 0: + lst.pop(i) + return lst + +lst = [2, 8, 10, 11] +print(delete_starting_evens(lst)) -list = [2, 8, 10, 11] -print(delete_starting_evens(list)) diff --git a/py/p4.py b/py/p4.py index 2ae30d4..d8c031f 100644 --- a/py/p4.py +++ b/py/p4.py @@ -6,12 +6,12 @@ def countSort(arr): ans = [0 for i in range(len(arr))] for i in arr: count[i] += 1 - for i in range(k+1): + for i in range(1, k+1): count[i] += count[i-1] - for i in range(len(arr)-1, -1, -1): - ans[count[arr[i]]] = arr[i] + for i in range(len(arr)): + ans[count[arr[i]]-1] = arr[i] count[arr[i]] -= 1 - return ans + return ans arr = [3, 4, 12, 2, 4, 5, 5, 6, 12, 33, 10, 1, 2, 8, 6] ans = countSort(arr) diff --git a/py/p5.py b/py/p5.py index 01e1d53..ca907fc 100644 --- a/py/p5.py +++ b/py/p5.py @@ -1,5 +1,4 @@ #recursive program to accept a number between 1 and 10 and print it - def get_number(): val1 = input('Enter a number: ') try: diff --git a/py/p6.py b/py/p6.py index fc571ba..9bfaa34 100644 --- a/py/p6.py +++ b/py/p6.py @@ -1,5 +1,4 @@ #program to perform binary addition - def addBinary(a: str, b: str) -> str: s = [] carry = 0 diff --git a/py/p9.py b/py/p9.py index 4ea1c58..430f386 100644 --- a/py/p9.py +++ b/py/p9.py @@ -3,7 +3,6 @@ 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)