Monday, March 11, 2019
has it been 3 months?
I wanted to share this information with a fellow reddit user but I feel like anyone could benefit form the information here. Hope it helps someone.
I do not mean for this to be bragy, preachy, or long-winded. I'm sure it's all of those things.
If you are a beginner looking for advice but want to skip the drivel of my experience, skip to the bottom, there's some solid links for problems for beginners.
#################################################
#Project Euler experience
################################################
I was working on a project euler problem this morning, #19.
#################
Counting Sundays
Problem 19
You are given the following information, but you may prefer to do some research for yourself.
1 Jan 1900 was a Monday.
Thirty days has September,
April, June and November.
All the rest have thirty-one,
Saving February alone,
Which has twenty-eight, rain or shine.
And on leap years, twenty-nine.
A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.
How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
#######################
I tried a few different approaches and I couldn't get it. I don't know exactly where my code went wrong yet but that's ok. I used a blog post to answer it. I know cheating right? except my project euler score isn't a bragging position for me, it's there for me to learn. There are some I have solved honestly and with my own effort. I post my solutions if I solve them this way. If I look them up I don't post. I do give it an honest effort, trying my own code, looking up, researching different possible approaches or how to do certain things with code. Sometimes, I just can't get it. This was one I was scratching my head.
Once it's solved, you can look at other people's posted solutions.
I love python and C++. So I look at both of those languages solutions but for the moment, I'll just discuss the differences in the python solutions.
Here is one solution I saw that was almost identical to mine. That happens sometimes, similar approaches to the same problem.
months = {1: 31, 2: 28, 3: 31, 4: 30, 5: 31, 6:30, 7: 31, 8: 31, 9: 30, 10: 31, 11: 30, 12: 31}
year = 1900
month = 1
day = 7
sundays = 0
while year <= 2000:
if year % 4 == 0:
if year % 100 == 0 and year % 400 == 0:
months[2] = 29
else:
pass
months[2] = 29
else:
months[2] = 28
if month > 12:
month = 1
year += 1
if day > months[month]:
day = day - months[month]
month += 1
if day == 1 and 1901 <= year <= 2000:
sundays += 1
day += 7
print(sundays)
Now, I plugged this into the python ide and it worked. Spit out the correct answer and is almost exactly how my code was setup. I messed up with the calculation of leap years which is about what I figured. This solution is brute_force straight forward, non pythonic, not elegant at all.
And more importantly, this taught me almost nothing because that's how I was approaching the problem in the first place.
Here's a far more pythonic/elegant approach to the problem and one I would never have dreamed of.
from datetime import *
print(len([1 for y in range(1901,2001) for m in range(1,13) if date(y,m,1).weekday()==6]))
ok so here is a solution I can learn from
They imported the date and time module. They are looking to print the length of the list produced from the function. The function says for each year in range of 1901 and 2001 while each month jan to dec (1 to 13) ###13 is used due to how the list is iterated through using the date/time module, ### if the date of any particular year/month being the 1st of that month is equal to the weekday 6... So if the 1st is a sunday, it adds it to the list. finally it prints the length of the list because the question is how many sundays hit the first of the month during the time period of jan 1, 1901 to dec 31, 2000.
date.weekday()
Return the day of the week as an integer, where Monday is 0 and Sunday is 6. For example, date(2002, 12, 4).weekday() == 2, a Wednesday. See also isoweekday().
The documentation shows this to be solid, ran it through the ide, works perfect.
Now, does this mean I'll be an expert in pushing these problems to a one line solution (I don't count the imported library)? No, not at all, but it exposes me to other approaches that I hadn't considered. Seeing how other people tackle real problems, even if you don't know the math behind it, can give you insight into how the language can be handled and ultimately helps you grow.
#######################
#A bit of history and the future
########################
I've been teaching myself how to code in python since Jan 2019. So about 3 months. I've been working on coding since 2017, Halloween. I started with Ruby. I moved on to C++. I got incredibly frustrated, like you because it wasn't clicking and memory management is a real thing in c++.
I got no where with Ruby/C++/ or Python at first because all I learned was some serious basics that are common between all the tutorials and I had taken them just about all the free ones.
Var types, var declaration, int manipulation, lists, dictionaries, pointers, class declarations, getters, setters, the basic terms that every programming language has but the syntax is slightly different.
I said ok, what do I want to learn, end goal? I made that decision. While games are not my end goal, I started going about learning how to code games. The game part isn't what's important. The important thing is I started building programs(projects) that were more than just solve this single problem. I created my own problems, sometimes without knowing it and I had to find my own solution. Granted my code looks like crap, I'm sure. What of it works, does work and the rest does not. I'm ok with that. I'm learning, and the progress I've made in such a short time is immense.
You know what, there are things I don't have to think about when constructing a project, I can look and see the progression. And those small things I don't have to think about, that come naturally now, help me make room to tackle the things I have no idea how to solve. It lightens the load. That's what you need to push for.
No matter what the project, get to a point where some of the things are done from muscle memory, get a good IDE, i recommend pycharm or sublime text (although it has annoying winrar type payment reminders, still good). Work on projects. I'll include some links to suggested beginner projects. Even if you don't know the first thing about doing them. Think about (psudo code) how you would logically go about doing it. Break it down. Ok first I would have a variable for this string or number. Then I would have to do this to it. I don't know how to do that. I'm looking up how to do just that one thing. Then next step.
if you break it down into smaller problems, it's much easier to tackle and you'll be surprised how much you learn.
#####################
#Not bragging, simply excited
####################
I started entering game jams. These are timed events with lots of other coding/game creation enthusiasts. It gives you a format/restrictions or boundaries to work in both theme, time, etc. You go at it and see what comes of it. They vary on these things as well as the time allotted. The ones here are 3/4 day weekend jams.
here is my first game jam entry:
alakajam 5
https://youtu.be/DC0ZdK3I20s
https://github.com/psychicash/alakajam5
extra credit jam(second jam):
half way through
https://www.youtube.com/watch?v=ytva4MjX3P0
end of the jam
https://www.youtube.com/watch?v=1cWHGfxdW18
https://github.com/psychicash/lottery
Now I am not saying this to brag on myself, hell I didn't even finish either of the games. The first one, all I had was a parallax background and an intro sequence, that's it. 3 days of disorganized coding and I found myself... more than floundering. All original art btw. (everyone could tell) And sounds. (no one doubts this) That takes up time to be sure. But even after the game jam was over, the project is so... disjointed in my head, I have to scrap it and start over.
Second jam, much more organized, 3rd party art, sound, no title, no intro, but lots of game play, mechanics and though I did not get it finished, I learned so much. There's a polish to the progression that though still rough, is there. I'm telling you man, doing projects and pushing your boundaries is the way to go.
I succeeded with both the jams because I learned so much.
Thomas Edison was once asked how he could continue with his light bulb project after failing over 10,000 times. He replied, "I didn't fail. I just found 10,000 ways that didn't work."
He also is quoted as saying, “Negative results are just what I want. They’re just as valuable to me as positive results. I can never find the thing that does the job best until I find the ones that don’t.”
Do not get discouraged.
################################
Beginner Links
################################
If you don't want to tackle the "math problems" of the code abby, project euler...
try the following links. But you'll get no further, at least from what I've found, from doing tutorial after tutorial after tutorial.
https://adriann.github.io/programming_problems.html
(caveat: forget #11 under beginner for now)
https://www.reddit.com/r/learnprogramming/comments/2a9ygh/1000_beginner_programming_projects_xpost/
(still working through #1 which is a link to another list of programming problems for beginners)
http://www.dreamincode.net/forums/topic/78802-martyr2s-mega-project-ideas-list/
(aforementioned #1 on the list) - did all up to text editor, personally not a fan of tkinter but I plan on returning to it shortly
if nothing else try automate the boring things:
https://automatetheboringstuff.com/
the whole book is online for free or you can buy a copy and support the author, upto you but he makes the information available. He has other books too. All his books are hands on and do things and you'll get stuff done and understand why it's working even if it isn't the prettiest cleanest code out there.
https://www.codewars.com/
site is... different but it has lots of training problems that aren't math based. Challenges your skills and gives you access to other people that will help you if you run into a problem.
Subscribe to:
Posts (Atom)