From 93cc2afc50c1bda3a751c83147db2a32cc46ca41 Mon Sep 17 00:00:00 2001 From: Jay Gaha Date: Thu, 9 Jan 2025 15:47:27 +0900 Subject: [PATCH] day #13 udacity data structure --- .../quiz/5_type_conversion.py | 14 ++++ .../quiz/6_string_methods.py | 66 +++++++++++++++++++ .../quiz/7_debugging.py | 24 +++++++ .../50_udacity/3_dsa/quiz/1_1_list_method.py | 44 +++++++++++++ workspace/50_udacity/3_dsa/quiz/1_lists.py | 37 +++++++++++ workspace/50_udacity/3_dsa/quiz/2_tuple.py | 36 ++++++++++ workspace/50_udacity/3_dsa/quiz/3_set.py | 34 ++++++++++ 7 files changed, 255 insertions(+) create mode 100644 workspace/50_udacity/2_data_type_and_operators/quiz/5_type_conversion.py create mode 100644 workspace/50_udacity/2_data_type_and_operators/quiz/6_string_methods.py create mode 100644 workspace/50_udacity/2_data_type_and_operators/quiz/7_debugging.py create mode 100644 workspace/50_udacity/3_dsa/quiz/1_1_list_method.py create mode 100644 workspace/50_udacity/3_dsa/quiz/1_lists.py create mode 100644 workspace/50_udacity/3_dsa/quiz/2_tuple.py create mode 100644 workspace/50_udacity/3_dsa/quiz/3_set.py diff --git a/workspace/50_udacity/2_data_type_and_operators/quiz/5_type_conversion.py b/workspace/50_udacity/2_data_type_and_operators/quiz/5_type_conversion.py new file mode 100644 index 0000000..f81d383 --- /dev/null +++ b/workspace/50_udacity/2_data_type_and_operators/quiz/5_type_conversion.py @@ -0,0 +1,14 @@ +mon_sales = "121" +tues_sales = "105" +wed_sales = "110" +thurs_sales = "98" +fri_sales = "95" + +#TODO: Print a string with this format: This week's total sales: xxx +# You will probably need to write some lines of code before the print statement. +# total = int(mon_sales) + int(tues_sales) + int(wed_sales) + int(thurs_sales) + int(fri_sales) +# print("This week's total sales:", total) + +weekly_sales = int(mon_sales) + int(tues_sales) + int(wed_sales) + int(thurs_sales) + int(fri_sales) +weekly_sales = str(weekly_sales) #convert the type back!! +print("This week's total sales: " + weekly_sales) diff --git a/workspace/50_udacity/2_data_type_and_operators/quiz/6_string_methods.py b/workspace/50_udacity/2_data_type_and_operators/quiz/6_string_methods.py new file mode 100644 index 0000000..dc9f605 --- /dev/null +++ b/workspace/50_udacity/2_data_type_and_operators/quiz/6_string_methods.py @@ -0,0 +1,66 @@ +# String methods: same as functions, but they are called on a string, not on a variable with dot notation +name = "ram bahadur" +age = 20 +email = "ram@gmail.com" + +print("{} is {} years old and has email {}".format(name.capitalize(), age, email)) + +# split: splits a string into a list of strings +new_string = "ram bahadur is 20 years old, and has email ram@gmail.com" +print(new_string.split()) # ['ram', 'bahadur', 'is', '20', 'years', 'old,', 'and', 'has', 'email', 'ram@gmail.com'] + +# maxsplit: splits a string into a list of strings, but only splits at the first maxsplit number of splits +print(new_string.split(maxsplit=1)) # ['ram', 'bahadur is 20 years old, and has email ram@gmail.com'] + +# dot notation: accesses a string attribute +print(new_string.split(",")) # ['ram bahadur is 20 years old', ' and has email ram@gmail.com'] + +# None: returns None if the string is empty +print(new_string.split(",", maxsplit=1)) # ['ram bahadur is 20 years old', ' and has email ram@gmail.com'] + +verse = "If you can keep your head when all about you\n Are losing theirs and blaming it on you,\nIf you can trust yourself when all men doubt you,\n But make allowance for their doubting too;\nIf you can wait and not be tired by waiting,\n Or being lied about, don’t deal in lies,\nOr being hated, don’t give way to hating,\n And yet don’t look too good, nor talk too wise:" +print(verse) + +# Use the appropriate functions and methods to answer the questions above +# Bonus: practice using .format() to output your answers in descriptive messages! +# +# What is the length of the string variable verse? +print(len(verse)) # 362 + +split_verse = verse.split() +print(split_verse) +# ['If', 'you', 'can', 'keep', 'your', 'head', 'when', 'all', 'about', 'you', 'Are', 'losing', 'theirs', 'and', 'blaming', 'it', 'on', 'you,', +# 'If', 'you', 'can', 'trust', 'yourself', 'when', 'all', 'men', 'doubt', 'you,', 'But', 'make', 'allowance', 'for', 'their', 'doubting', 'too;', +# 'If', 'you', 'can', 'wait', 'and', 'not', 'be', 'tired', 'by', 'waiting,', 'Or', 'being', 'lied', 'about,', 'don’t', 'deal', 'in', 'lies,', 'Or', +# 'being', 'hated,', 'don’t', 'give', 'way', 'to', 'hating,', 'And', 'yet', 'don’t', 'look', 'too', 'good,', 'nor', 'talk', 'too', 'wise:'] + +# What is the index of the first occurrence of the word 'and' in verse? +# Hint: use the .index() method +print(verse.index("and")) # 65 +print(verse.find("and")) # 65 + +# What is the index of the last occurrence of the word 'you' in verse? +# Hint: use the .rindex() method +print(verse.rindex("you")) # 186 +print(verse.rfind("you")) # 186 + +# What is the count of occurrences of the word 'you' in the verse +# Hint: use the .count() method +print(verse.count("you")) # 8 + +print() + +# VERSION 2: use the appropriate functions and methods to answer the questions above +print(verse, "\n") + +message = "Verse has a length of {} characters.\nThe first occurence of the \ +word 'and' occurs at the {}th index.\nThe last occurence of the word 'you' \ +occurs at the {}th index.\nThe word 'you' occurs {} times in the verse." + +length = len(verse) + +first_idx = verse.find('and') +last_idx = verse.rfind('you') +count = verse.count('you') + +print(message.format(length, first_idx, last_idx, count)) diff --git a/workspace/50_udacity/2_data_type_and_operators/quiz/7_debugging.py b/workspace/50_udacity/2_data_type_and_operators/quiz/7_debugging.py new file mode 100644 index 0000000..2b9c295 --- /dev/null +++ b/workspace/50_udacity/2_data_type_and_operators/quiz/7_debugging.py @@ -0,0 +1,24 @@ +# Debugging +# +# Common errors: +# +# * Syntax error +# * NameError +# * TypeError +# * IndexError +# * KeyError +# * ValueError +# * AttributeError +# +# How to debug: +# +# * Read the error message +# * Use the interpreter to find the line of code that caused the error +# * Use the interpreter to find the value of variables at that point +# * Use the interpreter to find the type of the value of a variable +# +# Tips: +# +# * Use the interpreter to find the line of code that caused the error +# * Use the interpreter to find the value of variables at that point +# * Use the interpreter to find the type of the value of a variable diff --git a/workspace/50_udacity/3_dsa/quiz/1_1_list_method.py b/workspace/50_udacity/3_dsa/quiz/1_1_list_method.py new file mode 100644 index 0000000..c646bd2 --- /dev/null +++ b/workspace/50_udacity/3_dsa/quiz/1_1_list_method.py @@ -0,0 +1,44 @@ +# Lists: Methods +# +# 1. list.append(item) +# 2. list.extend(iterable) +# 3. list.insert(index, item) +# 4. list.pop(index) +# 5. list.remove(item) +# 6. list.reverse() +# 7. list.sort() +# 8. list.index(item) +# 9. list.count(item) +# 10. list.copy() +# 11. list.clear() +# 12. list.count(item) +# 13. list.max(item) +# 14. list.min(item) +# 15. list.sum(item) +# 16. list.sorted(item) + +# What would the output of the following code be? (Treat the comma in the multiple choice answers as newlines.) +a = [1, 5, 8] +b = [2, 6, 9, 10] +c = [100, 200] + +print(max([len(a), len(b), len(c)])) +print(min([len(a), len(b), len(c)])) + + +# What would the output of the following code be? (Treat the comma in the multiple choice answers as newlines.) + +names = ["Carol", "Albert", "Ben", "Donna"] +print(" & ".join(sorted(names))) + +# What would the output of the following code be? (Treat the commas in the multiple choice answers as newlines.) + +names = ["Carol", "Albert", "Ben", "Donna"] +names.append("Eugenia") +print(sorted(names)) + +# Choose the correct syntax to slice each of the following elements from the list: +arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] +print(arr[2:6]) +print(arr[:3]) +print(arr[4:]) diff --git a/workspace/50_udacity/3_dsa/quiz/1_lists.py b/workspace/50_udacity/3_dsa/quiz/1_lists.py new file mode 100644 index 0000000..a15eacd --- /dev/null +++ b/workspace/50_udacity/3_dsa/quiz/1_lists.py @@ -0,0 +1,37 @@ +# Types of Data Structures: Lists, Tuples, Sets, Dictionaries, Compound Data Structures +# Operators: Membership, Identity +# Built-In Functions or Methods +# +# 1. Lists: a data type for mutable ordered sequences of items +# 2. Tuples: a data type for immutable ordered sequences of items +# 3. Sets: a data type for unordered collections of unique items +# 4. Dictionaries: a data type for unordered collections of key-value pairs +# 5. Compound Data Structures: a data type for combining multiple data type + +month = 8 +days_in_month = [31,28,31,30,31,30,31,31,30,31,30,31] + +# use list indexing to determine the number of days in month +num_days = days_in_month[month - 1] +print(num_days) + + +eclipse_dates = ['June 21, 2001', 'December 4, 2002', 'November 23, 2003', + 'March 29, 2006', 'August 1, 2008', 'July 22, 2009', + 'July 11, 2010', 'November 13, 2012', 'March 20, 2015', + 'March 9, 2016'] + + +# TODO: Modify this line so it prints the last three elements of the list +eclipse_dates = eclipse_dates[-3:] +print(eclipse_dates) + +sentence1 = "I wish to register a complaint." +sentence2 = ["I", "wish", "to", "register", "a", "complaint", "."] +sentence2[6]="!" +print(sentence2) + +# sentence1[30] = '!' # TypeError: string indices must be integers + +sentence2[0:2] = ["We", "want"] +print(sentence2) diff --git a/workspace/50_udacity/3_dsa/quiz/2_tuple.py b/workspace/50_udacity/3_dsa/quiz/2_tuple.py new file mode 100644 index 0000000..791158c --- /dev/null +++ b/workspace/50_udacity/3_dsa/quiz/2_tuple.py @@ -0,0 +1,36 @@ +# 2 Tuples +# +# 1. Tuples are immutable +# 2. Tuples are ordered +# 3. Tuples are indexed +# 4. Tuples are not hashable +# 5. Tuples are not iterable + + + +angkor_wat = (13.4125, 103.866667) + +print(type(angkor_wat)) +# + +print("AngkorWat is at latitude: {}".format(angkor_wat[0])) +# AngkorWat is at latitude: 13.4125 + +print("AngkorWat is at longitude: {}".format(angkor_wat[1])) +# AngkorWat is at longitude: 103.866667 + +# Tuple unpacking +latitude, longitude = angkor_wat +print("AngkorWat is at latitude: {}".format(latitude)) +# AngkorWat is at latitude: 13.4125 + +print("AngkorWat is at longitude: {}".format(longitude)) +# AngkorWat is at longitude: 103.866667 +# + +# What would the output of the following code be? (Treat the comma in the multiple choice answers as newlines.) +tuple_a = 1, 2 +tuple_b = (1, 2) + +print(tuple_a == tuple_b) +print(tuple_a[1]) diff --git a/workspace/50_udacity/3_dsa/quiz/3_set.py b/workspace/50_udacity/3_dsa/quiz/3_set.py new file mode 100644 index 0000000..2431dc5 --- /dev/null +++ b/workspace/50_udacity/3_dsa/quiz/3_set.py @@ -0,0 +1,34 @@ +# 3 Sets +# +# 1. Sets are unordered +# 2. Sets are not hashable +# 3. Sets are not iterable + +continents = {"Asia", "Africa", "North America", "South America", "Antarctica"} +print(len(continents)) +# 5 +# +# Set: a collection of unique items +continent_set = set(continents) + +print('Asia' in continents) +print('Asia' in continent_set) + +# Add a new continent to the set +continent_set.add("Europe") + +# Pop() removes and returns an arbitrary element from the set +europe = continent_set.pop() +print(europe) + +# What would the output of the following code be? + +a = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] +b = set(a) +print(b) +print(len(a) - len(b)) + +a = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] +b = set(a) +b.add(5) +b.pop()