Week 9 Coding Challenges

 

    For this weeks challenge I did a coding challenge called generate document. The way this works is you are given a function that takes in 2 strings. The first string is called characters and the second is document. Your goal is to see if you can generate a document with the given characters in your string. The only way you can generate a document is if you have the same amount of characters in your character string that will enable you to generate the document. For example if characters = "abcabc" and document = "aabbccc" you can't make the document because you are missing one 'c' in the characters string. If you are able to make the document you will return True to the user else False. This program will also account for spaces to be able to generate the document. 

    The first thing I thought up when I saw this challenge was to automatically use a for loop. Once I did though I realized I needed a way to keep track how many instances of each character are in my characters string. I also needed to make sure if I used a certain char inside of my characters list I needed to make sure I make note of that. That is why I decided to use a dictionary. A dictionary in Python is a type that not only contains elements but it also contains a key to that element. For example if I find a letter z in my characters if I append it to my dictionary it would look like {'z':1}. So my z would be my k and the 1 is the value in my key. The value represents how many instances of the char z I can use to represent my document. I decided to use a for loop on my character string and check if that character is already inside of my dictionary. If that letter is already inside of my dictionary well I would just add one to the value since I found another same letter. However if it is not already inside of my dictionary I would make a new key and add 1 since that would be the first letter I find. 

    I then created another for loop but this time we go through our document. Since we already have a dictionary with all of our available characters at our disposal the rest is pretty obvious on what we will do. One we are inside of our loop we will check to see if that certain character from document exists as a key inside of our dictionary. If even one of the characters inside of our document is not a key inside of our dictionary then we will return False because we must have every single character represented in order to generate the document. If it is then we will just subtract one from that key to represent one character used from that list. Finally while inside of that same iteration we also have to check whatever character we just subtracted from if the value inside of that key is less than 0 we know that we have used more characters than we initially had. If the value inside of that key is less than 0 we also return False because we went past our available characters available. If we get out of that for loop with no issues then we know we can generate our document and at the end we just return True to the user. 

This has been a really heavy week since I am took a calculus exam, studying for a computer science exam and also have 1 computer science lab due today. Next week I will be back with doing 2 coding challenges and hopefully making another video visually explaining my solution.

Comments

  1. This is an interesting way to improve the readability of the code. I learned about for loops this week in MATLAB, so I loved to see that tie into this because I got to see some of the negative drawbacks of for loops and when they might not be ideal to use on their own because of their drawbacks.

    ReplyDelete
  2. Nice work, Sergio. Hope your exams went well.

    ReplyDelete

Post a Comment

Popular posts from this blog

Ping Pong in Python

Sunset Views Coding Challenge