From bf02f7727331bf4c60173e752f34062c635ef31f Mon Sep 17 00:00:00 2001 From: Multivalence <47169252+Multivalence@users.noreply.github.com> Date: Wed, 9 Dec 2020 16:23:28 -0500 Subject: [PATCH 1/4] Create main.swift --- examples/objects/Swift/main.swift | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 examples/objects/Swift/main.swift diff --git a/examples/objects/Swift/main.swift b/examples/objects/Swift/main.swift new file mode 100644 index 0000000..a90ebae --- /dev/null +++ b/examples/objects/Swift/main.swift @@ -0,0 +1,23 @@ + +import Foundation + +let bookOne = Book(author: "Terry Pratchett", title: "Guards! Guards!", price: 5.99) +let bookTwo = Book(author: "Robert Jordan", title: "The Eye of the World", price: 8.99) + +print(bookOne.getAuthor()) +print(bookOne.getPrice()) +print(bookOne.getTitle()) +bookOne.increasePrice(1.00) +print(bookOne.getPrice()) + +print(bookTwo.getAuthor()) +print(bookTwo.getPrice()) +print(bookTwo.getTitle()) +bookTwo.increasePrice(-6.00) +print(bookTwo.getPrice()) + + + + + + From 57e1940d66e1ee90a88c1af727d1409321ca6b94 Mon Sep 17 00:00:00 2001 From: Multivalence <47169252+Multivalence@users.noreply.github.com> Date: Wed, 9 Dec 2020 16:23:50 -0500 Subject: [PATCH 2/4] Create book.swift --- examples/objects/Swift/book.swift | 98 +++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 examples/objects/Swift/book.swift diff --git a/examples/objects/Swift/book.swift b/examples/objects/Swift/book.swift new file mode 100644 index 0000000..5db522f --- /dev/null +++ b/examples/objects/Swift/book.swift @@ -0,0 +1,98 @@ + +import Foundation + +/// A Book Object that holds the price, author, and title of a book +class Book { + + + + var author : String + var title : String + var price : Float + + init(author : String, title : String, price : Float=0.00) { + + /** + Constructor to build a book object + + - Parameters: + - author: The full name of the author of the book + - title: The full title of the book + - price: The price of the book in dollars and cents (example format ###.##) + + */ + + self.author = author + self.price = price + self.title = title + + } + + func getAuthor() -> String { + + /** + Returns the author of the book + + - Returns: The author of the book as a String + + */ + + + + return self.author + } + + func getPrice() -> Float { + + /** + Returns the price of the book + + - Returns: The price of the book as a Float + + */ + + return self.price + } + + func getTitle() -> String { + + /** + Returns the title of the book to the console + + - Returns: The title of the book + + */ + + return self.title + } + + func increasePrice(_ increase : Float) -> Bool { + + /** + Attempts to increase the price of the book + + - Parameters: + - increase: The value to increase the price by. This value can be negative; however, it will never lower the value below zero. If this happens, the function will return false + + - Returns: true if the method was successful + - Returns: false if the method attempted to bring the value below zero + + + + + */ + + if (self.price + increase) > 0 { + self.price += increase + return true + } + + return false + + } + +} + + + + From 63f5b92e331ed2c9db3f7786d2ece9ff174cc873 Mon Sep 17 00:00:00 2001 From: Multivalence <47169252+Multivalence@users.noreply.github.com> Date: Thu, 10 Dec 2020 14:26:49 -0500 Subject: [PATCH 3/4] Create Python.py --- examples/string-interpolation/Python.py | 92 +++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 examples/string-interpolation/Python.py diff --git a/examples/string-interpolation/Python.py b/examples/string-interpolation/Python.py new file mode 100644 index 0000000..c64fd5e --- /dev/null +++ b/examples/string-interpolation/Python.py @@ -0,0 +1,92 @@ +''' +STRING INTERPOLATION +''' + +name = "John Doe" +age = 20 + +#These are different ways you can combine variables together + + +#Concatenation (The one you are most familiar with) +string = "Hello! My name is " + name + " and I am " + str(age) + " years old." +print(string) + +#f-strings (Works only for Python 3.6+) +# placing 'f' before initializing a string, denotes the start of an f-string +string = f"Hello! My name is {name} and I am {age} years old." +print(string) + +#Formatting (Works for all versions of Python) +# {} denotes a placeholder for a variable +string = "Hello! My name is {} and I am {} years old.".format(name, age) #Variables are put in order at which they appear +string = "Hello! My name is {name} and I am {age} years old.".format(name=name, age=age) #Can also be written like this +print(string) + +#% String Formatting (Works for all versions of Python) +# %s denotes a placeholder for a variable +string = "Hello! My name is %s and I am %s years old." % (name, age) #Variables are put in order at which they appear +print(string) + + + +''' +Concatenation CONS + +- Lack of Readability +- Have to convert all variables to strings +- Difficult to type if you have more variables +- Lengthy + +Concatenation PROS + +- Works on all versions of Python +''' + + + +''' +Formatting CONS +- Lengthy +- Difficult to type if you have more variables + +Formatting PROS + +- Decent Readability +- Don't have to convert variables into strings +- Works on all versions of Python +''' + + + +''' +%s String Formatting CONS +- Lengthy +- Difficult to type if you have more variables +- Poor Readability + +%s String Formatting PROS + +- Don't have to convert variables into strings +- Works on all versions of Python +''' + + +''' +f-string CONS +- Only works on Python versions 3.6 and up + +f-string PROS + +- Excellent Readability +- Don't have to convert variables into strings +- Not Lengthy +- Easy to type even with multiple variables +''' + + +#Conclusion +# - Formatting is the best to use if you are on a python version lower than 3.6 +# - F-Strings are the best to use if you are on python 3.6 or above +# - Concatenation should be avoided when possible +# - % String Formatting can be used but should be avoided if not needed (e.g. PyMySQL requires the use of % String formatting to avoid SQL Injections) From 8ebc3a0cf787c781ec01b92bf0b01a9887668de0 Mon Sep 17 00:00:00 2001 From: Multivalence <47169252+Multivalence@users.noreply.github.com> Date: Thu, 10 Dec 2020 14:31:07 -0500 Subject: [PATCH 4/4] Delete Python.py --- examples/string-interpolation/Python.py | 92 ------------------------- 1 file changed, 92 deletions(-) delete mode 100644 examples/string-interpolation/Python.py diff --git a/examples/string-interpolation/Python.py b/examples/string-interpolation/Python.py deleted file mode 100644 index c64fd5e..0000000 --- a/examples/string-interpolation/Python.py +++ /dev/null @@ -1,92 +0,0 @@ -''' -STRING INTERPOLATION -''' - -name = "John Doe" -age = 20 - -#These are different ways you can combine variables together - - -#Concatenation (The one you are most familiar with) -string = "Hello! My name is " + name + " and I am " + str(age) + " years old." -print(string) - -#f-strings (Works only for Python 3.6+) -# placing 'f' before initializing a string, denotes the start of an f-string -string = f"Hello! My name is {name} and I am {age} years old." -print(string) - -#Formatting (Works for all versions of Python) -# {} denotes a placeholder for a variable -string = "Hello! My name is {} and I am {} years old.".format(name, age) #Variables are put in order at which they appear -string = "Hello! My name is {name} and I am {age} years old.".format(name=name, age=age) #Can also be written like this -print(string) - -#% String Formatting (Works for all versions of Python) -# %s denotes a placeholder for a variable -string = "Hello! My name is %s and I am %s years old." % (name, age) #Variables are put in order at which they appear -print(string) - - - -''' -Concatenation CONS - -- Lack of Readability -- Have to convert all variables to strings -- Difficult to type if you have more variables -- Lengthy - -Concatenation PROS - -- Works on all versions of Python -''' - - - -''' -Formatting CONS -- Lengthy -- Difficult to type if you have more variables - -Formatting PROS - -- Decent Readability -- Don't have to convert variables into strings -- Works on all versions of Python -''' - - - -''' -%s String Formatting CONS -- Lengthy -- Difficult to type if you have more variables -- Poor Readability - -%s String Formatting PROS - -- Don't have to convert variables into strings -- Works on all versions of Python -''' - - -''' -f-string CONS -- Only works on Python versions 3.6 and up - -f-string PROS - -- Excellent Readability -- Don't have to convert variables into strings -- Not Lengthy -- Easy to type even with multiple variables -''' - - -#Conclusion -# - Formatting is the best to use if you are on a python version lower than 3.6 -# - F-Strings are the best to use if you are on python 3.6 or above -# - Concatenation should be avoided when possible -# - % String Formatting can be used but should be avoided if not needed (e.g. PyMySQL requires the use of % String formatting to avoid SQL Injections)