threadandstrings
threadandstrings
On Thread & Strings
4 posts
Thoughts on solving problems and making useful things, with programming and patternmaking.
Don't wanna be here? Send us removal request.
threadandstrings · 9 years ago
Photo
This is my life right now.
Tumblr media
HIIIIIYAH!!! You’re mastering code like nobody’s business. 
91 notes · View notes
threadandstrings · 9 years ago
Video
This makes every cell in my body smile. If you know who made this video PLEASE please answer below!
tumblr
Beach meets beats!
124 notes · View notes
threadandstrings · 9 years ago
Text
Decoding Cryptic Solutions: App Academy’s Caesar Cipher
As I worked through the practice problems for the first time in preparing for the first code challenge of App Academy’s admissions process, I found that it was crucial to my long term learning to walk myself through their given solutions as I solved (or got painfully stuck on) each problem for two key reasons. 
For one, my solution may work, but It’s never a bad thing to know more than one way to solve a problem, right?
It’s important to check if, and understand exactly why, their logic differs from mine. 
You may realize there are some edge cases that your code misses and theirs doesn’t. You may also end up feeling pretty solid about your solution, but gain ideas for refactoring your code to be even cleaner. You may even find that the (super) humans who’ve graciously taken the time to write and provide these problems and solutions for your benefit are just like you (*gasp!), human, and just as capable of missing bugs. How reassuring is that! More on that in my next post...   
Secondly, most of their solutions are confusing as hell to read through, especially if I’m stuck and my brain is leaking out of my nostrils! 
After taking the time to break down their solutions, I’m able to quickly go back and solve the problem on my own. 
More importantly, what I learn in the short term from that process actually sticks when I come back to race the clock against the problem again the next day. You’ll never understand and be able to implement the underlying concepts by memorizing their solutions and regurgitating them into the IDE! Level up on Codewars like the glorious ninja you are after breaking down, absorbing, and applying the logic behind those solutions and see just what the fuck I mean. Just do it. 
Their solution to the Caesar Cipher problem was a hot mess to try to read through. Read on, and hopefully my refactor and line-by-line breakdown will be of use to you. 
Here’s the problem:
Write a method that takes in an integer `offset` and a string. Produce a new string, where each letter is shifted by `offset`. You may assume that the string contains only lowercase letters and spaces. When shifting "z" by three letters, wrap around to the front of the alphabet to produce the letter "c". You'll want to use String's `ord` method and Integer's `chr` method. `ord` converts a letter to an ASCII number code. `chr` converts an ASCII number code to a letter. You may look at the ASCII printable characters chart:
http://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters Notice that the letter 'a' has code 97, 'b' has code 98, etc., up to 'z' having code 122.
You may also want to use the `%` modulo operation to handle wrapping of "z" to the front of the alphabet. Difficulty: hard. Because this problem relies on outside information, we would not give it to you on the timed challenge. :-)
Here’s their solution untouched.
(Tell me you can read through this once and understand exactly what on Earth is going on with the help of the cryptic variable names and unnecessarily long iterator names, and I’ll call you a lier, or a genius, whichever you prefer. In any case, read on...)
Tumblr media
Here’s my refactor-breakdown-comment extravaganza:
Tumblr media Tumblr media Tumblr media
TL;DR: 
Don’t memorize the solutions to coding problems (on Codewars, or anywhere else). Refactor them for clarity, and break them down line by line by commenting the shit out of them until your neurons are exploding with understanding. Then immediately go back and solve the problem on your own again. Then high five yourself and move on to the next problem, rinse and repeat. Re-solve the problems over and over until you literally scoff at how quickly you solve them. Then solve harder problems and repeat the same process. ¡Ándale! 
0 notes
threadandstrings · 9 years ago
Text
a/A practice challenge problem: luckysevens?
As I’m still figuring out what coding bootcamp is best for me, I’m preparing for admissions to all three of my top choices. App Academy is one of them, and right now I’m working through their admissions prep. The following is exactly how I worked through the first problem of their practice code challenge. This is copy/pasted directly from my codeanywhere.com IDE--I notate the shit out of my code because it forces me to think through the problem and understand my solution well enough that I can teach the methods learned to another newbie. Ruby docs are my world right now! 
**Warning: Solution included! Hopefully you’ve found this page after struggling with the problem on your own first.
#"Write a function lucky_sevens?(numbers), which takes in an array of integers and returns true if any three consecutive elements sum to 7.
#e.g.,
#lucky_sevens?([2,1,5,1,0]) == true # => 1 + 5 + 1 == 7 #lucky_sevens?([0,-2,1,8]) == true # => -2 + 1 + 8 == 7 #lucky_sevens?([7,7,7,7]) == false #lucky_sevens?([3,4,3,4]) == false #Make sure your code correctly checks for edge cases (i.e. the first #and last elements of the array)."
My process:
#Step 1: Define lucky_sevens?(numbers) #Step 2: Iterate over every element in groups of 3. #Step 3: During each iteration, sum the group and push sum to an array. #Step 4: If sums array elements include 7, return true; else, return false.
def lucky_sevens?(numbers)     sums = [ ]     numbers.each_cons(3) {|e| sums.push(e.reduce(:+))} #a/b     if sums.include?(7) #c         puts true     else         puts false     end end
#test: lucky_sevens?([1, 2, 4, 6]) # expected true lucky_sevens?([8, 9, -10, 11]) # expected true lucky_sevens?([3, 5, 6, 1]) # expected false lucky_sevens?([2,1,5,1,0]) # expected true lucky_sevens?([0,-2,1,8])  # expected true lucky_sevens?([7,7,7,7])  # expected false lucky_sevens?([3,4,3,4]) # expected false
#a: My initial thought process had me determining within each loop whether there's a sum of 7. Instead, this line iterates over each element of the input array in groups of 3, then the 3 element chunks are reduced within the iteration to a sum. That sum is then pushed to the empty array declared within the method. I wrote this solution hours before getting it to work. If I've learned anything about myself in the last 28 years, it's that I prefer to learn through suffering than learn from someone else's answer. So, I spent hours stuck in a cycle of incorrect output before I finally searched Google for the hints to a solution.
#b: The solutions I found on a couple of different forums were unsatisfactory in my opinion. They were not as simple or as easy to read and understand as the one I was trying to write. While reading through the comments of one forum (facebash) I had an aha moment and realized my problem was stupidly simple--I was using '<<' to populate the array with the sums instead of '.push'. These essentially do the exact same job, with one important distinction that I wasn't aware of: '<<' only allows the population of a single element at a time, while '.push' allows multiple elements to be pushed in one line of code! After fixing this, it finally worked.
#c: This if statement checks the resulting array of sums for the presence of the number 7, and outputs 'true' if detected; otherwise the output is false.
Have a refactor suggestion or a different solution to share? Help other new programmers and answer below!
Happy coding, y’all.
1 note · View note