This week represents the first month and a fifth of the course completed, consisting of three Django Projects and reviewing python coding problems in the morning. Without further ado, let's get into my findings for this week.
I want to share my thoughts on four python coding problems. The python coding problems are the area of my coding journey with which I am most challenged. The following issues were given during class, and I have provided the questions with my solution, along with writing a brief description of how to answer these questions.
Question 1 was about reviewing a dictionary. Here is the coding question.
Complete the function make_description below, which takes a person's name and a dictionary of their attributes and constructs a single string from them.
For example, say the person's name is "Lulu," and their attribute dictionary contains the following entries: { "hair": "red," "yes": "blue,"}.
Then, the return value of the function should be the string: Lulu, red hair, blue eyes
The return value should be the person's name if the dictionary has no entries.
Let's start by writing my pseudocode (it helps map out my structure to solving and keeps me organized in writing my code).
Now from the pseudocode code that I have written, I already know that I will need to create a loop because I have to perform an iteration. And in this case, it is for iterating over the dictionary. I made a variable with the name parameter from the m function since the directions ask to return the person's name first, followed by their attributes. f Before I added my return statement, I wrote out my variable set early to itself plus the attributes and formatted it correctly to agree with what was being asked. You want to avoid setting your variable equal to an empty string as this will create an error with how the testing is set for this problem, and you would end up repeating the name twice in your print statement. You add your name every time your looping through the attributes.sites(). See the error picture below for details.
The next problem was using the same dictionary inputs but, this time, reversing the output. Here is the question.
Question 2 Complete the function reverse_entries
below that takes a dictionary and returns a new dictionary whose key-value pairs are the values and keys from the input.
For example, if the input is this dictionary: { "hair": "red", "eyes": "blue", }.
Then the output should be this dictionary: { "red": "hair", "blue": "eyes", }.
First, try solving this with a for
loop.
Then, once you have that figured out, read about list comprehensions and then try it with a dictionary comprehension. You have to look at the third example in the "Dictionaries" section to see an example of a dictionary comprehension.
Let us start with my pseudocode. I will first need to create an empty dictionary as we will create and use a for a loop. Now how do we reverse this? Let's think about how we make a dictionary entry. There is a key and value. So I called my dictionary, changed the key to value, and I swapped the key with a deal. My final step is then to return my reverse dictionary.
The third python coding challenge was on classes and using the __str__ method. Here is the question:
Question 3 Create a Car
class that has the required state of:
make
, a stringmodel
, a stringyear
, an integerin that order.
Create a __str__
method on the class that returns the year, make, and model separated by spaces.
Example values:
make: Oldsmobile
model: Alero
year: 2001
__str__ would return "2001 Oldsmobile Alero"
I created a car class for my pseudocode and added the make/model/year attributes. Then I need to include a __str__ method and return the __STR__ string with the year, make, and model. I figured this out independently after conducting searches and referencing my notes. My answer is seen below, and I want to mention that you can use two versions of the return statement after my __str__(self) function. That is, use the f-strings included in Python version 3.6 and return with the self. instances. In this case, the self.year, self.make, and self.model. The other way is by returning your answer in a string and referencing the .format(self=self).
The last python question was the hardest for me: how to reverse a string and display the letters based on how many times they occur in a horizontal bar chart. Please see the following question.
Question 4
Complete the function horizontal_bar_chart
below that takes in a string and creates a horizontal bar chart made out of the instances of the letters in that string based on the number of each letter.
The return value is a list of the strings of the letters in alphabetical order.
For example, given the sentence "abba has a banana", it would return this list of strings because there are seven "a", three "b", one "h", two "n", and one "s" letters in it:
All letters in the input will be lowercase letters.
You may want to look at the methods for dictionaries and the built-in functions. Remember, you can also compare strings with >=
and <=
.
Take note of my "if char >= "a" and char <= "z":" The question does ask to use the "<>+" operators. But one could use a count and strip methods to determine how many spaces exist in the string. When my first if statement is true, I know my char is a letter, and if the char doesn't already exist in my dictionary, I will create it in a key. To generate the dictionary key, you access the dictionary by typing dict[char]. The char represents the key. Now if the char is in the dictionary (dict) I will add it to the key value (char). At this point, we have successfully populated the dictionary with key-value pairs of letters and counts. The next step is to create an empty list and use our dictionary method with another for a loop. Now this will iterate through the dictionary and only grab the values for each value, it will be placed in my empty list named character_list. The last step is to return the character_list now and make sure to add the sorted method, or else this will create an error when the tests are run.
Now let's move on to what I learned from two Django projects for this week. The first Django project for this week was called Django-one-shot. this project was divided up into 13 steps building estienally the backend components of creating a Django project,
a Django app, writing two models, seven views, and seven templates. And lastly, including the configured paths of all seven views. This exercise of completing this Django project and doing so in one day helped further my understanding of the Web browser request and response with Django. Django has a built-in folder/steps to handle these requests and responses.
Django will first have the information being requested by the web browser through the URL dispatcher(located in the urls.py folders of a Django project). This will store the URL pathways for how the URL path is being named. Then the URL dispatcher passes this info to the view functions located in the views.py folder of an app, which is then tied into classes created in the model.py folder. The types are stored in the built database with the python web framework. This data can also be stored on another database outside the built Django database, but for now, let's use the perspective of the Django framework. Now wants the database has been accessed from the request, Django will now run through the same process but backward, starting with the model.py and repopulating the information being requested with the correct fields of date, for example, and then moving onto the view.py folder that housed where a webpage should be redirected to as well as the specific HTML template to display to the end user. This information will be directly shared with the web browser for the user to view. I'm grateful I could figure out my errors throughout this project, as they provided great insight into understanding the structure and order of how the Django Web framework worked. It is always a beautiful feeling to solve a debugging error after testing numerous fixes for three hours until you get the Django Project functioning correctly.
On to the Django two-shot project. The name is to signify that you only have two days to complete it; it is similar and built off of the Django shot steps. This project consisted of 15 steps and built-in tests to ensure your structure and coding are in the right direction. This project consisted of two main parts. The first is a Sign-up, logging-in, and logging-out functionality to the site. And the other an accounts, expenses, and receipts data management. This project was more complex and time intensive than the previous error, but the structure of building it on the Django web framework was all the same. Now there was some additional new add-on that I was familiar with when creating specific view functions and foreign keys. But by referencing the Django documentation of my other Django projects and viewing the Hack Reactor Slack channel of my Feb cohort, I could complete this project and understand everything I created. The main goal by the end was to Ensure you've signed up with two different users—functionality to develop accounts, expense categories, and receipts for one user. Then log out from that account and log in as the other user. Then ensure you cannot see the first user's information when logged into a different account. The following video from my screen sharing documents and highlights each step I completed on the backend of this Django project to achieve this goal.
An additional note that I learned this week was to practice going slow. By planning first, we go faster and only attempt a solution after I have a plan.
And to practice making atomic git commits. Outlined in this article by Aleksandr Hovhannisyan. I liked the Django projects we did this week because built into the steps were the directions to git add, commit, and push our code after every completed step—a great habit of having as a software engineer/developer.
The other article written by Sankalp Jonna I would like to reference about explaining the many-to-many relationship in a database.
The author explains that when remaining in a many-to-many relationship in a database between two tables. There is only one way to connect this with a third table, and this is done with a table called a "through" table. Then every entry in that table will link up with the other two tables.
This week's challenges were understanding the flow of the Django web framework between an HTTP request in my web browser to then urls.py in the Django app or project folder to link up with the views, models, and forms folder. However, internalizing the database, however, it is accessible at the moment to work with the database since we only work with a handful of data points in our Django projects. The other additional challenges were my errors. Here are a few screenshots of my mistakes and my solutions to these errors.
My focus will be refamiliarizing myself with python practice problems and reworking through the 80 practice problems given a few weeks ago. In addition, our first practice test for Module one is on Monday. Next week, we will also be working on our final project for this module, another Django project where we are not allowed to get assistance from our instructors or classmates. On to next week!
Also, my girlfriend started her new job with Family Office Club as a project manager role. I'm proud of her work, achievement, and continual growth as a professional, and very happy for her and the new opportunities she is creating for herself. And we are moving into a designated office space in another apartment that we will rent out for the next six months here in Santa Rosa De Cabal solely to use as a workspace. To give you an idea of how much rent is for the place I show in my video update for this week, it is about $600,000 mill Colombian pesos per month. This is roughly $125. So this is an affordable option for an office space!
And last, I attended my second class for going through the process of becoming baptized here in Santa Rosa de Cabal. After hearing about the course with my Suegra (mother of my GF), we agreed to find another program that is more organized and does not have a long time commitment of 1 year as the class structure. More details to follow.
I'd be happy to discuss your project and how we can work together to create unique, fun and engaging content.
Go ahead and click here to be taken to my business service page and see what I can do for you. Book a call with me now. Looking forward to chatting soon!