What's new
Apple iPad Forum 🍎

Welcome to the Apple iPad Forum, your one stop source for all things iPad. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Playing with Playgrounds

twerppoet

iPad Fan
Joined
Jan 8, 2011
Messages
24,132
Reaction score
15,329
Location
Milton-Freewater, OR
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. :)
 

Mickey330

Administrator
Staff member
Joined
Aug 30, 2010
Messages
11,890
Reaction score
2,226
Location
Western NY state (USA)
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..[emoji57]

Again, thanks.

Marilyn
 
OP
twerppoet

twerppoet

iPad Fan
Joined
Jan 8, 2011
Messages
24,132
Reaction score
15,329
Location
Milton-Freewater, OR
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.

 

Mickey330

Administrator
Staff member
Joined
Aug 30, 2010
Messages
11,890
Reaction score
2,226
Location
Western NY state (USA)
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... [emoji6]

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
 
OP
twerppoet

twerppoet

iPad Fan
Joined
Jan 8, 2011
Messages
24,132
Reaction score
15,329
Location
Milton-Freewater, OR
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.
 
OP
twerppoet

twerppoet

iPad Fan
Joined
Jan 8, 2011
Messages
24,132
Reaction score
15,329
Location
Milton-Freewater, OR
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("----------------------------------------------")
    
}
 
OP
twerppoet

twerppoet

iPad Fan
Joined
Jan 8, 2011
Messages
24,132
Reaction score
15,329
Location
Milton-Freewater, OR
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.
 
OP
twerppoet

twerppoet

iPad Fan
Joined
Jan 8, 2011
Messages
24,132
Reaction score
15,329
Location
Milton-Freewater, OR
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
Largest palindrome product
Problem 4
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.

Find the largest palindrome made from the product of two 3-digit numbers.
 
OP
twerppoet

twerppoet

iPad Fan
Joined
Jan 8, 2011
Messages
24,132
Reaction score
15,329
Location
Milton-Freewater, OR
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.
 
OP
twerppoet

twerppoet

iPad Fan
Joined
Jan 8, 2011
Messages
24,132
Reaction score
15,329
Location
Milton-Freewater, OR
I found something easy to play with on iTunes U.

Geometry with Swift Playgrounds - Free Course by Apple Distinguished Educators on iTunes U

It's a Logo like approach to programing. I haven't bothered with the course itself, but in the resource page there's a playground that you can download and use. It stands alone pretty well.

This is easier than Apple's second lesson book, and way easier the Project Euler. All you need is some basic geometry, and what you don't know you can probably pick up along the way.

Note. Trying to install the playground directly from iTunes U did not work. I had to open it in GoodReader and unzip the folder, then open into Playgrounds from there.

I'll be exploring iTunes U for more stuff. I should have thought to look there earlier.
 
OP
twerppoet

twerppoet

iPad Fan
Joined
Jan 8, 2011
Messages
24,132
Reaction score
15,329
Location
Milton-Freewater, OR
Feel that Apple's "Learn to Code" 1 and 2 are for kids?

This iTunes-U course is more in-line with "The Swift Programing Language" manual by Apple, simple instructions and code examples; no cute animated aliens in a 3D world.

A Swift Time to Code - Student - Free Course by Trumbull Career and Technical Center on iTunes U

It also makes a good refresher. Repetition is a good way to learn. While the same concepts are taught, the slightly different approach and exercises make for less boredom.
 

Mickey330

Administrator
Staff member
Joined
Aug 30, 2010
Messages
11,890
Reaction score
2,226
Location
Western NY state (USA)
Thanks for this! I like it and am going to start the course.

I may not comment here all the time - but I am sure grateful that you are finding all this neat programming stuff. Gives me some fun "brain" time. Thanks!

Marilyn
 

Most reactions

Latest posts

Top