Skip to content

Instantly share code, notes, and snippets.

View ayubmetah's full-sized avatar

Ayub Metah ayubmetah

View GitHub Profile
@ayubmetah
ayubmetah / Calling a JSON API.py
Created January 19, 2021 20:10
Write a Python program to prompt for a location, contact a web service and retrieve JSON for the web service and parse that data, and retrieve the first place_id from the JSON. A place ID is a textual identifier that uniquely identifies a place as within Google Maps.
# To complete this assignment, you should use this API endpoint that has a static subset of the Google Data:
# http://py4e-data.dr-chuck.net/json?
# This API uses the same parameter (address) as the Google API. This API also has no rate limit so you can test as often as you like.
# If you visit the URL with no parameters, you get "No address..." response.
# To call the API, you need to include a key= parameter and provide the address that you are requesting as the address= parameter that
# is properly URL encoded using the urllib.parse.urlencode() function as shown in http://www.py4e.com/code3/geojson.py
# Make sure to check that your code is using the API endpoint is as shown above.
# Please run your program to find the place_id for this location: University of Akron
# Make sure to enter the name and case exactly as above and enter the place_id and your Python code below. Hint: The first seven characters of the place_id are "ChIJbye ..."
@ayubmetah
ayubmetah / Extracting Data from XML.py
Created January 16, 2021 09:30
write a Python program to prompt for a URL, read the XML data from that URL using urllib and then parse and extract the comment counts from the XML data, compute the sum of the numbers in the file.
#Data file url: http://py4e-data.dr-chuck.net/comments_1070214.xml
#VERSION 1:
import urllib.request
import xml.etree.ElementTree as ET
import ssl
# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
@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))
@ayubmetah
ayubmetah / formatting_strings.py
Created January 19, 2021 03:41
Modify the student_grade function using the format method, so that it returns the phrase "X received Y% on the exam". For example, student_grade("Reed", 80) should return "Reed received 80% on the exam".
def student_grade(name, grade):
# return ""
return "{} received {}% on the exam".format(name, grade)
print(student_grade("Reed", 80))
print(student_grade("Paige", 92))
print(student_grade("Jesse", 85))
@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 / 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 / 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 / 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 / string_methods.py
Created January 16, 2021 08:15
Fill in the gaps in the initials function so that it returns the initials of the words contained in the phrase received, in upper case. For example: "Universal Serial Bus" should return "USB"; "local area network" should return "LAN”.
def initials(phrase):
words = phrase.split()
result = ""
for word in words:
result += word[0].upper()
return result
print(initials("Universal Serial Bus")) # Should be: USB
print(initials("local area network")) # Should be: LAN
print(initials("Operating system")) # Should be: OS