100 Days of Python, Projects 1-14 #100DaysofCode

I’ve mentioned before about the concept of “always learning”.    One of those thing is coding.  I’d like to think I’m actually pretty good at basic to intermediate level coding, though I am certainly not an expert.  I kind of feel like I am at a point where I would definitely like to “level up” my ability a bit.  So I’ve been working on the Challenge, though a course on Udemy.  Specifically, 100 days of Python, and specifically, this course.

I’ve also been using this as an good excuse to sharpen up my Github skills a bit, so you can follow my progress along in my Github Repository.  I also figure I could talk about about some of my though processes and flow here as well, though some of the projects are very simple, so there may not be a lot to say about them specifically.  Especially since, frankly, I am already beyond the “Beginner Level” of this course.

I thought about making a post for each module, but that’s kind of overkill as well, so instead I’ll just break it up a bit across maybe the skill levels, or whenever I feel like it.

Project 1 – Band Name Generator

This one is pretty basic, and basically, the same sort of thing you see on Facebook trying to steal your information.  I promise I’m not trying to steal your data though.  It takes some simple inputs, the city you were born in, your pet’s name, and outputs them as a combination for a “Band Name”.  You can really put anything you want into these fields and it will just combine them.  Maybe a fun alternative would be to use the letters of the words entered, to pull a different word from a list or something.  

Project 2 – Tip Calculator

This one is mildly more complicated than the Band Name Calculator, but it is still just input fields, but this time, with MATH!  You enter the total bill, how much to tip, how many people ate, and it splits the bill among the various people evenly.  Be sure you order the steak dinner, so your salad eating friends can foot part of the bill for your expensive steak.

Project 3 – Treasure Island Game

I really enjoyed this one, maybe a little TOO much.  The core project is just practice on “if, elif, else” statements.  You build a little “choose your own adventure” game.  Like a very simple Zork Game.  I kind of just kept getting goofier and goofier with the descriptions though.  I guess that’s the “writer” part of my personality or something.

Project 4 – Rock Paper Scissors Game

Pretty straight forward, and mostly a practice for random numbers.  A rock paper scissors game.  One part I like about this game though it that it introduces the idea of using ascii graphics.  I mean, the core idea is simple, but in all the various online coding classes I have done, none have done this sort of thing.  It really helps these little projects feel way less mundane.

Project 5 – Password Generator

More random number practice, this one actually is probably the most actually useful project so far.  You enter how many letters, numbers, and symbols you want for a password and well, it generates it, randomly.  Especially useful because strong passwords are good to have.  Though using a random password, you probably will want a password keeper, and well, most of those include a random password generator.

Project 6 – Escaping the Maze

Project 6 was a little different, since it wasn’t strictly writing pure code, but instead was using a site called Reborg’s World.  https://www.reeborg.ca/index_en.html This site has a few puzzles where you use functions to navigate a little robot through some challenges.  It’s purpose is to help the learner get better at logic puzzles mostly.  It was interesting and I’ve made a note to go back and do the rest of the puzzles at some point.

Project 7 – Hangman Game

Hey, another game.  This one is honestly, pretty full featured, at least for what it’s supposed to be.  It’s still an ASCII based CLI game but it works like Hangman.  Guess the letters, your little man slowly gets hung.  Better save him.

Project 8 – Caesar Cypher

This round is essentially just a sort of “intro to cryptography,” that I feel like may come back around in a later lesson.   This program builds a basic Caesar Cypher.  You enter some text, pick a Cypher, then it just rotates each letter by the number of letters equal to the Cypher number.  It also lets you decode the messages.

Project 9 – Secret Auction

This one was kind of neat, though not overly complicated.  Essentially, you enter your name, place a bit, if there are more people, they do the same, then it announces who the highest bidder is.  My main frustration from this project.  It’s suggested you clear the screen between bidders.   Simple enough.  but it turns out that Python doesn’t really have a built in clear function.  The samples run on the Replit website, and you have to import a special library from Replit.  This doesn’t work in my local VS Code interpreter.  I looked into this, I could write a custom include for it, but it would only work in Windows, or Mac, or Linux, not all three.  Because it’s essentially a command specific to each OS’s respective shells.

