Skip to content

Instantly share code, notes, and snippets.

View ayubmetah's full-sized avatar

Ayub Metah ayubmetah

View GitHub Profile
@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 / 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 / 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 / digits_of_number.py
Created December 20, 2020 04:53
Complete the function digits(n) that returns how many digits the number has. For example: 25 has 2 digits and 144 has 3 digits. Tip: you can figure out the digits of a number by dividing it by 10 once per digit until there are no digits left.
def digits(n):
count = 0
if n == 0:
return 1
while (n > 0):
count += 1
n = n // 10
return count
print(digits(25)) # Should print 2
@ayubmetah
ayubmetah / file_data_process.py
Created December 20, 2020 03:32
Write a program that prompts for a file name, then opens that file and reads through the file, looking for lines. Count these lines and extract the floating point values from each of the lines and compute the average of those values and produce an output as shown below. Do not use the sum() function or a variable named sum in your solution.
#7.2 Write a program that prompts for a file name, then opens that file and reads through the file, looking for lines of the form:
#X-DSPAM-Confidence: 0.8475. Count these lines and extract the floating point values from each of the lines and compute the average of those values and produce an output as shown below. Do not use the sum() function or a variable named sum in your solution.
#You can download the sample data at http://www.py4e.com/code3/mbox-short.txt when you are testing below enter mbox-short.txt as the file name.
fname = input("Enter file name: ")
fh = open(fname)
count = 0
average = 0
for line in fh:
if not line.startswith("X-DSPAM-Confidence:") : continue
@ayubmetah
ayubmetah / factorial_example-2.py
Created December 20, 2020 01:17
An example of recursion in Python at work.
#recursions example-2
def factorial(n):
print("Factoriaal called with " + str(n))
if n < 2:
print("Returning 1")
return 1
result = n * factorial(n - 1)
print("Returning " + str(result) + " for factorial of " + str(n))
return result
factorial(4)
@ayubmetah
ayubmetah / factorial_example-2.py
Created December 20, 2020 01:16
An example of recursion in Python at work
#recursions example-2
def factorial(n):
print("Factoriaal called with " + str(n))
if n < 2:
print("Returning 1")
return 1
result = n * factorial(n - 1)
print("Returning " + str(result) + " for factorial of " + str(n))
return result
factorial(4)
@ayubmetah
ayubmetah / sum_positive_numbers.py
Last active December 31, 2022 03:45
Implement the sum_positive_numbers function, as a recursive function that returns the sum of all positive numbers between the number n received and 1. For example, when n is 3 it should return 1+2+3=6, and when n is 5 it should return 1+2+3+4+5=15.
#using recursion
def sum_positive_numbers(n):
if n <= 1:
return n
return n + sum_positive_numbers(n-1)
print(sum_positive_numbers(3)) # Should be 6
print(sum_positive_numbers(5)) # Should be 15
@ayubmetah
ayubmetah / recursion.py
Created December 19, 2020 23:24
Fill in the blanks to make the is_power_of function return whether the number is a power of the given base. Note: base is assumed to be a positive number. Tip: for functions that return a boolean value, you can return the result of a comparison.
def is_power_of(number, base):
# Base case: when number is smaller than base.
number = number/base
if number < base:
# If number is equal to 1, it's a power (base**0).
return False
else:
return True
# Recursive case: keep dividing number by base.
@ayubmetah
ayubmetah / multiples.py
Created December 19, 2020 07:40
Question 4 Write a script that prints the multiples of 7 between 0 and 100. Print one multiple per line and avoid printing any numbers that aren't multiples of 7. Remember that 0 is also a multiple of 7.
def multiples(x):
for n in range(100):
n = (n*x)
if n <= 100:
print(n)
print(multiples(7))