diff --git a/Ch2 - Basics/ch2_challenge_start.py b/Ch2 - Basics/ch2_challenge_start.py new file mode 100644 index 0000000..4a50fe4 --- /dev/null +++ b/Ch2 - Basics/ch2_challenge_start.py @@ -0,0 +1,47 @@ +# Python code​​​​​​‌‌​​​‌‌​​​​‌‌​‌‌​‌‌​‌‌​​‌ below +# Use print("messages...") to debug your solution. + +show_expected_result = True +show_hints = True + +def is_palindrome(teststr): + # Your code goes here. + #return False + + finished = False + capstring = str.upper(teststr) + i = 0; + j = len(capstring)-1; + print("i, j, capstring",i,j,capstring) + + while i < j: + print("In while loop, i, j, capstring", i, j, capstring) + if str.isalpha(capstring[i]) : + print("i is alpha", i, capstring[i]) + else : + print("i is not alpha", i, capstring[i]) + i = i + 1 + continue + + if str.isalpha(capstring[j]) : + print("j is alpha", j, capstring[j]) + else : + print("j is not alpha", j, capstring[j]) + j = j -1 + continue + + print("see if capstring[i] equals capstring[j]", i, j, capstring) + if capstring[i] == capstring[j] : + print("yes...so far") + i = i+1 + j=j-1 + else: + print("no...return false") + return(False) + + return(True) + +name = input("Enter phrase: ") +result = is_palindrome(name) +print("Is a palindrome?", result) + diff --git a/Ch2 - Basics/helloworld_start.py b/Ch2 - Basics/helloworld_start.py index 7d6b753..83c4379 100644 --- a/Ch2 - Basics/helloworld_start.py +++ b/Ch2 - Basics/helloworld_start.py @@ -3,4 +3,10 @@ # LinkedIn Learning Python course by Joe Marini # +def mine(): + print("Hello Universe") + name = input("Who are you?") + print("Nice to meet you",name) +if __name__ == ("__main__"): + mine() \ No newline at end of file diff --git a/Ch2 - Basics/variables_start.py b/Ch2 - Basics/variables_start.py index b2756cc..e2225bf 100644 --- a/Ch2 - Basics/variables_start.py +++ b/Ch2 - Basics/variables_start.py @@ -21,17 +21,27 @@ print(mytuple) print(mydict) -# re-declaring a variable works +print("# re-declaring a variable works") +mfloat = "bob" +print("mfloat =", mfloat) -# to access a member of a sequence type, use [] +print("# to access a member of a sequence type, use []") +print("mylist[2]", mylist[2]) -# use slices to get parts of a sequence +print("# use slices to get parts of a sequence") +print("mylist[0:2]", mylist[0:2]) -# you can use slices to reverse a sequence +print("# you can use slices to reverse a sequence") +print(mylist[::-1]) -# dictionaries are accessed via keys +print("# dictionaries are accessed via keys") +print(mydict["one"]) # ERROR: variables of different types cannot be combined # Global vs. local variables in functions +def somefunction(): + myint = 10 + print(myint) +print(myint) diff --git a/Ch3 - Files/challenge.py b/Ch3 - Files/challenge.py new file mode 100644 index 0000000..70b8de2 --- /dev/null +++ b/Ch3 - Files/challenge.py @@ -0,0 +1,31 @@ +# Python code​​​​​​‌‌​​​‌‌​​​‌‌​‌​​‌​​‌‌​‌‌​ below +# Use print("messages...") to debug your solution. + +show_expected_result = False +show_hints = False +import os +from os import path + + +def file_info(): + # Your code goes here. + # return 0 + totalBytes = 0 + + filelist = os.listdir("./deps") + print("filelist:", filelist) + for filename in filelist: + print("filename:", filename) + filepathname = "./deps/"+filename + fileprefix, file_extension = os.path.splitext(filepathname) + print("filename and ext:", filepathname, fileprefix, file_extension) + if file_extension == '.txt': + file_stats = os.stat(filepathname) + totalBytes = totalBytes + file_stats.st_size + print("file size, running total:", file_stats.st_size, totalBytes) + else: + print("not right file type", filename) + return (totalBytes) + +if __name__ == "__main__": + file_info() \ No newline at end of file diff --git a/Ch4 - Dates and Times/challenge_start.py b/Ch4 - Dates and Times/challenge_start.py index 9da42cb..fde5bd5 100644 --- a/Ch4 - Dates and Times/challenge_start.py +++ b/Ch4 - Dates and Times/challenge_start.py @@ -3,3 +3,42 @@ # import calendar + +# Python code​​​​​​‌‌​​​‌‌​​​‌‌​‌‌​​‌‌‌​‌​​​ below +# Use print("messages...") to debug your solution. + +show_expected_result = False +show_hints = False + +def count_days(year, month, whichday): + # Your code goes here. + count = 4 + + # returns an array of weeks that represent the month + cal = calendar.monthcalendar(year, month) + # The first Friday has to be within the first two weeks + # Check the 1st and 5th weeks + weekone = cal[0] + weekfive = cal[4] + + if weekone[whichday] != 0 and weekfive[whichday] != 0 : + count = 5 + else: + count = 4 + + print("weekone weekfive whichday count", weekone, weekfive, whichday, count) + return(count) + + # You can edit this code to try different testing cases. +testyear = 2025 +testmonth = 10 +testday = 4 +result = count_days(testyear, testmonth, testday) + +result = count_days(testyear, testmonth, 0) +result = count_days(testyear, testmonth, 1) +result = count_days(testyear, testmonth, 2) +result = count_days(testyear, testmonth, 3) +result = count_days(testyear, testmonth, 4) +result = count_days(testyear, testmonth, 5) +result = count_days(testyear, testmonth, 6) diff --git a/Ch5 - Internet Data/htmlparsing_start.py b/Ch5 - Internet Data/htmlparsing_start.py index a759ac3..545c47a 100644 --- a/Ch5 - Internet Data/htmlparsing_start.py +++ b/Ch5 - Internet Data/htmlparsing_start.py @@ -7,13 +7,22 @@ class MyHTMLParser(HTMLParser): def handle_comment(self, data): - pass + pos = self.getpos() + print("Comment at line", pos[0], "character", pos[1], "comment:", data) def handle_starttag(self, tag, attrs): - pass + pos = self.getpos() + print("Start tag at line", pos[0], "character", pos[1], "tag:", tag) + if attrs.__len__() > 0: + print ("\tAttributes:") + for a in attrs: + print ("\t", a[0],"=",a[1]) def handle_data(self, data): - pass + if (data.isspace()): + return + pos = self.getpos() + print("Data at line", pos[0], "character", pos[1], "data:", data) def main(): # instantiate the parser and feed it some HTML diff --git a/Ch5 - Internet Data/jsondata_start.py b/Ch5 - Internet Data/jsondata_start.py index e0da623..489bebd 100644 --- a/Ch5 - Internet Data/jsondata_start.py +++ b/Ch5 - Internet Data/jsondata_start.py @@ -4,24 +4,39 @@ # import urllib.request +import json def printResults(data): # Use the json module to load the string data into a dictionary theJSON = json.loads(data) # now we can access the contents of the JSON like any other Python object + if "title" in theJSON["metadata"]: + print(theJSON["metadata"]["title"]) - # output the number of events, plus the magnitude and each event name - + if "count" in theJSON["metadata"]: + count = theJSON["metadata"]["count"] + print("Number of events recorded", count) # for each event, print the place where it occurred - + for i in theJSON["features"]: + print(i["properties"]["place"], "with a magnitude of", i["properties"]["mag"]) + print("-----------------\n") # print the events that only have a magnitude greater than 4 - + for i in theJSON["features"]: + if i["properties"]["mag"] >= 4.0: + print(i["properties"]["place"], "with a magnitude of", i["properties"]["mag"]) # print only the events where at least 1 person reported feeling something + print("-----------------\n") + + # print the events that only have a magnitude greater than 4 + for i in theJSON["features"]: + if i["properties"]["felt"] != None: + if i["properties"]["felt"] > 0: + print(i["properties"]["place"], "with a magnitude of", i["properties"]["mag"], "felt by", i["properties"]["felt"], "people.") def main(): @@ -32,8 +47,11 @@ def main(): # Open the URL and read the data webUrl = urllib.request.urlopen(urlData) - print ("result code: " + str(webUrl.getcode())) - + resultcode = str(webUrl.getcode()) + if (resultcode == '200') : + printResults(webUrl.read()) + else: + print("Could not print results due to error:", resultcode) if __name__ == "__main__": main() diff --git a/Ch5 - Internet Data/samplexml.xml b/Ch5 - Internet Data/samplexml.xml index 7e29316..ce9e498 100644 --- a/Ch5 - Internet Data/samplexml.xml +++ b/Ch5 - Internet Data/samplexml.xml @@ -7,4 +7,4 @@ - + \ No newline at end of file diff --git a/Ch5 - Internet Data/samplexml2.xml b/Ch5 - Internet Data/samplexml2.xml new file mode 100644 index 0000000..fe9f710 --- /dev/null +++ b/Ch5 - Internet Data/samplexml2.xml @@ -0,0 +1,20 @@ + + + + Joe + Marini + Seattle + + + + + + + Robbie + Robot + Austin + + + + + \ No newline at end of file diff --git a/Ch5 - Internet Data/xmlparsing_start.py b/Ch5 - Internet Data/xmlparsing_start.py index 3129b0c..ac0a315 100644 --- a/Ch5 - Internet Data/xmlparsing_start.py +++ b/Ch5 - Internet Data/xmlparsing_start.py @@ -3,15 +3,37 @@ # LinkedIn Learning Python course by Joe Marini # +# import xml.dom.minidom as MD +import xml.etree.ElementTree as ET -def main(): +def get_skills(xml_file): # use the parse() function to load and parse an XML file - +# doc = MD.parse(xml_file) # print out the document node and the name of the first child tag +# print("Doc name:", doc.nodeName, "\nFirst Child:", doc.firstChild.tagName) + + + try: + # parse XML file + tree = ET.parse(xml_file) + root = tree.getroot() + print(tree.findall("skill"), root) + + #iterate through each person element in the XML + for person in root.findall("person"): + print("------------") + firstname = person.find("firstname").text + lastname = person.find("lastname").text + print("Skills for", firstname, lastname,":") + for skill in person.findall("skill"): + print(skill) + print("------------") + print("-End List-") - # get a list of XML tags from the document and print each one + except Exception as e: + print("Could not parse file:", xml_file) # create a new XML tag and add it into the document @@ -19,5 +41,5 @@ def main(): if __name__ == "__main__": - main() + get_skills("samplexml.xml")