Super annoying.

Project 10 – Calculator

This would almost be really cool, if it were actually clickable and not just something you type numbers and operators into.  Still, it’s one of my favorites I think in the end, because it introduces a really interesting and neat concept involving Dictionaries.  Specifically, you have a dictionary

operations = {
    "+": add,
    "-": sub,
    "*": mult,
    "/": div,
}

You can use the key, to assign it’s value to a variable, then call that variable, as the name of a function.  The names inside the dictionary all match the names of functions.  Really slick.  Feels like it reduces code readability quite a lot though.

Project 11 – Blackjack Game

This project was alright to do but it’s a little disappointing because it fakes the cards but just using value.  It doesn’t handle Aces properly, it doesn’t handle splits or anything.   The one mistake I made, that I fixed, originally, I had set it up so the Dealer would always “Hit” of it was below the player score.  Except the dealer wouldn’t know what cards the Player had to know the player score.

Also there is not actual betting, which could make things more interesting I suppose.

Project 12 – Number Guesser

This one almost feels a bit like a filler project, since it’s a pretty straight forward if else level project.  I will also add that the hard mode gives you 5 guesses, which feels extremely low.  The logical method is halving things, so 100 -> 50 -> 25 -> 12 -> 6, so you essentially have a 1/6 chance of getting the answer.\

Project 13 – Debugging

No actual projects on day 13, so maybe it’s not actually “100 Projects in 100 days”. except several of the days have what are two to three projects, so I am sure it makes it up somewhere.   Day 13 was revisiting 3 old projects, using different methods of debugging the code provided.

Project 14 – Higher Lower Game

This game was presented as if it’s super commonly know but I had never heard of it.  It’s almost a variation of the whole Facemash/Hot or Not Idea, but with less focus on looks.  Int he example shown from the web, it uses number of Google Results for a topic, in this version it uses number of Instagram Followers.   The user is presented with two celebrities, and they have to guess which has more Instagram Followers.

It’s all pulled from pre built data, so it’s not current follower counts.   I could see this making a return when the course gets into the Web Scraping portions.

And that’s the end of the “Beginner Section”.  The course has several sections, Beginner, Intermediate, Intermediate +, some Web training, Advanced, and Professional. Most of what I’ve done so far is not really anything new.  i could have worked most of these out.  I’ve actually been spicing them up a bit with my own bits to make things more robust, like most of the inputs have some level of input check to ake sure it’s value.  Like is it a number, or did the user enter “t” or “T” or “True” or “true”.

I’m more looking forward to the next sections as it gets into GUI style training.

Git Gud at Git

Something that I’ve have tried and fail to quite get a hang of is how to use Git, and more specifically, GitHub. I’ve had a GitHub account for a good while, and at one point I “had” a ton of repositories because instead of simply following something, I would fork it to make a copy of it.  I feel like that’s not quite the proper way to go about using things.  At some point I went through and removed all of these forks and was left with only my code projects.  

I still wanted to get a better feel for the proper flow, which I probably still don’t have, and won’t really ever get until I (if ever) have the chance to actually work with a team using Git.  Most of my projects are just single upload drops.  Almost everything I do I just revise and edit locally until it works, then if I want to “show it off” I scrub out any static IPs, usernames, and passwords, and upload it to a repository.  I suppose there really isn’t anything inherently wrong with this method.  I have, more recently been pushing myself to use Visual Studio Code (VSCode) lately and a few other code apps, and thus I’ve been pushing some repositories with those various program’s interfaces.  

I did get to go through the proper, Fork, Branch, Edit, Push process a few times while doing some lessons through Twilioquest, and a follow up afterwards.  

Still, I think my main take away is… it doesn’t really matter how the code gets there.   Especially for single person projects.  I don’t really have any overall “goal” here.  It feels like the common goal is to try to get a programming job, but I am not really sure I would want that, at least not anytime soon.  I mostly just want to show off some mediocre code to the nobody who would look at it.  

