Playing with Playgrounds

Discussion in 'iPad Development' started by twerppoet, Sep 26, 2016.

  1. twerppoet

    twerppoet
    Expand Collapse
    iPad Legend II

    Joined:
    Jan 8, 2011
    Messages:
    20,349
    Thanks Received:
    5,943
    The Playgrounds app isn't strictly a developer tool, but it's related and developers can use it to tinker with code ideas, and this part of the site is pretty neglected; so here it goes.

    I'm about half way through the second lesson book, (just finished Twin Peaks in "Learn to Code 2") and am having a pretty good time. This being the umpteenth time I've started to teach myself to code I'm not struggling with the concepts yet; but I'm enjoying the problem solving. It's more like a game than actual learning.

    If anyone want so share experiences, code, or bat around ideas I'm here.

    I'm also willing to give hints for those stuck, provided I've gotten that far myself. Or, if any real coders are lurking, I may end up asking for help. :)
     
  2. Mickey330

    Mickey330
    Expand Collapse
    Administrator
    Staff Member

    Joined:
    Aug 30, 2010
    Messages:
    11,892
    Thanks Received:
    2,224
    Thanks for the reminder! I was very intrigued when I saw this app demonstrated in one of the keynotes, but I had forgotten about it.

    I've now downloaded it and am gonna play with it. Since you're ahead of me - I may end up taking you up on your offer of assistance. Been a long time since I've coded anything ... and that was college classes..

    Again, thanks.

    Marilyn
     
  3. twerppoet

    twerppoet
    Expand Collapse
    iPad Legend II

    Joined:
    Jan 8, 2011
    Messages:
    20,349
    Thanks Received:
    5,943
    You're welcome. I hope you enjoy it as much (or more) as I am.
     
  4. twerppoet

    twerppoet
    Expand Collapse
    iPad Legend II

    Joined:
    Jan 8, 2011
    Messages:
    20,349
    Thanks Received:
    5,943
    I've finished the 2nd lesson book; htough I did skip one page that sounded boring. Now I've started re-reading Apple's Swift Language Programing book with a blank playgrounds page in split view. That way I can try the code examples, and even tweak them a bit.

    Trying to better understand Tuples and Arrays I used some of the basics learned in the lessons to tweak this code together. All it does is create an array of 3D coordinates describing a virtual space. Yes, that sounds complicated, but it's not, really.

    Playgrounds lets you create videos of your code, and since for some reason I'm proud of my minor coding accomplishement, here's my code, and the video. It would be really boring if you couldn't make some of the variables show inline with the code.

     
  5. Mickey330

    Mickey330
    Expand Collapse
    Administrator
    Staff Member

    Joined:
    Aug 30, 2010
    Messages:
    11,892
    Thanks Received:
    2,224
    You are going to drag me back into programming, I know it! I'm already halfway through Lesson 2 of playgrounds (is funny to see creature (?) spin 3 times just to turn right) and now I've downloaded all of Apple's books on using and programming with Swift.

    So much for just playing mindless games when I've got nothing else going on...

    Correct me if I'm wrong - but are you saying you can use Playgrounds as a "live" test of programming you've learned from the Swift books? Cause that'd be easier to just run examples. And by easier I mean lazier as I can sit in my comfy chair with my ipad.

    Marilyn
     
  6. twerppoet

    twerppoet
    Expand Collapse
    iPad Legend II

    Joined:
    Jan 8, 2011
    Messages:
    20,349
    Thanks Received:
    5,943
    Yep. You can use Playgrounds and iBooks side by side.

    IMG_1838-2.jpeg

    To use it as an example tester, pull PlayGrounds into splitview, then create a blank playground. All of Swits core code works here. Once you run the code you'll see some icons to the right. Tap on those to see the results or status of the code to the left. You can even add the view inline with the code. There are three types: List, Single, and Graph. If applicable you can switch between them when the viewer is in-line.

    There is no output screen/terminal, so this is how you'll view all your code results with a blank playground. It's good enough for testing code bits, which is all it's meant for.

    The other way I use it is to pull iBooks in from the right when working in Playgrounds; as a reference tool. You can use it quickly in slideover, or go ahead and pull it into split view for longer sessions.

    IMG_1837-2.png

    Here I'm exploring ways to make my earlier SpaceCoordinates a bit more visual. I'm using the Shapes template, since it includes some shape opbjects like circles, lines, and rectangles.
     
  7. twerppoet

    twerppoet
    Expand Collapse
    iPad Legend II

    Joined:
    Jan 8, 2011
    Messages:
    20,349
    Thanks Received:
    5,943
    This one started with another example from the book, then I got obessed with generating some random data (a real pain). Once I figured out how to do that I decided I really wanted some output, so I ported it to the Answers template where I could use their 'show' function.

    Anyway, it generates some random scores, stores them in an array, then uses a function to give you the minimum, maximum, and average scores. This time I'll include the code, in case you want to play with it yourself.



    Code:
    // Run in the Answers template in Playgrounds on the iPad
    
    import CoreFoundation
    
    // Expample function from Swift 3 Progrmaing manual.
    // Calculates the min, max, and sum(avg) of a series of
    // game scores using an array.
    func calculateStatistics(scores: [Int]) -> (min: Int, max: Int, avg: Int) {
        var min = scores[0]
        var max = scores[0]
        var avg = 0
        var sum = 0
        
        for score in scores {
            if score > max {
                max = score
            } else if score < min {
                min = score
            }
            sum += score
        }
        avg = sum / scores.count
        return (min, max, avg)
    }
    
    // Func randomInt: Requires CoreFoundation
    // returns a random integer in the specified range
    func randomInt(lowest: Int, highest: Int) -> Int {
        let range: Int = highest - lowest
        let number = arc4random_uniform(UInt32(range)) + UInt32(lowest)
        
        return Int(number)
    }
    
    for i in 1 ... 5 {
        
        // Create a random number of scores in an array named ourScores
        var ourScores = [Int]()
        for i in 1 ... randomInt(lowest: 1, highest: 20) {
            ourScores.append(randomInt(lowest: 0, highest: 100))
        }
        
        // Call the calculateStatistics function on ourScores
        let statistics = calculateStatistics(scores: ourScores)
        
        
        // show the results
        let numberOfGames = "Number of games:  " + String(ourScores.count)
        let gameStats = "Score Stats:  Min = " + String(statistics.min) + "   Max = " + String(statistics.max) + "   Avg = " + String(statistics.avg)
        
        show(numberOfGames)
        show(gameStats)
        show("----------------------------------------------")
        
    }
     
  8. twerppoet

    twerppoet
    Expand Collapse
    iPad Legend II

    Joined:
    Jan 8, 2011
    Messages:
    20,349
    Thanks Received:
    5,943
    Well, after exhausting the Playground Books supplied by Apple, and getting tired of typing examples out of the Swift Programing Language book, I started looking for other challenges. After much head banging, I found a great site: Project Euler.

    Project Euler has a huge collection of computer math problems to be solved (over 500), ranging from easy to super hard. It tracks your progress, and there are forums where you can discuss the solutions (after you've solved it).

    Even better, you can download a Playground Book with many of hte problems as pages at a github repository: GitHub - jad6/Project-Euler: A Swift Playground Book for Project Euler problems

    This should keep me busy for a while.
     
  9. twerppoet

    twerppoet
    Expand Collapse
    iPad Legend II

    Joined:
    Jan 8, 2011
    Messages:
    20,349
    Thanks Received:
    5,943
    Nearly broke my brain on this one, and it's only the 4th problem. But I did succeed, and feel pretty proud of myself, even if it was ugly code.

    From Project Euler
     
  10. twerppoet

    twerppoet
    Expand Collapse
    iPad Legend II

    Joined:
    Jan 8, 2011
    Messages:
    20,349
    Thanks Received:
    5,943
    A little more info on Project Euler in Playgrounds.

    Turns out that Playgrounds isn't a super powerful number cruncher. While some of the problems can be solved using Playgrounds, a lot of the more computation intensive problems break in the Playground enviroment.

    I managed to solve all the problems up to 7 on the iPad (with a lot of head scratching and optimization). Problem 7 however, required some heavy duty prime factoring. It might be solvable with more advance prime tests, but I don't understand those well enough to write the code, so I cheated. I downloaded XCode on the iMac and use playgrounds there.

    However, this kind of defeats the whole playing with playgrounds on the iPad thing.

    So. . .

    I'm done trying to solve every problem in Project Euler. Instead I'll be picking through them looking for problems I believe are solvable in Playgroudns on the iPad. Here is the list so far.

    • 1 - 6 : I have solutions for these. Not the best solutions, but I've proven they can be solved.
    • 7 : I could only solve on the iMac. There might be a solution that works on the iPad, but I don't understand the more sophisticated prime tests well enough to try them.
    • 9 : Solved without too much trouble
    • 10 : I couldn't solve even on the iMac in a playground. I'm betting it would work compiled, but that's not my goal.
    • 11 : I have not solved this one yet, but it looks perfectly solvable. I just haven't cared enough to figure out the details.
    • 12 : Another one that involves a lot of primes and factors. I've just about given up on it.
    It's possible, even likely, that every single problem can be solved in Playgrounds, but I'm not good enough, and I'm unlikely to become that good any time soon, so I'm going to be looking around for other playgrounds, playground.books, and genreal problems to play with. I let you know what/if I find anything else interesting or challenging.

    Heck, maybe we can even come up with our own projects and challenges; if anyone is interested.
     

Share This Page