Speaking of “showing off”, I also found that the little games I had made last year with Microsoft’s MakeCode Arcade are all browser based, so I set up a little page to specifically show off Code Projects.  Right now, it’s all just MakeCode games, but I plan to add more if it seems like a good idea.

Another motivator for this push to tidy up my GitHub is it’s an excuse to practice using Markdown more.  Like Git, it feels like a skill I should have practiced more much sooner than now.  The main page for each repository is just a specific Markdown file, so having more repositories means I can make many pages of different types.  I’ve also started using Markdown to draft out blog posts as well as for my archive of old writings.

So anyway, if you want to see some random bits of code, some from 20+ years ago, feel free to poke around my growing pile of repositories.   Actual Repositories, where I did some level of work on them.

Lastly, this whole post is annoying full of things I wanted to blog about but have not.  Markdown, MakeCode, Twilioquest, my huge writing archive, to name a few.

Advent of Code 2020 – Wrap Up

Well, my desire to complete this totally fell apart about halfway through. I managed to get around 25 out of 50 stars total for the 25 days of tasks. I two started 11 of the 25 days, and one stared 3 more.

There are a myriad of reasons for failing to finish all the tasks. For starters, I started to have a few one star days that I would “get back to”. Most of these I have a good idea of how to solve them, but there are little problems somewhere that are stopping me from solving them.

I also just got busy/bored of it. After Day 17, I just kind of got tired of it. I also started being busier with life activities like the holidays and Doctor appointments for my wife, and just didn’t have the energy to bother. I did check on what a few of the other later tasks were and have some ideas on how to go about completing them, but it just all became too tedious.

I’m not a professional programmer, so every tasks just becomes this uphill slog and int he end, they don’t run very quickly or great. Meanwhile people are posting their one line trick code work on Reddit. I know it’s not that kind of competition, but it still kind of drags my motivation down.

I may come back and finish things up, but for now, I am still pretty good with getting half the stars.

Advent of Code 2020 – Day 15

Hey look, Day 15 is another one that took a super long time to calculate, though it was one of the easiest to code. It’s a fancy memory game, for Elves who apparently have computer brains. You start with a series of numbers, then the next number you say, is the difference between when the previous number was said, the last two times.

For example if you start saying this sequence, with 0,1,2,3 , the next number is 0, because 3 has only been said once, after that it’s 4, because 0 was said as the 5th number and the first number (5-1). After that it’s 0 again, because 4 has not been said before, then it’s 2, because 0 is at 7 and 5 (7-5), following that is 5, because 2 is at position 8 and 5 (8-5), and so on.

Both parts were the same problem, just with a different number of iterations, 2020 and 30,000,000. Calculating the 2020th number was quick, calculating the 30 millionth number, not so much.

Anyway, here is my code:


values = [0,8,15,2,12,1,4]

current = 0


while current < 30000000-7:
  check = values[-1]
  last = len(values) - values[::-1].index(check)
  if check in values[:last-1]:  
    temp=values[:last-1]
    temp.reverse()
    #print len(values)
    sec_last = len(values) - temp.index(check)-1
    next = last-sec_last
  else:
    next = 0
  values.append(next)
  #print next

  #print len(values)
  current+=1

print values[:-1]
print len(values)

I had some extra prints in there for the 2020, but printing everything for 30 million just slows it down. The final length print is just to verify that it’s giving me the correct iteration value. The main this this could use for clean up is to replace the while loop “-7” subtraction with subtracting a variable equal to the length of the initial values set, so that you could potentially feed it a value set that is larger or smaller.

The trickiest part here was figuring out the positions of the last two occurrences of a number, which meant counting backwards through the array of values. For the second number, I ended up using a reverse copy of the array, because I kept getting screwball answers trying to do it directly like I had for the first number. I would have liked to make it work with the more elegant solution but I didn’t have time to keep working on it.

Advent of Code 2020 – Day 13

Today’s lesson in extremely, incredibly, stupidly, inefficient methodology, is brought to you by, the letter “i” and the number “640856202464541”. Day 13 consisted of calculating bus departure times. Part 1 was stupidly simple. You have a list of buses, each bus makes a regular round trip to wherever, and the round trip always takes the same amount of minutes. At some point in the past, the buses all left at once.

Say the buses were 4, 5, and 6, each number also being the minutes for a round trip. Every 4 minutes, bus 4 returns, every 5 minutes bus 5 returns, every 6 minutes, bus 6 returns. If you are available to leave at 9 minutes, which bus leaves first. In this example, the first bus to return would be 5 (5*2=10)

Part 1 was easy, start at your departure time, check to see if any of the numbers divide into it evenly, if not, increment by one and repeat.

with open('day13data.txt') as f:
    lines = [line.rstrip() for line in f]

time = int(lines[0])
raw = lines[1].split(',')
busses= [] 

for i in raw:
  if i != 'x':
    busses.append(int(i))

print time
print busses

for x in busses:
  bus_time=x*(round((float(time)/float(x))+.5))
  wait=bus_time-time
  print "For bus "+str(x)+": "+str(bus_time)+" and you will wait: "+str(wait)+" Ans: "+str(x*wait)

The trickiest part is that the data contained non existent buses, labeled with an “x”. These are not a factor for part 1, so they just get stripped out.

Part 2 is where the pain in the ass was. For Part 2, you have to find a starting time where each bus, will leave, one minute apart, according to their offset. This includes the “missing” buses. So for the quick example I had above of 4,5,6, this time would occur at well, 4 minutes, because I made them sequential, but the next one would occur at 64,65,66 (16*4, 13*5, 11*6). This gets even more complicated when you add in the blanks, for example, 4,x,5,x,6 occurs at 68,70,72. As you spread the numbers apart and rearrange them to be non sequential, it complicates everything more.

I actually figured out the methodology pretty quickly, and wrote a working, good bit of code pretty quickly, my problem, was picking a starting point. I am sure there is some numerology methods (I am pretty sure you can do something using remainders), to calculate even an approximate starting point. In my standard of ugly code, I was just brute forcing it.

with open('day13data.txt') as f:
    lines = [line.rstrip() for line in f]

lines_array = lines[1].split(',')
times = []
counter=0
#First for sample Data Set day13datab.txt, second for real data
#multiplier=152600
multiplier=15630639084400
offsets=[]
busses=[]

for i in lines_array:
  if i != 'x':
    busses.append(i)
    offsets.append(lines_array.index(i))

#print busses
#print offsets

while (1):
  for i in offsets:
    times.append(((int(busses[0]))*multiplier)+i)

  #print times
  counter=1
  for j in busses[1:]:
    #print j
    if (times[counter]/float(j)).is_integer():
      counter+=1
    else:
      break
        
  #print times
  #print counter
  if counter == len(busses):
    break
  counter = 0
  times =[]
  multiplier+=1
  #print multiplier

print times

The problem is, this takes a very, very, very, very, very, long time. I left this code running for hours on my server and it didn’t finish, I honestly feel like it could very likely run for days, weeks, years, and never finish. The iteration on my multiplier that gives the correct set, for my data (every person has a different data set) was 15,630,639,084,501. 15 TRILLION.

So I fudged it a bit, because I was pretty sure my code was good, but I didn’t have an eternity to wait. So I found someone else’s code, found the correct answer, then worked out my starting offset from there. Sure enough, when I start at 15,630,6390,084,400. It quickly finds the correct answer.

If I needed this code for something important, I would certainly try to clean it up. I even started working on an iteration that would sort out my bus list to start with the largest number, instead of iterating the list every (low value) it would iterate every (high value), which in my case I believe was something like 15 versus 900. This was trickier than a straight sort though because I had a second list of offsets that I needed to also re order. I got this code working for the sample data set, but it failed in my proper data set because, while the sample data set started with the lowest bus number, the actual data set did not, and I had not calculated for that.

PS, yes, I misspelled “busses” in my code